Skip to content

Commit fe757a1

Browse files
author
Benjamin Forster
committed
set options in hyperdb when new graph is made
1 parent 1e4ba05 commit fe757a1

5 files changed

Lines changed: 142 additions & 10 deletions

File tree

index.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Variable = require('./lib/Variable')
1212
const HyperdbReadTransform = require('./lib/HyperdbReadTransform')
1313
const JoinStream = require('./lib/JoinStream')
1414
const planner = require('./lib/planner')
15+
const pkg = require('./package.json')
1516
const attachCreateReadStream = require('./lib/hyperdbModifier').attachCreateReadStream
1617

1718
const Transform = stream.Transform
@@ -25,11 +26,19 @@ if (!hyperdb.createReadStream) {
2526
function Graph (storage, key, opts) {
2627
if (!(this instanceof Graph)) return new Graph(storage, key, opts)
2728
events.EventEmitter.call(this)
28-
this.db = hyperdb(storage, key, opts)
2929

30-
// what are the default prefixes
31-
this._prefixes = constants.DEFAULT_PREFIXES
32-
this._indexes = (opts && opts.index === 'small')
30+
if (typeof key === 'string') key = Buffer.from(key, 'hex')
31+
32+
if (!Buffer.isBuffer(key) && !opts) {
33+
opts = key
34+
key = null
35+
}
36+
37+
if (!opts) opts = {}
38+
this.db = hyperdb(storage, key, opts)
39+
this._prefixes = opts.prefixes || constants.DEFAULT_PREFIXES
40+
this._prefixes._ = opts.base || constants.DEFAULT_BASE
41+
this._indexes = opts.index === 'tri'
3342
? constants.HEXSTORE_INDEXES_REDUCED
3443
: constants.HEXSTORE_INDEXES
3544
this._indexKeys = Object.keys(this._indexes)
@@ -38,13 +47,33 @@ function Graph (storage, key, opts) {
3847
this.emit('error', e)
3948
})
4049
this.db.on('ready', (e) => {
41-
this.emit('ready', e)
42-
// check prefixes
50+
if (utils.isNewDatabase(this.db)) {
51+
this._onNew((err) => {
52+
if (err) return this.emit('error', err)
53+
this.emit('ready', e)
54+
})
55+
} else {
56+
this.emit('ready', e)
57+
}
4358
})
4459
}
4560

4661
inherits(Graph, events.EventEmitter)
4762

63+
Graph.prototype._onNew = function (cb) {
64+
const metadata = [
65+
['@version', pkg.version],
66+
['@index', Object.keys(this._indexes).length === 3 ? 'tri' : 'hex'],
67+
['@name', this._prefixes._]
68+
]
69+
Object.keys(this._prefixes).forEach((key) => {
70+
if (key !== '_') {
71+
metadata.push([prefixes.toKey(key), this._prefixes[key]])
72+
}
73+
})
74+
utils.put(this.db, metadata, cb)
75+
}
76+
4877
Graph.prototype.v = (name) => new Variable(name)
4978

5079
Graph.prototype.listPrefixes = function (callback) {
@@ -206,9 +235,7 @@ Graph.prototype._possibleIndexes = function (types) {
206235
}
207236
})
208237
})
209-
210238
result.sort()
211-
212239
return result
213240
}
214241

lib/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ const HEXSTORE_INDEXES_REDUCED = {
1212
osp: HEXSTORE_INDEXES.osp
1313
}
1414
const PREFIX_KEY = '@prefix/'
15+
const DEFAULT_BASE = 'hg://'
1516
const DEFAULT_PREFIXES = {
16-
_: 'hg://',
1717
foaf: 'http://xmlns.com/foaf/0.1/'
1818
}
1919

