@@ -42,39 +42,25 @@ export async function applyBulkUpdateToCrdt(
42
42
updates : DocUpdate [ ]
43
43
) : Promise < CRDTMeta > {
44
44
let result : Result | null = null
45
- // const batch = await Batch.create(tblocks, init.cid)
46
- // console.log('applyBulkUpdateToCrdt', updates.length)
47
45
if ( updates . length > 1 ) {
48
- // throw new Error('batch not implemented')
49
46
const batch = await Batch . create ( tblocks , head )
50
47
for ( const update of updates ) {
51
48
const link = await writeDocContent ( tblocks , update )
52
49
await batch . put ( update . key , link )
53
50
}
54
51
result = await batch . commit ( )
55
- // console.log('batch result', result)
56
- } else {
57
- for ( const update of updates ) {
58
- const link = await writeDocContent ( tblocks , update )
59
- result = await put ( tblocks , head , update . key , link )
60
- const resRoot = result . root . toString ( )
61
- const isReturned = result . additions . some ( a => a . cid . toString ( ) === resRoot )
62
- if ( ! isReturned ) {
63
- const hasRoot = await tblocks . get ( result . root ) // is a db-wide get
64
- if ( ! hasRoot ) {
65
- throw new Error (
66
- `missing root in additions: ${ result . additions . length } ${ resRoot } keys: ${ updates
67
- . map ( u => u . key )
68
- . toString ( ) } `
69
- )
70
- }
71
- }
72
- }
52
+ } else if ( updates . length === 1 ) {
53
+ const link = await writeDocContent ( tblocks , updates [ 0 ] )
54
+ result = await put ( tblocks , head , updates [ 0 ] . key , link )
73
55
}
74
- if ( ! result ) throw new Error ( 'Missing result' )
56
+ if ( ! result ) throw new Error ( 'Missing result, updates: ' + updates . length )
75
57
76
58
if ( result . event ) {
77
- for ( const { cid, bytes } of [ ...result . additions , ...result . removals , result . event ] ) {
59
+ for ( const { cid, bytes } of [
60
+ ...result . additions ,
61
+ ...result . removals , // todo mute
62
+ result . event
63
+ ] ) {
78
64
tblocks . putSync ( cid , bytes )
79
65
}
80
66
}
0 commit comments