Browse Source

changed serialization logic to json strings, added batch compaction

batching_initial_approach
imw 6 years ago
parent
commit
b4b687eeda
4 changed files with 66 additions and 20 deletions
  1. +40
    -18
      batch/batch.go
  2. +17
    -1
      db/leveldb.go
  3. +6
    -1
      main.go
  4. +3
    -0
      net/net.go

+ 40
- 18
batch/batch.go

@ -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))
}
} }

+ 17
- 1
db/leveldb.go

@ -33,10 +33,26 @@ type storageInfo struct {
KeyCount int KeyCount int
} }
func (l *LevelDbStorage) Count() int {
keycount := 0
db := l.ldb
iter := db.NewIterator(util.BytesPrefix(l.prefix), nil)
for iter.Next() {
keycount++
}
iter.Release()
if err := iter.Error(); err != nil {
panic(err)
}
return keycount
}
func (l *LevelDbStorage) Info() string { func (l *LevelDbStorage) Info() string {
keycount := 0 keycount := 0
iter := l.ldb.NewIterator(nil, nil)
db := l.ldb
iter := db.NewIterator(util.BytesPrefix(l.prefix), nil)
for iter.Next() { for iter.Next() {
keycount++ keycount++
} }

+ 6
- 1
main.go

@ -45,7 +45,12 @@ func main() {
case signal := <-batchSignal: case signal := <-batchSignal:
if signal == true { if signal == true {
fmt.Println("Signal triggered") fmt.Println("Signal triggered")
batch.Create()
n, b := batch.Create()
fmt.Println("Nullifiers:")
fmt.Println(n)
fmt.Println("Batch:")
fmt.Println(b)
batch.Compact(n)
} }
default: default:
continue continue

+ 3
- 0
net/net.go

@ -45,6 +45,9 @@ func parse(rw http.ResponseWriter, request *http.Request) {
} }
j, err := json.Marshal(e) j, err := json.Marshal(e)
if err != nil {
panic(err)
}
io.WriteString(rw, string(j)) io.WriteString(rw, string(j))
} }

Loading…
Cancel
Save