Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkkao committed Nov 27, 2015
1 parent 9812e39 commit 257c921
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 70 deletions.
125 changes: 59 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ RigidDB.prototype.debugPrint = function(collection) {
return this.client.zrange(`${this.prefix}:${collection}:ids`, 0, -1).then(ids =>
ids.reduce((sequence, id) => sequence.then(() =>
this.client.hgetall(`${this.prefix}:${collection}:${id}`).then(result => {
result = this._denormalizeAttrsForPrinting(collection, result);
result = this._processRedisAttrsForPrinting(collection, result);
result.id = id;
data.push(result);
})), Promise.resolve())).then(() => {
Expand Down Expand Up @@ -202,7 +202,7 @@ RigidDB.prototype._normalizeAndVerifySchema = function(schema) {
}

if (fieldNames.indexOf(field.name) === -1) {
return { err: `Invalid index field: '${field.name}'` };
return { err: `Invalid index field name: '${field.name}'` };
}

for (let indexFieldProp of Object.keys(field)) {
Expand Down Expand Up @@ -317,7 +317,7 @@ RigidDB.prototype._exec = function(ctx) {
}

if (method === 'get') {
val = this._denormalizeAttrs(ret[3], val);
val = this._processRedisGetReturnValue(ret[3], val);
} else if (method === 'exists') {
val = !!val; // Lua returns 0 (not found) or 1 (found)
} else if (method === 'list' || method === 'find') {
Expand All @@ -341,7 +341,7 @@ RigidDB.prototype._whenSchemaLoaded = function(cb) {
};

RigidDB.prototype._create = function(ctx, collection, attrs) {
let redisAttrs = this._normalizeAttrs(collection, attrs);
let redisAttrs = this._normalizeRedisAttrs(collection, attrs);

if (redisAttrs.err) {
ctx.error = { method: 'create', err: redisAttrs.err };
Expand All @@ -367,7 +367,7 @@ RigidDB.prototype._create = function(ctx, collection, attrs) {
};

RigidDB.prototype._update = function(ctx, collection, id, attrs) {
let redisAttrs = this._normalizeAttrs(collection, attrs);
let redisAttrs = this._normalizeRedisAttrs(collection, attrs);

if (redisAttrs.err) {
ctx.error = { method: 'update', err: redisAttrs.err };
Expand Down Expand Up @@ -464,7 +464,7 @@ RigidDB.prototype._find = function(ctx, collection, attrs) {
return;
}

let redisAttrs = this._normalizeAttrs(collection, attrs);
let redisAttrs = this._normalizeRedisAttrs(collection, attrs);

if (redisAttrs.err) {
ctx.error = { method: 'find', err: redisAttrs.err };
Expand Down Expand Up @@ -507,13 +507,15 @@ RigidDB.prototype._genAllIndices = function(ctx, collection) {
};

RigidDB.prototype._indexName = function(collection, index) {
let fields = index.fields.map(field => field.name);
let fields = index.fields.map(field => field.name).sort().join(':');

return `${this.prefix}:${collection}:i:${fields.sort().join(':')}`;
return `${this.prefix}:${collection}:i:${fields}`;
};

RigidDB.prototype._indexValues = function(index) {
return index.fields.sort((a, b) => (a.name < b.name) ? -1 : ((a.name > b.name) ? 1 : 0)).map(field => {
let compareFunc = (a, b) => (a.name < b.name) ? -1 : ((a.name > b.name) ? 1 : 0);

return index.fields.sort(compareFunc).map(field => {
let valueCode = `values["${field.name}"]`;

if (field.caseInsensitive) {
Expand Down Expand Up @@ -593,27 +595,57 @@ RigidDB.prototype._addValuesVar = function(ctx, attrs) {
genCode(ctx, `}`);
};

RigidDB.prototype._normalizeAttrs = function(collection, attrs) {
RigidDB.prototype._processRedisGetReturnValue = function(collection, redisRetVal) {
let redisObject = {};

while (redisRetVal.length > 0) {
let prop = redisRetVal.shift();
redisObject[prop] = redisRetVal.shift();
}

return this._deNormalizeRedisAttrs(collection, redisObject);
};

RigidDB.prototype._processRedisAttrsForPrinting = function(collection, redisObject) {
redisObject = this._deNormalizeRedisAttrs(collection, redisObject);

for (let prop in redisObject) {
let redisVal = redisObject[prop];
let propType = this.schema[collection].definition[prop].type;

if (redisVal === null) {
redisObject[prop] = '[NULL]';
} else if (propType === 'string') {
redisObject[prop] = `"${redisVal}"`;
} else {
redisObject[prop] = redisVal.toString();
}
}

return redisObject;
};

RigidDB.prototype._normalizeRedisAttrs = function(collection, attrs) {
let redisAttrs = {};
let definition = this.schema[collection].definition;
let schema = this.schema[collection].definition;

for (let prop in attrs) {
let propType = definition[prop];
let definition = schema[prop];
let propVal = attrs[prop];
let redisVal;

if (!propType) {
if (!definition) {
continue;
}

if (propVal === null) {
if (!propType.allowNull) {
if (!definition.allowNull) {
return { err: `nullNotAllowed` };
}

redisVal = '~';
} else {
switch (propType.type) {
switch (definition.type) {
case 'boolean':
redisVal = propVal ? 'true' : 'false';
break;
Expand Down Expand Up @@ -641,76 +673,37 @@ RigidDB.prototype._normalizeAttrs = function(collection, attrs) {
return { val: redisAttrs };
};

RigidDB.prototype._denormalizeAttrs = function(collection, redisRetVal) {
let ret = {};

while (redisRetVal.length > 0) {
let prop = redisRetVal.shift();
let redisVal = redisRetVal.shift();
let propType = this.schema[collection].definition[prop];
RigidDB.prototype._deNormalizeRedisAttrs = function(collection, redisObject) {
for (let prop in redisObject) {
let redisVal = redisObject[prop];
let propType = this.schema[collection].definition[prop].type;

if (redisVal === '~') {
ret[prop] = null;
redisObject[prop] = null;
} else {
switch (propType.type) {
switch (propType) {
case 'boolean':
redisVal = redisVal === 'true';
redisObject[prop] = redisObject[prop] === 'true';
break;
case 'int':
redisVal = parseInt(redisVal);
redisObject[prop] = parseInt(redisVal);
break;
case 'string':
if (/^~+$/.test(redisVal)) {
redisVal = redisVal.substring(1);
redisObject[prop] = redisObject[prop].substring(1);
}
break;
case 'date':
redisVal = new Date(redisVal);
break;
case 'timestamp':
redisVal = new Date(parseInt(redisVal));
break;
}

ret[prop] = redisVal;
}
}

return ret;
};

RigidDB.prototype._denormalizeAttrsForPrinting = function(collection, redisObject) {
let ret = {};

for (let prop of Object.keys(redisObject)) {
let redisVal = redisObject[prop];
let propType = this.schema[collection].definition[prop];

if (redisVal === '~') {
ret[prop] = '[NULL]';
} else {
switch (propType.type) {
case 'int':
ret[prop] = parseInt(redisVal);
break;
case 'string':
if (/^~+$/.test(redisVal)) {
redisVal = redisVal.substring(1);
}

ret[prop] = `"${redisVal}"`;
break;
case 'date':
ret[prop] = new Date(redisVal).toString();
redisObject[prop] = new Date(redisObject[prop]);
break;
case 'timestamp':
ret[prop] = new Date(parseInt(redisVal)).toString();
redisObject[prop] = new Date(parseInt(redisObject[prop]));
break;
}
}
}

return ret;
return redisObject;
};

function newContext() {
Expand Down
8 changes: 5 additions & 3 deletions test/debugPrint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let redisClient = new Redis({

let store;

describe('Find', function() {
describe('DebugPrint', function() {
beforeEach(function() {
return redisClient.flushdb().then(function() {
store = new RigidDB('foo', { db: 15 });
Expand Down Expand Up @@ -65,14 +65,16 @@ describe('Find', function() {
color: '[NULL]',
extra: '\"~\"',
id: '1',
mileage: 12345,
mileage: '12345',
convertible: 'true',
purchaseDate: new Date('Sun Nov 01 2015 17:41:24 GMT+0100 (CET)').toString(),
serviceDate: new Date('Sun Nov 01 2015 17:41:24 GMT+0100 (CET)').toString()
}, {
color: '\"blue\"',
extra: '\"~~~\"',
id: '2',
mileage: 12345,
mileage: '12345',
convertible: 'true',
purchaseDate: new Date('Sun Nov 01 2015 22:41:24 GMT+0100 (CET)').toString(),
serviceDate: new Date('Sun Nov 01 2015 17:41:24 GMT+0100 (CET)').toString()
}
Expand Down
2 changes: 1 addition & 1 deletion test/setSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('SetSchema', function() {
}).then(function(result) {
expect(result).to.deep.equal({
method: 'setSchema',
reason: 'Invalid index field: \'mileage\'',
reason: 'Invalid index field name: \'mileage\'',
val: false
});
});
Expand Down

0 comments on commit 257c921

Please sign in to comment.