@fireproof/core 0.0.4 → 0.0.6

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.
package/src/prolly.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  EventFetcher,
4
4
  EventBlock,
5
5
  findCommonAncestorWithSortedEvents,
6
- findUnknownSortedEvents
6
+ findEventsToSync
7
7
  } from './clock.js'
8
8
  import { create, load } from 'prolly-trees/map'
9
9
  import * as codec from '@ipld/dag-cbor'
@@ -12,7 +12,7 @@ import { MemoryBlockstore, MultiBlockFetcher } from './block.js'
12
12
  import { doTransaction } from './blockstore.js'
13
13
 
14
14
  import { nocache as cache } from 'prolly-trees/cache'
15
- import { bf, simpleCompare as compare } from 'prolly-trees/utils'
15
+ import { CIDCounter, bf, simpleCompare as compare } from 'prolly-trees/utils'
16
16
  import { create as createBlock } from 'multiformats/block'
17
17
  const opts = { cache, chunker: bf(3), codec, hasher, compare }
18
18
 
@@ -22,9 +22,18 @@ const withLog = async (label, fn) => {
22
22
  return resp
23
23
  }
24
24
 
25
- const makeGetBlock = (blocks) => async (address) => {
26
- const { cid, bytes } = await withLog(address, () => blocks.get(address))
27
- return createBlock({ cid, bytes, hasher, codec })
25
+ // todo should also return a CIDCounter
26
+ export const makeGetBlock = (blocks) => {
27
+ // const cids = new CIDCounter() // todo this could be used for proofs of mutations
28
+ const getBlockFn = async (address) => {
29
+ const { cid, bytes } = await withLog(address, () => blocks.get(address))
30
+ // cids.add({ address: cid })
31
+ return createBlock({ cid, bytes, hasher, codec })
32
+ }
33
+ return {
34
+ // cids,
35
+ getBlock: getBlockFn
36
+ }
28
37
  }
29
38
 
30
39
  /**
@@ -57,6 +66,7 @@ async function createAndSaveNewEvent (
57
66
  additions,
58
67
  removals = []
59
68
  ) {
69
+ let cids
60
70
  const data = {
61
71
  type: 'put',
62
72
  root: {
@@ -77,13 +87,14 @@ async function createAndSaveNewEvent (
77
87
 
78
88
  const event = await EventBlock.create(data, head)
79
89
  bigPut(event)
80
- head = await advance(inBlocks, head, event.cid)
90
+ ;({ head, cids } = await advance(inBlocks, head, event.cid))
81
91
 
82
92
  return {
83
93
  root,
84
94
  additions,
85
95
  removals,
86
96
  head,
97
+ clockCIDs: cids,
87
98
  event
88
99
  }
89
100
  }
@@ -91,7 +102,7 @@ async function createAndSaveNewEvent (
91
102
  const makeGetAndPutBlock = (inBlocks) => {
92
103
  const mblocks = new MemoryBlockstore()
93
104
  const blocks = new MultiBlockFetcher(mblocks, inBlocks)
94
- const getBlock = makeGetBlock(blocks)
105
+ const { getBlock, cids } = makeGetBlock(blocks)
95
106
  const put = inBlocks.put.bind(inBlocks)
96
107
  const bigPut = async (block, additions) => {
97
108
  // console.log('bigPut', block.cid.toString())
@@ -102,7 +113,7 @@ const makeGetAndPutBlock = (inBlocks) => {
102
113
  additions.set(cid.toString(), block)
103
114
  }
104
115
  }
105
- return { getBlock, bigPut, mblocks, blocks }
116
+ return { getBlock, bigPut, mblocks, blocks, cids }
106
117
  }
107
118
 
108
119
  const bulkFromEvents = (sorted) =>
@@ -166,7 +177,7 @@ export async function put (inBlocks, head, event, options) {
166
177
  for (const nb of newBlocks) {
167
178
  bigPut(nb, additions)
168
179
  }
169
-
180
+ // additions are new blocks
170
181
  return createAndSaveNewEvent(
171
182
  inBlocks,
172
183
  mblocks,
@@ -175,7 +186,7 @@ export async function put (inBlocks, head, event, options) {
175
186
  prollyRootBlock,
176
187
  event,
177
188
  head,
178
- Array.from(additions.values()) /*, Array.from(removals.values()) */
189
+ Array.from(additions.values()) /*, todo? Array.from(removals.values()) */
179
190
  )
