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

feat: partitioned index #225

Merged
merged 1 commit into from
Nov 15, 2019
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
1 change: 1 addition & 0 deletions lib/cloudant.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ Cloudant.prototype.ping = function(cb) {
// mixins
require('./view')(Cloudant);
require('./geo')(Cloudant);
require('./migrate')(Cloudant);
207 changes: 207 additions & 0 deletions lib/migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: loopback-connector-cloudant
// This file is licensed under the Apache License 2.0.
// License text available at https://opensource.org/licenses/Apache-2.0

'use strict';

const _ = require('lodash');
const inspect = require('util').inspect;
const debug = require('debug')('loopback:connector:cloudant');

module.exports = mixinMigrate;

function mixinMigrate(Cloudant) {
/**
* Used in function `migrateOrUpdateIndex`.
* Create index with index object.
* @param {Object} mo The model configuration
* @param {String} model The model name.
* @param {String} name The index name.
* @param {Object} indexObj The index object
* e.g. {fields: [{"foo": "asc"}, {"bar": "asc"}], partitioned: true}
* @param {Function} cb
*/
Cloudant.prototype._createIndex = function(mo, model, name, indexObj, cb) {
const self = this;
debug('createIndex fields before coercion: %s', inspect(indexObj.fields));
let fields = self.coerceIndexFields(indexObj.fields);
debug('createIndex fields after coercion: %s', inspect(fields));
fields = self.addModelViewToIndex(mo.modelView, fields);
debug('createIndex fields after add model index: %s', inspect(fields));
// naming convertion: '_design/LBModel__Foo__LBIndex__foo_index',
// here the driver api takes in the name without prefix '_design/'
const config = {
ddocName: self.getIndexModelPrefix(mo) + '__' + model + '__' +
self.getIndexPropertyPrefix(mo) + '__' + name,
indexName: name,
fields: fields,
partitioned: indexObj.partitioned,
};
self.createIndexWithConfig(config, cb);
};

/**
* Used in function `getModifyIndexes()`.
* Generate indexes for model properties that are configured as
* `{index: true}`, return model property indexes as objects,
* e.g. {fields: [{name: 'asc'}], partitioned: true}.
*
* @param {Object} indexes indexes from model config, retrieved in
* `getModifyIndexes()`
*/
Cloudant.prototype._generatePropertyLevelIndexes = function(indexes) {
const results = {};
for (const key in indexes) {
const field = {};
// By default the order will be `asc`, partitioned is false,
// please create Model level index if you need `desc` or partitioned index
field[key.split('_index')[0]] = 'asc';
const fields = [field];
results[key] = {fields: fields, partitioned: false};
}
return results;
};

/**
* Used in function `getModifyIndexes()`.
* Generate indexes for indexes defined in the model config.
* Return indexes as objects, e.g.
* {fields: [{foo: 'asc'}, {bar: 'desc'}], partitioned: true}
*
* @param {Object} indexes indexes from model config, provided by
* `getModifyIndexes()`
*/
Cloudant.prototype._generateModelLevelIndexes = function(indexes, cb) {
const results = {};
for (const key in indexes) {
const keys = indexes[key].keys;
if (!keys || typeof keys !== 'object') return cb(new Error(
'the keys in your model index are not well defined! please see' +
'https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html#indexes',
));

const partitioned = indexes[key].partitioned || false;

const fields = [];
_.forEach(keys, function(value, key) {
const obj = {};
let order;
if (keys[key] === 1) order = 'asc';
else order = 'desc';
obj[key] = order;
fields.push(obj);
});
results[key] = {fields, partitioned};
}
return results;
};

/**
* Perform the indexes comparison for `autoupdate`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function for comparing if indexes exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agnes512 yep, it returns the new indexes to add and old indexes to drop

* @param {Object} newIndexes
* newIndexes in format:
* ```js
* {
* indexName: {fields: [{afield: 'asc'}], partitioned: false},
* compositeIndexName: {fields: [{field1: 'asc'}, {field2: 'asc'}], partitioned: true}
* }
* ```
* @param {Object} oldIndexes
* oldIndexes in format:
* ```js
* {
* indexName: {
* ddoc: '_design/LBModel__Foo__LBIndex__bar_index',
* fields: [{afield: 'asc'}],
* partitioned: true
* }
* }
* ```
* @callback {Function} cb The callback function
* @param {Object} result indexes to add and drop after comparison
* ```js
* result: {indexesToAdd: {$someIndexes}, indexesToDrop: {$someIndexes}}
* ```
*/
Cloudant.prototype.compare = function(newIndexes, oldIndexes, cb) {
debug('Cloudant.prototype.compare');
const result = {};
let indexesToDrop = {};
let indexesToAdd = {};
let iAdd;
for (const niKey in newIndexes) {
if (!oldIndexes.hasOwnProperty(niKey)) {
// Add item to `indexesToAdd` if it's new
iAdd = {};
iAdd[niKey] = newIndexes[niKey];
indexesToAdd = _.merge(indexesToAdd, iAdd);
} else {
if (arrEqual(newIndexes[niKey].fields, oldIndexes[niKey].fields)) {
// Don't change it if index already exists
delete oldIndexes[niKey];
} else {
// Update index if fields change
iAdd = {};
const iDrop = {};
iAdd[niKey] = newIndexes[niKey];
indexesToAdd = _.merge(indexesToAdd, iAdd);
}

function arrEqual(arr1, arr2) {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) return false;
let isEqual = true;
for (const item in arr1) {
const i = _.findIndex(arr2, arr1[item]);
isEqual = isEqual && (i > -1);
}
return isEqual;
}
}
}
indexesToDrop = oldIndexes;
result.indexesToAdd = indexesToAdd;
result.indexesToDrop = indexesToDrop;
debug('result for compare: %s', inspect(result, {depth: null}));
return result;
};

/**
* Create an index in cloudant with the configuration properties of an index body.
* This method is created to replace `createIndex`, which only takes in
* limited parameters(ddocName, indexName, fields).
* @param {Object} config The index config properties in format
* ```
* {
* fields: Array,
* ddocName: String,
* indexName: String,
* partitioned: Boolean,
* ...otherProperties: any
* }
* ```
* @callback {Function} cb The callback function
*/
Cloudant.prototype.createIndexWithConfig = function(config, cb) {
debug('createIndexWithConfig: config %s', config);
const self = this;
const indexBody = {
index: {
fields: config.fields,
},
partitioned: config.partitioned || false,
ddoc: config.ddocName,
name: config.indexName,
type: 'json',
};

const requestObject = {
db: self.settings.database,
path: '_index',
method: 'post',
body: indexBody,
};

self.getDriverInst().request(requestObject, cb);
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"debug": "^4.1.1",
"lodash": "^4.17.11",
"loopback-connector": "^4.0.0",
"loopback-connector-couchdb2": "^1.2.0",
"loopback-connector-couchdb2": "^1.5.1",
"request": "^2.81.0",
"strong-globalize": "^5.0.0"
},
Expand Down
26 changes: 25 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ms = require('ms');
const mochaBin = require.resolve('mocha/bin/_mocha');

process.env.CLOUDANT_DATABASE = 'test-db';
process.env.CLOUDANT_PARTITIONED_DATABASE = 'test-partitioned-db';
process.env.CLOUDANT_PASSWORD = 'pass';
process.env.CLOUDANT_USERNAME = 'admin';

Expand All @@ -37,7 +38,8 @@ async.waterfall([
setCloudantEnv,
waitFor('/_all_dbs'),
createAdmin(),
createDB('test-db'),
createDB(process.env.CLOUDANT_DATABASE),
createPartitionedDB(process.env.CLOUDANT_PARTITIONED_DATABASE),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to be called in other places?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jannyHou showed me where it's being used.

run([mochaBin, 'test/*.test.js', 'node_modules/juggler-v3/test.js',
'node_modules/juggler-v4/test.js', '--timeout', '40000',
'--require', 'strong-mocha-interfaces', '--require', 'test/init.js',
Expand Down Expand Up @@ -208,6 +210,28 @@ function createDB(db) {
};
}

function createPartitionedDB(db) {
return function create(container, next) {
const opts = {
method: 'PUT',
path: '/' + db + '?partitioned=true',
host: process.env.CLOUDANT_HOST,
port: process.env.CLOUDANT_PORT,
auth: process.env.CLOUDANT_USERNAME + ':' + process.env.CLOUDANT_PASSWORD,
};
console.log('creating db: %j', db);
http.request(opts, function(res) {
res.pipe(devNull());
res.on('error', next);
res.on('end', function() {
setImmediate(next, null, container);
});
})
.on('error', next)
.end();
};
}

function run(cmd) {
return function spawnNode(container, next) {
console.log('running mocha...');
Expand Down
Loading