Use append-only format for removes too

pull/2/head
Louis Chatriot 12 years ago
parent aae1743542
commit 4b780922d6
  1. 13
      lib/datastore.js

@ -242,7 +242,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
, multi, upsert , multi, upsert
, modifiedDoc , modifiedDoc
, newData = [] , newData = []
, updatedData = [] , updatedDocs = []
; ;
if (typeof options === 'function') { cb = options; options = {}; } if (typeof options === 'function') { cb = options; options = {}; }
@ -276,7 +276,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
modifiedDoc = model.modify(d, updateQuery); modifiedDoc = model.modify(d, updateQuery);
newData.push(modifiedDoc); newData.push(modifiedDoc);
updatedData.push(modifiedDoc); updatedDocs.push(modifiedDoc);
} else { } else {
newData.push(d); newData.push(d);
} }
@ -285,7 +285,7 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) {
return callback(err); return callback(err);
} }
self.persistNewState(updatedData, function (err) { self.persistNewState(updatedDocs, function (err) {
if (err) { return callback(err); } if (err) { return callback(err); }
self.data = newData; self.data = newData;
return callback(null, numReplaced); return callback(null, numReplaced);
@ -313,7 +313,9 @@ Datastore.prototype._remove = function (query, options, cb) {
, self = this , self = this
, numRemoved = 0 , numRemoved = 0
, multi , multi
, newData = []; , newData = []
, removedDocs = []
;
if (typeof options === 'function') { cb = options; options = {}; } if (typeof options === 'function') { cb = options; options = {}; }
callback = cb || function () {}; callback = cb || function () {};
@ -323,6 +325,7 @@ Datastore.prototype._remove = function (query, options, cb) {
self.data.forEach(function (d) { self.data.forEach(function (d) {
if (model.match(d, query) && (multi || numRemoved === 0)) { if (model.match(d, query) && (multi || numRemoved === 0)) {
numRemoved += 1; numRemoved += 1;
removedDocs.push({ $$deleted: true, _id: d._id });
} else { } else {
newData.push(d); newData.push(d);
} }
@ -331,7 +334,7 @@ Datastore.prototype._remove = function (query, options, cb) {
return callback(err); return callback(err);
} }
self.persistWholeDatabase(newData, function (err) { self.persistNewState(removedDocs, function (err) {
if (err) { return callback(err); } if (err) { return callback(err); }
self.data = newData; self.data = newData;
return callback(null, numRemoved); return callback(null, numRemoved);

Loading…
Cancel
Save