Skip to content

Commit 467c430

Browse files
authored
feat: store pins in datastore (#221)
1 parent 8660e4f commit 467c430

9 files changed

+105
-8
lines changed

.aegir.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
module.exports = {
4+
webpack: {
5+
node: {
6+
// this is needed until level stops using node buffers in browser code
7+
Buffer: true
8+
}
9+
}
10+
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"npm": ">=3.0.0"
4444
},
4545
"devDependencies": {
46-
"aegir": "^23.0.0",
46+
"aegir": "^25.0.0",
4747
"chai": "^4.2.0",
4848
"chai-as-promised": "^7.1.1",
4949
"dirty-chai": "^2.0.1",
@@ -68,7 +68,7 @@
6868
"debug": "^4.1.0",
6969
"err-code": "^2.0.0",
7070
"interface-datastore": "^1.0.2",
71-
"ipfs-repo-migrations": "^1.0.0",
71+
"ipfs-repo-migrations": "^2.0.0",
7272
"ipfs-utils": "^2.2.0",
7373
"ipld-block": "^0.9.1",
7474
"it-map": "^1.0.2",

src/default-options-browser.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = {
77
root: require('datastore-level'),
88
blocks: require('datastore-level'),
99
keys: require('datastore-level'),
10-
datastore: require('datastore-level')
10+
datastore: require('datastore-level'),
11+
pins: require('datastore-level')
1112
},
1213
storageBackendOptions: {
1314
root: {
@@ -29,6 +30,11 @@ module.exports = {
2930
sharding: false,
3031
prefix: '',
3132
version: 2
33+
},
34+
pins: {
35+
sharding: false,
36+
prefix: '',
37+
version: 2
3238
}
3339
}
3440
}

src/default-options.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = {
77
root: require('datastore-fs'),
88
blocks: require('datastore-fs'),
99
keys: require('datastore-fs'),
10-
datastore: require('datastore-level')
10+
datastore: require('datastore-level'),
11+
pins: require('datastore-level')
1112
},
1213
storageBackendOptions: {
1314
root: {

src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const lockers = {
3131

3232
/**
3333
* IpfsRepo implements all required functionality to read and write to an ipfs repo.
34-
*
3534
*/
3635
class IpfsRepo {
3736
/**
@@ -122,6 +121,10 @@ class IpfsRepo {
122121
log('creating keystore')
123122
this.keys = backends.create('keys', pathJoin(this.path, 'keys'), this.options)
124123
await this.keys.open()
124+
log('creating pins')
125+
this.pins = backends.create('pins', pathJoin(this.path, 'pins'), this.options)
126+
await this.pins.open()
127+
125128
const isCompatible = await this.version.check(constants.repoVersion)
126129
if (!isCompatible) {
127130
if (await this._isAutoMigrationEnabled()) {
@@ -262,7 +265,8 @@ class IpfsRepo {
262265
this.root,
263266
this.blocks,
264267
this.keys,
265-
this.datastore
268+
this.datastore,
269+
this.pins
266270
].map((store) => store.close()))
267271

268272
log('unlocking')

test/browser.js

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ describe('IPFS Repo Tests on the Browser', () => {
3838
require('./config-test')(repo)
3939
require('./api-addr-test')(repo)
4040
require('./lock-test')(repo)
41+
require('./pins-test')(repo)
4142
require('./is-initialized')
4243
})

test/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ describe('IPFS Repo Tests onNode.js', () => {
114114
if (!r.init) {
115115
require('./interop-test')(repo)
116116
}
117+
require('./pins-test')(repo)
117118
require('./is-initialized')
118119
}))
119120

test/pins-test.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const { Buffer } = require('buffer')
6+
const { expect } = require('./utils/chai')
7+
const range = require('just-range')
8+
const Key = require('interface-datastore').Key
9+
10+
module.exports = (repo) => {
11+
describe('pins', () => {
12+
const dataList = range(100).map((i) => Buffer.from(`hello-${i}-${Math.random()}`))
13+
const data = Buffer.from('hello world')
14+
const b = new Key('hello')
15+
16+
it('exists', () => {
17+
expect(repo).to.have.property('pins')
18+
})
19+
20+
describe('.put', () => {
21+
it('simple', async () => {
22+
await repo.pins.put(b, data)
23+
})
24+
25+
it('multi write (locks)', async () => {
26+
await Promise.all([repo.pins.put(b, data), repo.pins.put(b, data)])
27+
})
28+
29+
it('massive multiwrite', async function () {
30+
this.timeout(15000) // add time for ci
31+
await Promise.all(range(100).map((i) => {
32+
return repo.pins.put(new Key('hello' + i), dataList[i])
33+
}))
34+
})
35+
})
36+
37+
describe('.get', () => {
38+
it('simple', async () => {
39+
const val = await repo.pins.get(b)
40+
expect(val).to.be.eql(data)
41+
})
42+
43+
it('massive read', async function () {
44+
this.timeout(15000) // add time for ci
45+
await Promise.all(range(20 * 100).map(async (i) => {
46+
const j = i % dataList.length
47+
const val = await repo.pins.get(new Key('hello' + j))
48+
expect(val).to.be.eql(dataList[j])
49+
}))
50+
}).timeout(10 * 1000)
51+
})
52+
53+
describe('.has', () => {
54+
it('existing pin', async () => {
55+
const exists = await repo.pins.has(b)
56+
expect(exists).to.eql(true)
57+
})
58+
59+
it('non existent pin', async () => {
60+
const exists = await repo.pins.has(new Key('world'))
61+
expect(exists).to.eql(false)
62+
})
63+
})
64+
65+
describe('.delete', () => {
66+
it('simple', async () => {
67+
await repo.pins.delete(b)
68+
const exists = await repo.pins.has(b)
69+
expect(exists).to.equal(false)
70+
})
71+
})
72+
})
73+
}

test/repo-test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ module.exports = (repo) => {
162162
root: FakeDatastore,
163163
blocks: FakeDatastore,
164164
keys: FakeDatastore,
165-
datastore: FakeDatastore
165+
datastore: FakeDatastore,
166+
pins: FakeDatastore
166167
}
167168
})
168169
await repo.init({})
169170
await repo.open()
170171
await repo.close()
171-
expect(count).to.be.eq(4)
172+
expect(count).to.be.eq(5)
172173
})
173174

174175
it('open twice throws error', async () => {

0 commit comments

Comments
 (0)