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

Commit

Permalink
fix: working interop with go
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Mar 13, 2017
1 parent 5bb079d commit f5e03c6
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"aegir": "^10.0.0",
"async": "^2.1.5",
"chai": "^3.5.0",
"cids": "^0.4.1",
"flow-bin": "^0.41.0",
"memdown": "^1.2.4",
"rimraf": "^2.6.1"
Expand Down
17 changes: 9 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ class LevelDatastore {
this.db = levelup(path, Object.assign({}, {
compression: false // same default as go
}, opts, {
keyEncoding: 'binary',
valueEncoding: 'binary'
}))
}

put (key /* : Key */, value /* : Buffer */, callback /* : Callback<void> */) /* : void */ {
this.db.put(key.toString(), value, callback)
this.db.put(key.toBuffer(), value, callback)
}

get (key /* : Key */, callback /* : Callback<Buffer> */) /* : void */ {
this.db.get(key.toString(), callback)
this.db.get(key.toBuffer(), callback)
}

has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
this.db.get(key.toString(), (err, res) => {
this.db.get(key.toBuffer(), (err, res) => {
if (err) {
if (err.notFound) {
callback(null, false)
Expand All @@ -55,7 +56,7 @@ class LevelDatastore {
}

delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
this.db.del(key.toString(), callback)
this.db.del(key.toBuffer(), callback)
}

close (callback /* : Callback<void> */) /* : void */ {
Expand All @@ -68,14 +69,14 @@ class LevelDatastore {
put: (key /* : Key */, value /* : Buffer */) /* : void */ => {
ops.push({
type: 'put',
key: key.toString(),
key: key.toBuffer(),
value: value
})
},
delete: (key /* : Key */) /* : void */ => {
ops.push({
type: 'del',
key: key.toString()
key: key.toBuffer()
})
},
commit: (callback /* : Callback<void> */) /* : void */ => {
Expand All @@ -93,7 +94,7 @@ class LevelDatastore {
const iter = this.db.db.iterator({
keys: true,
values: values,
keyAsBuffer: false
keyAsBuffer: true
})

const rawStream = (end, cb) => {
Expand All @@ -115,7 +116,7 @@ class LevelDatastore {
}

const res /* : QueryEntry<Buffer> */ = {
key: new Key(key)
key: new Key(key, false)
}

if (values) {
Expand Down
18 changes: 17 additions & 1 deletion test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
/* eslint-env mocha */
'use strict'

const pull = require('pull-stream')
const path = require('path')
const utils = require('interface-datastore').utils
const rimraf = require('rimraf')
const each = require('async/each')
const MountStore = require('interface-datastore').MountDatastore
const Key = require('interface-datastore').Key
const expect = require('chai').expect
const CID = require('cids')

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

Expand Down Expand Up @@ -48,6 +52,18 @@ describe('LevelDatastore', () => {
})
})

it('interop with go', () => {
it('interop with go', (done) => {
const store = new LevelStore(path.join(__dirname, 'test-repo', 'datastore'))

pull(
store.query({}),
pull.map((e) => new CID(e.key.toBuffer())),
pull.collect((err, cids) => {
expect(err).to.not.exist
expect(cids[0].version).to.be.eql(0)
expect(cids).to.have.length(4)
done()
})
)
})
})
2 changes: 1 addition & 1 deletion test/test-repo/datastore/CURRENT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MANIFEST-000009
MANIFEST-000036
8 changes: 3 additions & 5 deletions test/test-repo/datastore/LOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
=============== Aug 16, 2016 (CEST) ===============
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
12:27:26.258463 db@open opening
12:27:26.260891 db@janitor F·4 G·0
12:27:26.260933 db@open done T·2.438178ms
2017/03/13-13:52:56.574311 70000f009000 Recovering log #35
2017/03/13-13:52:56.575960 70000f009000 Delete type=0 #35
2017/03/13-13:52:56.576081 70000f009000 Delete type=3 #34
13 changes: 3 additions & 10 deletions test/test-repo/datastore/LOG.old
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
=============== Dec 10, 2015 (PST) ===============
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
07:50:02.057231 db@open opening
07:50:02.057312 journal@recovery F·1
07:50:02.057514 journal@recovery recovering @3
07:50:02.058921 mem@flush created L0@5 N·4 S·1KiB "/ip..\xf6\xe4\xa9,v5":"/pk..\xf6\xe4\xa9,v6"
07:50:02.059983 db@janitor F·4 G·0
07:50:02.060001 db@open done T·2.755926ms
07:50:02.073183 db@close closing
07:50:02.073285 db@close done T·97.522µs
2017/03/13-13:52:16.502220 70000f6c7000 Recovering log #33
2017/03/13-13:52:16.503900 70000f6c7000 Delete type=0 #33
2017/03/13-13:52:16.504085 70000f6c7000 Delete type=3 #32
Binary file removed test/test-repo/datastore/MANIFEST-000009
Binary file not shown.
Binary file added test/test-repo/datastore/MANIFEST-000036
Binary file not shown.
44 changes: 44 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,14 @@ balanced-match@^0.4.1:
version "0.4.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838"

base-x@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.0.tgz#77b56f0311070b780b3c8a5f534beac47e506702"

base-x@^2.0.1:
version "2.0.5"
resolved "https://registry.yarnpkg.com/base-x/-/base-x-2.0.5.tgz#11967f90b42995cfdb5f50a1be499d7675db4fce"

base64-arraybuffer@0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz#73926771923b5a19747ad666aa5cd4bf9c6e9ce8"
Expand Down Expand Up @@ -1344,6 +1352,12 @@ browserify-zlib@^0.1.4:
dependencies:
pako "~0.2.0"

bs58@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.0.tgz#65f5deaf6d74e6135a99f763ca6209ab424b9172"
dependencies:
base-x "^2.0.1"

buffer-crc32@^0.2.1:
version "0.2.13"
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
Expand Down Expand Up @@ -1512,6 +1526,14 @@ chownr@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.0.1.tgz#e2a75042a9551908bebd25b8523d5f9769d79181"

cids@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/cids/-/cids-0.4.1.tgz#fefac1c0af4579e99ffb1caf6619abc1145ff98a"
dependencies:
multibase "~0.3.1"
multicodec "~0.1.5"
multihashes "~0.3.3"

cipher-base@^1.0.0, cipher-base@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/cipher-base/-/cipher-base-1.0.3.tgz#eeabf194419ce900da3018c207d212f2a6df0a07"
Expand Down Expand Up @@ -4989,6 +5011,24 @@ ms@0.7.2:
version "0.7.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"

multibase@~0.3.1:
version "0.3.2"
resolved "https://registry.yarnpkg.com/multibase/-/multibase-0.3.2.tgz#f3cf79d86aa8364432d6691dbd8c0cdf12ede66a"
dependencies:
base-x "3.0.0"

multicodec@~0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/multicodec/-/multicodec-0.1.5.tgz#799c33bdaffec9749fa52253c106edbab1976812"
dependencies:
varint "^5.0.0"

multihashes@~0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/multihashes/-/multihashes-0.3.3.tgz#18d74d8c2393af39aaf0ac8f4fd6d888d43f2f29"
dependencies:
bs58 "^4.0.0"

multimatch@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-2.1.0.tgz#9c7906a22fb4c02919e2f5f75161b4cdbd4b2a2b"
Expand Down Expand Up @@ -7058,6 +7098,10 @@ vargs@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/vargs/-/vargs-0.1.0.tgz#6b6184da6520cc3204ce1b407cac26d92609ebff"

varint@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/varint/-/varint-5.0.0.tgz#d826b89f7490732fabc0c0ed693ed475dcb29ebf"

verror@1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/verror/-/verror-1.3.6.tgz#cff5df12946d297d2baaefaa2689e25be01c005c"
Expand Down

0 comments on commit f5e03c6

Please sign in to comment.