Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add generateBatch method to db API.

Fix #61.
  • Loading branch information...
commit ea2e8d7127e279cffbe08485899fe36635e2640a 1 parent 89e70f9
@phuu phuu authored
View
15 README.md
@@ -486,6 +486,21 @@ stream.on("close", function() {
});
```
+### Generate batch operations
+
+You can also generate a `put` and `del` batch, so you can
+manage the batching yourself:
+
+```javascript
+var triple = { subject: "a", predicate: "b", object: "c" };
+
+// Produces a batch of put operations
+var putBatch = db.generateBatch(triple);
+
+// Produces a batch of del operations
+var delBatch = db.generateBatch(triple, 'del');
+```
+
## LevelUp integration
LevelGraph allows to leverage the full power of all
View
3  lib/levelgraph.js
@@ -96,6 +96,7 @@ module.exports = function levelgraph(leveldb, options, readyCallback) {
, nav: function(start) {
return new Navigator({ start: start, db: this });
}
+ , generateBatch: utilities.generateBatch
};
db.joinStream = function(a, b, c) {
@@ -183,7 +184,7 @@ doAction = function(action, leveldb) {
}
var actions = triples.reduce(function(acc, triple) {
- return acc.concat(utilities.genActions(action, triple));
+ return acc.concat(utilities.generateBatch(triple, action));
}, []);
leveldb.batch(actions, cb);
View
7 lib/utilities.js
@@ -131,14 +131,17 @@ function createQuery(pattern, options) {
module.exports.createQuery = createQuery;
-function genActions(action, triple) {
+function generateBatch(triple, action) {
+ if (!action) {
+ action = 'put';
+ }
var json = JSON.stringify(triple);
return genKeys(triple).map(function(key) {
return { type: action, key: key, value: json };
});
}
-module.exports.genActions = genActions;
+module.exports.generateBatch = generateBatch;
function materializer(pattern, data) {
return Object.keys(pattern)
View
33 test/triple_store_spec.js
@@ -393,3 +393,36 @@ describe('deferred open support', function() {
});
});
});
+
+describe('generateBatch', function () {
+ var db, leveldb = leveldb;
+
+ beforeEach(function() {
+ leveldb = level();
+ db = levelgraph(leveldb);
+ });
+
+ afterEach(function(done) {
+ db.close(done);
+ });
+
+ it('should generate a batch from a triple', function() {
+ var triple = { subject: 'a', predicate: 'b', object: 'c' };
+ var ops = db.generateBatch(triple);
+ expect(ops).to.have.property('length', 6);
+ ops.forEach(function (op) {
+ expect(op).to.have.property('type', 'put');
+ expect(JSON.parse(op.value)).to.eql(triple);
+ });
+ });
+
+ it('should generate a batch of type', function() {
+ var triple = { subject: 'a', predicate: 'b', object: 'c' };
+ var ops = db.generateBatch(triple, 'del');
+ expect(ops).to.have.property('length', 6);
+ ops.forEach(function (op) {
+ expect(op).to.have.property('type', 'del');
+ expect(JSON.parse(op.value)).to.eql(triple);
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.