Browse Source

libs: Let prefixIterator implements Iterator correctly (#2581)

Fixes #2577
pull/2584/head
Joon 6 years ago
committed by Alexander Simmerl
parent
commit
e7708850c0
1 changed files with 11 additions and 9 deletions
  1. +11
    -9
      libs/db/prefix_db.go

+ 11
- 9
libs/db/prefix_db.go View File

@ -265,6 +265,8 @@ func (pb prefixBatch) WriteSync() {
//---------------------------------------- //----------------------------------------
// prefixIterator // prefixIterator
var _ Iterator = (*prefixIterator)(nil)
// Strips prefix while iterating from Iterator. // Strips prefix while iterating from Iterator.
type prefixIterator struct { type prefixIterator struct {
prefix []byte prefix []byte
@ -274,9 +276,9 @@ type prefixIterator struct {
valid bool valid bool
} }
func newPrefixIterator(prefix, start, end []byte, source Iterator) prefixIterator {
func newPrefixIterator(prefix, start, end []byte, source Iterator) *prefixIterator {
if !source.Valid() || !bytes.HasPrefix(source.Key(), prefix) { if !source.Valid() || !bytes.HasPrefix(source.Key(), prefix) {
return prefixIterator{
return &prefixIterator{
prefix: prefix, prefix: prefix,
start: start, start: start,
end: end, end: end,
@ -284,7 +286,7 @@ func newPrefixIterator(prefix, start, end []byte, source Iterator) prefixIterato
valid: false, valid: false,
} }
} else { } else {
return prefixIterator{
return &prefixIterator{
prefix: prefix, prefix: prefix,
start: start, start: start,
end: end, end: end,
@ -294,15 +296,15 @@ func newPrefixIterator(prefix, start, end []byte, source Iterator) prefixIterato
} }
} }
func (itr prefixIterator) Domain() (start []byte, end []byte) {
func (itr *prefixIterator) Domain() (start []byte, end []byte) {
return itr.start, itr.end return itr.start, itr.end
} }
func (itr prefixIterator) Valid() bool {
func (itr *prefixIterator) Valid() bool {
return itr.valid && itr.source.Valid() return itr.valid && itr.source.Valid()
} }
func (itr prefixIterator) Next() {
func (itr *prefixIterator) Next() {
if !itr.valid { if !itr.valid {
panic("prefixIterator invalid, cannot call Next()") panic("prefixIterator invalid, cannot call Next()")
} }
@ -314,21 +316,21 @@ func (itr prefixIterator) Next() {
} }
} }
func (itr prefixIterator) Key() (key []byte) {
func (itr *prefixIterator) Key() (key []byte) {
if !itr.valid { if !itr.valid {
panic("prefixIterator invalid, cannot call Key()") panic("prefixIterator invalid, cannot call Key()")
} }
return stripPrefix(itr.source.Key(), itr.prefix) return stripPrefix(itr.source.Key(), itr.prefix)
} }
func (itr prefixIterator) Value() (value []byte) {
func (itr *prefixIterator) Value() (value []byte) {
if !itr.valid { if !itr.valid {
panic("prefixIterator invalid, cannot call Value()") panic("prefixIterator invalid, cannot call Value()")
} }
return itr.source.Value() return itr.source.Value()
} }
func (itr prefixIterator) Close() {
func (itr *prefixIterator) Close() {
itr.source.Close() itr.source.Close()
} }


Loading…
Cancel
Save