Skip to content

Commit

Permalink
Merge pull request #62 from phuu/gen-batch-pairs
Browse files Browse the repository at this point in the history
Add generateBatch method to db API.
  • Loading branch information
mcollina committed Jan 19, 2014
2 parents bd85235 + ea2e8d7 commit 0167e80
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/levelgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions test/triple_store_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 0167e80

Please sign in to comment.