Browse Source

reworked db logic based on iden3 pattern - allows for single storage location with prefixing

batching_initial_approach
imw 6 years ago
parent
commit
ddfc8e1187
5 changed files with 126 additions and 46 deletions
  1. +14
    -34
      batch/batch.go
  2. +97
    -0
      db/leveldb.go
  3. +13
    -10
      main.go
  4. +1
    -1
      net/net.go
  5. +1
    -1
      types/types.go

+ 14
- 34
batch/batch.go

@ -1,46 +1,30 @@
package batch
import (
"github.com/syndtr/goleveldb/leveldb"
// "encoding/json"
"fmt"
"github.com/vocdoni/dvote-relay/types"
"github.com/vocdoni/dvote-relay/db"
)
var DBPath string
var BDBPath string
var DB *leveldb.DB
var BDB *leveldb.DB
var rdb *db.LevelDbStorage
var bdb *db.LevelDbStorage
var BatchSignal chan bool
var BatchSize int
var currentBatchSize int
var err error
func Setup() {
DB, err = leveldb.OpenFile(DBPath, nil)
if err != nil {
panic(err)
}
defer DB.Close()
BDB, err = leveldb.OpenFile(BDBPath, nil)
if err != nil {
panic(err)
}
defer BDB.Close()
fmt.Println(BDB)
func Setup(l *db.LevelDbStorage) {
rdb = l.WithPrefix([]byte("relay"))
bdb = l.WithPrefix([]byte("batch"))
fmt.Println("Batch storage:")
fmt.Println(bdb)
}
//add (queue for counting)
func Add(p types.Packet) error {
BDB, err = leveldb.OpenFile(BDBPath, nil)
if err != nil {
panic(err)
}
defer BDB.Close()
err := BDB.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)), nil)
fmt.Println(bdb)
err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)))
if err != nil {
return err
}
@ -54,22 +38,18 @@ func Add(p types.Packet) error {
//create (return batch)
func Create() []byte {
DB, err = leveldb.OpenFile(DBPath, nil)
if err != nil {
panic(err)
}
defer DB.Close()
var b []byte
iter := BDB.NewIterator(nil, nil)
iter := bdb.Iter()
for iter.Next() {
//k := iter.Key()
//v := iter.Value()
err := iter.Error()
if err != nil {
panic(err)
}
//put p in batch
//db.Put(iter.Key(), iter.Val(), nil)
//rdb.Put(iter.Key(), iter.Val(), nil)
//bdb.Delete(iter.Key(), nil)
}
iter.Release()

+ 97
- 0
db/leveldb.go

@ -0,0 +1,97 @@
package db
// modified from https://github.com/iden3/go-iden3/blob/master/db/leveldb.go
import (
"encoding/json"
"github.com/syndtr/goleveldb/leveldb"
// "github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
"github.com/syndtr/goleveldb/leveldb/iterator"
)
type LevelDbStorage struct {
ldb *leveldb.DB
prefix []byte
}
func NewLevelDbStorage(path string, errorIfMissing bool) (*LevelDbStorage, error) {
o := &opt.Options{
ErrorIfMissing: errorIfMissing,
}
ldb, err := leveldb.OpenFile(path, o)
if err != nil {
return nil, err
}
return &LevelDbStorage{ldb, []byte{}}, nil
}
type storageInfo struct {
KeyCount int
}
func (l *LevelDbStorage) Info() string {
keycount := 0
iter := l.ldb.NewIterator(nil, nil)
for iter.Next() {
keycount++
}
iter.Release()
if err := iter.Error(); err != nil {
return err.Error()
}
json, _ := json.MarshalIndent(
storageInfo{keycount},
"", " ",
)
return string(json)
}
func (l *LevelDbStorage) WithPrefix(prefix []byte) *LevelDbStorage {
return &LevelDbStorage{l.ldb, append(l.prefix, prefix...)}
}
func (l *LevelDbStorage) Get(key []byte) ([]byte, error) {
v, err := l.ldb.Get(append(l.prefix, key[:]...), nil)
if err != nil {
return nil, err
}
return v, err
}
func (l *LevelDbStorage) Put(key []byte, value []byte) error {
err := l.ldb.Put(append(l.prefix, key[:]...), value, nil)
if err != nil {
return err
}
return nil
}
func (l *LevelDbStorage) Delete(key []byte) error {
err := l.ldb.Delete(append(l.prefix, key[:]...), nil)
if err != nil {
return err
}
return nil
}
func (l *LevelDbStorage) Close() {
if err := l.ldb.Close(); err != nil {
panic(err)
}
}
func (l *LevelDbStorage) Iter() iterator.Iterator {
db := l.ldb
i := db.NewIterator(util.BytesPrefix(l.prefix), nil)
return i
}
func (l *LevelDbStorage) LevelDB() *leveldb.DB {
return l.ldb
}

+ 13
- 10
main.go

@ -5,11 +5,11 @@ import (
"time"
"github.com/vocdoni/dvote-relay/batch"
"github.com/vocdoni/dvote-relay/net"
"github.com/vocdoni/dvote-relay/db"
)
var dbPath = "$HOME/.dvote/relay.db"
var bdbPath = "$HOME/.dvote/batch.db"
var batchSeconds = 30 //seconds
var dbPath = "~/.dvote/relay.db"
var batchSeconds = 10 //seconds
var batchSize = 3 //packets
var err error
@ -18,19 +18,22 @@ var batchSignal chan bool
var signal bool
func setup() {
func main() {
db, err := db.NewLevelDbStorage(dbPath, false)
if err != nil {
panic(err)
}
defer db.Close()
batch.Setup(db)
batchTimer = time.NewTicker(time.Second * time.Duration(batchSeconds))
batchSignal = make(chan bool)
batch.DBPath = dbPath
batch.BDBPath = bdbPath
batch.BatchSignal = batchSignal
batch.BatchSize = batchSize
}
func main() {
setup()
// batch.Setup()
fmt.Println("Entering main loop")
go net.Listen("8080")
for {

+ 1
- 1
net/net.go

@ -27,7 +27,7 @@ func parse(rw http.ResponseWriter, request *http.Request) {
//check franchise
//construct packet
p.PID = 1
p.PID = "1"
p.Nullifier = []byte{1,2,3}
p.Vote = []byte{4,5,6}
p.Franchise = []byte{7,8,9}

+ 1
- 1
types/types.go

@ -22,7 +22,7 @@ type Batch struct {
}
type Packet struct {
PID int
PID string
Nullifier []byte
Vote []byte
Franchise []byte

Loading…
Cancel
Save