This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatastore.js
237 lines (216 loc) · 6.98 KB
/
datastore.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import interfaceDatastore from 'interface-datastore'
const { filter, map } = interfaceDatastore.utils
/**
* CarDatastore is a class to manage reading from, and writing to a CAR archives
* using [CID](https://github.com/multiformats/js-multiformats)s as keys and
* file names in the CAR and binary block data as the file contents.
*
* @class
*/
class CarDatastore {
constructor (multiformats, reader, writer) {
this.multiformats = multiformats
this._reader = reader
this._writer = writer
}
/**
* @name CarDatastore#get
* @description
* Retrieve a block from this archive. `key`s are converted to `CID`
* automatically, whether you provide a native Datastore `Key` object, a
* `String` or a `CID`. `key`s that cannot be converted will throw an error.
*
* This operation may not be supported in some create-modes; a write-only mode
* may throw an error if unsupported.
* @function
* @async
* @memberof CarDatastore
* @param {string|Key|CID} key a `CID` or `CID`-convertable object to identify
* the block.
* @return {Uint8Array} the IPLD block data referenced by the CID.
*/
async get (key) {
key = toKey(this.multiformats, key, 'get')
return this._reader.get(key)
}
/**
* @name CarDatastore#has
* @description
* Check whether a block exists in this archive. `key`s are converted to `CID`
* automatically, whether you provide a native Datastore `Key` object, a
* `String` or a `CID`. `key`s that cannot be converted will throw an error.
*
* This operation may not be supported in some create-modes; a write-only mode
* may throw an error if unsupported.
* @function
* @async
* @memberof CarDatastore
* @param {string|Key|CID} key a `CID` or `CID`-convertable object to identify
* the block.
* @return {boolean} indicating whether the key exists in this Datastore.
*/
async has (key) {
key = toKey(this.multiformats, key, 'has')
return this._reader.has(key)
}
/**
* @name CarDatastore#put
* @description
* Store a block in this archive. `key`s are converted to `CID` automatically,
* whether you provide a native Datastore `Key` object, a `String` or a `CID`.
* `key`s that cannot be converted will throw an error.
*
* Only supported by the `CarDatastore.writeStream()` create-mode.
* CarDatastores constructed by other create-modes will not support `put()`
* and an Error will be thrown when it is called.
* @function
* @async
* @memberof CarDatastore
* @param {string|Key|CID} key a `CID` or `CID`-convertable object to identify
* the `value`.
* @param {Uint8Array} value an IPLD block matching the given `key`
* `CID`.
*/
async put (key, value) {
key = toKey(this.multiformats, key, 'put')
if (!(value instanceof Uint8Array)) {
throw new TypeError('put() can only receive Uint8Arrays or Buffers')
}
return this._writer.put(key, value)
}
/**
* @name CarDatastore#delete
* @description
* **Currently not supported by any create-mode**. CarDatastore is currently
* an append-only and read-only construct.
* @function
* @async
* @memberof CarDatastore
* @param {string|Key|CID} key a `CID` or `CID`-convertable object to identify
* the block.
*/
async delete (key) {
key = toKey(this.multiformats, key, 'delete')
return this._writer.delete(key)
}
/**
* @name CarDatastore#setRoots
* @description
* Set the list of roots in the CarDatastore archive on this CAR archive.
*
* The roots will be written to the comment section of the CAR archive when
* `close()` is called, in the meantime it is stored in memory.
*
* Only supported by the `CarDatastore.writeStream()` create-mode.
* CarDatastores constructed by other create-modes will not support `put()`
* and an Error will be thrown when it is called.
* @function
* @async
* @param {string} comment an arbitrary comment to store in the CAR archive.
*/
async setRoots (roots) {
return this._writer.setRoots(roots)
}
/**
* @name CarDatastore#getRoots
* @description
* Get the list of roots set on this CAR archive if they exist exists. See
* {@link CarDatastore#setRoots}.
* @function
* @async
* @return {Array<CID>} an array of CIDs
*/
async getRoots () {
return this._reader.getRoots()
}
/**
* @name CarDatastore#close
* @description
* Close this archive, free resources and write its new contents if required
* and supported by the create-mode used.
*
* This may or may not have any effect on the use of the underlying resource
* depending on the create-mode of the CarDatastore.
* @function
* @async
*/
async close () {
if (this._closed) {
throw new Error('close() already called')
}
this._closed = true
return Promise.all([this._reader.close(), this._writer.close()])
}
async batch () {
/* c8 ignore next */
throw new Error('Unimplemented operation')
}
/**
* @name CarDatastore#query
* @description
* Create an async iterator for the entries of this CarDatastore. Ideally for
* use with `for await ... of` to lazily iterate over the entries.
*
* By default, each element returned by the iterator will be an object with a
* `key` property with the string CID of the entry and a `value` property with
* the binary data.
*
* Supply `{ keysOnly: true }` as an argument and the elements will only
* contain the keys, without needing to load the values from storage.
*
* The `filters` parameter is also supported as per the Datastore interface.
*
* This operation may not be supported in some create-modes; a write-only mode
* may throw an error if unsupported.
* @function
* @async
* @generator
* @param {Object} [q] query parameters
* @return {AsyncIterator<key,value>}
* @yields {Object<key,value>}
*/
query (q) {
if (q === undefined) {
q = {}
}
if (typeof q !== 'object') {
throw new TypeError('query argument must be an object, supply `{}` to match all')
}
let it
if (typeof this._reader.iterator === 'function') {
it = this._reader.iterator(q.keysOnly)
} else {
const keys = this._reader.keys()
if (!q.keysOnly) {
const mapper = async (key) => ({ key, value: await this.get(key) })
it = map(keys, mapper)
} else {
it = map(keys, (key) => ({ key }))
}
}
if (Array.isArray(q.filters)) {
it = q.filters.reduce((it, key) => filter(it, key), it)
}
/* not supported
if (Array.isArray(q.orders)) {
it = q.orders.reduce((it, key) => sortAll(it, key), it)
}
if (q.offset != null) {
let i = 0
it = filter(it, () => i++ >= q.offset)
}
if (q.limit != null) {
it = take(it, q.limit)
}
*/
return it
}
}
function toKey (multiformats, key, method) {
try {
return multiformats.CID.from(key.toString())
} catch (e) {
throw new TypeError(`${method}() only accepts CIDs or CID strings`)
}
}
export default CarDatastore