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

fixed index cache feature #137

Merged
merged 3 commits into from
Jan 29, 2016
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
37 changes: 32 additions & 5 deletions bin/kuzzle-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,45 @@ module.exports = function () {
);

console.log(`
████████████████████████████████████
██ KUZZLE ` + (kuzzle.isServer ? 'SERVER' : 'WORKER') + ` STARTED ██
████████████████████████████████████`);
████████████████████████████████████
██ KUZZLE ` + (kuzzle.isServer ? 'SERVER' : 'WORKER') + ` STARTED ██`);

if (kuzzle.isServer) {
console.log(`
██ ...WAITING FOR WORKERS... ██
████████████████████████████████████`);
} else {
console.log(' ████████████████████████████████████');
}
})
.then(() => {
/*
Waits for at least one write worker to be connected to the server before trying to use them
*/
return kuzzle.services.list.broker.waitForListeners(kuzzle.config.queues.workerWriteTaskQueue);
})
.then(() => { return kuzzle.cleanDb(); })
.then(() => { return kuzzle.prepareDb(); })
.then(() => {
if (kuzzle.isServer) {
console.log(`
████████████████████████████████████
██ WORKER CONNECTED ██
██ ...PREPARING DATABASE... ██
████████████████████████████████████`);
}

return kuzzle.cleanDb(kuzzle);
})
.then(() => {
return kuzzle.prepareDb(kuzzle);
})
.then(() => {
if (kuzzle.isServer) {
console.log(`
████████████████████████████████████
██ KUZZLE READY ██
████████████████████████████████████`);
}
})
.catch(error => {
console.error(error);
process.exit(1);
Expand Down
2 changes: 0 additions & 2 deletions lib/api/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ function Kuzzle () {
this.workers = new Workers(this);
this.services = new Services(this);

this.indexes = {};

// Add methods
this.cleanDb = require('./cleanDb');
this.prepareDb = require('./prepareDb');
Expand Down
13 changes: 7 additions & 6 deletions lib/api/cleanDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ var
RequestObject = require('./core/models/requestObject'),
q = require('q');

module.exports = function () {
module.exports = function (kuzzle) {
var
deferred = q.defer(),
requestObject = new RequestObject({controller: 'admin', action: 'deleteIndexes'});

// is a reset has been asked and we are launching a server ?
if (process.env.LIKE_A_VIRGIN !== '1' || !this.isServer) {
if (process.env.LIKE_A_VIRGIN !== '1' || !kuzzle.isServer) {
deferred.resolve();
return deferred.promise;
}

// @todo : manage internal index properly
this.pluginsManager.trigger('data:deleteIndexes', requestObject);
kuzzle.pluginsManager.trigger('data:deleteIndexes', requestObject);

this.workerListener.add(requestObject)
kuzzle.workerListener.add(requestObject)
.then(() => {
this.pluginsManager.trigger('cleanDb:done', 'Reset done: Kuzzle is now like a virgin, touched for the very first time !');
kuzzle.indexCache.reset();
kuzzle.pluginsManager.trigger('cleanDb:done', 'Reset done: Kuzzle is now like a virgin, touched for the very first time !');

deferred.resolve();
})
.catch(err => {
this.pluginsManager.trigger('cleanDb:error', err);
kuzzle.pluginsManager.trigger('cleanDb:error', err);

// We resolve the promise, because we don't want stop the start process just because reset didn't work.
// (can fail if the database is pristine)
Expand Down
30 changes: 25 additions & 5 deletions lib/api/controllers/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module.exports = function AdminController (kuzzle) {
*/
this.deleteCollection = function (requestObject) {
kuzzle.pluginsManager.trigger('data:deleteCollection', requestObject);
return kuzzle.workerListener.add(requestObject);
return kuzzle.workerListener.add(requestObject)
.then(response => {
kuzzle.indexCache.remove(requestObject.index, requestObject.collection);
return response;
});
};

/**
Expand All @@ -16,7 +20,11 @@ module.exports = function AdminController (kuzzle) {
*/
this.updateMapping = function (requestObject) {
kuzzle.pluginsManager.trigger('data:updateMapping', requestObject);
return kuzzle.workerListener.add(requestObject);
return kuzzle.workerListener.add(requestObject)
.then(response => {
kuzzle.indexCache.add(requestObject.index, requestObject.collection);
return response;
});
};

/**
Expand Down Expand Up @@ -81,7 +89,11 @@ module.exports = function AdminController (kuzzle) {
*/
this.deleteIndexes = function (requestObject) {
kuzzle.pluginsManager.trigger('data:deleteIndexes', requestObject);
return kuzzle.workerListener.add(requestObject);
return kuzzle.workerListener.add(requestObject)
.then(response => {
kuzzle.indexCache.reset();
return response;
});
};

/**
Expand All @@ -91,7 +103,11 @@ module.exports = function AdminController (kuzzle) {
*/
this.createIndex = function (requestObject) {
kuzzle.pluginsManager.trigger('data:createIndex', requestObject);
return kuzzle.workerListener.add(requestObject);
return kuzzle.workerListener.add(requestObject)
.then(response => {
kuzzle.indexCache.add(requestObject.index);
return response;
});
};

/**
Expand All @@ -101,7 +117,11 @@ module.exports = function AdminController (kuzzle) {
*/
this.deleteIndex = function (requestObject) {
kuzzle.pluginsManager.trigger('data:deleteIndex', requestObject);
return kuzzle.workerListener.add(requestObject);
return kuzzle.workerListener.add(requestObject)
.then(response => {
kuzzle.indexCache.remove(requestObject.index);
return response;
});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/api/controllers/funnelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function FunnelController (kuzzle) {
}

// check if the current user is allowed to process
if (context.token.user.profile.isActionAllowed(requestObject, context, kuzzle.indexes) === false) {
if (context.token.user.profile.isActionAllowed(requestObject, context, kuzzle.indexCache.indexes) === false) {
return q.reject(new UnauthorizedError('Unauthorized action [' + requestObject.index + '/' + requestObject.collection + '/' + requestObject.controller + '/' + requestObject.action + '] for user ' + context.token.user._id, 401));
}

Expand Down
14 changes: 11 additions & 3 deletions lib/api/controllers/writeController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function WriteController(kuzzle) {
module.exports = function WriteController (kuzzle) {
/**
* Create a new document
*
Expand Down Expand Up @@ -34,9 +34,13 @@ module.exports = function WriteController(kuzzle) {
*/
this.createOrReplace = function (requestObject) {
return requestObject.isValid()
.then(function () {
.then(() => {
kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
return kuzzle.workerListener.add(requestObject);
})
.then(response => {
kuzzle.indexCache.add(requestObject.index, requestObject.collection);
return response;
});
};

Expand Down Expand Up @@ -95,6 +99,10 @@ module.exports = function WriteController(kuzzle) {
*/
this.createCollection = function (requestObject) {
kuzzle.pluginsManager.trigger('data:createCollection', requestObject);
return kuzzle.workerListener.add(requestObject);
return kuzzle.workerListener.add(requestObject)
.then(response => {
kuzzle.indexCache.add(requestObject.index, requestObject.collection);
return response;
});
};
};
2 changes: 1 addition & 1 deletion lib/api/core/hotelClerk.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module.exports = function HotelClerk (kuzzle) {
requestObjectCollection.index = room.index;
requestObjectCollection.collection = room.collection;

if (context.token.user.profile.isActionAllowed(requestObjectCollection, context, kuzzle.indexes) === false) {
if (context.token.user.profile.isActionAllowed(requestObjectCollection, context, kuzzle.indexCache.indexes) === false) {
collectionNotAllowed.push(room.index + '.' + room.collection);
return true;
}
Expand Down
63 changes: 63 additions & 0 deletions lib/api/core/indexCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var
RequestObject = require('./models/requestObject');

/**
* Index/collection cache management
*/
module.exports = function indexCache (kuzzle) {
this.indexes = {};

this.init = function () {
kuzzle.services.list.readEngine.listIndexes(new RequestObject({}))
.then(result => {
result.data.body.indexes.forEach(index => {
this.indexes[index] = [];

kuzzle.services.list.readEngine.listCollections(new RequestObject({index: index}))
.then(resultCollections => {
this.indexes[index] = resultCollections.data.body.collections.stored;
});
});
});
};

this.add = function (index, collection) {
if (index !== undefined) {
if (!this.indexes[index]) {
this.indexes[index] = [];
}

if (collection) {
if (this.indexes[index].indexOf(collection) === -1) {
this.indexes[index].push(collection);
}
}
}
};

this.remove = function (index, collection) {
var position;

if (index && this.indexes[index]) {
if (collection) {
position = this.indexes[index].indexOf(collection);

if (position >= 0) {
this.indexes[index].splice(position, 1);
}
}
else {
delete this.indexes[index];
}
}
};

this.reset = function (index) {
if (index !== undefined) {
this.indexes[index] = [];
}
else {
this.indexes = {};
}
};
};
20 changes: 10 additions & 10 deletions lib/api/core/models/security/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ function Role () {
}

function doesIndexExist(requestObject, indexes) {
return indexes[requestObject.index] !== undefined;
return indexes && indexes[requestObject.index] !== undefined;
}

function doesCollectionExist(requestObject, indexes) {
return _.contains(indexes[requestObject.index], requestObject.collection);
return indexes && _.contains(indexes[requestObject.index], requestObject.collection);
}

/**
* @param requestObject
* @param context
* @param indexes
* @param context - user's context
* @param indexes - list of existing indexes/collections
* @returns {*}
*/
Role.prototype.isActionAllowed = function (requestObject, context, indexes) {
Expand Down Expand Up @@ -62,10 +62,10 @@ Role.prototype.isActionAllowed = function (requestObject, context, indexes) {
return false;
}

if (this.indexes._canCreate !== undefined &&
if (!doesIndexExist(requestObject, indexes) &&
this.indexes._canCreate !== undefined &&
!this.indexes._canCreate &&
_.contains(['import', 'create', 'createCollection', 'updateMapping', 'createOrReplace'], requestObject.action) &&
!doesIndexExist(requestObject, indexes)) {
_.contains(['import', 'create', 'createCollection', 'updateMapping', 'createOrReplace'], requestObject.action)) {
return false;
}

Expand All @@ -86,10 +86,10 @@ Role.prototype.isActionAllowed = function (requestObject, context, indexes) {
return false;
}

if (indexRights.collections._canCreate !== undefined &&
if (!doesCollectionExist(requestObject, indexes) &&
indexRights.collections._canCreate !== undefined &&
!indexRights.collections._canCreate &&
_.contains(['import', 'create', 'createCollection', 'updateMapping', 'createOrReplace'], requestObject.action) &&
!doesCollectionExist(requestObject, indexes)) {
_.contains(['import', 'create', 'createCollection', 'updateMapping', 'createOrReplace'], requestObject.action)) {
return false;
}

Expand Down
Loading