From f7a6ebdb34b8f547b5aa03a2805d877ba304cd36 Mon Sep 17 00:00:00 2001 From: Brian Woodward Date: Mon, 27 Apr 2015 17:22:08 -0400 Subject: [PATCH 1/2] applyUpperBoundChar --- lib/utilities.js | 10 ++- test/triple_store_spec.js | 145 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 2 deletions(-) diff --git a/lib/utilities.js b/lib/utilities.js index 042598a..82a2e93 100644 --- a/lib/utilities.js +++ b/lib/utilities.js @@ -138,6 +138,12 @@ function typesFromPattern(pattern) { }); } +function applyUpperBoundChar(key) { + var parts = key.split('::'); + var len = parts.length; + return len === 4 && parts[len-1] !== '' ? key : key + upperBoundChar; +} + function createQuery(pattern, options) { var types = typesFromPattern(pattern) , preferiteIndex = options && options.index @@ -145,8 +151,8 @@ function createQuery(pattern, options) { , key = genKey(index, pattern, '') , limit = pattern.limit , reverse = pattern.reverse || false - , start = reverse ? key + upperBoundChar : key - , end = reverse ? key : key + upperBoundChar + , start = reverse ? applyUpperBoundChar(key) : key + , end = reverse ? key : applyUpperBoundChar(key) , query = { start: start , end: end diff --git a/test/triple_store_spec.js b/test/triple_store_spec.js index 82227f7..02c6a31 100644 --- a/test/triple_store_spec.js +++ b/test/triple_store_spec.js @@ -301,6 +301,151 @@ describe('a basic triple store', function() { }); }); + describe('with two triple inserted with the same predicate and same object', function() { + + var triple1 + , triple2; + + beforeEach(function(done) { + triple1 = { subject: 'a', predicate: 'b', object: 'c' }; + triple2 = { subject: 'a2', predicate: 'b', object: 'c' }; + db.put([triple1, triple2], done); + }); + + it('should get one by specifiying the subject', function(done) { + db.get({ subject: 'a' }, function(err, list) { + expect(list).to.eql([triple1]); + done(); + }); + }); + + it('should get one by specifiying the exact triple', function(done) { + db.get({ subject: 'a', predicate: 'b', object: 'c'}, function(err, list) { + expect(list).to.eql([triple1]); + done(); + }); + }); + + it('should get one by specifiying the subject and a falsy predicate', function(done) { + db.get({ subject: 'a', predicate: null }, function(err, list) { + expect(list).to.eql([triple1]); + done(); + }); + }); + + it('should get two by specifiying the predicate', function(done) { + db.get({ predicate: 'b' }, function(err, list) { + expect(list).to.eql([triple1, triple2]); + done(); + }); + }); + + it('should get two by specifiying the predicate and a falsy subject', function(done) { + db.get({ subject: null, predicate: 'b' }, function(err, list) { + expect(list).to.eql([triple1, triple2]); + done(); + }); + }); + + it('should remove one and still return the other', function(done) { + db.del(triple2, function() { + db.get({ predicate: 'b' }, function(err, list) { + expect(list).to.eql([triple1]); + done(); + }); + }); + }); + + it('should return both triples through the getStream interface', function(done) { + var triples = [triple1, triple2] + , stream = db.getStream({ predicate: 'b' }); + stream.on('data', function(data) { + expect(data).to.eql(triples.shift()); + }); + + stream.on('end', done); + }); + + it('should return only one triple with limit 1', function(done) { + db.get({ predicate: 'b', limit: 1 }, function(err, list) { + expect(list).to.eql([triple1]); + done(); + }); + }); + + it('should return two triples with limit 2', function(done) { + db.get({ predicate: 'b', limit: 2 }, function(err, list) { + expect(list).to.eql([triple1, triple2]); + done(); + }); + }); + + it('should return three triples with limit 3', function(done) { + db.get({ predicate: 'b', limit: 3 }, function(err, list) { + expect(list).to.eql([triple1, triple2]); + done(); + }); + }); + + it('should support limit over streams', function(done) { + var triples = [triple1] + , stream = db.getStream({ predicate: 'b', limit: 1 }); + stream.on('data', function(data) { + expect(data).to.eql(triples.shift()); + }); + + stream.on('end', done); + }); + + it('should return only one triple with offset 1', function(done) { + db.get({ predicate: 'b', offset: 1 }, function(err, list) { + expect(list).to.eql([triple2]); + done(); + }); + }); + + it('should return only no triples with offset 2', function(done) { + db.get({ predicate: 'b', offset: 2 }, function(err, list) { + expect(list).to.eql([]); + done(); + }); + }); + + it('should support offset over streams', function(done) { + var triples = [triple2] + , stream = db.getStream({ predicate: 'b', offset: 1 }); + stream.on('data', function(data) { + expect(data).to.eql(triples.shift()); + }); + + stream.on('end', done); + }); + + it('should return the triples in reverse order with reverse true', function(done) { + db.get({ predicate: 'b', reverse: true }, function(err, list) { + expect(list).to.eql([triple2, triple1]); + done(); + }); + }); + + it('should return the last triple with reverse true and limit 1', function(done) { + db.get({ predicate: 'b', reverse: true, limit: 1 }, function(err, list) { + expect(list).to.eql([triple2]); + done(); + }); + }); + + it('should support reverse over streams', function(done) { + var triples = [triple2, triple1] + , stream = db.getStream({ predicate: 'b', reverse: true }); + stream.on('data', function(data) { + expect(data).to.eql(triples.shift()); + }); + + stream.on('end', done); + }); + }); + describe('with 10 triples inserted', function() { beforeEach(function (done) { var triples = []; From 9b92c00a280705019c37698f1726d43fb742ec8c Mon Sep 17 00:00:00 2001 From: Brian Woodward Date: Mon, 27 Apr 2015 17:31:44 -0400 Subject: [PATCH 2/2] adding myself to contributors --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 347f05d..bd3ac8a 100644 --- a/README.md +++ b/README.md @@ -660,6 +660,7 @@ LevelGraph is only possible due to the excellent work of the following contribut href="https://github.com/jez0990">GitHub/jez0990 Elf PavlikGitHub/elf-pavlikTwitter/@elfpavlik Riceball LEEGitHub/snowyu +Brian WoodwardGitHub/doowbTwitter/@doowb ## LICENSE - "MIT License"