Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 1462fcc

Browse files
feat: add open method
1 parent d098575 commit 1462fcc

11 files changed

+877
-352
lines changed

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
},
3535
"homepage": "https://github.com/ipfs/js-datastore-core#readme",
3636
"devDependencies": {
37-
"aegir": "^10.0.0",
37+
"aegir": "^11.0.1",
3838
"chai": "^3.5.0",
39-
"flow-bin": "^0.41.0"
39+
"dirty-chai": "^1.2.2",
40+
"flow-bin": "^0.42.0"
4041
},
4142
"dependencies": {
4243
"async": "^2.1.5",
@@ -52,4 +53,4 @@
5253
"contributors": [
5354
"Friedel Ziegelmayer <[email protected]>"
5455
]
55-
}
56+
}

src/keytransform.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const pull = require('pull-stream')
55

66
/* ::
7-
import type {Key, Datastore, Batch, Query, QueryResult} from 'interface-datastore'
7+
import type {Key, Datastore, Batch, Query, QueryResult, Callback} from 'interface-datastore'
88
*/
99

1010
/**
@@ -38,19 +38,23 @@ class KeyTransformDatastore /* :: <Value> */ {
3838
this.transform = transform
3939
}
4040

41-
put (key /* : Key */, val /* : Value */, callback /* : (?Error) => void */) /* : void */ {
41+
open (callback /* : Callback<void> */) /* : void */ {
42+
this.child.open(callback)
43+
}
44+
45+
put (key /* : Key */, val /* : Value */, callback /* : Callback<void> */) /* : void */ {
4246
this.child.put(this.transform.convert(key), val, callback)
4347
}
4448

45-
get (key /* : Key */, callback /* : (?Error, ?Value) => void */) /* : void */ {
49+
get (key /* : Key */, callback /* : Callback<Value> */) /* : void */ {
4650
this.child.get(this.transform.convert(key), callback)
4751
}
4852

49-
has (key /* : Key */, callback /* : (?Error, ?bool) => void */) /* : void */ {
53+
has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
5054
this.child.has(this.transform.convert(key), callback)
5155
}
5256

53-
delete (key /* : Key */, callback /* : (?Error) => void */) /* : void */ {
57+
delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
5458
this.child.delete(this.transform.convert(key), callback)
5559
}
5660

@@ -63,7 +67,7 @@ class KeyTransformDatastore /* :: <Value> */ {
6367
delete: (key /* : Key */) /* : void */ => {
6468
b.delete(this.transform.convert(key))
6569
},
66-
commit: (callback /* : (err: ?Error) => void */) => {
70+
commit: (callback /* : Callback<void> */) /* : void */ => {
6771
b.commit(callback)
6872
}
6973
}
@@ -79,7 +83,7 @@ class KeyTransformDatastore /* :: <Value> */ {
7983
)
8084
}
8185

82-
close (callback /* : (err: ?Error) => void */) /* : void */ {
86+
close (callback /* : Callback<void> */) /* : void */ {
8387
this.child.close(callback)
8488
}
8589
}

src/mount.js

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class MountDatastore /* :: <Value> */ {
3333
this.mounts = mounts.slice()
3434
}
3535

36+
open (callback /* : Callback<void> */) /* : void */ {
37+
each(this.mounts, (m, cb) => {
38+
m.datastore.open(cb)
39+
}, callback)
40+
}
41+
3642
/**
3743
* Lookup the matching datastore for the given key.
3844
*

src/sharding.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class ShardingDatastore {
3636
this.shard = shard
3737
}
3838

39+
open (callback /* : Callback<void> */) /* : void */ {
40+
this.child.open(callback)
41+
}
42+
3943
_convertKey (key/* : Key */)/* : Key */ {
4044
const s = key.toString()
4145
if (s === shardKey.toString() || s === shardReadmeKey.toString()) {
@@ -65,9 +69,12 @@ class ShardingDatastore {
6569
}
6670

6771
static open (store /* : Datastore<Buffer> */, callback /* : Callback<ShardingDatastore> */) /* : void */ {
68-
waterfall([cb => sh.readShardFun('/', store, cb), (shard, cb) => {
69-
cb(null, new ShardingDatastore(store, shard))
70-
}], callback)
72+
waterfall([
73+
(cb) => sh.readShardFun('/', store, cb),
74+
(shard, cb) => {
75+
cb(null, new ShardingDatastore(store, shard))
76+
}
77+
], callback)
7178
}
7279

7380
static create (store /* : Datastore<Buffer> */, shard /* : ShardV1 */, callback /* : Callback<void> */) /* : void */ {

src/tiered.js

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class TieredDatastore /* :: <Value> */ {
2222
this.stores = stores.slice()
2323
}
2424

25+
open (callback /* : Callback<void> */) /* : void */ {
26+
each(this.stores, (store, cb) => {
27+
store.open(cb)
28+
}, callback)
29+
}
30+
2531
put (key /* : Key */, value /* : Value */, callback /* : Callback<void> */) /* : void */ {
2632
each(this.stores, (store, cb) => {
2733
store.put(key, value, cb)

test/keytransform.spec.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
/* eslint-env mocha */
33
'use strict'
44

5-
const expect = require('chai').expect
5+
const chai = require('chai')
6+
chai.use(require('dirty-chai'))
7+
const expect = chai.expect
68
const series = require('async/series')
79
const each = require('async/each')
810
const map = require('async/map')
@@ -52,15 +54,15 @@ describe('KeyTransformDatastore', () => {
5254
mStore.get(new Key('abc').child(k), cb)
5355
}, cb)
5456
], (err, res) => {
55-
expect(err).to.not.exist
57+
expect(err).to.not.exist()
5658
expect(res[0]).to.eql(res[1])
5759
cb()
5860
}),
5961
(cb) => parallel([
6062
(cb) => pull(mStore.query({}), pull.collect(cb)),
6163
(cb) => pull(kStore.query({}), pull.collect(cb))
6264
], (err, res) => {
63-
expect(err).to.not.exist
65+
expect(err).to.not.exist()
6466
expect(res[0]).to.have.length(res[1].length)
6567

6668
res[0].forEach((a, i) => {

test/mount.spec.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
/* eslint max-nested-callbacks: ["error", 8] */
44
'use strict'
55

6+
const chai = require('chai')
7+
chai.use(require('dirty-chai'))
8+
const expect = chai.expect
69
const pull = require('pull-stream')
7-
const expect = require('chai').expect
810
const series = require('async/series')
911

1012
const Key = require('interface-datastore').Key
@@ -45,7 +47,7 @@ describe('MountStore', () => {
4547
series([
4648
(cb) => m.put(new Key('/cool/hello'), val, cb),
4749
(cb) => mds.get(new Key('/hello'), (err, res) => {
48-
expect(err).to.not.exist
50+
expect(err).to.not.exist()
4951
expect(res).to.eql(val)
5052
cb()
5153
})
@@ -63,7 +65,7 @@ describe('MountStore', () => {
6365
series([
6466
(cb) => mds.put(new Key('/hello'), val, cb),
6567
(cb) => m.get(new Key('/cool/hello'), (err, res) => {
66-
expect(err).to.not.exist
68+
expect(err).to.not.exist()
6769
expect(res).to.eql(val)
6870
cb()
6971
})
@@ -81,7 +83,7 @@ describe('MountStore', () => {
8183
series([
8284
(cb) => mds.put(new Key('/hello'), val, cb),
8385
(cb) => m.has(new Key('/cool/hello'), (err, exists) => {
84-
expect(err).to.not.exist
86+
expect(err).to.not.exist()
8587
expect(exists).to.eql(true)
8688
cb()
8789
})
@@ -100,12 +102,12 @@ describe('MountStore', () => {
100102
(cb) => m.put(new Key('/cool/hello'), val, cb),
101103
(cb) => m.delete(new Key('/cool/hello'), cb),
102104
(cb) => m.has(new Key('/cool/hello'), (err, exists) => {
103-
expect(err).to.not.exist
105+
expect(err).to.not.exist()
104106
expect(exists).to.eql(false)
105107
cb()
106108
}),
107109
(cb) => mds.has(new Key('/hello'), (err, exists) => {
108-
expect(err).to.not.exist
110+
expect(err).to.not.exist()
109111
expect(exists).to.eql(false)
110112
cb()
111113
})
@@ -126,7 +128,7 @@ describe('MountStore', () => {
126128
pull(
127129
m.query({prefix: '/cool'}),
128130
pull.collect((err, res) => {
129-
expect(err).to.not.exist
131+
expect(err).to.not.exist()
130132
expect(res).to.eql([{
131133
key: new Key('/cool/hello'),
132134
value: val

test/namespace.spec.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
/* eslint-env mocha */
33
'use strict'
44

5-
const expect = require('chai').expect
5+
const chai = require('chai')
6+
chai.use(require('dirty-chai'))
7+
const expect = chai.expect
68
const series = require('async/series')
79
const each = require('async/each')
810
const map = require('async/map')
@@ -44,15 +46,15 @@ describe('KeyTransformDatastore', () => {
4446
mStore.get(new Key(prefix).child(k), cb)
4547
}, cb)
4648
], (err, res) => {
47-
expect(err).to.not.exist
49+
expect(err).to.not.exist()
4850
expect(res[0]).to.eql(res[1])
4951
cb()
5052
}),
5153
(cb) => parallel([
5254
(cb) => pull(mStore.query({}), pull.collect(cb)),
5355
(cb) => pull(store.query({}), pull.collect(cb))
5456
], (err, res) => {
55-
expect(err).to.not.exist
57+
expect(err).to.not.exist()
5658
expect(res[0]).to.have.length(res[1].length)
5759

5860
res[0].forEach((a, i) => {

test/sharding.spec.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
/* eslint max-nested-callbacks: ["error", 8] */
44
'use strict'
55

6-
const expect = require('chai').expect
6+
const chai = require('chai')
7+
chai.use(require('dirty-chai'))
8+
const expect = chai.expect
79
const series = require('async/series')
810
const parallel = require('async/parallel')
911
const waterfall = require('async/waterfall')
@@ -45,8 +47,8 @@ describe('ShardingStore', () => {
4547
const ms = new MemoryStore()
4648

4749
ShardingStore.open(ms, (err, ss) => {
48-
expect(err).to.exist
49-
expect(ss).to.not.exist
50+
expect(err).to.exist()
51+
expect(ss).to.not.exist()
5052
done()
5153
})
5254
})
@@ -65,7 +67,7 @@ describe('ShardingStore', () => {
6567
const ms = new MemoryStore()
6668
const shard = new sh.NextToLast(2)
6769
ShardingStore.createOrOpen(ms, shard, (err, ss) => {
68-
expect(err).to.not.exist
70+
expect(err).to.not.exist()
6971
if (ss == null) {
7072
return done(new Error('missing store'))
7173
}
@@ -74,7 +76,7 @@ describe('ShardingStore', () => {
7476
series([
7577
(cb) => store.put(new Key('hello'), new Buffer('test'), cb),
7678
(cb) => ms.get(new Key('ll').child(new Key('hello')), (err, res) => {
77-
expect(err).to.not.exist
79+
expect(err).to.not.exist()
7880
expect(res).to.eql(new Buffer('test'))
7981
cb()
8082
})

test/tiered.spec.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
/* eslint max-nested-callbacks: ["error", 8] */
44
'use strict'
55

6-
const expect = require('chai').expect
6+
const chai = require('chai')
7+
chai.use(require('dirty-chai'))
8+
const expect = chai.expect
79
const series = require('async/series')
810
const parallel = require('async/parallel')
911

@@ -31,7 +33,7 @@ describe('Tiered', () => {
3133
(cb) => ms[0].get(k, cb),
3234
(cb) => ms[1].get(k, cb)
3335
], (err, res) => {
34-
expect(err).to.not.exist
36+
expect(err).to.not.exist()
3537
res.forEach((val) => {
3638
expect(val).to.be.eql(v)
3739
})
@@ -47,12 +49,12 @@ describe('Tiered', () => {
4749
series([
4850
(cb) => ms[1].put(k, v, cb),
4951
(cb) => store.get(k, (err, val) => {
50-
expect(err).to.not.exist
52+
expect(err).to.not.exist()
5153
expect(val).to.be.eql(v)
5254
cb()
5355
}),
5456
(cb) => store.has(k, (err, exists) => {
55-
expect(err).to.not.exist
57+
expect(err).to.not.exist()
5658
expect(exists).to.be.eql(true)
5759
cb()
5860
})
@@ -68,7 +70,7 @@ describe('Tiered', () => {
6870
(cb) => ms[0].has(k, cb),
6971
(cb) => ms[1].has(k, cb)
7072
], (err, res) => {
71-
expect(err).to.not.exist
73+
expect(err).to.not.exist()
7274
expect(res).to.be.eql([true, true])
7375
cb()
7476
}),
@@ -77,7 +79,7 @@ describe('Tiered', () => {
7779
(cb) => ms[0].has(k, cb),
7880
(cb) => ms[1].has(k, cb)
7981
], (err, res) => {
80-
expect(err).to.not.exist
82+
expect(err).to.not.exist()
8183
expect(res).to.be.eql([false, false])
8284
cb()
8385
})

0 commit comments

Comments
 (0)