Split up db and trie and added interface

This commit is contained in:
obscuren 2013-12-30 01:09:57 +01:00
parent a926686445
commit f17930eb46
5 changed files with 124 additions and 87 deletions

View File

@ -7,12 +7,12 @@ import (
"fmt"
)
type Database struct {
type LDBDatabase struct {
db *leveldb.DB
trie *Trie
}
func NewDatabase() (*Database, error) {
func NewLDBDatabase() (*LDBDatabase, error) {
// This will eventually have to be something like a resource folder.
// it works on my system for now. Probably won't work on Windows
usr, _ := user.Current()
@ -24,7 +24,7 @@ func NewDatabase() (*Database, error) {
return nil, err
}
database := &Database{db: db}
database := &LDBDatabase{db: db}
// Bootstrap database. Sets a few defaults; such as the last block
database.Bootstrap()
@ -32,63 +32,25 @@ func NewDatabase() (*Database, error) {
return database, nil
}
func (db *Database) Bootstrap() error {
func (db *LDBDatabase) Bootstrap() error {
db.trie = NewTrie(db)
return nil
}
func (db *Database) Put(key []byte, value []byte) {
func (db *LDBDatabase) Put(key []byte, value []byte) {
err := db.db.Put(key, value, nil)
if err != nil {
fmt.Println("Error put", err)
}
}
func (db *Database) Close() {
func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
return nil, nil
}
func (db *LDBDatabase) Close() {
// Close the leveldb database
db.db.Close()
}
type Trie struct {
root string
db *Database
}
func NewTrie(db *Database) *Trie {
return &Trie{db: db, root: ""}
}
func (t *Trie) Update(key string, value string) {
k := CompactHexDecode(key)
t.root = t.UpdateState(t.root, k, value)
}
func (t *Trie) Get(key []byte) ([]byte, error) {
return nil, nil
}
// Inserts a new sate or delete a state based on the value
func (t *Trie) UpdateState(node, key, value string) string {
if value != "" {
return t.InsertState(node, key, value)
} else {
// delete it
}
return ""
}
func (t *Trie) InsertState(node, key, value string) string {
return ""
}
func (t *Trie) Put(node []byte) []byte {
enc := Encode(node)
sha := Sha256Bin(enc)
t.db.Put([]byte(sha), enc)
return sha
}

View File

@ -1,43 +1,7 @@
package main
import (
"testing"
_"testing"
_"fmt"
)
func TestTriePut(t *testing.T) {
db, err := NewDatabase()
defer db.Close()
if err != nil {
t.Error("Error starting db")
}
key := db.trie.Put([]byte("testing node"))
data, err := db.db.Get(key, nil)
if err != nil {
t.Error("Nothing at node")
}
s, _ := Decode(data, 0)
if str, ok := s.([]byte); ok {
if string(str) != "testing node" {
t.Error("Wrong value node", str)
}
} else {
t.Error("Invalid return type")
}
}
func TestTrieUpdate(t *testing.T) {
db, err := NewDatabase()
defer db.Close()
if err != nil {
t.Error("Error starting db")
}
db.trie.Update("test", "test")
}

View File

@ -9,13 +9,13 @@ type Server struct {
// Channel for shutting down the server
shutdownChan chan bool
// DB interface
db *Database
db *LDBDatabase
// Peers (NYI)
peers *list.List
}
func NewServer() (*Server, error) {
db, err := NewDatabase()
db, err := NewLDBDatabase()
if err != nil {
return nil, err
}

50
trie.go Normal file
View File

@ -0,0 +1,50 @@
package main
// Database interface
type Database interface {
Put(key []byte, value []byte)
Get(key []byte) ([]byte, error)
}
type Trie struct {
root string
db Database
}
func NewTrie(db Database) *Trie {
return &Trie{db: db, root: ""}
}
func (t *Trie) Update(key string, value string) {
k := CompactHexDecode(key)
t.root = t.UpdateState(t.root, k, value)
}
func (t *Trie) Get(key []byte) ([]byte, error) {
return nil, nil
}
// Inserts a new sate or delete a state based on the value
func (t *Trie) UpdateState(node string, key []int, value string) string {
if value != "" {
return t.InsertState(node, ""/*key*/, value)
} else {
// delete it
}
return ""
}
func (t *Trie) InsertState(node, key, value string) string {
return ""
}
func (t *Trie) Put(node []byte) []byte {
enc := Encode(node)
sha := Sha256Bin(enc)
t.db.Put([]byte(sha), enc)
return sha
}

61
trie.test.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"testing"
)
type MemDatabase struct {
db map[string][]byte
trie *Trie
}
func NewMemDatabase() (*MemDatabase, error) {
db := &MemDatabase{db: make(map[string][]byte)}
db.trie = NewTrie(db)
return db, nil
}
func (db *MemDatabase) Put(key []byte, value []byte) {
db.db[string(key)] = value
}
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
return db.db[string(key)], nil
}
func TestTriePut(t *testing.T) {
db, err := NewMemDatabase()
if err != nil {
t.Error("Error starting db")
}
key := db.trie.Put([]byte("testing node"))
data, err := db.Get(key)
if err != nil {
t.Error("Nothing at node")
}
s, _ := Decode(data, 0)
if str, ok := s.([]byte); ok {
if string(str) != "testing node" {
t.Error("Wrong value node", str)
}
} else {
t.Error("Invalid return type")
}
}
func TestTrieUpdate(t *testing.T) {
db, err := NewMemDatabase()
if err != nil {
t.Error("Error starting db")
}
db.trie.Update("test", "test")
}