You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

  1. /**
  2. * @file
  3. * @copyright defined in aergo/LICENSE.txt
  4. */
  5. package trie
  6. import (
  7. "sync"
  8. "github.com/p4u/asmt/db"
  9. )
  10. // DbTx represents Set and Delete interface to store data
  11. type DbTx interface {
  12. Set(key, value []byte)
  13. Delete(key []byte)
  14. }
  15. type CacheDB struct {
  16. // liveCache contains the first levels of the trie (nodes that have 2 non default children)
  17. liveCache map[Hash][][]byte
  18. // liveMux is a lock for liveCache
  19. liveMux sync.RWMutex
  20. // updatedNodes that have will be flushed to disk
  21. updatedNodes map[Hash][][]byte
  22. // updatedMux is a lock for updatedNodes
  23. updatedMux sync.RWMutex
  24. // nodesToRevert will be deleted from db
  25. nodesToRevert [][]byte
  26. // revertMux is a lock for updatedNodes
  27. revertMux sync.RWMutex
  28. // lock for CacheDB
  29. lock sync.RWMutex
  30. // store is the interface to disk db
  31. Store db.DB
  32. }
  33. // commit adds updatedNodes to the given database transaction.
  34. func (c *CacheDB) commit(txn *DbTx) {
  35. c.updatedMux.Lock()
  36. defer c.updatedMux.Unlock()
  37. for key, batch := range c.updatedNodes {
  38. var node []byte
  39. (*txn).Set(append(node, key[:]...), c.serializeBatch(batch))
  40. }
  41. }
  42. // serializeBatch serialises the 2D [][]byte into a []byte for db
  43. func (c *CacheDB) serializeBatch(batch [][]byte) []byte {
  44. serialized := make([]byte, 4) //, 30*33)
  45. if batch[0][0] == 1 {
  46. // the batch node is a shortcut
  47. bitSet(serialized, 31)
  48. }
  49. for i := 1; i < 31; i++ {
  50. if len(batch[i]) != 0 {
  51. bitSet(serialized, i-1)
  52. serialized = append(serialized, batch[i]...)
  53. }
  54. }
  55. return serialized
  56. }