Skip to content

Instantly share code, notes, and snippets.

@jviereck
Created March 7, 2010 21:15
Show Gist options
  • Save jviereck/324631 to your computer and use it in GitHub Desktop.
Save jviereck/324631 to your computer and use it in GitHub Desktop.
From: Julian Viereck <julian.viereck@gmail.com>
Fix for issue: remove() removes the doc even if was set again.
diff --git a/dirty.js b/dirty.js
--- a/dirty.js
+++ b/dirty.js
@@ -19,16 +19,17 @@ var Dirty = dirty.Dirty = function(file,
this.file = (file)
? new File(file, 'a+', {encoding: 'utf8'})
: false;
this._docs = [];
this._ids = {};
this._memoryIds = [];
+ this._inMemory = [];
// Interval after which docs should be flushed to disk at latest
this.flushInterval = options.flushInterval;
// Listeners waiting for the next flush to finish
this.flushCallbacks = [];
// Wait for x docs to be in memory before flushing to disk
@@ -94,25 +95,32 @@ Dirty.prototype.add = function(doc, cb)
};
Dirty.prototype.set = function(id, doc, cb) {
doc._id = id;
if (this._ids[id] === undefined && !doc._deleted) {
this.length++;
}
- this._ids[id] = (this._docs.push(doc)-1);
+
+ if (!this._inMemory[id]) {
+ this._ids[id] = (this._docs.push(doc)-1);
+ this._memoryIds.push(id);
+ this._inMemory[id] = true;
+ } else {
+ this._docs[this._ids[id]] = doc;
+ }
+
if (!this.file) {
process.nextTick(function() {
cb(doc);
});
return;
}
- this._memoryIds.push(id);
this.memoryQueueLength++;
if (this.memoryQueueLength === this.flushLimit) {
this.flush().addCallback(function() {
if (cb) {
cb(doc);
}
});
@@ -155,22 +163,33 @@ Dirty.prototype.flush = function() {
dupes = {};
this.flushQueueLength += length;
this._memoryIds.forEach(function(id, i) {
if (!(id in dupes)) {
chunk += JSON.stringify(self._docs[self._ids[id]])+"\n";
}
+
dupes[id] = true;
if (chunk.length < 16*1024 && i < (length-1)) {
return;
}
+ if (self._docs[self._ids[id]]._delete == true) {
+ var cb = self._docs[self._ids[id]]._delete_cb;
+ delete self._docs[self._ids[id]];
+ delete(self._ids[id]);
+
+ if (cb) {
+ cb();
+ }
+ }
+
writePromises++;
self.file.write(chunk).addCallback(function() {
writePromises--;
if (writePromises === 0) {
self.flushQueueLength -= length;
self.flushCallbacks.forEach(function(cb) {
cb();
});
@@ -204,21 +223,14 @@ Dirty.prototype.get = function(id) {
};
Dirty.prototype.remove = function(id, cb) {
var self = this;
delete this._docs[this._ids[id]];
this.length--;
- this.set(id, {_deleted: true}, function() {
- delete self._docs[self._ids[id]];
- delete(self._ids[id]);
-
- if (cb) {
- cb();
- }
- });
+ this.set(id, {_deleted: true, _delete_cb: cb});
};
Dirty.prototype.filter = function(fn) {
return this._docs.filter(fn);
};
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment