forked from creationix/node-leveldb
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request creationix#11 from my8bird/master
Merged in Iterator code
- Loading branch information
Showing
16 changed files
with
1,025 additions
and
239 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var leveldb = require('../build/default/leveldb.node'), | ||
DB = leveldb.DB, | ||
Iterator = leveldb.Iterator, | ||
WriteBatch = leveldb.WriteBatch; | ||
|
||
var path = __dirname + "/testdb"; | ||
|
||
var db = new DB(); | ||
|
||
// Opening | ||
console.log("Opening..."); | ||
db.open(path, {create_if_missing: true, paranoid_checks: true}, function(err) { | ||
if (err) throw err; | ||
console.log("ok"); | ||
|
||
// Putting | ||
console.log("\nPutting..."); | ||
var key = "Hello"; | ||
var value = "World"; | ||
db.put(key, value, function(err) { | ||
if (err) throw err; | ||
console.log("ok"); | ||
|
||
// Getting | ||
console.log("\nGetting..."); | ||
db.get(key, function(err, val) { | ||
if (err) throw err; | ||
if (val != value) throw "Expected: " + value + ", got: " + val; | ||
console.log("ok"); | ||
|
||
// Deleting | ||
console.log("\nDeleting..."); | ||
db.del(key, function(err) { | ||
if (err) throw err; | ||
console.log("ok"); | ||
|
||
// Closing | ||
console.log("\nClosing...") | ||
db.close(function(err) { | ||
if (err) throw err; | ||
console.log('ok'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
var leveldb = require('../build/default/leveldb.node'), | ||
DB = leveldb.DB, | ||
Iterator = leveldb.Iterator; | ||
|
||
var path = "/tmp/large.db"; | ||
|
||
var index = 0; | ||
var in_flight = 0; | ||
var found = 0; | ||
var not_found = 0; | ||
|
||
var db = new DB(); | ||
db.open(path, function(err) { | ||
if (err) throw err; | ||
|
||
setInterval(function() { | ||
console.log('Index: ' + index + ' In flight: ' + in_flight + ' Found: ' + found + ' Not found: ' + not_found); | ||
refresh(); | ||
}, 100); | ||
|
||
refresh(); | ||
}); | ||
|
||
function refresh() { | ||
while (in_flight < 10000) { | ||
var key = "row" + index; | ||
|
||
db.get(key, (function(index) { return function(err, value) { | ||
in_flight -= 1; | ||
if (value) { | ||
var obj = JSON.parse(value); | ||
if (obj.index != index) { | ||
console.log('key: ' + key + ' = ' + value); | ||
} | ||
found += 1; | ||
} | ||
else { | ||
not_found += 1; | ||
} | ||
}})(index)); | ||
|
||
in_flight += 1; | ||
index += 1; | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
DB = require('../build/default/leveldb.node').DB | ||
uuid = require('node-uuid') | ||
td = require("twisted-deferred") | ||
|
||
start = Date.now() | ||
|
||
entryCount = 1000 | ||
readCount = 0 | ||
|
||
defer = td.toDeferred | ||
DeferredList = td.DeferredList | ||
|
||
console.log("Creating test database") | ||
path = "/tmp/iterator.db" | ||
db = new DB() | ||
|
||
d = defer(db.open.bind(db), path, {create_if_missing: true}) | ||
d.addCallback () -> | ||
console.log('!! creating ' + entryCount + ' random key entries') | ||
deferreds = [] | ||
for i in [0 .. entryCount] | ||
id = uuid() | ||
|
||
put_d = defer db.put.bind(db), id, JSON.stringify({id: id, name: 'Bob', age: 33}) | ||
deferreds.push(put_d) | ||
|
||
return DeferredList(deferreds) | ||
|
||
d.addCallback () -> | ||
console.log('created in ' + (Date.now() - start) + 'ms') | ||
console.log('!! iterating db in key order') | ||
|
||
# reset the start counter | ||
start = Date.now() | ||
|
||
# iterate over the test database | ||
iterator = db.newIterator({}) | ||
deferred = new td.Deferred() | ||
|
||
iterator.seekToFirst () -> | ||
while iterator.valid() | ||
key = iterator.key().toString('utf8') | ||
|
||
if lastKey && lastKey > key | ||
console.log('found sorting error') | ||
|
||
lastKey = key | ||
readCount++ | ||
|
||
iterator.next() | ||
|
||
console.log('read sequential ' + readCount + ' db contents in ' + (Date.now() - start) + 'ms') | ||
deferred.callback() | ||
|
||
deferred | ||
|
||
d.addCallback () -> | ||
console.log 'Start Seek test' | ||
|
||
deferred = new td.Deferred() | ||
iterator = db.newIterator({}) | ||
testUUID = uuid() | ||
iterator.seek "" + testUUID, () -> | ||
console.log('looking for first key after: ' + testUUID) | ||
# if we found something the report | ||
if (iterator.valid()) | ||
console.log('FOUND: ' + iterator.key().toString('utf-8')) | ||
|
||
deferred.callback() | ||
|
||
deferred | ||
|
||
d.addCallback () -> | ||
console.log "Success" | ||
|
||
d.addErrback (err) -> | ||
console.log err.message.stack | ||
|
||
d.addBoth () -> | ||
db.close() | ||
DB.destroyDB(path, {}) | ||
console.log "Database removed and cleaned up." | ||
|
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var leveldb = require('../build/default/leveldb.node'), | ||
DB = leveldb.DB, | ||
WriteBatch = leveldb.WriteBatch; | ||
|
||
console.log("Creating test database"); | ||
var path = "/tmp/large.db"; | ||
var db = new DB(); | ||
|
||
var batchSize = 100; | ||
var totalSize = 1000000; | ||
|
||
db.open(path, {create_if_missing: true}, function() { | ||
|
||
console.log("Serializing and inserting 1,000,000 rows..."); | ||
var start = Date.now(); | ||
|
||
function doBatch(i, cb) { | ||
if (i % 10000 == 0) | ||
console.log("i = " + i); | ||
|
||
var wb = new WriteBatch(); | ||
|
||
for (var j = 0; j < batchSize; j += 1) { | ||
var key = "row" + i; | ||
var value = JSON.stringify({ | ||
index: i, | ||
name: "Tim", | ||
age: 28 | ||
}); | ||
wb.put(key, value); | ||
i += 1; | ||
} | ||
|
||
db.write(wb, function() { | ||
if (i < totalSize) | ||
doBatch(i, cb); | ||
else | ||
cb(); | ||
}); | ||
} | ||
|
||
doBatch(0, function() { | ||
var delta = Date.now() - start; | ||
console.log("Completed in %d ms", delta); | ||
console.log("%s inserts per second", Math.floor(totalSize * 1000 / delta)); | ||
|
||
console.log("\nClosing..."); | ||
db.close(function() { | ||
console.log("Done"); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
lib = require('../build/default/leveldb.node'); | ||
|
||
for (i in lib) { | ||
exports[i] = lib[i]; | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "leveldb", | ||
"tags": ["database", "cache"], | ||
"description": "Bindings for using LevelDB through node.", | ||
"homepage": "https://github.com/my8bird/node-leveldb", | ||
"version": "0.3.5", | ||
"author": { | ||
"name": "Nathan Landis", | ||
"email": "my8bird@gmail.com"}, | ||
"main": "lib", | ||
"scripts": { | ||
"preinstall": "node-waf configure build" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://github.com/my8bird/node-leveldb.git" | ||
}, | ||
"keywords": ["database", "leveldb"], | ||
"bugs" : { "web" : "https://github.com/my8bird/node-leveldb/issues" }, | ||
"dependencies": | ||
{ | ||
}, | ||
"contributors": [ | ||
"Randall Leeds <randall.leeds@gmail.com> (http://claimid.com/randallleeds)", | ||
"Damon Oehlman <damon.oehlman@sidelab.com> (http://www.sidelab.com/)" | ||
] | ||
} |
Oops, something went wrong.