Compress data on db level. Closes #174

This commit is contained in:
obscuren 2014-11-03 00:31:15 +01:00
parent bd4f51ff3c
commit 76c9c8d653
3 changed files with 62 additions and 35 deletions

View File

@ -4,12 +4,15 @@ import (
"fmt" "fmt"
"path" "path"
"github.com/ethereum/go-ethereum/compression/rle"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
) )
type LDBDatabase struct { type LDBDatabase struct {
db *leveldb.DB db *leveldb.DB
comp bool
} }
func NewLDBDatabase(name string) (*LDBDatabase, error) { func NewLDBDatabase(name string) (*LDBDatabase, error) {
@ -21,32 +24,42 @@ func NewLDBDatabase(name string) (*LDBDatabase, error) {
return nil, err return nil, err
} }
database := &LDBDatabase{db: db} database := &LDBDatabase{db: db, comp: true}
return database, nil return database, nil
} }
func (db *LDBDatabase) Put(key []byte, value []byte) { func (self *LDBDatabase) Put(key []byte, value []byte) {
err := db.db.Put(key, value, nil) if self.comp {
value = rle.Compress(value)
}
err := self.db.Put(key, value, nil)
if err != nil { if err != nil {
fmt.Println("Error put", err) fmt.Println("Error put", err)
} }
} }
func (db *LDBDatabase) Get(key []byte) ([]byte, error) { func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
return db.db.Get(key, nil) dat, err := self.db.Get(key, nil)
if err != nil {
return nil, err
}
if self.comp {
//fmt.Println("get", dat)
return rle.Decompress(dat)
}
return dat, nil
} }
func (db *LDBDatabase) Delete(key []byte) error { func (self *LDBDatabase) Delete(key []byte) error {
return db.db.Delete(key, nil) return self.db.Delete(key, nil)
} }
func (db *LDBDatabase) Db() *leveldb.DB { func (self *LDBDatabase) LastKnownTD() []byte {
return db.db data, _ := self.Get([]byte("LTD"))
}
func (db *LDBDatabase) LastKnownTD() []byte {
data, _ := db.db.Get([]byte("LTD"), nil)
if len(data) == 0 { if len(data) == 0 {
data = []byte{0x0} data = []byte{0x0}
@ -55,13 +68,17 @@ func (db *LDBDatabase) LastKnownTD() []byte {
return data return data
} }
func (db *LDBDatabase) Close() { func (self *LDBDatabase) NewIterator() iterator.Iterator {
// Close the leveldb database return self.db.NewIterator(nil, nil)
db.db.Close()
} }
func (db *LDBDatabase) Print() { func (self *LDBDatabase) Close() {
iter := db.db.NewIterator(nil, nil) // Close the leveldb database
self.db.Close()
}
func (self *LDBDatabase) Print() {
iter := self.db.NewIterator(nil, nil)
for iter.Next() { for iter.Next() {
key := iter.Key() key := iter.Key()
value := iter.Value() value := iter.Value()

View File

@ -1,6 +1,28 @@
package ethdb package ethdb
/*
import ( import (
_ "fmt" "bytes"
_ "testing" "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)
}
}
*/

View File

@ -297,23 +297,11 @@ func (self *StateObject) CreateOutputForDiff() {
// State object encoding methods // State object encoding methods
func (c *StateObject) RlpEncode() []byte { func (c *StateObject) RlpEncode() []byte {
var root interface{} return ethutil.Encode([]interface{}{c.Nonce, c.balance, c.State.Trie.Root, c.CodeHash()})
if c.State != nil {
root = c.State.Trie.Root
} else {
root = ""
}
return ethutil.Encode([]interface{}{c.Nonce, c.balance, root, c.CodeHash()})
} }
func (c *StateObject) CodeHash() ethutil.Bytes { func (c *StateObject) CodeHash() ethutil.Bytes {
var codeHash []byte return crypto.Sha3(c.Code)
if len(c.Code) > 0 {
codeHash = crypto.Sha3(c.Code)
}
return codeHash
} }
func (c *StateObject) RlpDecode(data []byte) { func (c *StateObject) RlpDecode(data []byte) {