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.

77 lines
1.3 KiB

  1. package batch
  2. import (
  3. "github.com/syndtr/goleveldb/leveldb"
  4. // "encoding/json"
  5. "fmt"
  6. "github.com/vocdoni/dvote-relay/types"
  7. )
  8. var DBPath string
  9. var BDBPath string
  10. var DB *leveldb.DB
  11. var BDB *leveldb.DB
  12. var BatchSignal chan bool
  13. var BatchSize int
  14. var currentBatchSize int
  15. var err error
  16. func Setup() {
  17. DB, err = leveldb.OpenFile(DBPath, nil)
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer DB.Close()
  22. BDB, err = leveldb.OpenFile(BDBPath, nil)
  23. if err != nil {
  24. panic(err)
  25. }
  26. defer BDB.Close()
  27. fmt.Println(BDB)
  28. }
  29. //add (queue for counting)
  30. func Add(p types.Packet) error {
  31. BDB, err = leveldb.OpenFile(BDBPath, nil)
  32. if err != nil {
  33. panic(err)
  34. }
  35. defer BDB.Close()
  36. err := BDB.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)), nil)
  37. if err != nil {
  38. return err
  39. }
  40. currentBatchSize++
  41. if currentBatchSize >= BatchSize {
  42. BatchSignal <- true
  43. }
  44. return nil
  45. }
  46. //create (return batch)
  47. func Create() []byte {
  48. DB, err = leveldb.OpenFile(DBPath, nil)
  49. if err != nil {
  50. panic(err)
  51. }
  52. defer DB.Close()
  53. var b []byte
  54. iter := BDB.NewIterator(nil, nil)
  55. for iter.Next() {
  56. err := iter.Error()
  57. if err != nil {
  58. panic(err)
  59. }
  60. //put p in batch
  61. //db.Put(iter.Key(), iter.Val(), nil)
  62. //bdb.Delete(iter.Key(), nil)
  63. }
  64. iter.Release()
  65. return b
  66. }