Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

allow snapshot to be in memory

  • Loading branch information...
commit 4aa4e4cc2c9cf123f7052839d4f12be6d80a47fc 1 parent b48e234
Paulo Lopes authored

Showing 3 changed files with 115 additions and 78 deletions. Show diff stats Hide diff stats

  1. +2 2 lib/helpers/cache.js
  2. +112 75 lib/index.js
  3. +1 1  package.json
4 lib/helpers/cache.js
@@ -13,8 +13,8 @@
13 13 */
14 14 function Cache(options) {
15 15 this.store = {};
16   - options = options || {cacheSize: 256, ttl: 30000};
17   - this.size = options.cacheSize || 256;
  16 + options = options || {cacheSize: 4096, ttl: 30000};
  17 + this.size = options.cacheSize || 4096;
18 18 this.ttl = options.ttl || 30000;
19 19 this.keys = 0;
20 20 }
187 lib/index.js
@@ -8,6 +8,7 @@ var Cursor = mongodb.Cursor;
8 8 // helpers
9 9 var arrayHelper = require('./helpers/arrays');
10 10 var parallelHelper = require('./helpers/parallel');
  11 +var Cache = require('./helpers/cache');
11 12 // protos
12 13 var model = require('./protos/model');
13 14 var schemaModel = require('./protos/schemamodel');
@@ -17,6 +18,7 @@ var view = require('./protos/view');
17 18 var common = require('./protos/common');
18 19
19 20 var schema = new (require('jsonschema').Environment)();
  21 +var snapshotsCache = new Cache();
