hypercore-storage 1.14.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +14 -4
  2. package/index.js +98 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -85,7 +85,7 @@ Return a bitfield page.
85
85
 
86
86
  #### `await rx.getUserData(key)`
87
87
 
88
- Return a user stored buffer.
88
+ Return a user stored buffer. `key` is a string.
89
89
 
90
90
  #### `rx.tryFlush()`
91
91
 
@@ -155,11 +155,11 @@ Delete bitfield pages between two indexes.
155
155
 
156
156
  #### `tx.putUserData(key, value)`
157
157
 
158
- Put a user provided buffer at a user provided key.
158
+ Put a user provided buffer at a user provided `key`. `key` is a string.
159
159
 
160
160
  #### `tx.deleteUserData(key)`
161
161
 
162
- Delete a user provided key.
162
+ Delete a user provided `key`. `key` is a string.
163
163
 
164
164
  #### `await tx.flush()`
165
165
 
@@ -179,7 +179,17 @@ Create a stream of all bitfield pages.
179
179
 
180
180
  #### `stream = core.createUserDataStream(opts)`
181
181
 
182
- Create a stream of all user data.
182
+ Create a stream of all user data. `opts` is a query object with the following possible properties:
183
+
184
+ ```
185
+ {
186
+ gt: 'only return keys > than this', // Not currently supported
187
+ gte: 'only return keys >= than this',
188
+ lt: 'only return keys < than this',
189
+ lte: 'only return keys <= than this', // Not currently supported
190
+ reverse: false // reverse results. Not currently supported
191
+ }
192
+ ```
183
193
 
184
194
  #### `await core.close()`
185
195
 
package/index.js CHANGED
@@ -347,6 +347,47 @@ class HypercoreStorage {
347
347
 
348
348
  return this.db.close()
349
349
  }
350
+
351
+ static async export (ptr, db, { batches = false } = {}) {
352
+ const rx = new CoreRX(ptr, db, EMPTY)
353
+
354
+ const core = {
355
+ head: null,
356
+ auth: null,
357
+ sessions: [],
358
+ data: null
359
+ }
360
+
361
+ const sessionsPromise = rx.getSessions()
362
+ const headPromise = rx.getHead()
363
+ const authPromise = rx.getAuth()
364
+
365
+ rx.tryFlush()
366
+
367
+ const [sessions, head, auth] = await Promise.all([
368
+ sessionsPromise,
369
+ headPromise,
370
+ authPromise
371
+ ])
372
+
373
+ core.head = head
374
+ core.auth = { ...auth, keyPair: null }
375
+ if (sessions) core.sessions = sessions.map(s => s.name)
376
+
377
+ const data = []
378
+
379
+ data.push(exportData(ptr, db))
380
+
381
+ if (batches) {
382
+ for (const { dataPointer } of sessions) {
383
+ data.push(exportData({ dataPointer, dependencies: [] }, db))
384
+ }
385
+ }
386
+
387
+ core.data = await Promise.all(data)
388
+
389
+ return core
390
+ }
350
391
  }
351
392
 
352
393
  class CorestoreStorage {
@@ -385,6 +426,10 @@ class CorestoreStorage {
385
426
  return this.db.ready()
386
427
  }
387
428
 
429
+ compact () {
430
+ return this.db.compactRange()
431
+ }
432
+
388
433
  async audit () {
389
434
  for await (const { core } of this.createCoreStream()) {
390
435
  const coreRx = new CoreRX(core, this.db, EMPTY)
@@ -773,6 +818,36 @@ class CorestoreStorage {
773
818
  return this._resumeFromPointers(EMPTY, discoveryKey, false, core)
774
819
  }
775
820
 
821
+ async export (discoveryKey, opts) {
822
+ const rx = new CorestoreRX(this.db, EMPTY)
823
+ const corePromise = rx.getCore(discoveryKey)
824
+
825
+ rx.tryFlush()
826
+ const core = await corePromise
827
+ if (core === null) return null
828
+
829
+ let { dataPointer, corePointer } = core
830
+
831
+ const ptr = { corePointer, dataPointer, dependencies: [] }
832
+
833
+ while (true) {
834
+ const rx = new CoreRX({ dataPointer, corePointer: 0, dependencies: [] }, this.db, EMPTY)
835
+ const dependencyPromise = rx.getDependency()
836
+ rx.tryFlush()
837
+ const dependency = await dependencyPromise
838
+ if (!dependency) break
839
+ ptr.dependencies.push(dependency)
840
+ dataPointer = dependency.dataPointer
841
+ }
842
+
843
+ const session = this.db.session()
844
+ try {
845
+ return await HypercoreStorage.export(ptr, session, opts)
846
+ } finally {
847
+ await session.close()
848
+ }
849
+ }
850
+
776
851
  async _resumeFromPointers (view, discoveryKey, create, { version, corePointer, dataPointer }) {
777
852
  const core = { corePointer, dataPointer, dependencies: [] }
778
853
 
@@ -927,3 +1002,26 @@ function tmpFixStorage (p) {
927
1002
  fs.renameSync(path.join(p, f), path.join(p, 'db', f))
928
1003
  }
929
1004
  }
1005
+
1006
+ async function exportData (ptr, db, opts) {
1007
+ // just need dataPointer
1008
+ const reads = [
1009
+ toArray(createBlockStream(ptr, db, EMPTY, opts)),
1010
+ toArray(createTreeNodeStream(ptr, db, EMPTY, opts)),
1011
+ toArray(createBitfieldStream(ptr, db, EMPTY, opts))
1012
+ ]
1013
+
1014
+ const [blocks, tree, bitfield] = await Promise.all(reads)
1015
+
1016
+ return {
1017
+ blocks,
1018
+ tree,
1019
+ bitfield
1020
+ }
1021
+ }
1022
+
1023
+ async function toArray (stream) {
1024
+ const all = []
1025
+ for await (const e of stream) all.push(e)
1026
+ return all
1027
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",