Skip to content

Commit

Permalink
Merge pull request creationix#11 from my8bird/master
Browse files Browse the repository at this point in the history
Merged in Iterator code
  • Loading branch information
creationix committed Sep 8, 2011
2 parents ef31944 + 83bc249 commit cc047d4
Show file tree
Hide file tree
Showing 16 changed files with 1,025 additions and 239 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Expand Up @@ -8,6 +8,10 @@ While implementing nStore, I realized there are a couple things that V8 and node

Since LevelDB provides good primitives like MVCC and binary support (It was designed to back IndexDB in the Chrome browser), then it can be used as a base to implement things like CouchDB.

## TODO

1. Trun Iterator code to be async

## Status

This project is still under heavy development in my free-time. It was started by a long night where I couldn't sleep. I am not a C++ coder and am learning as I go.
Expand Down
47 changes: 47 additions & 0 deletions demo/async.js
@@ -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');
});
});
});
});
});

46 changes: 46 additions & 0 deletions demo/dumpdata.js
@@ -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;
}
}

83 changes: 83 additions & 0 deletions demo/iterator.coffee
@@ -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."

52 changes: 52 additions & 0 deletions demo/million-async.js
@@ -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");
});
});
});
104 changes: 0 additions & 104 deletions leveldb.js

This file was deleted.

6 changes: 6 additions & 0 deletions lib/index.js
@@ -0,0 +1,6 @@
lib = require('../build/default/leveldb.node');

for (i in lib) {
exports[i] = lib[i];
}

27 changes: 27 additions & 0 deletions package.json
@@ -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/)"
]
}

0 comments on commit cc047d4

Please sign in to comment.