Skip to content

Commit

Permalink
feat: leveldb for keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
hea9549 committed Aug 16, 2020
1 parent 0776001 commit fcdd386
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions keychain/leveldb_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package keychain

import (
"encoding/json"
"github.com/DE-labtory/zulu/db/leveldb"
"github.com/ethereum/go-ethereum/log"
)

func NewLevelDBStore(handle leveldb.DBHandle) *LevelDBStore {
return &LevelDBStore{handle: handle}
}

type LevelDBStore struct {
handle leveldb.DBHandle
}

func (l *LevelDBStore) Store(k Key) error {
rawData, err := json.Marshal(k)
if err != nil {
return err
}
err = l.handle.Put([]byte(k.ID), rawData, true)
if err != nil {
return err
}
return nil
}

func (l *LevelDBStore) Get(id string) Key {
var k Key
rawData, err := l.handle.Get([]byte(id))

// check leveldb error
if err != nil {
log.Error("error while find key :" + id + ", in level db key store")
return k
}

// check empty data
if rawData == nil {
return k
}

// check unmarshal data
err = json.Unmarshal(rawData, &k)
if err != nil {
log.Error("error while unmarshal key :" + id + ", in level db key store")
return k
}

// with no err
return k
}

0 comments on commit fcdd386

Please sign in to comment.