2020
module.exports = {
2121
PREFIX_KEY,
22+
DEFAULT_BASE,
2223
DEFAULT_PREFIXES,
2324
HEXSTORE_INDEXES,
2425
HEXSTORE_INDEXES_REDUCED

lib/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ const tripleAliasMap = {
99
o: 'object'
1010
}
1111

12+
function isNewDatabase (db) {
13+
return db._writers.length > 0 || db._writers[0].feed.length > 0
14+
}
15+
16+
function put (db, data, callback) {
17+
var i = 0
18+
19+
next()
20+
21+
function next (err) {
22+
if (err) return callback(err)
23+
var v = data[i++]
24+
db.put(v[0], v[1], (i < data.length - 1) ? next : callback)
25+
}
26+
}
27+
1228
function collect (stream, cb) {
1329
var res = []
1430
stream.on('data', res.push.bind(res))
@@ -180,6 +196,8 @@ function materializer (pattern, data) {
180196
}
181197

182198
module.exports = {
199+
isNewDatabase,
200+
put,
183201
escapeKeyValue,
184202
unescapeKeyValue,
185203
encodeTriple,

test/basic.spec.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* eslint-env mocha */
2+
const expect = require('chai').expect
3+
const ram = require('random-access-memory')
4+
const hypergraph = require('../index')
5+
const constants = require('../lib/constants')
6+
7+
function ramStore (filename) {
8+
// filename will be one of: data, bitfield, tree, signatures, key, secret_key
9+
// the data file will contain all your data concattenated.
10+
// just store all files in ram by returning a random-access-memory instance
11+
return ram()
12+
}
13+
14+
describe('hypergraph', function () {
15+
let db
16+
context('when newly created it adds metadata to db', () => {
17+
it('includes graph version', (done) => {
18+
db = hypergraph(ramStore)
19+
db.on('ready', () => {
20+
db.db.get('@version', (err, node) => {
21+
expect(err).to.not.be.a('error')
22+
expect(node[0].value.toString()).to.match(/\d+.\d+.\d+.*/)
23+
done()
24+
})
25+
})
26+
})
27+
it('includes index type (default)', (done) => {
28+
db = hypergraph(ramStore)
29+
db.on('ready', () => {
30+
db.db.get('@index', (err, node) => {
31+
expect(err).to.not.be.a('error')
32+
expect(node[0].value.toString()).to.eql('hex')
33+
done()
34+
})
35+
})
36+
})
37+
it('includes index type (option.index = tri)', (done) => {
38+
db = hypergraph(ramStore, { index: 'tri' })
39+
db.on('ready', () => {
40+
db.db.get('@index', (err, node) => {
41+
expect(err).to.not.be.a('error')
42+
expect(node[0].value.toString()).to.eql('tri')
43+
done()
44+
})
45+
})
46+
})
47+
it('includes name (default)', (done) => {
48+
db = hypergraph(ramStore)
49+
db.on('ready', () => {
50+
db.db.get('@name', (err, node) => {
51+
expect(err).to.not.be.a('error')
52+
expect(node[0].value.toString()).to.eql(constants.DEFAULT_BASE)
53+
done()
54+
})
55+
})
56+
})
57+
it('includes name (option.name)', (done) => {
58+
db = hypergraph(ramStore, { base: 'this://' })
59+
db.on('ready', () => {
60+
db.db.get('@name', (err, node) => {
61+
expect(err).to.not.be.a('error')
62+
expect(node[0].value.toString()).to.eql('this://')
63+
done()
64+
})
65+
})
66+
})
67+
it('includes default prefixes', (done) => {
68+
db = hypergraph(ramStore)
69+
db.on('ready', () => {
70+
const stream = db.db.createReadStream('@prefix/')
71+
stream.on('data', (nodes) => {
72+
73+
})
74+
stream.on('error', done)
75+
stream.on('end', () => {
76+
done()
77+
})
78+
})
79+
})
80+
})
81+
context('when loading db that already exists', () => {
82+
it('does not add new metadata')
83+
it('overrides options with metadata set in hyperdb (index)')
84+
it('overrides options with metadata set in hyperdb (name)')
85+
})
86+
})

test/triple-store.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ describe('generateBatch', function () {
635635
})
636636
context('with index option set to small', () => {
637637
beforeEach(function () {
638-
db = hypergraph(ramStore, null, { index: 'small' })
638+
db = hypergraph(ramStore, null, { index: 'tri' })
639639
})
640640

641641
afterEach(function (done) {

0 commit comments

Comments
 (0)