Skip to content

Commit

Permalink
Updated documentation with API changes introduced in 79197cd
Browse files Browse the repository at this point in the history
Updated demo, added a WriteBatch example implemented in 9440117
  • Loading branch information
shinuza committed May 25, 2011
1 parent 79197cd commit 6021981
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.markdown
Expand Up @@ -24,8 +24,8 @@ The API is meant to be an almost one-to-one mapping to the underlying C++ librar

This is the main Class and probably the only one that a user needs to create manually.

var LevelDB = require('leveldb.node').LevelDB
var db = new LevelDB
var DB = require('leveldb.node').DB;
var db = new DB;
db.open({create_if_missing: true}, "path/to/my/db");
var key = new Buffer("myKey");
db.put({}, key, new Buffer("My Value!"));
Expand Down
67 changes: 35 additions & 32 deletions demo/demo.js
@@ -1,51 +1,54 @@
var leveldb = require('../build/default/leveldb.node'),
LevelDB = leveldb.LevelDB,
DB = leveldb.DB,
Iterator = leveldb.Iterator,
WriteBatch = leveldb.WriteBatch;

//console.dir(LevelDB);
//console.dir(LevelDB.prototype);

var path = __dirname + "/testdb";

var db = new LevelDB();
var db = new DB();

// Opening
console.log("Opening...");
var status = db.open({create_if_missing: true, paranoid_checks: true}, path);
console.log(status);

var key = new Buffer("Hello");

// Putting
console.log("\nPutting...");
for (var i = 0; i < 100; i++) {
db.put({}, new Buffer("item" + i), new Buffer("value" + i));
if (i%2) {
db.del({}, new Buffer("item" + i));
}
}
var status = db.put({}, key, new Buffer("World"));
var key = new Buffer("Hello");
var value = new Buffer("World");
var status = db.put({}, key, value);
console.log(status);

// Getting
console.log("\nGetting...");
var value = db.get({}, key);
console.dir(value.__proto__);
console.dir(Buffer.prototype);
console.dir(value);
console.log("OK");
if(value.toString() == db.get({}, key).toString()) {
console.log("OK");
} else {
console.log("FAIL");
}

//console.log("\nDeleting...");
//var status = db.del({}, key);
//console.log(status);
// Bulk writing
console.log("\nBulk writing...");
var wb = new WriteBatch();
wb.put(new Buffer('foo'), new Buffer('bar'));
wb.put(new Buffer('booz'), new Buffer('baz'));
var status = db.write({}, wb);
console.log("Bulk writing: putting: " + status);

if (db.get({}, new Buffer('booz')).toString() == 'baz' &&
db.get({}, new Buffer('foo')).toString() == 'bar') {
var status = "OK";
} else {
var status = "FAIL";
}
console.log("Bulk writing: getting: " + status);

// Deleting
console.log("\nDeleting...");
var status = db.del({}, key);
console.log(status);

// Closing
console.log("\nClosing...");
db.close();
console.log("OK");

//console.log("\nRepairing...");
//var status = LevelDB.repairDB(path, {});
//console.log(status);

//console.log("\nDestroying...");
//var status = LevelDB.destroyDB(path, {});
//console.log(status);

console.log("OK");

0 comments on commit 6021981

Please sign in to comment.