This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 686
Fixes TypeError: Cannot read property 'pop' of undefined
#530
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ var AbstractLevelDOWN = require("abstract-leveldown").AbstractLevelDOWN; | |
var async = require("async"); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var tmp = require("tmp"); | ||
tmp.setGracefulCleanup(); | ||
|
||
util.inherits(FileDown, AbstractLevelDOWN); | ||
|
||
|
@@ -15,17 +17,78 @@ FileDown.prototype._open = function(options, callback) { | |
var self = this; | ||
callback(null, self); | ||
}; | ||
|
||
const accessQueue = { | ||
next: (lKey) => { | ||
const cont = accessQueue.cache[lKey].shift(); | ||
if (cont) { | ||
cont(); | ||
} else { | ||
delete accessQueue.cache[lKey]; | ||
} | ||
}, | ||
execute: (lKey, callback) => { | ||
let cache = accessQueue.cache[lKey]; | ||
if (cache) { | ||
cache.push(callback); | ||
} else { | ||
accessQueue.cache[lKey] = []; | ||
callback(); | ||
} | ||
}, | ||
cache: {} | ||
}; | ||
Comment on lines
+20
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 on the abstraction. Much cleaner |
||
FileDown.prototype._put = function(key, value, options, callback) { | ||
fs.writeFile(path.join(this.location, key), value, "utf8", callback); | ||
const lKey = path.join(this.location, key); | ||
// This fixes an issue caused by writing AND reading the same key multiple times | ||
// simultaneously. Sometimes the read operation would end up reading the value as 0 bytes | ||
// due to the way writes are performed in node. To fix this, we implemented a queue so only a | ||
// single read or write operation can occur at a time for each key; basically an in-memory lock. | ||
// Additionally, during testing we found that it was possible for a write operation to fail | ||
// due to program termination. This failure would sometimes cause the key to _exist_ but contain | ||
// 0 bytes (which is always invalid). To fix this we write to a temp file, and only if it works | ||
// do we move this temp file to its correct key location. This prevents early termination from | ||
// writing partial/empty values. | ||
// Of course, all this will eventually be for nothing as we are migrating the db to actual an | ||
// leveldb implementation that doesn't use a separate file for every key Soon(TM). | ||
accessQueue.execute(lKey, () => { | ||
// get a tmp file to write the contents to... | ||
tmp.file((err, path, fd) => { | ||
if (err) { | ||
callback(err); | ||
accessQueue.next(lKey); | ||
return; | ||
} | ||
|
||
// write the contents to that temp file | ||
fs.writeFile(fd, value, "utf8", (err) => { | ||
if (err) { | ||
callback(err); | ||
accessQueue.next(lKey); | ||
return; | ||
} | ||
// move the temp file to its final destination | ||
fs.rename(path, lKey, (err) => { | ||
callback(err); | ||
|
||
// if there is more work to be done on this key, do it. | ||
accessQueue.next(lKey); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
FileDown.prototype._get = function(key, options, callback) { | ||
fs.readFile(path.join(this.location, key), "utf8", function(err, data) { | ||
if (err) { | ||
return callback(new Error("NotFound")); | ||
} | ||
callback(null, data); | ||
const lKey = path.join(this.location, key); | ||
accessQueue.execute(lKey, () => { | ||
fs.readFile(lKey, "utf8", (err, data) => { | ||
if (err) { | ||
callback(new Error("NotFound")); | ||
} else { | ||
callback(null, data); | ||
} | ||
accessQueue.next(lKey); | ||
}); | ||
}); | ||
}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