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.

81 lines
1.4 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. fmt.Println(currentBatchSize)
  42. if currentBatchSize >= BatchSize {
  43. fmt.Println("sending ready")
  44. BatchSignal <- true
  45. fmt.Println("sent ready")
  46. }
  47. fmt.Println("returning")
  48. return nil
  49. }
  50. //create (return batch)
  51. func Create() []byte {
  52. fmt.Println("Creating batch")
  53. DB, err = leveldb.OpenFile(DBPath, nil)
  54. if err != nil {
  55. panic(err)
  56. }
  57. defer DB.Close()
  58. var b []byte
  59. iter := BDB.NewIterator(nil, nil)
  60. for iter.Next() {
  61. err := iter.Error()
  62. if err != nil {
  63. panic(err)
  64. }
  65. //put p in batch
  66. //db.Put(iter.Key(), iter.Val(), nil)
  67. //bdb.Delete(iter.Key(), nil)
  68. }
  69. iter.Release()
  70. return b
  71. }