Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Da#239 move indexes inside collection #301

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@
},
{
"command": "vscode-couchbase.createCollection",
"when": "view == couchbase && viewItem == collectionDirectory"
"when": "view == couchbase && viewItem == scope"
},
{
"command": "vscode-couchbase.removeCollection",
Expand All @@ -839,7 +839,7 @@
},
{
"command": "vscode-couchbase.refreshCollections",
"when": "view == couchbase && viewItem == collectionDirectory"
"when": "view == couchbase && viewItem == scope"
},
{
"command": "vscode-couchbase.refreshIndexes",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/collections/createCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import * as vscode from "vscode";
import { IConnection } from "../../types/IConnection";
import { Memory } from "../../util/util";
import { logger } from "../../logger/logger";
import { CollectionDirectory } from "../../model/CollectionDirectory";
import { Constants } from "../../util/constants";
import { ScopeNode } from "../../model/ScopeNode";

export const createCollection = async (node: CollectionDirectory) => {
export const createCollection = async (node: ScopeNode) => {
const connection = Memory.state.get<IConnection>(Constants.ACTIVE_CONNECTION);
if (!connection) {
return;
Expand Down
32 changes: 16 additions & 16 deletions src/commands/indexes/openIndexInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ import { MemFS } from "../../util/fileSystemProvider";
import IndexNode from "../../model/IndexNode";

export const openIndexInfo = async (indexNode: IndexNode, memFs: MemFS) => {
try {
const uri = vscode.Uri.parse(
`couchbase:/${indexNode.bucketName}/${indexNode.scopeName}/Indexes/${indexNode.indexName}.n1ql`
);
memFs.writeFile(
uri,
Buffer.from(indexNode.data),
{ create: true, overwrite: true }
);
const document = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(document, { preview: false });
return true;
} catch (err: any) {
logger.error("Failed to open index information");
logger.debug(err);
}
try {
const uri = vscode.Uri.parse(
`couchbase:/${indexNode.bucketName}/${indexNode.scopeName}/Indexes/${indexNode.indexName}.sqlpp`
);
memFs.writeFile(
uri,
Buffer.from(indexNode.data),
{ create: true, overwrite: true }
);
const document = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(document, { preview: false });
return true;
} catch (err: any) {
logger.error("Failed to open index information");
logger.debug(err);
}
};
3 changes: 1 addition & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { QueryKernel } from "./notebook/controller";
import { Constants } from "./util/constants";
import { createNotebook } from "./notebook/notebook";
import IndexNode from "./model/IndexNode";
import { CollectionDirectory } from "./model/CollectionDirectory";
import { IndexDirectory } from "./model/IndexDirectory";
import { openIndexInfo } from "./commands/indexes/openIndexInformation";
import { Commands } from "./commands/extensionCommands/commands";
Expand Down Expand Up @@ -299,7 +298,7 @@ export function activate(context: vscode.ExtensionContext) {
subscriptions.push(
vscode.commands.registerCommand(
Commands.createCollection,
async (node: CollectionDirectory) => {
async (node: ScopeNode) => {
await createCollection(node);
clusterConnectionTreeProvider.refresh();
}
Expand Down
101 changes: 0 additions & 101 deletions src/model/CollectionDirectory.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/model/CollectionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { IFilterDocuments } from "../types/IFilterDocuments";
import { SchemaDirectory } from "./SchemaDirectory";
import { getActiveConnection } from "../util/connections";
import { Commands } from "../commands/extensionCommands/commands";
import { IndexDirectory } from "./IndexDirectory";

export default class CollectionNode implements INode {
constructor(
Expand Down Expand Up @@ -75,6 +76,19 @@ export default class CollectionNode implements INode {

public async getChildren(): Promise<INode[]> {
let documentList: INode[] = [];
// Index directory to contains list of indexes
const indexItem = new IndexDirectory(
this,
this.connection,
"Indexes",
this.bucketName,
this.scopeName,
this.collectionName,
[],
vscode.TreeItemCollapsibleState.None
);

documentList.push(indexItem);
documentList.push(new SchemaDirectory(this, this.connection, "Schema", this.bucketName, this.scopeName, this.collectionName));
// TODO: default limit could be managed as user settings / preference
let result;
Expand Down
28 changes: 4 additions & 24 deletions src/model/IndexDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* limitations under the License.
*/
import * as vscode from "vscode";
import * as path from "path";
import { IConnection } from "../types/IConnection";
import { INode } from "../types/INode";
import IndexNode from "./IndexNode";
import { Constants } from "../util/constants";
import { getActiveConnection, getConnectionId } from "../util/connections";
import { getActiveConnection } from "../util/connections";
import InformationNode from "./InformationNode";
import { logger } from "../logger/logger";
import { getIndexDefinition } from "../util/indexUtils";
Expand All @@ -31,6 +29,7 @@ export class IndexDirectory implements INode {
public readonly itemName: string,
public readonly bucketName: string,
public readonly scopeName: string,
public readonly collection: string,
public readonly indexes: any[],
public readonly collapsibleState: vscode.TreeItemCollapsibleState
) {
Expand All @@ -46,22 +45,6 @@ export class IndexDirectory implements INode {
label: `${this.itemName}`,
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
contextValue: "indexDirectory",
iconPath: {
light: path.join(
__filename,
"..",
"..",
"images/light",
"scope-item.svg"
),
dark: path.join(
__filename,
"..",
"..",
"images/dark",
"scope-item.svg"
),
},
};
}
/*
Expand All @@ -74,7 +57,7 @@ export class IndexDirectory implements INode {
try {
const connection = getActiveConnection();
//TODO: Change it to not include IndexNode with undefined scope once the issues with undefined scope and collections fixed
result = await connection?.cluster?.queryIndexes().getAllIndexes(this.bucketName, { scopeName: this.scopeName });
result = await connection?.cluster?.queryIndexes().getAllIndexes(this.bucketName, { scopeName: this.scopeName, collectionName: this.collection });
if (result === undefined) { return []; }
for (const query of result) {
if (query.scopeName === this.scopeName || this.scopeName === "_default") {
Expand All @@ -83,7 +66,7 @@ export class IndexDirectory implements INode {
this.connection,
this.scopeName,
this.bucketName,
`${query.name.substring(1)}_${(query.collectionName ?? "")}`,
`${query.name[0] === '#' ? query.name.substring(1) : query.name}${(query.collectionName ? ("_" + query.collectionName) : "")}`,
getIndexDefinition(query),
vscode.TreeItemCollapsibleState.None
);
Expand All @@ -95,9 +78,6 @@ export class IndexDirectory implements INode {
logger.error("Failed to load Indexes");
logger.debug(err);
}
if (indexesList.length === 0) {
indexesList.push(new InformationNode("No Indexes found"));
}
return indexesList;
};
}
63 changes: 37 additions & 26 deletions src/model/ScopeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import * as vscode from "vscode";
import * as path from "path";
import { IConnection } from "../types/IConnection";
import { INode } from "../types/INode";
import { IndexDirectory } from "./IndexDirectory";
import { CollectionDirectory } from "./CollectionDirectory";
import { Memory } from "../util/util";
import { getActiveConnection } from "../util/connections";
import { IFilterDocuments } from "../types/IFilterDocuments";
import CollectionNode from "./CollectionNode";
import { logger } from "../logger/logger";
import InformationNode from "./InformationNode";

export class ScopeNode implements INode {
constructor(
Expand Down Expand Up @@ -57,29 +61,36 @@ export class ScopeNode implements INode {
* @returns Two Directory one contains Index definitions and other contains Collections
* */
public async getChildren(): Promise<INode[]> {
const scopeItem: any[] = []; // Declare scope Item which contains two directories
// Index directory to contains list of indexes
const indexItem = new IndexDirectory(
this,
this.connection,
"Indexes",
this.bucketName,
this.scopeName,
[],
vscode.TreeItemCollapsibleState.None
);
// Collection Directory to contains Collections
const collectionItem = new CollectionDirectory(
this,
this.connection,
"Collections",
this.bucketName,
this.scopeName,
this.collections,
vscode.TreeItemCollapsibleState.None
);
scopeItem.push(indexItem);
scopeItem.push(collectionItem);
return scopeItem;
const collectionList: any[] = [];
for (const collection of this.collections) {
try {
let docFilter = Memory.state.get<IFilterDocuments>(`filterDocuments-${this.connection.connectionIdentifier}-${this.bucketName}-${this.scopeName}-${collection.name}`);
const filter: string = (docFilter && docFilter.filter.length > 0) ? docFilter.filter : "";
const connection = getActiveConnection();
const queryResult = await connection?.cluster?.query(
`select count(1) as count from \`${this.bucketName}\`.\`${this.scopeName}\`.\`${collection.name}\` ${filter.length > 0 ? "WHERE " + filter : ""};`
);
const count = queryResult?.rows[0].count;
const collectionTreeItem = new CollectionNode(
this,
this.connection,
this.scopeName,
count,
this.bucketName,
collection.name,
filter !== "",
vscode.TreeItemCollapsibleState.None
);
collectionList.push(collectionTreeItem);
} catch (err: any) {
logger.error("Failed to load Collections");
logger.debug(err);
throw new Error(err);
}
}
if (collectionList.length === 0) {
collectionList.push(new InformationNode("No Collections found"));
}
return collectionList;
}
}