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.

57 lines
1.0 KiB

  1. package batch
  2. import (
  3. "fmt"
  4. "github.com/vocdoni/dvote-relay/types"
  5. "github.com/vocdoni/dvote-relay/db"
  6. )
  7. var rdb *db.LevelDbStorage
  8. var bdb *db.LevelDbStorage
  9. var BatchSignal chan bool
  10. var BatchSize int
  11. var currentBatchSize int
  12. var err error
  13. func Setup(l *db.LevelDbStorage) {
  14. rdb = l.WithPrefix([]byte("relay"))
  15. bdb = l.WithPrefix([]byte("batch"))
  16. fmt.Println("Batch storage:")
  17. fmt.Println(bdb)
  18. }
  19. //add (queue for counting)
  20. func Add(p types.Packet) error {
  21. fmt.Println(bdb)
  22. err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)))
  23. if err != nil {
  24. return err
  25. }
  26. currentBatchSize++
  27. if currentBatchSize >= BatchSize {
  28. BatchSignal <- true
  29. }
  30. return nil
  31. }
  32. //create (return batch)
  33. func Create() []byte {
  34. var b []byte
  35. iter := bdb.Iter()
  36. for iter.Next() {
  37. //k := iter.Key()
  38. //v := iter.Value()
  39. err := iter.Error()
  40. if err != nil {
  41. panic(err)
  42. }
  43. //put p in batch
  44. //rdb.Put(iter.Key(), iter.Val(), nil)
  45. //bdb.Delete(iter.Key(), nil)
  46. }
  47. iter.Release()
  48. return b
  49. }