Skip to content

Commit

Permalink
addition and deletion of aspect writers are carried forward to cached…
Browse files Browse the repository at this point in the history
… aspect
  • Loading branch information
shriramshankar committed Apr 20, 2017
1 parent 4738577 commit 5e55802
Show file tree
Hide file tree
Showing 7 changed files with 568 additions and 11 deletions.
1 change: 0 additions & 1 deletion api/v1/controllers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
const featureToggles = require('feature-toggles');
const sampleStore = require('../../../cache/sampleStore');
const sampleStoreInit = require('../../../cache/sampleStoreInit');
const sampleStorePersist = require('../../../cache/sampleStorePersist');
const apiErrors = require('../apiErrors');
const httpStatus = require('../constants').httpStatus;
const authUtils = require('../helpers/authUtils');
Expand Down
90 changes: 85 additions & 5 deletions api/v1/controllers/aspects.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const doPost = require('../helpers/verbs/doPost');
const doPut = require('../helpers/verbs/doPut');
const u = require('../helpers/verbs/utils');
const httpStatus = require('../constants').httpStatus;
const ZERO = 0;
const ONE = 1;
const redisOps = require('../../../cache/redisOps');

/**
* Validates the given fields from request body or url.
Expand Down Expand Up @@ -175,10 +174,37 @@ module.exports = {
const toPost = params.queryBody.value;
const options = {};
options.where = u.whereClauseForNameInArr(toPost);
let users;
userProps.model.findAll(options)
.then((usrs) => {
users = usrs;
if (featureToggles.isFeatureEnabled('enableRedisSampleStore')) {
return u.findByKey(helper, params)
.then((o) => u.isWritable(req, o,
featureToggles.isFeatureEnabled('enforceWritePermission')))
.then((o) => redisOps.getValue('aspect', o.name))
.then((cachedAspect) => {
if (cachedAspect) {
const userSet = new Set();
usrs.forEach((user) => {
userSet.add(user.dataValues.name);
});
cachedAspect.writers = cachedAspect.writers || [];
Array.from(userSet).forEach((user) => {
cachedAspect.writers.push(user);
});
return redisOps.hmSet('aspect', cachedAspect.name, cachedAspect);
}

return Promise.resolve(true);
})
.catch((err) => Promise.resolve(err));
}
return Promise.resolve(true);
})
.then(() => {
doPostAssoc(req, res, next, helper,
helper.belongsToManyAssoc.users, usrs);
helper.belongsToManyAssoc.users, users);
});
}, // postAspectWriters

Expand Down Expand Up @@ -237,7 +263,25 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
deleteAspectWriters(req, res, next) {
doDeleteAllAssoc(req, res, next, helper, helper.belongsToManyAssoc.users);
if (featureToggles.isFeatureEnabled('enableRedisSampleStore')) {
const params = req.swagger.params;
u.findByKey(helper, params)
.then((o) => u.isWritable(req, o,
featureToggles.isFeatureEnabled('enforceWritePermission')))
.then((o) => redisOps.getValue('aspect', o.name))
.then((cachedAspect) => {
if (cachedAspect) {
cachedAspect.writers = [];
return redisOps.hmSet('aspect', cachedAspect.name, cachedAspect);
}
return Promise.resolve(true);
})
.then(() => doDeleteAllAssoc(req, res, next, helper,
helper.belongsToManyAssoc.users))
.catch((err) => u.handleError(next, err, helper.modelName));
} else {
doDeleteAllAssoc(req, res, next, helper, helper.belongsToManyAssoc.users);
}
},

/**
Expand All @@ -251,8 +295,44 @@ module.exports = {
*/
deleteAspectWriter(req, res, next) {
const userNameOrId = req.swagger.params.userNameOrId.value;
doDeleteOneAssoc(req, res, next, helper,
let aspectName;
let userName;
if (featureToggles.isFeatureEnabled('enableRedisSampleStore')) {
const params = req.swagger.params;
u.findByKey(helper, params)
.then((o) => u.isWritable(req, o,
featureToggles.isFeatureEnabled('enforceWritePermission')))
.then((o) => {
aspectName = o.name;
console.log('aspectName', aspectName);
const options = {};
options.where = u.whereClauseForNameOrId(params.userNameOrId.value);
return u.findAssociatedInstances(helper,
params, helper.belongsToManyAssoc.users, options);
})
.then((o) => {
console.log('userREturned', o);
u.throwErrorForEmptyArray(o,
params.userNameOrId.value, userProps.modelName);
userName = o[0].dataValues.name;
return redisOps.getValue('aspect', aspectName);
})
.then((cachedAspect) => {
if (cachedAspect) {
console.log('userName to delete', userName);
cachedAspect.writers = cachedAspect.writers
.filter((writer) => writer !== userName);
return redisOps.hmSet('aspect', cachedAspect.name, cachedAspect);
}
return Promise.resolve(true);
})
.then(() => doDeleteOneAssoc(req, res, next, helper,
helper.belongsToManyAssoc.users, userNameOrId))
.catch((err) => u.handleError(next, err, helper.modelName));
} else {
doDeleteOneAssoc(req, res, next, helper,
helper.belongsToManyAssoc.users, userNameOrId);
}
},

/**
Expand Down
1 change: 0 additions & 1 deletion api/v1/controllers/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
'use strict'; // eslint-disable-line strict

const featureToggles = require('feature-toggles');
const apiErrors = require('../apiErrors');
const helper = require('../helpers/nouns/samples');
const subHelper = require('../helpers/nouns/subjects');
const doDelete = require('../helpers/verbs/doDelete');
Expand Down
25 changes: 22 additions & 3 deletions cache/redisOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

const redisStore = require('./sampleStore');
const redisClient = require('./redisCache').client.sampleStore;
const rsConstant = redisStore.constants;
const subjectType = redisStore.constants.objectType.subject;
const aspectType = redisStore.constants.objectType.aspect;
const sampleType = redisStore.constants.objectType.sample;
Expand Down Expand Up @@ -45,6 +46,23 @@ function hmSet(objectName, name, value) {
.catch((err) => Promise.reject(err));
} // hmSet

/**
* Get the value that is mapped to a key
* @param {String} type - The type of the object on which the operation is to
* be performed
* @param {String} name - Name of the key
* @returns {Promise} - which resolves to the value associated with the key
*/
function getValue(type, name) {
const nameKey = redisStore.toKey(type, name);
return redisClient.hgetallAsync(nameKey)
.then((value) => {
redisStore.arrayStringsToJson(value, rsConstant.fieldsToStringify[type]);
return Promise.resolve(value);
})
.catch((err) => Promise.reject(err));
} // getValue

/**
* Adds an entry identified by name to the master list of indices identified
* by "type"
Expand Down Expand Up @@ -246,7 +264,7 @@ function renameKeys(type, objectName, oldName, newName) {
*
* @param {Array} arr Contains strings
* @param {String} str The string to check against strings in array
* @retuns {Boolean} whether the string is unique or not
* @returns {Boolean} whether the string is unique or not
*/
function isStringInArray(arr, str) {
let isStringUnique = true;
Expand Down Expand Up @@ -325,7 +343,6 @@ module.exports = {
* @returns {Promise} - to update the subject
*/
addAspectNameToSubject(subjKey, subjectId, name) {
let isNameUnique = true;
return redisClient.hgetallAsync(subjKey)
.then((subject) => {
const aspectNames = JSON.parse(subject.aspectNames || '[]');
Expand All @@ -352,7 +369,7 @@ module.exports = {
.then((subject) => {

// if case insensitive match, return true
let aspects = JSON.parse(subject.aspectNames);
const aspects = JSON.parse(subject.aspectNames);
return !isStringInArray(aspects, aspName);
});
},
Expand Down Expand Up @@ -464,4 +481,6 @@ module.exports = {
aspectType,

sampleType,

getValue,
}; // export
2 changes: 1 addition & 1 deletion cache/sampleStoreInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function populateAspects() {
const aspectIdx = [];
const cmds = [];

// for each aspect, add its writers to its writers field.
// for each aspect, add the associated writers to its "writers" field.
for (let i = 0; i < aspects.length; i++) {
const a = aspects[i];
a.dataValues.writers = [];
Expand Down

0 comments on commit 5e55802

Please sign in to comment.