2013-12-29 16:54:50 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"os/user"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2013-12-29 18:09:57 -06:00
|
|
|
type LDBDatabase struct {
|
2013-12-29 16:54:50 -06:00
|
|
|
db *leveldb.DB
|
|
|
|
trie *Trie
|
|
|
|
}
|
|
|
|
|
2013-12-29 18:09:57 -06:00
|
|
|
func NewLDBDatabase() (*LDBDatabase, error) {
|
2013-12-29 16:54:50 -06:00
|
|
|
// 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()
|
|
|
|
dbPath := path.Join(usr.HomeDir, ".ethereum", "database")
|
|
|
|
|
|
|
|
// Open the db
|
|
|
|
db, err := leveldb.OpenFile(dbPath, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-29 18:09:57 -06:00
|
|
|
database := &LDBDatabase{db: db}
|
2013-12-29 16:54:50 -06:00
|
|
|
|
|
|
|
// Bootstrap database. Sets a few defaults; such as the last block
|
|
|
|
database.Bootstrap()
|
|
|
|
|
|
|
|
return database, nil
|
|
|
|
}
|
|
|
|
|
2013-12-29 18:09:57 -06:00
|
|
|
func (db *LDBDatabase) Bootstrap() error {
|
2013-12-29 16:54:50 -06:00
|
|
|
db.trie = NewTrie(db)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-29 18:09:57 -06:00
|
|
|
func (db *LDBDatabase) Put(key []byte, value []byte) {
|
2013-12-29 16:54:50 -06:00
|
|
|
err := db.db.Put(key, value, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error put", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-29 18:09:57 -06:00
|
|
|
func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
|
2013-12-29 16:54:50 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2013-12-29 18:09:57 -06:00
|
|
|
func (db *LDBDatabase) Close() {
|
|
|
|
// Close the leveldb database
|
|
|
|
db.db.Close()
|
2013-12-29 16:54:50 -06:00
|
|
|
}
|
|
|
|
|