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

Commit f5e03c6

Browse files
fix: working interop with go
1 parent 5bb079d commit f5e03c6

File tree

9 files changed

+78
-25
lines changed

9 files changed

+78
-25
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"aegir": "^10.0.0",
4646
"async": "^2.1.5",
4747
"chai": "^3.5.0",
48+
"cids": "^0.4.1",
4849
"flow-bin": "^0.41.0",
4950
"memdown": "^1.2.4",
5051
"rimraf": "^2.6.1"

src/index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@ class LevelDatastore {
2727
this.db = levelup(path, Object.assign({}, {
2828
compression: false // same default as go
2929
}, opts, {
30+
keyEncoding: 'binary',
3031
valueEncoding: 'binary'
3132
}))
3233
}
3334

3435
put (key /* : Key */, value /* : Buffer */, callback /* : Callback<void> */) /* : void */ {
35-
this.db.put(key.toString(), value, callback)
36+
this.db.put(key.toBuffer(), value, callback)
3637
}
3738

3839
get (key /* : Key */, callback /* : Callback<Buffer> */) /* : void */ {
39-
this.db.get(key.toString(), callback)
40+
this.db.get(key.toBuffer(), callback)
4041
}
4142

4243
has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
43-
this.db.get(key.toString(), (err, res) => {
44+
this.db.get(key.toBuffer(), (err, res) => {
4445
if (err) {
4546
if (err.notFound) {
4647
callback(null, false)
@@ -55,7 +56,7 @@ class LevelDatastore {
5556
}
5657

5758
delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
58-
this.db.del(key.toString(), callback)
59+
this.db.del(key.toBuffer(), callback)
5960
}
6061

6162
close (callback /* : Callback<void> */) /* : void */ {
@@ -68,14 +69,14 @@ class LevelDatastore {
6869
put: (key /* : Key */, value /* : Buffer */) /* : void */ => {
6970
ops.push({
7071
type: 'put',
71-
key: key.toString(),
72+
key: key.toBuffer(),
7273
value: value
7374
})
7475
},
7576
delete: (key /* : Key */) /* : void */ => {
7677
ops.push({
7778
type: 'del',
78-
key: key.toString()
79+
key: key.toBuffer()
7980
})
8081
},
8182
commit: (callback /* : Callback<void> */) /* : void */ => {
@@ -93,7 +94,7 @@ class LevelDatastore {
9394
const iter = this.db.db.iterator({
9495
keys: true,
9596
values: values,
96-
keyAsBuffer: false
97+
keyAsBuffer: true
9798
})
9899

99100
const rawStream = (end, cb) => {
@@ -115,7 +116,7 @@ class LevelDatastore {
115116
}
116117

117118
const res /* : QueryEntry<Buffer> */ = {
118-
key: new Key(key)
119+
key: new Key(key, false)
119120
}
120121

121122
if (values) {

test/node.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
/* eslint-env mocha */
33
'use strict'
44

5+
const pull = require('pull-stream')
6+
const path = require('path')
57
const utils = require('interface-datastore').utils
68
const rimraf = require('rimraf')
79
const each = require('async/each')
810
const MountStore = require('interface-datastore').MountDatastore
911
const Key = require('interface-datastore').Key
12+
const expect = require('chai').expect
13+
const CID = require('cids')
1014

1115
const LevelStore = require('../src')
1216

@@ -48,6 +52,18 @@ describe('LevelDatastore', () => {
4852
})
4953
})
5054

51-
it('interop with go', () => {
55+
it('interop with go', (done) => {
56+
const store = new LevelStore(path.join(__dirname, 'test-repo', 'datastore'))
57+
58+
pull(
59+
store.query({}),
60+
pull.map((e) => new CID(e.key.toBuffer())),
61+
pull.collect((err, cids) => {
62+
expect(err).to.not.exist
63+
expect(cids[0].version).to.be.eql(0)
64+
expect(cids).to.have.length(4)
65+
done()
66+
})
67+
)
5268
})
5369
})

test/test-repo/datastore/CURRENT

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
MANIFEST-000009
1+
MANIFEST-000036

test/test-repo/datastore/LOG

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
=============== Aug 16, 2016 (CEST) ===============
2-
12:27:26.257958 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
3-
12:27:26.258463 db@open opening
4-
12:27:26.260891 db@janitor F·4 G·0
5-
12:27:26.260933 db@open done T·2.438178ms
1+
2017/03/13-13:52:56.574311 70000f009000 Recovering log #35
2+
2017/03/13-13:52:56.575960 70000f009000 Delete type=0 #35
3+
2017/03/13-13:52:56.576081 70000f009000 Delete type=3 #34

test/test-repo/datastore/LOG.old

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
=============== Dec 10, 2015 (PST) ===============
2-
07:50:02.056578 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
3-
07:50:02.057231 db@open opening
4-
07:50:02.057312 journal@recovery F·1
5-
07:50:02.057514 journal@recovery recovering @3
6-
07:50:02.058921 mem@flush created L0@5 N·4 S·1KiB "/ip..\xf6\xe4\xa9,v5":"/pk..\xf6\xe4\xa9,v6"
7-
07:50:02.059983 db@janitor F·4 G·0
8-
07:50:02.060001 db@open done T·2.755926ms
9-
07:50:02.073183 db@close closing
10-
07:50:02.073285 db@close done T·97.522µs
1+
2017/03/13-13:52:16.502220 70000f6c7000 Recovering log #33
2+
2017/03/13-13:52:16.503900 70000f6c7000 Delete type=0 #33
3+
2017/03/13-13:52:16.504085 70000f6c7000 Delete type=3 #32
-192 Bytes
Binary file not shown.
201 Bytes
Binary file not shown.

yarn.lock

+44
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,14 @@ balanced-match@^0.4.1:
11391139
version "0.4.2"
11401140
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838"
11411141

1142+
base-x@3.0.0:
1143+
version "3.0.0"
1144+
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.0.tgz#77b56f0311070b780b3c8a5f534beac47e506702"
1145+
1146+
base-x@^2.0.1:
1147+
version "2.0.5"
1148+
resolved "https://registry.yarnpkg.com/base-x/-/base-x-2.0.5.tgz#11967f90b42995cfdb5f50a1be499d7675db4fce"
1149+
11421150
base64-arraybuffer@0.1.5:
11431151
version "0.1.5"
11441152
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz#73926771923b5a19747ad666aa5cd4bf9c6e9ce8"
@@ -1344,6 +1352,12 @@ browserify-zlib@^0.1.4:
13441352
dependencies:
13451353
pako "~0.2.0"
13461354

1355+
bs58@^4.0.0:
1356+
version "4.0.0"
1357+
resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.0.tgz#65f5deaf6d74e6135a99f763ca6209ab424b9172"
1358+
dependencies:
1359+
base-x "^2.0.1"
1360+
13471361
buffer-crc32@^0.2.1:
13481362
version "0.2.13"
13491363
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
@@ -1512,6 +1526,14 @@ chownr@^1.0.1:
15121526
version "1.0.1"
15131527
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.0.1.tgz#e2a75042a9551908bebd25b8523d5f9769d79181"
15141528

1529+
cids@^0.4.1:
1530+
version "0.4.1"
1531+
resolved "https://registry.yarnpkg.com/cids/-/cids-0.4.1.tgz#fefac1c0af4579e99ffb1caf6619abc1145ff98a"
1532+
dependencies:
1533+
multibase "~0.3.1"
1534+
multicodec "~0.1.5"
1535+
multihashes "~0.3.3"
1536+
15151537
cipher-base@^1.0.0, cipher-base@^1.0.1:
15161538
version "1.0.3"
15171539
resolved "https://registry.yarnpkg.com/cipher-base/-/cipher-base-1.0.3.tgz#eeabf194419ce900da3018c207d212f2a6df0a07"
@@ -4989,6 +5011,24 @@ ms@0.7.2:
49895011
version "0.7.2"
49905012
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"
49915013

5014+
multibase@~0.3.1:
5015+
version "0.3.2"
5016+
resolved "https://registry.yarnpkg.com/multibase/-/multibase-0.3.2.tgz#f3cf79d86aa8364432d6691dbd8c0cdf12ede66a"
5017+
dependencies:
5018+
base-x "3.0.0"
5019+
5020+
multicodec@~0.1.5:
5021+
version "0.1.5"
5022+
resolved "https://registry.yarnpkg.com/multicodec/-/multicodec-0.1.5.tgz#799c33bdaffec9749fa52253c106edbab1976812"
5023+
dependencies:
5024+
varint "^5.0.0"
5025+
5026+
multihashes@~0.3.3:
5027+
version "0.3.3"
5028+
resolved "https://registry.yarnpkg.com/multihashes/-/multihashes-0.3.3.tgz#18d74d8c2393af39aaf0ac8f4fd6d888d43f2f29"
5029+
dependencies:
5030+
bs58 "^4.0.0"
5031+
49925032
multimatch@^2.0.0:
49935033
version "2.1.0"
49945034
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-2.1.0.tgz#9c7906a22fb4c02919e2f5f75161b4cdbd4b2a2b"
@@ -7058,6 +7098,10 @@ vargs@0.1.0:
70587098
version "0.1.0"
70597099
resolved "https://registry.yarnpkg.com/vargs/-/vargs-0.1.0.tgz#6b6184da6520cc3204ce1b407cac26d92609ebff"
70607100

7101+
varint@^5.0.0:
7102+
version "5.0.0"
7103+
resolved "https://registry.yarnpkg.com/varint/-/varint-5.0.0.tgz#d826b89f7490732fabc0c0ed693ed475dcb29ebf"
7104+
70617105
verror@1.3.6:
70627106
version "1.3.6"
70637107
resolved "https://registry.yarnpkg.com/verror/-/verror-1.3.6.tgz#cff5df12946d297d2baaefaa2689e25be01c005c"

0 commit comments

Comments
 (0)