Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
openhoat committed Dec 5, 2015
1 parent 5acbc11 commit 132b1ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 57 deletions.
95 changes: 42 additions & 53 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,10 @@ Entity.prototype.checkIndexes = function () {
});
};

function commit(localMulti, multi, result) {
return localMulti ? ohm.processMulti(multi) : result;
}

Entity.prototype.delete = function (multi) {
var entity = this
, localMulti, id, type, clazz;
localMulti = !multi;
multi = multi || ohm.multi();
localMulti = ohm.createLocalMulti.apply(null, arguments);
return p.do(
function () {
id = entity.getId();
Expand All @@ -250,15 +245,15 @@ Entity.prototype.delete = function (multi) {
logger.enabledLevels.info && log.info('deleting "%s" entity #%s', type, id);
},
function removeRefs() {
return entity.removeLinks(multi)
.then(entity.removeIndexes.bind(entity, multi));
return entity.removeLinks(localMulti)
.then(entity.removeIndexes.bind(entity, localMulti));
},
function () {
var k;
k = ohm.toHash(type, id);
return ohm.execMulti(multi, 'del', k);
return ohm.execMulti(localMulti, 'del', k);
},
commit.bind(null, localMulti, multi)
ohm.processLocalMulti.bind(null, localMulti, multi)
);
};

Expand Down Expand Up @@ -414,8 +409,7 @@ Entity.prototype.loadLinks = function () {
Entity.prototype.removeIndexes = function (multi) {
var entity = this
, localMulti, clazz, type, schema, id;
localMulti = !multi;
multi = multi || ohm.multi();
localMulti = ohm.createLocalMulti.apply(null, arguments);
return p.do(function () {
clazz = entity.getClass();
type = entity.type;
Expand All @@ -428,20 +422,19 @@ Entity.prototype.removeIndexes = function (multi) {
return entity
.iterateIndexes(function (index, name, k) {
if (index.unique) {
return ohm.execMulti(multi, 'del', k);
return ohm.execMulti(localMulti, 'del', k);
} else {
return ohm.execMulti(multi, 'srem', k, id);
return ohm.execMulti(localMulti, 'srem', k, id);
}
})
.then(commit.bind(null, localMulti, multi));
.then(ohm.processLocalMulti.bind(null, localMulti, multi));
});
};

Entity.prototype.removeLinks = function (multi) {
var entity = this
, localMulti, type, id;
localMulti = !multi;
multi = multi || ohm.multi();
localMulti = ohm.createLocalMulti.apply(null, arguments);
return p.do(function () {
type = entity.type;
id = entity.getId();
Expand All @@ -451,31 +444,30 @@ Entity.prototype.removeLinks = function (multi) {
return p.do(
function removeLinkToEntity() {
if (link.reverseUnique) {
return ohm.execMulti(multi, 'del', k);
return ohm.execMulti(localMulti, 'del', k);
} else {
return ohm.execMulti(multi, 'srem', k, id);
return ohm.execMulti(localMulti, 'srem', k, id);
}
},
function removeLinkFromEntity() {
if (!reverseK) {
return;
}
if (link.unique) {
return ohm.execMulti(multi, 'del', reverseK);
return ohm.execMulti(localMulti, 'del', reverseK);
} else {
return ohm.execMulti(multi, 'srem', reverseK, value);
return ohm.execMulti(localMulti, 'srem', reverseK, value);
}
});
})
.then(commit.bind(null, localMulti, multi));
.then(ohm.processLocalMulti.bind(null, localMulti, multi));
});
};

Entity.prototype.save = function (multi) {
var entity = this
, localMulti, clazz, type, id;
localMulti = !multi;
multi = multi || ohm.multi();
localMulti = ohm.createLocalMulti.apply(null, arguments);
return p.do(
function check() {
clazz = entity.getClass();
Expand All @@ -496,8 +488,8 @@ Entity.prototype.save = function (multi) {
return entity.checkIndexes();
},
function updateRefs() {
return entity.saveIndexes(multi)
.then(entity.saveLinks.bind(entity, multi));
return entity.saveIndexes(localMulti)
.then(entity.saveLinks.bind(entity, localMulti));
},
function saveEntity() {
var result, ignoreProps, data, k;
Expand All @@ -513,15 +505,15 @@ Entity.prototype.save = function (multi) {
return value !== null;
});
}
return ohm.execMulti(multi, clazz.isObject ? 'hmset' : 'set', k, data)
return ohm.execMulti(localMulti, clazz.isObject ? 'hmset' : 'set', k, data)
.then(function () {
if (entity.ttl) {
result.ttl = entity.ttl;
return ohm.execMulti(multi, 'expire', k, entity.ttl);
return ohm.execMulti(localMulti, 'expire', k, entity.ttl);
}
});
},
commit.bind(null, localMulti, multi),
ohm.processLocalMulti.bind(null, localMulti, multi),
function () {
return entity.loadLinks();
},
Expand All @@ -535,8 +527,7 @@ Entity.prototype.save = function (multi) {
Entity.prototype.saveIndexes = function (multi) {
var entity = this
, localMulti, clazz, type, id;
localMulti = !multi;
multi = multi || ohm.multi();
localMulti = ohm.createLocalMulti.apply(null, arguments);
return p.do(function () {
clazz = entity.getClass();
type = entity.type;
Expand All @@ -548,25 +539,24 @@ Entity.prototype.saveIndexes = function (multi) {
return entity
.iterateIndexes(function (index, name, k) {
if (index.unique) {
return ohm.execMulti(multi, 'set', k, id)
return ohm.execMulti(localMulti, 'set', k, id)
.then(function () {
if (entity.ttl || index.ttl) {
return ohm.execMulti(multi, 'expire', k, entity.ttl || index.ttl);
return ohm.execMulti(localMulti, 'expire', k, entity.ttl || index.ttl);
}
});
} else {
return ohm.execMulti(multi, 'sadd', k, id);
return ohm.execMulti(localMulti, 'sadd', k, id);
}
})
.then(commit.bind(null, localMulti, multi));
.then(ohm.processLocalMulti.bind(null, localMulti, multi));
});
};

Entity.prototype.saveLinks = function (multi) {
var entity = this
, localMulti, type, id;
localMulti = !multi;
multi = multi || ohm.multi();
localMulti = ohm.createLocalMulti.apply(null, arguments);
return p.do(function () {
type = entity.type;
id = entity.getId();
Expand All @@ -576,41 +566,40 @@ Entity.prototype.saveLinks = function (multi) {
return p.do(
function saveLinkToEntity() {
if (link.reverseUnique) {
return ohm.execMulti(multi, 'set', k, id)
return ohm.execMulti(localMulti, 'set', k, id)
.then(function () {
if (entity.ttl || link.ttl) {
return ohm.execMulti(multi, 'expire', k, entity.ttl || link.ttl);
return ohm.execMulti(localMulti, 'expire', k, entity.ttl || link.ttl);
}
});
} else {
return ohm.execMulti(multi, 'sadd', k, id);
return ohm.execMulti(localMulti, 'sadd', k, id);
}
},
function saveLinkFromEntity() {
if (!reverseK) {
return;
}
if (link.unique) {
return ohm.execMulti(multi, 'set', reverseK, value)
return ohm.execMulti(localMulti, 'set', reverseK, value)
.then(function () {
if (entity.ttl || link.ttl) {
return ohm.execMulti(multi, 'expire', k, entity.ttl || link.ttl);
return ohm.execMulti(localMulti, 'expire', k, entity.ttl || link.ttl);
}
});
} else {
return ohm.execMulti(multi, 'sadd', reverseK, value);
return ohm.execMulti(localMulti, 'sadd', reverseK, value);
}
});
})
.then(commit.bind(null, localMulti, multi));
.then(ohm.processLocalMulti.bind(null, localMulti, multi));
});
};

Entity.prototype.update = function (multi) {
var entity = this
, localMulti, type, clazz, id, existing;
localMulti = !multi;
multi = multi || ohm.multi();
localMulti = ohm.createLocalMulti.apply(null, arguments);
return p.do(
function check() {
type = entity.type;
Expand All @@ -629,13 +618,13 @@ Entity.prototype.update = function (multi) {
});
},
function removeRefs() {
return existing.removeLinks(multi)
.then(existing.removeIndexes.bind(existing, multi));
return existing.removeLinks(localMulti)
.then(existing.removeIndexes.bind(existing, localMulti));
},
function updateRefs() {
_.defaults(entity.value, existing.value);
return entity.saveIndexes(multi)
.then(entity.saveLinks.bind(entity, multi));
return entity.saveIndexes(localMulti)
.then(entity.saveLinks.bind(entity, localMulti));
},
function saveEntity() {
var result, ignoreProps, data, k;
Expand All @@ -653,14 +642,14 @@ Entity.prototype.update = function (multi) {
}
return p.do(
function () {
return ohm.execMulti(multi, clazz.isObject ? 'hmset' : 'set', k, data);
return ohm.execMulti(localMulti, clazz.isObject ? 'hmset' : 'set', k, data);
},
function () {
if (entity.ttl) {
return ohm.execMulti(multi, 'expire', k, entity.ttl);
return ohm.execMulti(localMulti, 'expire', k, entity.ttl);
}
},
commit.bind(null, localMulti, multi),
ohm.processLocalMulti.bind(null, localMulti, multi),
function () {
entity.value = ohm.filterProperties(entity.value, type, 'get');
return entity;
Expand Down
12 changes: 9 additions & 3 deletions lib/ohm.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ ohm = {
})
.nodeify(cb);
},
createLocalMulti: function (multi) {
return multi || ohm.multi();
},
createEntityClass: function (schemaName) {
var defaults, clazz, schema;
clazz = function () {
Expand Down Expand Up @@ -328,6 +331,12 @@ ohm = {
resolve(multi[cmd].apply(multi, args));
});
},
multi: function () {
return ohm.cli.multi();
},
processLocalMulti: function (localMulti, multi, result) {
return localMulti !== multi ? ohm.processMulti(localMulti) : result;
},
processMulti: function (multi) {
var promise, queue;
promise = multi.execAsync();
Expand All @@ -354,9 +363,6 @@ ohm = {
}
return promise;
},
multi: function () {
return ohm.cli.multi();
},
publish: function (channel, message) {
return p.do(function () {
ohm.pubCli = redis.createClient(ohm.config.port, ohm.config.host, ohm.config.options);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hw-redis-ohm",
"version": "0.2.1",
"version": "0.2.2",
"description": "Redis Object Hash Mapping",
"main": "lib/ohm.js",
"author": "Olivier Penhoat <openhoat@gmail.com> (http://headwood.net/)",
Expand Down

0 comments on commit 132b1ef

Please sign in to comment.