Skip to content

Commit 0a3df35

Browse files
indexes: add re of indexes
1 parent 19b2343 commit 0a3df35

File tree

1 file changed

+95
-81
lines changed

1 file changed

+95
-81
lines changed

reverse_engineering/api.js

Lines changed: 95 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ module.exports = {
117117
const offerInfo = await getOfferType(collection);
118118
const { autopilot, throughput } = getOfferProps(offerInfo);
119119
const partitionKey = getPartitionKey(collection);
120-
const bucketInfo = {
120+
const indexes = getIndexes(collection.indexingPolicy);
121+
const bucketInfo = Object.assign({
121122
dbId: data.database,
122123
throughput,
123124
autopilot,
@@ -128,8 +129,7 @@ module.exports = {
128129
udfs,
129130
TTL: getTTL(collection.defaultTtl),
130131
TTLseconds: collection.defaultTtl
131-
};
132-
const indexes = getIndexes(collection.indexingPolicy);
132+
}, indexes);
133133

134134
await gremlinHelper.connect({ collection: collectionName });
135135
const nodesData = await getNodesData(collectionName, labels, logger, {
@@ -215,7 +215,6 @@ const getNodesData = (dbName, labels, logger, data) => {
215215
includeEmptyCollection: data.includeEmptyCollection,
216216
fieldInference: data.fieldInference,
217217
indexes: [],
218-
bucketIndexes: data.indexes,
219218
bucketInfo: data.bucketInfo,
220219
partitionKey: data.partitionKey,
221220
});
@@ -385,14 +384,6 @@ async function listCollections(databaseId) {
385384
return containers;
386385
}
387386

388-
function getIndexes(indexingPolicy){
389-
const rangeIndexes = getRangeIndexes(indexingPolicy);
390-
const spatialIndexes = getSpatialIndexes(indexingPolicy);
391-
const compositeIndexes = getCompositeIndexes(indexingPolicy);
392-
393-
return rangeIndexes.concat(spatialIndexes).concat(compositeIndexes);
394-
}
395-
396387
async function getOfferType(collection) {
397388
const querySpec = {
398389
query: 'SELECT * FROM root r WHERE r.resource = @link',
@@ -407,82 +398,14 @@ async function getOfferType(collection) {
407398
return offer.length > 0 && offer[0];
408399
}
409400

410-
function getRangeIndexes(indexingPolicy) {
411-
let rangeIndexes = [];
412-
const excludedPaths = indexingPolicy.excludedPaths.map(({ path }) => path).join(', ');
413-
414-
if(indexingPolicy) {
415-
indexingPolicy.includedPaths.forEach((item, i) => {
416-
if (item.indexes) {
417-
const indexes = item.indexes.map((index, j) => {
418-
return {
419-
name: `New Index(${j+1})`,
420-
indexPrecision: index.precision,
421-
automatic: indexingPolicy.automatic,
422-
mode: indexingPolicy.indexingMode,
423-
indexIncludedPath: item.path,
424-
indexExcludedPath: excludedPaths,
425-
dataType: index.dataType,
426-
kind: index.kind
427-
};
428-
});
429-
rangeIndexes = rangeIndexes.concat(rangeIndexes, indexes);
430-
} else {
431-
const index = {
432-
name: `New Index(${i+1})`,
433-
automatic: indexingPolicy.automatic,
434-
mode: indexingPolicy.indexingMode,
435-
indexIncludedPath: item.path,
436-
indexExcludedPath: excludedPaths,
437-
kind: 'Range'
438-
}
439-
rangeIndexes.push(index);
440-
}
441-
});
442-
}
443-
return rangeIndexes;
444-
}
445-
446-
function getSpatialIndexes(indexingPolicy) {
447-
if (!indexingPolicy.spatialIndexes) {
448-
return [];
449-
}
450-
return indexingPolicy.spatialIndexes.map(item => {
451-
return {
452-
name: 'Spatial index',
453-
automatic: indexingPolicy.automatic,
454-
mode: indexingPolicy.indexingMode,
455-
kind: 'Spatial',
456-
indexIncludedPath: item.path,
457-
dataTypes: item.types.map(type => ({ spatialType: type }))
458-
};
459-
});
460-
}
461-
462-
function getCompositeIndexes(indexingPolicy) {
463-
if (!indexingPolicy.compositeIndexes) {
464-
return [];
465-
}
466-
return indexingPolicy.compositeIndexes.map(item => {
467-
return {
468-
name: 'Composite index',
469-
automatic: indexingPolicy.automatic,
470-
mode: indexingPolicy.indexingMode,
471-
kind: 'Composite',
472-
compositeFields: item.map(({ order, path }) => ({ compositeFieldPath: path, compositeFieldOrder: order }))
473-
};
474-
});
475-
}
476-
477401
function getPartitionKey(collection) {
478402
if (!collection.partitionKey) {
479403
return '';
480404
}
481405
if (!Array.isArray(collection.partitionKey.paths)) {
482406
return '';
483407
}
484-
485-
return collection.partitionKey.paths.join(',');
408+
return collection.partitionKey.paths.map(getKeyPath);
486409
}
487410

488411
function getUniqueKeys(collection) {
@@ -625,3 +548,94 @@ async function getAdditionalAccountInfo(connectionInfo, logger) {
625548
return {};
626549
}
627550
}
551+
552+
function capitalizeFirstLetter(str) {
553+
return str.charAt(0).toUpperCase() + str.slice(1);
554+
}
555+
556+
function getRangeIndex(item) {
557+
return {
558+
kind: item.kind.toLowerCase() === 'hash' ? 'Hash' : 'Range',
559+
dataType: capitalizeFirstLetter(item.dataType),
560+
indexPrecision: !isNaN(Number(item.precision)) ? Number(item.precision) : -1,
561+
};
562+
}
563+
564+
function getIndexes(indexingPolicy){
565+
return {
566+
indexingMode: capitalizeFirstLetter(indexingPolicy.indexingMode || ''),
567+
indexingAutomatic: indexingPolicy.automatic === true ? 'true' : 'false',
568+
includedPaths: (indexingPolicy.includedPaths || []).map((index, i) => {
569+
return {
570+
name: `Included (${i + 1})`,
571+
indexIncludedPath: [getIndexPath(index.path)],
572+
inclIndexes: (index.indexes || []).map(getRangeIndex),
573+
};
574+
}),
575+
excludedPaths: indexingPolicy.excludedPaths.map((index, i) => {
576+
return {
577+
name: `Excluded (${i + 1})`,
578+
indexExcludedPath: [getIndexPath(index.path)],
579+
exclIndexes: (index.indexes || []).map(getRangeIndex),
580+
};
581+
}),
582+
spatialIndexes: (indexingPolicy.spatialIndexes || []).map((index, i) => {
583+
return {
584+
name: `Spatial (${i + 1})`,
585+
indexIncludedPath: [getIndexPath(index.path)],
586+
dataTypes: (index.types || []).map(spatialType => ({
587+
spatialType,
588+
})),
589+
};
590+
}),
591+
compositeIndexes: (indexingPolicy.compositeIndexes || []).map((indexes, i) => {
592+
const compositeFieldPath = indexes.map((index, i) => {
593+
return {
594+
name: getKeyPath(index.path),
595+
type: index.order || 'ascending',
596+
};
597+
}, {});
598+
599+
return {
600+
name: `Composite (${i + 1})`,
601+
compositeFieldPath,
602+
};
603+
}),
604+
};
605+
}
606+
607+
const getIndexPathType = (path) => {
608+
if (/\?$/.test(path)) {
609+
return '?';
610+
} else if (/\*$/.test(path)) {
611+
return '*';
612+
} else {
613+
return '';
614+
}
615+
};
616+
617+
const getIndexPath = (path) => {
618+
const type = getIndexPathType(path);
619+
const name = path.replace(/\/(\?|\*)$/, '');
620+
621+
return {
622+
name: getKeyPath(name),
623+
type,
624+
};
625+
};
626+
627+
const trimKey = (key) => {
628+
const trimRegexp = /^\"([\s\S]+)\"$/i;
629+
630+
if (!trimRegexp.test(key)) {
631+
return key;
632+
}
633+
634+
const result = key.match(trimRegexp);
635+
636+
return result[1] || key;
637+
};
638+
639+
const getKeyPath = (keyPath) => {
640+
return (keyPath || '').split('/').filter(Boolean).map(trimKey).map(item => item === '[]' ? 0 : item).join('.');
641+
};

0 commit comments

Comments
 (0)