2021-01-12 10:39:31 +00:00
|
|
|
package storage
|
|
|
|
|
2021-10-04 14:01:42 +00:00
|
|
|
import (
|
2021-10-06 12:54:44 +00:00
|
|
|
"context"
|
|
|
|
|
2021-10-04 14:01:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
2021-01-12 10:39:31 +00:00
|
|
|
|
|
|
|
// Storage iterator options.
|
|
|
|
const (
|
|
|
|
FindDefault = 0
|
|
|
|
FindKeysOnly = 1 << 0
|
|
|
|
FindRemovePrefix = 1 << 1
|
|
|
|
FindValuesOnly = 1 << 2
|
2021-01-12 12:09:05 +00:00
|
|
|
FindDeserialize = 1 << 3
|
2021-01-12 12:32:27 +00:00
|
|
|
FindPick0 = 1 << 4
|
|
|
|
FindPick1 = 1 << 5
|
2021-01-12 10:39:31 +00:00
|
|
|
|
2021-01-12 12:09:05 +00:00
|
|
|
FindAll = FindDefault | FindKeysOnly | FindRemovePrefix | FindValuesOnly |
|
2021-01-12 12:32:27 +00:00
|
|
|
FindDeserialize | FindPick0 | FindPick1
|
2021-01-12 10:39:31 +00:00
|
|
|
)
|
|
|
|
|
2021-01-15 18:06:35 +00:00
|
|
|
// Iterator is an iterator state representation.
|
2021-01-12 10:39:31 +00:00
|
|
|
type Iterator struct {
|
2021-10-04 14:01:42 +00:00
|
|
|
seekCh chan storage.KeyValue
|
2021-10-06 12:54:44 +00:00
|
|
|
cancel context.CancelFunc
|
2021-10-04 14:01:42 +00:00
|
|
|
curr storage.KeyValue
|
|
|
|
next bool
|
|
|
|
opts int64
|
|
|
|
prefix []byte
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:01:42 +00:00
|
|
|
// NewIterator creates a new Iterator with given options for a given channel of store.Seek results.
|
2021-10-06 12:54:44 +00:00
|
|
|
func NewIterator(seekCh chan storage.KeyValue, cancel context.CancelFunc, prefix []byte, opts int64) *Iterator {
|
2021-01-12 10:39:31 +00:00
|
|
|
return &Iterator{
|
2021-10-04 14:01:42 +00:00
|
|
|
seekCh: seekCh,
|
2021-10-06 12:54:44 +00:00
|
|
|
cancel: cancel,
|
2021-10-04 14:01:42 +00:00
|
|
|
opts: opts,
|
|
|
|
prefix: slice.Copy(prefix),
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:06:35 +00:00
|
|
|
// Next advances the iterator and returns true if Value can be called at the
|
|
|
|
// current position.
|
2021-01-12 10:39:31 +00:00
|
|
|
func (s *Iterator) Next() bool {
|
2021-10-04 14:01:42 +00:00
|
|
|
s.curr, s.next = <-s.seekCh
|
|
|
|
return s.next
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 18:06:35 +00:00
|
|
|
// Value returns current iterators value (exact type depends on options this
|
|
|
|
// iterator was created with).
|
2021-01-12 10:39:31 +00:00
|
|
|
func (s *Iterator) Value() stackitem.Item {
|
2021-10-04 14:01:42 +00:00
|
|
|
if !s.next {
|
|
|
|
panic("iterator index out of range")
|
|
|
|
}
|
|
|
|
key := s.curr.Key
|
|
|
|
if s.opts&FindRemovePrefix == 0 {
|
|
|
|
key = append(s.prefix, key...)
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
if s.opts&FindKeysOnly != 0 {
|
|
|
|
return stackitem.NewByteArray(key)
|
|
|
|
}
|
2021-10-04 14:01:42 +00:00
|
|
|
value := stackitem.Item(stackitem.NewByteArray(s.curr.Value))
|
2021-01-12 12:09:05 +00:00
|
|
|
if s.opts&FindDeserialize != 0 {
|
2021-10-04 14:01:42 +00:00
|
|
|
bs := s.curr.Value
|
2021-01-12 12:09:05 +00:00
|
|
|
var err error
|
2021-07-06 16:56:23 +00:00
|
|
|
value, err = stackitem.Deserialize(bs)
|
2021-01-12 12:09:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 12:32:27 +00:00
|
|
|
if s.opts&FindPick0 != 0 {
|
|
|
|
value = value.Value().([]stackitem.Item)[0]
|
|
|
|
} else if s.opts&FindPick1 != 0 {
|
|
|
|
value = value.Value().([]stackitem.Item)[1]
|
|
|
|
}
|
2021-01-12 10:39:31 +00:00
|
|
|
if s.opts&FindValuesOnly != 0 {
|
2021-01-12 12:09:05 +00:00
|
|
|
return value
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(key),
|
2021-01-12 12:09:05 +00:00
|
|
|
value,
|
2021-01-12 10:39:31 +00:00
|
|
|
})
|
|
|
|
}
|
2021-10-06 12:54:44 +00:00
|
|
|
|
|
|
|
// Close releases resources occupied by the Iterator.
|
|
|
|
// TODO: call this method on program unloading.
|
|
|
|
func (s *Iterator) Close() {
|
|
|
|
s.cancel()
|
|
|
|
}
|