This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathblock-storage.js
174 lines (152 loc) · 4.08 KB
/
block-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
const { BlockstoreAdapter } = require('interface-blockstore')
const merge = require('it-merge')
const pushable = require('it-pushable')
const filter = require('it-filter')
/**
* @typedef {import('interface-blockstore').Blockstore} Blockstore
* @typedef {import('interface-blockstore').Query} Query
* @typedef {import('interface-blockstore').KeyQuery} KeyQuery
* @typedef {import('multiformats/cid').CID} CID
* @typedef {import('ipfs-bitswap').IPFSBitswap} Bitswap
* @typedef {import('ipfs-core-types/src/utils').AbortOptions} AbortOptions
* @typedef {import('ipfs-core-types/src/block').RmOptions} RmOptions
*/
/**
* BlockStorage is a hybrid block datastore. It stores data in a local
* datastore and may retrieve data from a remote Exchange.
* It uses an internal `datastore.Datastore` instance to store values.
*
* @implements {Blockstore}
*/
class BlockStorage extends BlockstoreAdapter {
/**
* Create a new BlockStorage
*
* @param {Blockstore} blockstore
* @param {Bitswap} bitswap
*/
constructor (blockstore, bitswap) {
super()
this.child = blockstore
this.bitswap = bitswap
}
open () {
return this.child.open()
}
close () {
return this.child.close()
}
unwrap () {
return this.child
}
/**
* Put a block to the underlying datastore
*
* @param {CID} cid
* @param {Uint8Array} block
* @param {AbortOptions} [options]
*/
async put (cid, block, options = {}) {
if (await this.has(cid)) {
return
}
if (this.bitswap.isStarted()) {
await this.bitswap.put(cid, block, options)
} else {
await this.child.put(cid, block, options)
}
}
/**
* Put a multiple blocks to the underlying datastore
*
* @param {AsyncIterable<{ key: CID, value: Uint8Array }> | Iterable<{ key: CID, value: Uint8Array }>} blocks
* @param {AbortOptions} [options]
*/
async * putMany (blocks, options = {}) {
const missingBlocks = filter(blocks, async ({ key }) => { return !(await this.has(key)) })
if (this.bitswap.isStarted()) {
yield * this.bitswap.putMany(missingBlocks, options)
} else {
yield * this.child.putMany(missingBlocks, options)
}
}
/**
* Get a block by cid
*
* @param {CID} cid
* @param {AbortOptions} [options]
*/
async get (cid, options = {}) {
if (!(await this.has(cid)) && this.bitswap.isStarted()) {
return this.bitswap.get(cid, options)
} else {
return this.child.get(cid, options)
}
}
/**
* Get multiple blocks back from an array of cids
*
* @param {AsyncIterable<CID> | Iterable<CID>} cids
* @param {AbortOptions} [options]
*/
async * getMany (cids, options = {}) {
const getFromBitswap = pushable()
const getFromChild = pushable()
Promise.resolve().then(async () => {
for await (const cid of cids) {
if (!(await this.has(cid)) && this.bitswap.isStarted()) {
getFromBitswap.push(cid)
} else {
getFromChild.push(cid)
}
}
getFromBitswap.end()
getFromChild.end()
})
yield * merge(
this.bitswap.getMany(getFromBitswap, options),
this.child.getMany(getFromChild, options)
)
}
/**
* Delete a block from the blockstore
*
* @param {CID} cid
* @param {RmOptions} [options]
*/
async delete (cid, options) {
await this.child.delete(cid, options)
}
/**
* Delete multiple blocks from the blockstore
*
* @param {AsyncIterable<CID> | Iterable<CID>} cids
* @param {RmOptions} [options]
*/
async * deleteMany (cids, options) {
yield * this.child.deleteMany(cids, options)
}
/**
* @param {CID} cid
* @param {AbortOptions} options
*/
async has (cid, options = {}) {
return this.child.has(cid, options)
}
/**
* @param {Query} q
* @param {AbortOptions} options
*/
async * query (q, options = {}) {
yield * this.child.query(q, options)
}
/**
* @param {KeyQuery} q
* @param {AbortOptions} options
*/
async * queryKeys (q, options = {}) {
yield * this.child.queryKeys(q, options)
}
}
module.exports = BlockStorage