Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

dbSyncIterator #15

Merged
merged 1 commit into from
Oct 31, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions bzz/dbstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ethereum/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
)

const gcArraySize = 10000
Expand Down Expand Up @@ -75,11 +76,7 @@ func getIndexKey(hash Key) []byte {
HashSize := len(hash)
key := make([]byte, HashSize+1)
key[0] = 0
// db keys derived from hash:
// two halves swapped for uniformly distributed prefix
copy(key[1:HashSize/2+1], hash[HashSize/2:HashSize])
copy(key[HashSize/2+1:HashSize+1], hash[0:HashSize/2])

copy(key[1:], hash[:])
return key
}

Expand Down Expand Up @@ -188,6 +185,7 @@ func (s *dbStore) collectGarbage(ratio float32) {
s.gcPos = nil
}
}
it.Release()

cutidx := gcListSelect(s.gcArray, 0, gcnt-1, int(float32(gcnt)*ratio))
cutval := s.gcArray[cutidx].value
Expand Down Expand Up @@ -375,3 +373,39 @@ func newDbStore(path string) (s *dbStore, err error) {
func (s *dbStore) close() {
s.db.Close()
}

type dbSyncIterator struct {
it iterator.Iterator
stop Key
scFrom, scTo uint64
}

func (s *dbStore) newDbSyncIterator(start Key, stop Key, scFrom uint64, scTo uint64) (si *dbSyncIterator) {
si.it = s.db.NewIterator()
si.it.Seek(getIndexKey(start))
si.stop = stop
si.scFrom = scFrom
si.scTo = scTo
return
}

func (si *dbSyncIterator) next() (hash Key) {
for si.it.Valid() {
key := si.it.Key()
if key[0] != 0 {
break
}
hash = key[1:]
if bytes.Compare(hash, si.stop) > 0 {
break
}
var index dpaDBIndex
decodeIndex(si.it.Value(), &index)
si.it.Next()
if (index.Idx >= si.scFrom) && (index.Idx <= si.scTo) {
return
}
}
si.it.Release()
return nil
}