180
191
  }
181
192
 
@@ -207,8 +218,7 @@ export async function root (inBlocks, head) {
207
218
  }
208
219
  bigPut(prollyRootBlock)
209
220
  })
210
-
211
- return newProllyRootNode // .block).cid // todo return live object not cid
221
+ return { cids: events.cids, node: newProllyRootNode }
212
222
  }
213
223
 
214
224
  /**
@@ -223,12 +233,8 @@ export async function eventsSince (blocks, head, since) {
223
233
  throw new Error('no head')
224
234
  }
225
235
  const sinceHead = [...since, ...head]
226
- const unknownSorted3 = await findUnknownSortedEvents(
227
- blocks,
228
- sinceHead,
229
- await findCommonAncestorWithSortedEvents(blocks, sinceHead)
230
- )
231
- return unknownSorted3.map(({ value: { data } }) => data)
236
+ const { cids, events: unknownSorted3 } = await findEventsToSync(blocks, sinceHead)
237
+ return { clockCIDs: cids, result: unknownSorted3.map(({ value: { data } }) => data) }
232
238
  }
233
239
 
234
240
  /**
@@ -243,11 +249,11 @@ export async function getAll (blocks, head) {
243
249
  // todo use the root node left around from put, etc
244
250
  // move load to a central place
245
251
  if (!head.length) {
246
- return []
252
+ return { clockCIDs: new CIDCounter(), cids: new CIDCounter(), result: [] }
247
253
  }
248
- const prollyRootNode = await root(blocks, head)
249
- const { result } = await prollyRootNode.getAllEntries()
250
- return result.map(({ key, value }) => ({ key, value }))
254
+ const { node: prollyRootNode, cids: clockCIDs } = await root(blocks, head)
255
+ const { result, cids } = await prollyRootNode.getAllEntries() // todo params
256
+ return { clockCIDs, cids, result: result.map(({ key, value }) => ({ key, value })) }
251
257
  }
252
258
 
253
259
  /**
@@ -258,9 +264,9 @@ export async function getAll (blocks, head) {
258
264
  export async function get (blocks, head, key) {
259
265
  // instead pass root from db? and always update on change
260
266
  if (!head.length) {
261
- return null
267
+ return { cids: new CIDCounter(), result: null }
262
268
  }
263
- const prollyRootNode = await root(blocks, head)
264
- const { result } = await prollyRootNode.get(key)
265
- return result
269
+ const { node: prollyRootNode, cids: clockCIDs } = await root(blocks, head)
270
+ const { result, cids } = await prollyRootNode.get(key)
271
+ return { result, cids, clockCIDs }
266
272
  }
package/src/valet.js CHANGED
@@ -32,9 +32,15 @@ export default class Valet {
32
32
  )
33
33
  if (this.uploadFunction) {
34
34
  // todo we can coalesce these into a single car file
35
- for (const task of tasks) {
36
- await this.uploadFunction(task.carCid, task.value)
37
- }
35
+ return await this.withDB(async (db) => {
36
+ for (const task of tasks) {
37
+ await this.uploadFunction(task.carCid, task.value)
38
+ // update the indexedb to mark this car as no longer pending
39
+ const carMeta = await db.get('cidToCar', task.carCid)
40
+ delete carMeta.pending
41
+ await db.put('cidToCar', carMeta)
42
+ }
43
+ })
38
44
  }
39
45
  callback()
40
46
  })