Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 00be067

Browse files
feat: Add integration with bitswap
1 parent 0c6c8b0 commit 00be067

File tree

3 files changed

+281
-213
lines changed

3 files changed

+281
-213
lines changed

src/block-service.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/index.js

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,103 @@
11
'use strict'
22

3-
exports = module.exports = require('./block-service.js')
3+
const async = require('async')
4+
5+
// BlockService is a hybrid block datastore. It stores data in a local
6+
// datastore and may retrieve data from a remote Exchange.
7+
// It uses an internal `datastore.Datastore` instance to store values.
8+
module.exports = class BlockService {
9+
constructor (ipfsRepo) {
10+
this._repo = ipfsRepo
11+
this._bitswap = null
12+
}
13+
14+
goOnline (bitswap) {
15+
this._bitswap = bitswap
16+
}
17+
18+
isOnline () {
19+
return this._bitswap != null
20+
}
21+
22+
addBlock (block, extension, callback) {
23+
if (this.isOnline()) {
24+
if (typeof extension === 'function') {
25+
callback = extension
26+
extension = undefined
27+
}
28+
29+
this._bitswap.hasBlock(block, callback)
30+
} else {
31+
this._repo.datastore.put(block, extension, callback)
32+
}
33+
}
34+
35+
addBlocks (blocks, callback) {
36+
if (!Array.isArray(blocks)) {
37+
return callback(new Error('expects an array of Blocks'))
38+
}
39+
40+
async.eachLimit(blocks, 100, (block, next) => {
41+
this.addBlock(block, next)
42+
}, callback)
43+
}
44+
45+
getBlock (key, extension, callback) {
46+
if (this.isOnline()) {
47+
if (typeof extension === 'function') {
48+
callback = extension
49+
extension = undefined
50+
}
51+
52+
this._bitswap.getBlock(key, callback)
53+
} else {
54+
this._repo.datastore.get(key, extension, callback)
55+
}
56+
}
57+
58+
getBlocks (multihashes, extension, callback) {
59+
if (typeof extension === 'function') {
60+
callback = extension
61+
extension = undefined
62+
}
63+
64+
if (!Array.isArray(multihashes)) {
65+
return callback(new Error('Invalid batch of multihashes'))
66+
}
67+
68+
var results = {}
69+
70+
async.eachLimit(multihashes, 100, (multihash, next) => {
71+
this.getBlock(multihash, extension, (err, block) => {
72+
results[multihash] = {
73+
err: err,
74+
block: block
75+
}
76+
next()
77+
})
78+
}, (err) => {
79+
callback(err, results)
80+
})
81+
}
82+
83+
deleteBlock (key, extension, callback) {
84+
this._repo.datastore.delete(key, extension, callback)
85+
}
86+
87+
deleteBlocks (multihashes, extension, callback) {
88+
if (typeof extension === 'function') {
89+
callback = extension
90+
extension = undefined
91+
}
92+
93+
if (!Array.isArray(multihashes)) {
94+
return callback(new Error('Invalid batch of multihashes'))
95+
}
96+
97+
async.eachLimit(multihashes, 100, (multihash, next) => {
98+
this.deleteBlock(multihash, extension, next)
99+
}, (err) => {
100+
callback(err)
101+
})
102+
}
103+
}

0 commit comments

Comments
 (0)