Skip to content

Commit 6f6b1ea

Browse files
author
Benjamin Forster
committed
load graph metadata and options when not new graph.
1 parent 98a99c6 commit 6f6b1ea

2 files changed

Lines changed: 148 additions & 8 deletions

File tree

index.js

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ 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._basename = opts.base || constants.DEFAULT_BASE
40+
this._basename = opts.name || constants.DEFAULT_BASE
4141
this._prefixes._ = this._basename
42+
this._indexType = opts.index === 'tri' ? 'tri' : 'hex'
4243
this._indexes = opts.index === 'tri'
4344
? constants.HEXSTORE_INDEXES_REDUCED
4445
: constants.HEXSTORE_INDEXES
@@ -54,7 +55,10 @@ function Graph (storage, key, opts) {
5455
this.emit('ready', e)
5556
})
5657
} else {
57-
this.emit('ready', e)
58+
this._onInit((err) => {
59+
if (err) return this.emit('error', err)
60+
this.emit('ready', e)
61+
})
5862
}
5963
})
6064
}
@@ -65,7 +69,7 @@ Graph.prototype._onNew = function (cb) {
6569
this._version = pkg.version
6670
const metadata = [
6771
['@version', pkg.version],
68-
['@index', Object.keys(this._indexes).length === 3 ? 'tri' : 'hex'],
72+
['@index', this._indexType],
6973
['@name', this._basename]
7074
]
7175
Object.keys(this._prefixes).forEach((key) => {
@@ -76,9 +80,78 @@ Graph.prototype._onNew = function (cb) {
7680
utils.put(this.db, metadata, cb)
7781
}
7882

83+
Graph.prototype._onInit = function (cb) {
84+
// get and set graph version
85+
this._version = null
86+
this._basename = null
87+
this._indexType = null
88+
89+
let missing = 4
90+
let error = null
91+
// get and set version
92+
this.graphVersion((err, version) => {
93+
if (err) error = err
94+
this._version = version
95+
maybeDone()
96+
})
97+
// get and set graph name
98+
this.name((err, name) => {
99+
if (err) error = err
100+
this._basename = name || constants.DEFAULT_BASE
101+
// modify prefixes to ensure correct namespacing
102+
this._prefixes._ = this._basename
103+
maybeDone()
104+
})
105+
// get and set graph indexation
106+
this.indexType((err, index) => {
107+
if (err) error = err
108+
this._indexType = index || 'hex'
109+
this._indexes = index === 'tri'
110+
? constants.HEXSTORE_INDEXES_REDUCED
111+
: constants.HEXSTORE_INDEXES
112+
this._indexKeys = Object.keys(this._indexes)
113+
maybeDone()
114+
})
115+
// get and set prefixes
116+
this.prefixes((err, prefixes) => {
117+
if (err) error = err
118+
this._prefixes = Object.assign({ _: this._prefixes }, prefixes)
119+
maybeDone()
120+
})
121+
function maybeDone () {
122+
missing--
123+
if (!missing) {
124+
cb(error)
125+
}
126+
}
127+
}
128+
79129
Graph.prototype.v = (name) => new Variable(name)
80130

81-
Graph.prototype.listPrefixes = function (callback) {
131+
function returnValueAsString (cb) {
132+
return (err, nodes) => {
133+
if (err) return cb(err)
134+
if (!nodes) return cb(null, null)
135+
cb(null, nodes[0].value.toString())
136+
}
137+
}
138+
139+
Graph.prototype.graphVersion = function (cb) {
140+
if (this._version) return cb(null, this._version)
141+
this.db.get('@version', returnValueAsString(cb))
142+
}
143+
144+
Graph.prototype.name = function (cb) {
145+
if (this._basename) return cb(null, this._basename)
146+
this.db.get('@name', returnValueAsString(cb))
147+
}
148+
149+
Graph.prototype.indexType = function (cb) {
150+
if (this._indexType) return cb(null, this._indexType)
151+
this.db.get('@index', returnValueAsString(cb))
152+
}
153+
154+
Graph.prototype.prefixes = function (callback) {
82155
// should cache this somehow
83156
const prefixStream = this.db.createReadStream(constants.PREFIX_KEY)
84157
utils.collect(prefixStream, (err, data) => {

test/basic.spec.js

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ram = require('random-access-memory')
44
const tmp = require('tmp')
55
const path = require('path')
66
const hyperdb = require('hyperdb')
7+
const pkg = require('../package.json')
78

89
const hypergraph = require('../index')
910
const constants = require('../lib/constants')
@@ -60,7 +61,7 @@ describe('hypergraph', function () {
6061
})
6162
})
6263
it('includes name (option.name)', (done) => {
63-
db = hypergraph(ramStore, { base: 'this://' })
64+
db = hypergraph(ramStore, { name: 'this://' })
6465
db.on('ready', () => {
6566
db.db.get('@name', (err, node) => {
6667
expect(err).to.not.be.a('error')
@@ -124,7 +125,6 @@ describe('hypergraph', function () {
124125
// create new hyperdb
125126
const hyper = hyperdb(dbDir)
126127
hyper.on('ready', () => {
127-
// as something so that its not an empty feed
128128
hyper.put('test', 'data', (err) => {
129129
if (err) return finish(err)
130130
openExistingDBAsGraphDB()
@@ -148,7 +148,74 @@ describe('hypergraph', function () {
148148
}
149149
})
150150
})
151-
it('overrides options with metadata set in hyperdb (index)')
152-
it('overrides options with metadata set in hyperdb (name)')
151+
152+
context('with graph already containing index, version, name and prefixes', () => {
153+
const options = {
154+
index: 'tri',
155+
name: 'baseName',
156+
prefixes: {
157+
test: 'http://hyperreadings.info/test#',
158+
xsd: 'http://www.w3.org/2001/XMLSchema#'
159+
}
160+
}
161+
let cleanup = () => {}
162+
let graphDir
163+
before((done) => {
164+
tmp.dir({ unsafeCleanup: true }, (err, dir, cleanupCallback) => {
165+
if (err) return done(err)
166+
cleanup = cleanupCallback
167+
graphDir = dir
168+
const graph = hypergraph(graphDir, options)
169+
graph.on('ready', () => { done() })
170+
graph.on('error', done)
171+
})
172+
})
173+
after(() => {
174+
cleanup()
175+
})
176+
it('contains version that was used to create the db', (done) => {
177+
const graph = hypergraph(graphDir)
178+
graph.on('ready', () => {
179+
graph.graphVersion((err, version) => {
180+
expect(err).to.eql(null)
181+
expect(version).to.eql(pkg.version)
182+
done()
183+
})
184+
})
185+
})
186+
it('overrides options with metadata set in hyperdb (index)', (done) => {
187+
const graph = hypergraph(graphDir, { index: 'hex' })
188+
graph.on('ready', () => {
189+
graph.indexType((err, index) => {
190+
expect(err).to.eql(null)
191+
expect(index).to.eql(options.index)
192+
done()
193+
})
194+
})
195+
})
196+
it('overrides options with metadata set in hyperdb (name)', (done) => {
197+
const graph = hypergraph(graphDir, { index: 'hex', name: 'overrideMe' })
198+
graph.on('ready', () => {
199+
graph.name((err, name) => {
200+
expect(err).to.eql(null)
201+
expect(name).to.eql(options.name)
202+
done()
203+
})
204+
})
205+
})
206+
it('overrides options with metadata set in hyperdb (prefix)', (done) => {
207+
const prefixes = {
208+
thing: 'http://some.co/thing#'
209+
}
210+
const graph = hypergraph(graphDir, { index: 'hex', name: 'overrideMe', prefixes })
211+
graph.on('ready', () => {
212+
graph.prefixes((err, prefixes) => {
213+
expect(err).to.eql(null)
214+
expect(prefixes).to.deep.eql(options.prefixes)
215+
done()
216+
})
217+
})
218+
})
219+
})
153220
})
154221
})

0 commit comments

Comments
 (0)