20 22
21 23 // lazy connector
22 24 var _collections = {};
@@ -369,6 +371,24 @@ var ODM = {
369 371 * @param {Function} callback
370 372 */
371 373 snapshot: function (collection_name, oid, fields, options, callback) {
  374 +
  375 + var storeSnapshot = function (txdocument) {
  376 + if (options.transient === true) {
  377 + console.log('USING IN MEMORY CACHE');
  378 + snapshotsCache.set(oid.toHexString(), txdocument);
  379 + callback(null);
  380 + } else {
  381 + ODM.collection('snapshots', options, function (err, txcollection) {
  382 + if (err) {
  383 + return callback(err);
  384 + }
  385 +
  386 + options.safe = _safeOptions;
  387 + return txcollection.save(txdocument, options, callback);
  388 + });
  389 + }
  390 + };
  391 +
372 392 ODM.collection(collection_name, options, function (err, collection) {
373 393 if (err) {
374 394 return callback(err);
@@ -383,19 +403,7 @@ var ODM = {
383 403 return callback('not found');
384 404 }
385 405
386   - return ODM.collection('snapshots', options, function (err, txcollection) {
387   - if (err) {
388   - return callback(err);
389   - }
390   -
391   - options.safe = _safeOptions;
392   - var txdocument = {
393   - _id: oid,
394   - fields: fields,
395   - doc: document
396   - };
397   - return txcollection.save(txdocument, options, callback);
398   - });
  406 + storeSnapshot({_id: oid, fields: fields, doc: document});
399 407 });
400 408 });
401 409 },
@@ -410,78 +418,102 @@ var ODM = {
410 418 * @param {Function} callback
411 419 */
412 420 revert: function (collection_name, oid, options, callback) {
413   - ODM.collection('snapshots', options, function (err, txcollection) {
  421 +
  422 + var loadSnapshot = function (callback) {
  423 + if (options.transient === true) {
  424 + console.log('USING IN MEMORY CACHE');
  425 + callback(null, snapshotsCache.get(oid.toHexString()));
  426 + } else {
  427 + ODM.collection('snapshots', options, function (err, txcollection) {
  428 + if (err) {
  429 + return callback(err);
  430 + }
  431 +
  432 + return txcollection.findOne({_id: oid}, {}, options, callback);
  433 + });
  434 + }
  435 + };
  436 +
  437 + var cleanSnapshot = function (callback) {
  438 + if (options.transient === true) {
  439 + callback(null);
  440 + } else {
  441 + ODM.collection('snapshots', options, function (err, txcollection) {
  442 + if (err) {
  443 + return callback(err);
  444 + }
  445 +
  446 + txcollection.remove({_id: oid}, options, callback);
  447 + });
  448 + }
  449 + };
  450 +
  451 + loadSnapshot(function (err, document) {
414 452 if (err) {
415 453 return callback(err);
416 454 }
417 455
418   - return txcollection.findOne({_id: oid}, {}, options, function (err, document) {
  456 + if (document === null) {
  457 + return callback('not found');
  458 + }
  459 +
  460 + return ODM.collection(collection_name, options, function (err, collection) {
419 461 if (err) {
420 462 return callback(err);
421 463 }
422 464
423   - if (document === null) {
424   - return callback('not found');
425   - }
426   -
427   - return ODM.collection(collection_name, options, function (err, collection) {
428   - if (err) {
429   - return callback(err);
  465 + options.safe = _safeOptions;
  466 + // was this snapshot a full doc or partial?
  467 + var fields = Object.keys(document.fields);
  468 +
  469 + if (fields.length > 0) {
  470 + // partial
  471 + // remove _id
  472 + delete document.doc._id;
  473 + var update = {$set: document.doc};
  474 +
  475 + // generate unset list
  476 + var docFields = Object.keys(document.doc);
  477 + var i, j;
  478 + for (i = 0; i < docFields.length; i++) {
  479 + for (j = 0; j < fields.length; j++) {
  480 + if (docFields[i] === fields[j]) {
  481 + fields.splice(j, 1);
  482 + break;
  483 + }
  484 + }
430 485 }
431 486
432   - options.safe = _safeOptions;
433   - // was this snapshot a full doc or partial?
434   - var fields = Object.keys(document.fields);
435   -
436 487 if (fields.length > 0) {
437   - // partial
438   - // remove _id
439   - delete document.doc._id;
440   - var update = {$set: document.doc};
441   -
442   - // generate unset list
443   - var docFields = Object.keys(document.doc);
444   - var i, j;
445   - for (i = 0; i < docFields.length; i++) {
446   - for (j = 0; j < fields.length; j++) {
447   - if (docFields[i] === fields[j]) {
448   - fields.splice(j, 1);
449   - break;
450   - }
451   - }
  488 + update.$unset = {};
  489 + for (j = 0; j < fields.length; j++) {
  490 + update.$unset[fields[j]] = 1;
452 491 }
  492 + }
453 493
454   - if (fields.length > 0) {
455   - update.$unset = {};
456   - for (j = 0; j < fields.length; j++) {
457   - update.$unset[fields[j]] = 1;
458   - }
  494 + collection.update({_id: oid}, update, options, function (err) {
  495 + if (err) {
  496 + return callback(err);
459 497 }
460 498
461   - collection.update({_id: oid}, update, options, function (err) {
462   - if (err) {
463   - return callback(err);
464   - }
465   -
466   - // clean up
467   - delete options.safe;
468   - return txcollection.remove({_id: oid}, options, callback);
469   - });
470   - } else {
471   - // full
472   - // ensure _id
473   - document.doc._id = oid;
474   - collection.save(document.doc, options, function (err) {
475   - if (err) {
476   - return callback(err);
477   - }
  499 + // clean up
  500 + delete options.safe;
  501 + return cleanSnapshot(callback);
  502 + });
  503 + } else {
  504 + // full
  505 + // ensure _id
  506 + document.doc._id = oid;
  507 + collection.save(document.doc, options, function (err) {
  508 + if (err) {
  509 + return callback(err);
  510 + }
478 511
479   - // clean up
480   - delete options.safe;
481   - return txcollection.remove({_id: oid}, options, callback);
482   - });
483   - }
484   - });
  512 + // clean up
  513 + delete options.safe;
  514 + return cleanSnapshot(callback);
  515 + });
  516 + }
485 517 });
486 518 });
487 519 },
@@ -496,13 +528,18 @@ var ODM = {
496 528 * @param {Function} callback
497 529 */
498 530 flush: function (collection_name, oid, options, callback) {
499   - ODM.collection('snapshots', options, function (err, txcollection) {
500   - if (err) {
501   - return callback(err);
502   - }
  531 + if (options.transient === true) {
  532 + console.log('USING IN MEMORY CACHE');
  533 + snapshotsCache.del(oid.toHexString());
  534 + } else {
  535 + ODM.collection('snapshots', options, function (err, txcollection) {
  536 + if (err) {
  537 + return callback(err);
  538 + }
503 539
504   - return txcollection.remove({_id: oid}, options, callback);
505   - });
  540 + return txcollection.remove({_id: oid}, options, callback);
  541 + });
  542 + }
506 543 },
507 544
508 545 /**
2  package.json
@@ -5,7 +5,7 @@
5 5 "contributors": [
6 6 "Paulo Lopes"
7 7 ],
8   - "version": "3.1.8",
  8 + "version": "3.1.9",
9 9 "engines": {
10 10 "node": ">=0.4.12"
11 11 },

0 comments on commit 4aa4e4c

Please sign in to comment.
Something went wrong with that request. Please try again.