Skip to content

Commit

Permalink
Compress data on db level. Closes #174
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Nov 2, 2014
1 parent bd4f51f commit 76c9c8d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
51 changes: 34 additions & 17 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"fmt"
"path"

"github.com/ethereum/go-ethereum/compression/rle"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
)

type LDBDatabase struct {
db *leveldb.DB
db *leveldb.DB
comp bool
}

func NewLDBDatabase(name string) (*LDBDatabase, error) {
Expand All @@ -21,32 +24,42 @@ func NewLDBDatabase(name string) (*LDBDatabase, error) {
return nil, err
}

database := &LDBDatabase{db: db}
database := &LDBDatabase{db: db, comp: true}

return database, nil
}

func (db *LDBDatabase) Put(key []byte, value []byte) {
err := db.db.Put(key, value, nil)
func (self *LDBDatabase) Put(key []byte, value []byte) {
if self.comp {
value = rle.Compress(value)
}

err := self.db.Put(key, value, nil)
if err != nil {
fmt.Println("Error put", err)
}
}

func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
return db.db.Get(key, nil)
}
func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
dat, err := self.db.Get(key, nil)
if err != nil {
return nil, err
}

if self.comp {
//fmt.Println("get", dat)
return rle.Decompress(dat)
}

func (db *LDBDatabase) Delete(key []byte) error {
return db.db.Delete(key, nil)
return dat, nil
}

func (db *LDBDatabase) Db() *leveldb.DB {
return db.db
func (self *LDBDatabase) Delete(key []byte) error {
return self.db.Delete(key, nil)
}

func (db *LDBDatabase) LastKnownTD() []byte {
data, _ := db.db.Get([]byte("LTD"), nil)
func (self *LDBDatabase) LastKnownTD() []byte {
data, _ := self.Get([]byte("LTD"))

if len(data) == 0 {
data = []byte{0x0}
Expand All @@ -55,13 +68,17 @@ func (db *LDBDatabase) LastKnownTD() []byte {
return data
}

func (db *LDBDatabase) Close() {
func (self *LDBDatabase) NewIterator() iterator.Iterator {
return self.db.NewIterator(nil, nil)
}

func (self *LDBDatabase) Close() {
// Close the leveldb database
db.db.Close()
self.db.Close()
}

func (db *LDBDatabase) Print() {
iter := db.db.NewIterator(nil, nil)
func (self *LDBDatabase) Print() {
iter := self.db.NewIterator(nil, nil)
for iter.Next() {
key := iter.Key()
value := iter.Value()
Expand Down
26 changes: 24 additions & 2 deletions ethdb/database_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
package ethdb

/*
import (
_ "fmt"
_ "testing"
"bytes"
"testing"
)
func TestCompression(t *testing.T) {
ethutil.ReadConfig("", "/tmp", "")
db, err := NewLDBDatabase("testdb")
if err != nil {
t.Fatal(err)
}
in := make([]byte, 10)
db.Put([]byte("test1"), in)
out, err := db.Get([]byte("test1"))
if err != nil {
t.Fatal(err)
}
if bytes.Compare(out, in) != 0 {
t.Error("put get", in, out)
}
}
*/
16 changes: 2 additions & 14 deletions state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,11 @@ func (self *StateObject) CreateOutputForDiff() {

// State object encoding methods
func (c *StateObject) RlpEncode() []byte {
var root interface{}
if c.State != nil {
root = c.State.Trie.Root
} else {
root = ""
}

return ethutil.Encode([]interface{}{c.Nonce, c.balance, root, c.CodeHash()})
return ethutil.Encode([]interface{}{c.Nonce, c.balance, c.State.Trie.Root, c.CodeHash()})
}

func (c *StateObject) CodeHash() ethutil.Bytes {
var codeHash []byte
if len(c.Code) > 0 {
codeHash = crypto.Sha3(c.Code)
}

return codeHash
return crypto.Sha3(c.Code)
}

func (c *StateObject) RlpDecode(data []byte) {
Expand Down

0 comments on commit 76c9c8d

Please sign in to comment.