Split up db and trie and added interface

pull/2/head
obscuren 11 years ago
parent a926686445
commit f17930eb46
  1. 56
      database.go
  2. 38
      database_test.go
  3. 4
      server.go
  4. 50
      trie.go
  5. 61
      trie.test.go

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

@ -1,43 +1,7 @@
package main package main
import ( import (
"testing" _"testing"
_"fmt" _"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")
}

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

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

@ -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")
}
Loading…
Cancel
Save