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.

84 lines
1.6 KiB

  1. package batch
  2. import (
  3. "fmt"
  4. "github.com/vocdoni/dvote-relay/types"
  5. "github.com/vocdoni/dvote-relay/db"
  6. "encoding/json"
  7. )
  8. var rdb *db.LevelDbStorage
  9. var bdb *db.LevelDbStorage
  10. var BatchSignal chan bool
  11. var BatchSize int
  12. var err error
  13. func Setup(l *db.LevelDbStorage) {
  14. rdb = l.WithPrefix([]byte("relay_"))
  15. bdb = l.WithPrefix([]byte("batch_"))
  16. }
  17. //add (queue for counting)
  18. func Add(ballot types.Ballot) error {
  19. //this is probably adding []
  20. //err := bdb.Put(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)))
  21. var b []byte
  22. var n []byte
  23. var err error
  24. b, err = json.Marshal(ballot)
  25. n, err = json.Marshal(ballot.Nullifier)
  26. err = bdb.Put(n,b)
  27. if err != nil {
  28. return err
  29. }
  30. //this actually needs to see if it was added
  31. if bdb.Count() >= BatchSize {
  32. BatchSignal <- true
  33. }
  34. return nil
  35. }
  36. //create (return batch)
  37. //k is []byte 'batch_' + nullifier
  38. //v is []byte package
  39. //returns slice of nullifiers, batch json
  40. func Fetch() ([]string, []string) {
  41. var n []string
  42. var b []string
  43. iter := bdb.Iter()
  44. for iter.Next() {
  45. k := iter.Key()
  46. v := iter.Value()
  47. err := iter.Error()
  48. if err != nil {
  49. panic(err)
  50. }
  51. n = append(n, string(k[6:]))
  52. b = append(b, string(v))
  53. }
  54. iter.Release()
  55. // jn, err := json.Marshal(n)
  56. // if err != nil {
  57. // panic(err)
  58. // }
  59. // jb, err := json.Marshal(b)
  60. // if err != nil {
  61. // panic(err)
  62. // }
  63. return n, b
  64. }
  65. //move from bdb to rdb once pinned
  66. func Compact(n []string) {
  67. for _, k := range n {
  68. //fmt.Println(k)
  69. v, err := bdb.Get([]byte(k))
  70. if err != nil {
  71. fmt.Println(err.Error())
  72. }
  73. rdb.Put([]byte(k), v)
  74. bdb.Delete([]byte(k))
  75. }
  76. }