66
66
genesisHash hash.Hash256
67
67
kvStore db.KVStoreWithRange
68
68
batch batch.KVStoreBatch
69
+ cBatch batch.KVStoreBatch
69
70
dirtyAddr addrIndex
70
71
tbk db.CountingIndex
71
72
tac db.CountingIndex
@@ -84,6 +85,7 @@ func NewIndexer(kv db.KVStore, genesisHash hash.Hash256) (Indexer, error) {
84
85
x := blockIndexer {
85
86
kvStore : kvRange ,
86
87
batch : batch .NewBatch (),
88
+ cBatch : batch .NewBatch (),
87
89
dirtyAddr : make (addrIndex ),
88
90
genesisHash : genesisHash ,
89
91
}
@@ -172,7 +174,7 @@ func (x *blockIndexer) DeleteTipBlock(blk *block.Block) error {
172
174
if err := x .tac .Revert (uint64 (len (blk .Actions ))); err != nil {
173
175
return err
174
176
}
175
- return x .commit ( )
177
+ return x .kvStore . WriteBatch ( x . batch )
176
178
}
177
179
178
180
// Height return the blockchain height
@@ -293,21 +295,29 @@ func (x *blockIndexer) putBlock(blk *block.Block) error {
293
295
if height != x .tbk .Size () {
294
296
return errors .Wrapf (db .ErrInvalid , "wrong block height %d, expecting %d" , height , x .tbk .Size ())
295
297
}
298
+
296
299
// index hash --> height
297
300
hash := blk .HashBlock ()
298
301
x .batch .Put (blockHashToHeightNS , hash [hashOffset :], byteutil .Uint64ToBytesBigEndian (height ), "failed to put hash -> height mapping" )
302
+
299
303
// index height --> block hash, number of actions, and total transfer amount
300
304
bd := & blockIndex {
301
305
hash : hash [:],
302
306
numAction : uint32 (len (blk .Actions )),
303
307
tsfAmount : blk .CalculateTransferAmount ()}
308
+ if err := x .tbk .UseBatch (x .cBatch ); err != nil {
309
+ return err
310
+ }
304
311
if err := x .tbk .Add (bd .Serialize (), true ); err != nil {
305
312
return errors .Wrapf (err , "failed to put block %d index" , height )
306
313
}
307
314
308
315
// store height of the block, so getReceiptByActionHash() can use height to directly pull receipts
309
316
ad := (& actionIndex {
310
317
blkHeight : blk .Height ()}).Serialize ()
318
+ if err := x .tac .UseBatch (x .cBatch ); err != nil {
319
+ return err
320
+ }
311
321
// index actions in the block
312
322
for _ , selp := range blk .Actions {
313
323
actHash := selp .Hash ()
@@ -328,7 +338,7 @@ func (x *blockIndexer) commit() error {
328
338
var commitErr error
329
339
for k , v := range x .dirtyAddr {
330
340
if commitErr == nil {
331
- if err := v .Commit (); err != nil {
341
+ if err := v .AddTotalSize (); err != nil {
332
342
commitErr = err
333
343
}
334
344
}
@@ -338,10 +348,13 @@ func (x *blockIndexer) commit() error {
338
348
return commitErr
339
349
}
340
350
// total block and total action index
341
- if err := x .tbk .Commit (); err != nil {
351
+ if err := x .tbk .AddTotalSize (); err != nil {
352
+ return err
353
+ }
354
+ if err := x .tac .AddTotalSize (); err != nil {
342
355
return err
343
356
}
344
- if err := x . tac . Commit ( ); err != nil {
357
+ if err := db . CommitWithFillPercent ( x . kvStore , x . cBatch , 1.0 ); err != nil {
345
358
return err
346
359
}
347
360
return x .kvStore .WriteBatch (x .batch )
@@ -362,6 +375,9 @@ func (x *blockIndexer) getIndexerForAddr(addr []byte, batch bool) (db.CountingIn
362
375
if err != nil {
363
376
return nil , err
364
377
}
378
+ if err := indexer .UseBatch (x .cBatch ); err != nil {
379
+ return nil , err
380
+ }
365
381
x .dirtyAddr [address ] = indexer
366
382
}
367
383
return indexer , nil
0 commit comments