Skip to content

Commit a72063b

Browse files
authored
Merge pull request #8272 from shirady/nsfs-nc-list-with-name-filter
NC | NSFS | CLI | Improve Performance of List with Name Filter
2 parents 906ba38 + cd5d80e commit a72063b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/cmd/manage_nsfs.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ async function main(argv = minimist(process.argv.slice(2))) {
8383
}
8484

8585
async function bucket_management(action, user_input) {
86-
const data = await fetch_bucket_data(action, user_input);
86+
let data;
87+
if (action !== ACTIONS.LIST) {
88+
data = await fetch_bucket_data(action, user_input);
89+
}
8790
await manage_bucket_operations(action, data, user_input);
8891
}
8992

@@ -265,7 +268,10 @@ async function account_management(action, user_input) {
265268
// init if actions is add/update (require encryption) or show_secrets = true (require decryption)
266269
if ([ACTIONS.ADD, ACTIONS.UPDATE].includes(action) || show_secrets) await nc_mkm.init();
267270

268-
const data = await fetch_account_data(action, user_input);
271+
let data;
272+
if (action !== ACTIONS.LIST) {
273+
data = await fetch_account_data(action, user_input);
274+
}
269275
await manage_account_operations(action, data, show_secrets, user_input);
270276
}
271277

