Permalink
Browse files

allow snapshot to be in memory

  • Loading branch information...
1 parent b48e234 commit 4aa4e4cc2c9cf123f7052839d4f12be6d80a47fc Paulo Lopes committed Nov 20, 2012
Showing with 115 additions and 78 deletions.
  1. +2 −2 lib/helpers/cache.js
  2. +112 −75 lib/index.js
  3. +1 −1 package.json
View
@@ -13,8 +13,8 @@
*/
function Cache(options) {
this.store = {};
- options = options || {cacheSize: 256, ttl: 30000};
- this.size = options.cacheSize || 256;
+ options = options || {cacheSize: 4096, ttl: 30000};
+ this.size = options.cacheSize || 4096;
this.ttl = options.ttl || 30000;
this.keys = 0;
}
View
@@ -8,6 +8,7 @@ var Cursor = mongodb.Cursor;
// helpers
var arrayHelper = require('./helpers/arrays');
var parallelHelper = require('./helpers/parallel');
+var Cache = require('./helpers/cache');
// protos
var model = require('./protos/model');
var schemaModel = require('./protos/schemamodel');
@@ -17,6 +18,7 @@ var view = require('./protos/view');
var common = require('./protos/common');
var schema = new (require('jsonschema').Environment)();
+var snapshotsCache = new Cache();
// lazy connector
var _collections = {};
@@ -369,6 +371,24 @@ var ODM = {
* @param {Function} callback
*/
snapshot: function (collection_name, oid, fields, options, callback) {
+
+ var storeSnapshot = function (txdocument) {
+ if (options.transient === true) {
+ console.log('USING IN MEMORY CACHE');
+ snapshotsCache.set(oid.toHexString(), txdocument);
+ callback(null);
+ } else {
+ ODM.collection('snapshots', options, function (err, txcollection) {
+ if (err) {
+ return callback(err);
+ }
+
+ options.safe = _safeOptions;
+ return txcollection.save(txdocument, options, callback);
+ });
+ }
+ };
+
ODM.collection(collection_name, options, function (err, collection) {
if (err) {
return callback(err);
@@ -383,19 +403,7 @@ var ODM = {
return callback('not found');
}
- return ODM.collection('snapshots', options, function (err, txcollection) {
- if (err) {
- return callback(err);
- }
-
- options.safe = _safeOptions;
- var txdocument = {
- _id: oid,
- fields: fields,
- doc: document
- };
- return txcollection.save(txdocument, options, callback);
- });
+ storeSnapshot({_id: oid, fields: fields, doc: document});
});
});
},
@@ -410,78 +418,102 @@ var ODM = {
* @param {Function} callback
*/
revert: function (collection_name, oid, options, callback) {
- ODM.collection('snapshots', options, function (err, txcollection) {
+
+ var loadSnapshot = function (callback) {
+ if (options.transient === true) {
+ console.log('USING IN MEMORY CACHE');
+ callback(null, snapshotsCache.get(oid.toHexString()));
+ } else {
+ ODM.collection('snapshots', options, function (err, txcollection) {
+ if (err) {
+ return callback(err);
+ }
+
+ return txcollection.findOne({_id: oid}, {}, options, callback);
+ });
+ }
+ };
+
+ var cleanSnapshot = function (callback) {
+ if (options.transient === true) {
+ callback(null);
+ } else {
+ ODM.collection('snapshots', options, function (err, txcollection) {
+ if (err) {
+ return callback(err);
+ }
+
+ txcollection.remove({_id: oid}, options, callback);
+ });
+ }
+ };
+
+ loadSnapshot(function (err, document) {
if (err) {
return callback(err);
}
- return txcollection.findOne({_id: oid}, {}, options, function (err, document) {
+ if (document === null) {
+ return callback('not found');
+ }
+
+ return ODM.collection(collection_name, options, function (err, collection) {
if (err) {
return callback(err);
}
- if (document === null) {
- return callback('not found');
- }
-
- return ODM.collection(collection_name, options, function (err, collection) {
- if (err) {
- return callback(err);
+ options.safe = _safeOptions;
+ // was this snapshot a full doc or partial?
+ var fields = Object.keys(document.fields);
+
+ if (fields.length > 0) {
+ // partial
+ // remove _id
+ delete document.doc._id;
+ var update = {$set: document.doc};
+
+ // generate unset list
+ var docFields = Object.keys(document.doc);
+ var i, j;
+ for (i = 0; i < docFields.length; i++) {
+ for (j = 0; j < fields.length; j++) {
+ if (docFields[i] === fields[j]) {
+ fields.splice(j, 1);
+ break;
+ }
+ }
}
- options.safe = _safeOptions;
- // was this snapshot a full doc or partial?
- var fields = Object.keys(document.fields);
-
if (fields.length > 0) {
- // partial
- // remove _id
- delete document.doc._id;
- var update = {$set: document.doc};
-
- // generate unset list
- var docFields = Object.keys(document.doc);
- var i, j;
- for (i = 0; i < docFields.length; i++) {
- for (j = 0; j < fields.length; j++) {
- if (docFields[i] === fields[j]) {
- fields.splice(j, 1);
- break;
- }
- }
+ update.$unset = {};
+ for (j = 0; j < fields.length; j++) {
+ update.$unset[fields[j]] = 1;
}
+ }
- if (fields.length > 0) {
- update.$unset = {};
- for (j = 0; j < fields.length; j++) {
- update.$unset[fields[j]] = 1;
- }
+ collection.update({_id: oid}, update, options, function (err) {
+ if (err) {
+ return callback(err);
}
- collection.update({_id: oid}, update, options, function (err) {
- if (err) {
- return callback(err);
- }
-
- // clean up
- delete options.safe;
- return txcollection.remove({_id: oid}, options, callback);
- });
- } else {
- // full
- // ensure _id
- document.doc._id = oid;
- collection.save(document.doc, options, function (err) {
- if (err) {
- return callback(err);
- }
+ // clean up
+ delete options.safe;
+ return cleanSnapshot(callback);
+ });
+ } else {
+ // full
+ // ensure _id
+ document.doc._id = oid;
+ collection.save(document.doc, options, function (err) {
+ if (err) {
+ return callback(err);
+ }
- // clean up
- delete options.safe;
- return txcollection.remove({_id: oid}, options, callback);
- });
- }
- });
+ // clean up
+ delete options.safe;
+ return cleanSnapshot(callback);
+ });
+ }
});
});
},
@@ -496,13 +528,18 @@ var ODM = {
* @param {Function} callback
*/
flush: function (collection_name, oid, options, callback) {
- ODM.collection('snapshots', options, function (err, txcollection) {
- if (err) {
- return callback(err);
- }
+ if (options.transient === true) {
+ console.log('USING IN MEMORY CACHE');
+ snapshotsCache.del(oid.toHexString());
+ } else {
+ ODM.collection('snapshots', options, function (err, txcollection) {
+ if (err) {
+ return callback(err);
+ }
- return txcollection.remove({_id: oid}, options, callback);
- });
+ return txcollection.remove({_id: oid}, options, callback);
+ });
+ }
},
/**
View
@@ -5,7 +5,7 @@
"contributors": [
"Paulo Lopes"
],
- "version": "3.1.8",
+ "version": "3.1.9",
"engines": {
"node": ">=0.4.12"
},

0 comments on commit 4aa4e4c

Please sign in to comment.