From ddfc8e118711f862b3b8dbbc19fd9e03d999ff14 Mon Sep 17 00:00:00 2001 From: imw Date: Tue, 18 Dec 2018 15:02:31 +0100 Subject: [PATCH] reworked db logic based on iden3 pattern - allows for single storage location with prefixing --- batch/batch.go | 48 ++++++++----------------- db/leveldb.go | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 23 ++++++------ net/net.go | 2 +- types/types.go | 2 +- 5 files changed, 126 insertions(+), 46 deletions(-) create mode 100644 db/leveldb.go diff --git a/batch/batch.go b/batch/batch.go index 6216f3c..fdb793c 100644 --- a/batch/batch.go +++ b/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() diff --git a/db/leveldb.go b/db/leveldb.go new file mode 100644 index 0000000..35050aa --- /dev/null +++ b/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 +} + diff --git a/main.go b/main.go index 7f1f1c2..82c7608 100644 --- a/main.go +++ b/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 { diff --git a/net/net.go b/net/net.go index 1045264..6ac6bb9 100644 --- a/net/net.go +++ b/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} diff --git a/types/types.go b/types/types.go index 5e784c4..3c1a604 100644 --- a/types/types.go +++ b/types/types.go @@ -22,7 +22,7 @@ type Batch struct { } type Packet struct { - PID int + PID string Nullifier []byte Vote []byte Franchise []byte