Skip to content

Commit

Permalink
fixed fastsync & rocksdb iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
sadoci committed Jun 17, 2021
1 parent 3b3524c commit a001843
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
3 changes: 1 addition & 2 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ func NewDB(file string, cache int, handles int, namespace string) (ethdb.Databas

func NewDBWithFreezer(file string, cache int, handles int, freezer string, namespace string) (ethdb.Database, error) {
if params.UseRocksDb != 0 {
return NewRocksDBDatabase(file, cache, handles, namespace)
//return NewRocksDBDatabaseWithFreezer(file, cache, handles, freezer, namespace)
return NewRocksDBDatabaseWithFreezer(file, cache, handles, freezer, namespace)
} else {
return NewLevelDBDatabaseWithFreezer(file, cache, handles, freezer, namespace)
}
Expand Down
100 changes: 99 additions & 1 deletion ethdb/rocksdb/rocksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ type RDBDatabase struct {
ropts *C.rocksdb_readoptions_t
}

type RDBIterator struct {
it *C.rocksdb_iterator_t
opts *C.rocksdb_readoptions_t
lowerBound []byte
upperBound []byte
}

func cerror(cerr *C.char) error {
if cerr == nil {
return nil
Expand Down Expand Up @@ -151,17 +158,108 @@ func (db *RDBDatabase) Delete(key []byte) error {
}

func (db *RDBDatabase) NewIterator() ethdb.Iterator {
return nil
it := C.rocksdb_create_iterator(db.db, db.ropts)
opts := C.rocksdb_readoptions_create()
return &RDBIterator{
it: it,
opts: opts,
}
}

func (db *RDBDatabase) NewIteratorWithStart(start []byte) ethdb.Iterator {
opts := C.rocksdb_readoptions_create()
lowerBound := b2c(start)
C.rocksdb_readoptions_set_iterate_lower_bound(opts, lowerBound, C.size_t(len(start)))
it := C.rocksdb_create_iterator(db.db, opts)
return &RDBIterator{
it: it,
opts: opts,
lowerBound: start,
}
}

func incrBytes(bz []byte) []byte {
if len(bz) == 0 {
return nil
}
cz := make([]byte, len(bz))
copy(cz, bz)
for i := len(bz) - 1; i >= 0; i-- {
if cz[i] == 0xFF {
cz[i] = 0
} else {
cz[i]++
return cz
}
}
return nil
}

func (db *RDBDatabase) NewIteratorWithPrefix(prefix []byte) ethdb.Iterator {
start := prefix
end := incrBytes(start)

opts := C.rocksdb_readoptions_create()
lowerBound := b2c(start)
C.rocksdb_readoptions_set_iterate_lower_bound(opts, lowerBound, C.size_t(len(start)))
upperBound := b2c(end)
C.rocksdb_readoptions_set_iterate_lower_bound(opts, upperBound, C.size_t(len(end)))
it := C.rocksdb_create_iterator(db.db, opts)
return &RDBIterator{
it: it,
opts: opts,
lowerBound: start,
upperBound: end,
}
}

func (it *RDBIterator) Next() bool {
if C.rocksdb_iter_valid(it.it) == 0 {
return false
}
C.rocksdb_iter_next(it.it)
return C.rocksdb_iter_valid(it.it) != 0
}

func (it *RDBIterator) Error() error {
var cerr *C.char
C.rocksdb_iter_get_error(it.it, &cerr)
if cerr != nil {
err := errors.New(C.GoString(cerr))
C.rocksdb_free(unsafe.Pointer(cerr))
return err
}
return nil
}

func (it *RDBIterator) Key() []byte {
var cvl C.size_t
cv := C.rocksdb_iter_key(it.it, &cvl)
if cv == nil {
return nil
}
return C.GoBytes(unsafe.Pointer(cv), C.int(cvl))
}

func (it *RDBIterator) Value() []byte {
var cvl C.size_t
cv := C.rocksdb_iter_value(it.it, &cvl)
if cv == nil {
return nil
}
return C.GoBytes(unsafe.Pointer(cv), C.int(cvl))
}

func (it *RDBIterator) Release() {
if it.it != nil {
C.rocksdb_iter_destroy(it.it)
}
if it.opts != nil {
C.rocksdb_readoptions_destroy(it.opts)
}
it.it, it.opts, it.lowerBound, it.upperBound = nil, nil, nil, nil
}

func (db *RDBDatabase) Stat(property string) (string, error) {
return "", errors.New("Not implemented")
}
Expand Down

0 comments on commit a001843

Please sign in to comment.