Skip to content

Commit 48c8de6

Browse files
author
Benjamin Forster
committed
the beginning of smarter encoding and decoding of prefixes
1 parent 58d6acb commit 48c8de6

6 files changed

Lines changed: 24 additions & 21 deletions

File tree

index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function Graph (storage, key, opts) {
3737
if (!opts) opts = {}
3838
this.db = hyperdb(storage, key, opts)
3939
this._prefixes = Object.assign({}, opts.prefixes || constants.DEFAULT_PREFIXES)
40-
this._prefixes._ = opts.base || constants.DEFAULT_BASE
40+
this._basename = opts.base || constants.DEFAULT_BASE
41+
this._prefixes._ = this._basename
4142
this._indexes = opts.index === 'tri'
4243
? constants.HEXSTORE_INDEXES_REDUCED
4344
: constants.HEXSTORE_INDEXES
@@ -61,10 +62,11 @@ function Graph (storage, key, opts) {
6162
inherits(Graph, events.EventEmitter)
6263

6364
Graph.prototype._onNew = function (cb) {
65+
this._version = pkg.version
6466
const metadata = [
6567
['@version', pkg.version],
6668
['@index', Object.keys(this._indexes).length === 3 ? 'tri' : 'hex'],
67-
['@name', this._prefixes._]
69+
['@name', this._basename]
6870
]
6971
Object.keys(this._prefixes).forEach((key) => {
7072
if (key !== '_') {
@@ -95,8 +97,8 @@ Graph.prototype.addPrefix = function (prefix, uri, cb) {
9597
}
9698

9799
Graph.prototype.getStream = function (triple, opts) {
98-
const stream = this.db.createReadStream(this._createQuery(triple))
99-
return stream.pipe(new HyperdbReadTransform(this.db, this._prefixes, opts))
100+
const stream = this.db.createReadStream(this._createQuery(triple, { encode: (!opts || opts.encode === undefined) ? true : opts.encode }))
101+
return stream.pipe(new HyperdbReadTransform(this.db, this._basename, opts))
100102
}
101103

102104
Graph.prototype.get = function (triple, opts, callback) {
@@ -153,8 +155,7 @@ Graph.prototype.searchStream = function (query, options) {
153155
} else if (!Array.isArray(query)) {
154156
query = [ query ]
155157
}
156-
const plannedQuery = planner(query)
157-
158+
const plannedQuery = planner(query, this._prefixes)
158159
var streams = plannedQuery.map((triple, i) => {
159160
const limit = (options && i === plannedQuery.length - 1) ? options.limit : undefined
160161
return new JoinStream({
@@ -217,7 +218,7 @@ Graph.prototype._createQuery = function (pattern, options) {
217218
var types = utils.typesFromPattern(pattern)
218219
var preferedIndex = options && options.index
219220
var index = this._findIndex(types, preferedIndex)
220-
const encodedTriple = utils.encodeTriple(pattern, this._prefixes)
221+
const encodedTriple = utils.encodeTriple(pattern, options.encode ? this._prefixes : { _: this._basename })
221222
var key = utils.encodeKey(index, encodedTriple)
222223
return key
223224
}

lib/HyperdbReadTransform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const Transform = require('readable-stream').Transform
22
const inherits = require('inherits')
33
const utils = require('./utils')
44

5-
function HyperdbReadTransform (db, prefixes, options) {
5+
function HyperdbReadTransform (db, basename, options) {
66
if (!(this instanceof HyperdbReadTransform)) {
77
return new HyperdbReadTransform(db, options)
88
}
99
var opts = options || {}
1010
this.db = db
11-
this._prefixes = prefixes
11+
this._prefixes = { _: basename }
1212
this._finished = false
1313
this._count = 0
1414
this._filter = opts.filter

lib/JoinStream.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ function JoinStream (options) {
1616
this.matcher = matcher(options.triple)
1717
this.mask = queryMask(options.triple)
1818
this.maskUpdater = maskUpdater(options.triple)
19-
this.limit = options && options.limit
19+
this.limit = options.limit
2020
this._limitCounter = 0
2121
this.db = options.db
2222
this._ended = false
23-
this.filter = options && options.filter
24-
this.offset = options && options.offset
23+
this.filter = options.filter
24+
this.offset = options.offset
2525

2626
this.once('pipe', (source) => {
2727
source.on('error', (err) => {
@@ -49,7 +49,11 @@ function JoinStream (options) {
4949
}
5050
}
5151

52-
this._options = { filter: this.filter, offset: this.offset }
52+
this._options = {
53+
filter: this.filter,
54+
offset: this.offset,
55+
encode: options.encode ? !!options.encode : false
56+
}
5357
}
5458

5559
inherits(JoinStream, Transform)

lib/planner.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
const utils = require('./utils')
33

4-
function planner (query) {
5-
if (query.length === 1) return query
4+
function planner (query, prefixMap) {
5+
if (query.length === 1) return [utils.encodeTriple(query[0], prefixMap)]
66
// first group queries based on number of variables.
77
const solved = new Set()
88
var grouped = query.reduce((ordered, q) => {
@@ -24,7 +24,7 @@ function planner (query) {
2424
return ordered
2525
}, [])
2626
// then order vars > 1 by if they occur in
27-
const orderedQueries = grouped[1] ? grouped[1].map(v => v.query) : []
27+
const orderedQueries = grouped[1] ? grouped[1].map(v => utils.encodeTriple(v.query, prefixMap)) : []
2828

2929
for (let i = 2; i < grouped.length; i++) {
3030
if (grouped[i] === undefined) continue
@@ -53,7 +53,7 @@ function planner (query) {
5353
return 0
5454
})
5555
const next = grouped[i].shift()
56-
orderedQueries.push(next.query)
56+
orderedQueries.push(utils.encodeTriple(next.query, prefixMap))
5757
next.names.forEach(n => solved.add(n))
5858
}
5959
}

lib/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ function unescapeKeyValue (value, prefixMap) {
5454
}
5555

5656
function encodeTriple (triple, prefixMap) {
57-
// var newTriple = Object.assign({}, triple)
5857
spo.forEach((key) => {
5958
if (triple.hasOwnProperty(key)) {
6059
triple[key] = escapeKeyValue(triple[key], prefixMap)

test/basic.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ describe('hypergraph', function () {
118118
})
119119
context('when loading db that already exists', () => {
120120
it('does not add new metadata', (done) => {
121-
tmp.dir(function (err, dir, cleanupCallback) {
121+
tmp.dir({ unsafeCleanup: true }, function (err, dir, cleanupCallback) {
122122
if (err) return done(err)
123-
console.log('Dir: ', dir)
124123
const dbDir = path.join(dir, 'test.db')
125124
// create new hyperdb
126125
const hyper = hyperdb(dbDir)
@@ -138,7 +137,7 @@ describe('hypergraph', function () {
138137
db.on('ready', () => {
139138
db.db.get('@version', (err, nodes) => {
140139
expect(nodes).to.eql(null)
141-
done(err)
140+
finish(err)
142141
})
143142
})
144143
db.on('error', finish)

0 commit comments

Comments
 (0)