|
@ -4,13 +4,13 @@ import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"github.com/vocdoni/dvote-relay/types" |
|
|
"github.com/vocdoni/dvote-relay/types" |
|
|
"github.com/vocdoni/dvote-relay/db" |
|
|
"github.com/vocdoni/dvote-relay/db" |
|
|
|
|
|
"encoding/json" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var rdb *db.LevelDbStorage |
|
|
var rdb *db.LevelDbStorage |
|
|
var bdb *db.LevelDbStorage |
|
|
var bdb *db.LevelDbStorage |
|
|
var BatchSignal chan bool |
|
|
var BatchSignal chan bool |
|
|
var BatchSize int |
|
|
var BatchSize int |
|
|
var currentBatchSize int |
|
|
|
|
|
var err error |
|
|
var err error |
|
|
|
|
|
|
|
|
func Setup(l *db.LevelDbStorage) { |
|
|
func Setup(l *db.LevelDbStorage) { |
|
@ -20,27 +20,33 @@ func Setup(l *db.LevelDbStorage) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//add (queue for counting)
|
|
|
//add (queue for counting)
|
|
|
func Add(p types.Ballot) error { |
|
|
|
|
|
err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p))) |
|
|
|
|
|
|
|
|
func Add(ballot types.Ballot) error { |
|
|
|
|
|
//this is probably adding []
|
|
|
|
|
|
//err := bdb.Put(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)))
|
|
|
|
|
|
var b []byte |
|
|
|
|
|
var n []byte |
|
|
|
|
|
var err error |
|
|
|
|
|
b, err = json.Marshal(ballot) |
|
|
|
|
|
n, err = json.Marshal(ballot.Nullifier) |
|
|
|
|
|
err = bdb.Put(n,b) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
return err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//this actually needs to see if it was added
|
|
|
//this actually needs to see if it was added
|
|
|
currentBatchSize++ |
|
|
|
|
|
fmt.Println(bdb.Info()) |
|
|
|
|
|
if currentBatchSize >= BatchSize { |
|
|
|
|
|
|
|
|
if bdb.Count() >= BatchSize { |
|
|
BatchSignal <- true |
|
|
BatchSignal <- true |
|
|
} |
|
|
} |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//create (return batch)
|
|
|
//create (return batch)
|
|
|
//k is []byte nullifier
|
|
|
|
|
|
|
|
|
//k is []byte 'batch_' + nullifier
|
|
|
//v is []byte package
|
|
|
//v is []byte package
|
|
|
func Create() []byte { |
|
|
|
|
|
var b []byte |
|
|
|
|
|
var i int |
|
|
|
|
|
|
|
|
//returns slice of nullifiers, batch json
|
|
|
|
|
|
func Create() ([]string, []string) { |
|
|
|
|
|
var n []string |
|
|
|
|
|
var b []string |
|
|
iter := bdb.Iter() |
|
|
iter := bdb.Iter() |
|
|
for iter.Next() { |
|
|
for iter.Next() { |
|
|
k := iter.Key() |
|
|
k := iter.Key() |
|
@ -49,14 +55,30 @@ func Create() []byte { |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
panic(err) |
|
|
panic(err) |
|
|
} |
|
|
} |
|
|
fmt.Println(i) |
|
|
|
|
|
fmt.Println(string(k)) |
|
|
|
|
|
fmt.Println(string(v)) |
|
|
|
|
|
i++ |
|
|
|
|
|
//put p in batch
|
|
|
|
|
|
//rdb.Put(iter.Key(), iter.Val(), nil)
|
|
|
|
|
|
//bdb.Delete(iter.Key(), nil)
|
|
|
|
|
|
|
|
|
n = append(n, string(k[6:])) |
|
|
|
|
|
b = append(b, string(v)) |
|
|
} |
|
|
} |
|
|
iter.Release() |
|
|
iter.Release() |
|
|
return b |
|
|
|
|
|
|
|
|
// jn, err := json.Marshal(n)
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
// panic(err)
|
|
|
|
|
|
// }
|
|
|
|
|
|
// jb, err := json.Marshal(b)
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
// panic(err)
|
|
|
|
|
|
// }
|
|
|
|
|
|
return n, b |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//move from bdb to rdb once pinned
|
|
|
|
|
|
func Compact(n []string) { |
|
|
|
|
|
for _, k := range n { |
|
|
|
|
|
fmt.Println(k) |
|
|
|
|
|
v, err := bdb.Get([]byte(k)) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println(err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
rdb.Put([]byte(k), v) |
|
|
|
|
|
bdb.Delete([]byte(k)) |
|
|
|
|
|
} |
|
|
} |
|
|
} |