@@ -571,10 +577,18 @@ function filter_bucket(bucket, filters) {
571577
* @param {boolean} [show_secrets]
572578
* @param {object} [filters]
573579
*/
574-
async function list_config_files(type, wide, show_secrets, filters) {
575-
const entries = type === TYPES.ACCOUNT ?
580+
async function list_config_files(type, wide, show_secrets, filters = {}) {
581+
let entries;
582+
// in case we have a filter by name, we don't need to read all the entries and iterate them
583+
// instead we "mock" the entries array to have one entry and it is the name by the filter (we add it for performance)
584+
const is_filter_by_name = filters.name !== undefined;
585+
if (is_filter_by_name) {
586+
entries = [{'name': filters.name + JSON_SUFFIX}];
587+
} else {
588+
entries = type === TYPES.ACCOUNT ?
576589
await config_fs.list_root_accounts() :
577590
await config_fs.list_buckets();
591+
}
578592

579593
const should_filter = Object.keys(filters).length > 0;
580594
// decryption causing mkm initalization

src/test/unit_tests/jest_tests/test_nc_nsfs_account_cli.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ describe('manage nsfs cli account flow', () => {
11331133
const account_options = { config_root };
11341134
const action = ACTIONS.LIST;
11351135
const res = await exec_manage_cli(type, action, account_options);
1136+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
11361137
expect(JSON.parse(res).response.reply.map(item => item.name))
11371138
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
11381139
});
@@ -1141,6 +1142,7 @@ describe('manage nsfs cli account flow', () => {
11411142
const account_options = { config_root, wide: true };
11421143
const action = ACTIONS.LIST;
11431144
const res = await exec_manage_cli(type, action, account_options);
1145+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
11441146
expect(JSON.parse(res).response.reply.map(item => item.name))
11451147
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
11461148
// added additional properties that we can see with wide option (uid, new_buckets_path)
@@ -1155,6 +1157,7 @@ describe('manage nsfs cli account flow', () => {
11551157
const account_options = { config_root, wide: 'true' };
11561158
const action = ACTIONS.LIST;
11571159
const res = await exec_manage_cli(type, action, account_options);
1160+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
11581161
expect(JSON.parse(res).response.reply.map(item => item.name))
11591162
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
11601163
// added additional properties that we can see with wide option (uid, new_buckets_path)
@@ -1169,6 +1172,7 @@ describe('manage nsfs cli account flow', () => {
11691172
const account_options = { config_root, wide: 'TRUE' };
11701173
const action = ACTIONS.LIST;
11711174
const res = await exec_manage_cli(type, action, account_options);
1175+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
11721176
expect(JSON.parse(res).response.reply.map(item => item.name))
11731177
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
11741178
// added additional properties that we can see with wide option (uid, new_buckets_path)
@@ -1183,6 +1187,7 @@ describe('manage nsfs cli account flow', () => {
11831187
const account_options = { config_root, wide: 'false' };
11841188
const action = ACTIONS.LIST;
11851189
const res = await exec_manage_cli(type, action, account_options);
1190+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
11861191
expect(JSON.parse(res).response.reply.map(item => item.name))
11871192
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
11881193
});
@@ -1191,6 +1196,7 @@ describe('manage nsfs cli account flow', () => {
11911196
const account_options = { config_root, wide: 'FALSE' };
11921197
const action = ACTIONS.LIST;
11931198
const res = await exec_manage_cli(type, action, account_options);
1199+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
11941200
expect(JSON.parse(res).response.reply.map(item => item.name))
11951201
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
11961202
});
@@ -1218,6 +1224,7 @@ describe('manage nsfs cli account flow', () => {
12181224
} catch (e) {
12191225
res = e;
12201226
}
1227+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12211228
expect(JSON.parse(res).response.reply.map(item => item.name))
12221229
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
12231230
});
@@ -1226,6 +1233,7 @@ describe('manage nsfs cli account flow', () => {
12261233
const account_options = { config_root, uid: 999 };
12271234
const action = ACTIONS.LIST;
12281235
const res = await exec_manage_cli(type, action, account_options);
1236+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12291237
expect(JSON.parse(res).response.reply.map(item => item.name))
12301238
.toEqual(expect.arrayContaining(['account3', 'account1']));
12311239
});
@@ -1234,6 +1242,7 @@ describe('manage nsfs cli account flow', () => {
12341242
const account_options = { config_root, gid: 999 };
12351243
const action = ACTIONS.LIST;
12361244
const res = await exec_manage_cli(type, action, account_options);
1245+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12371246
expect(JSON.parse(res).response.reply.map(item => item.name))
12381247
.toEqual(expect.arrayContaining(['account1']));
12391248
});
@@ -1242,6 +1251,7 @@ describe('manage nsfs cli account flow', () => {
12421251
const account_options = { config_root, uid: 999, gid: 999 };
12431252
const action = ACTIONS.LIST;
12441253
const res = await exec_manage_cli(type, action, account_options);
1254+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12451255
expect(JSON.parse(res).response.reply.map(item => item.name))
12461256
.toEqual(expect.arrayContaining(['account1']));
12471257
});
@@ -1250,6 +1260,7 @@ describe('manage nsfs cli account flow', () => {
12501260
const account_options = { config_root, uid: 999, gid: 888 };
12511261
const action = ACTIONS.LIST;
12521262
const res = await exec_manage_cli(type, action, account_options);
1263+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12531264
expect(JSON.parse(res).response.reply.map(item => item.name))
12541265
.toEqual(expect.arrayContaining(['account3']));
12551266
});
@@ -1265,6 +1276,7 @@ describe('manage nsfs cli account flow', () => {
12651276
const account_options = { config_root, user: 'root' };
12661277
const action = ACTIONS.LIST;
12671278
const res = await exec_manage_cli(TYPES.ACCOUNT, action, account_options);
1279+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12681280
expect(JSON.parse(res).response.reply.map(item => item.name))
12691281
.toEqual(expect.arrayContaining(['account4']));
12701282
});
@@ -1273,6 +1285,7 @@ describe('manage nsfs cli account flow', () => {
12731285
const account_options = { config_root, user: 'shaul' };
12741286
const action = ACTIONS.LIST;
12751287
const res = await exec_manage_cli(TYPES.ACCOUNT, action, account_options);
1288+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12761289
expect(JSON.parse(res).response.reply.map(item => item.name))
12771290
.toEqual([]);
12781291
});
@@ -1281,6 +1294,7 @@ describe('manage nsfs cli account flow', () => {
12811294
const account_options = { config_root, access_key: 'GIGiFAnjaaE7OKD5N7hA' };
12821295
const action = ACTIONS.LIST;
12831296
const res = await exec_manage_cli(TYPES.ACCOUNT, action, account_options);
1297+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12841298
expect(JSON.parse(res).response.reply.map(item => item.name))
12851299
.toEqual(expect.arrayContaining(['account1']));
12861300
});
@@ -1289,14 +1303,25 @@ describe('manage nsfs cli account flow', () => {
12891303
const account_options = { config_root, name: 'account3' };
12901304
const action = ACTIONS.LIST;
12911305
const res = await exec_manage_cli(TYPES.ACCOUNT, action, account_options);
1306+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
12921307
expect(JSON.parse(res).response.reply.map(item => item.name))
12931308
.toEqual(expect.arrayContaining(['account3']));
12941309
});
12951310

1311+
it('cli list filter by name (non-existing-account) - (none)', async () => {
1312+
const account_options = { config_root, name: 'non-existing-account' };
1313+
const action = ACTIONS.LIST;
1314+
const res = await exec_manage_cli(TYPES.ACCOUNT, action, account_options);
1315+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
1316+
expect(JSON.parse(res).response.reply.map(item => item.name))
1317+
.toEqual([]);
1318+
});
1319+
12961320
it('cli list filter by access key (of account1) and name (of account3) - (none)', async () => {
12971321
const account_options = { config_root, name: 'account3', access_key: 'GIGiFAnjaaE7OKD5N7hA' };
12981322
const action = ACTIONS.LIST;
12991323
const res = await exec_manage_cli(TYPES.ACCOUNT, action, account_options);
1324+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
13001325
expect(JSON.parse(res).response.reply.map(item => item.name))
13011326
.toEqual([]);
13021327
});
@@ -1305,6 +1330,7 @@ describe('manage nsfs cli account flow', () => {
13051330
const account_options = { config_root, name: 'account3', access_key: 'non-existing-access-key' };
13061331
const action = ACTIONS.LIST;
13071332
const res = await exec_manage_cli(TYPES.ACCOUNT, action, account_options);
1333+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
13081334
expect(JSON.parse(res).response.reply.map(item => item.name))
13091335
.toEqual([]);
13101336
});
@@ -1320,6 +1346,7 @@ describe('manage nsfs cli account flow', () => {
13201346
const account_options = { config_root, wide: true, show_secrets: true };
13211347
const action = ACTIONS.LIST;
13221348
const res = await exec_manage_cli(type, action, account_options);
1349+
expect(Array.isArray(JSON.parse(res).response.reply)).toBe(true);
13231350
expect(JSON.parse(res).response.reply.map(item => item.name))
13241351
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
13251352
const res_arr = JSON.parse(res).response.reply;

0 commit comments

Comments
 (0)