2020-10-13 12:56:16 +00:00
|
|
|
package ethdb
|
|
|
|
|
|
|
|
import (
|
2021-01-02 19:28:37 +00:00
|
|
|
"bytes"
|
2020-10-13 12:56:16 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-05-08 08:45:40 +00:00
|
|
|
"sync"
|
2021-02-10 09:56:44 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
2020-10-13 12:56:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-05-08 08:45:40 +00:00
|
|
|
_ RwKV = &SnapshotKV{}
|
|
|
|
_ RoKV = &SnapshotKV{}
|
|
|
|
_ Tx = &snTX{}
|
|
|
|
_ BucketMigrator = &snTX{}
|
|
|
|
_ RwCursor = &snCursor{}
|
|
|
|
_ Cursor = &snCursor{}
|
2020-10-13 12:56:16 +00:00
|
|
|
)
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
type SnapshotUpdater interface {
|
|
|
|
UpdateSnapshots(buckets []string, snapshotKV RoKV, done chan struct{})
|
|
|
|
SnapshotKV(bucket string) RoKV
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:04:47 +00:00
|
|
|
type WriteDB interface {
|
|
|
|
WriteDB() RwKV
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func NewSnapshotKV() snapshotOpts {
|
|
|
|
return snapshotOpts{}
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
type snapshotData struct {
|
|
|
|
buckets []string
|
2021-05-08 08:45:40 +00:00
|
|
|
snapshot RoKV
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
type snapshotOpts struct {
|
2021-03-30 09:53:54 +00:00
|
|
|
db RwKV
|
2021-01-02 19:28:37 +00:00
|
|
|
snapshots []snapshotData
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (opts snapshotOpts) SnapshotDB(buckets []string, db RoKV) snapshotOpts {
|
2021-01-02 19:28:37 +00:00
|
|
|
opts.snapshots = append(opts.snapshots, snapshotData{
|
|
|
|
buckets: buckets,
|
|
|
|
snapshot: db,
|
|
|
|
})
|
|
|
|
return opts
|
2020-11-14 13:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (opts snapshotOpts) DB(db RwKV) snapshotOpts {
|
2021-01-02 19:28:37 +00:00
|
|
|
opts.db = db
|
|
|
|
return opts
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 09:54:29 +00:00
|
|
|
func (opts snapshotOpts) Open() *SnapshotKV {
|
2021-01-02 19:28:37 +00:00
|
|
|
snapshots := make(map[string]snapshotData)
|
|
|
|
for i, v := range opts.snapshots {
|
|
|
|
for _, bucket := range v.buckets {
|
|
|
|
snapshots[bucket] = opts.snapshots[i]
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
return &SnapshotKV{
|
2021-01-02 19:28:37 +00:00
|
|
|
snapshots: snapshots,
|
|
|
|
db: opts.db,
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
type SnapshotKV struct {
|
2021-03-30 09:53:54 +00:00
|
|
|
db RwKV
|
2021-05-08 08:45:40 +00:00
|
|
|
mtx sync.RWMutex
|
2021-01-02 19:28:37 +00:00
|
|
|
snapshots map[string]snapshotData
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) View(ctx context.Context, f func(tx Tx) error) error {
|
2021-04-03 06:26:00 +00:00
|
|
|
snTX, err := s.BeginRo(ctx)
|
2021-01-02 19:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer snTX.Rollback()
|
|
|
|
return f(snTX)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) Update(ctx context.Context, f func(tx RwTx) error) error {
|
2021-03-21 13:15:25 +00:00
|
|
|
tx, err := s.BeginRw(ctx)
|
2021-01-02 19:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
err = f(tx)
|
|
|
|
if err == nil {
|
2021-04-03 06:26:00 +00:00
|
|
|
return tx.Commit()
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
|
|
|
return err
|
2020-11-28 15:08:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) Close() {
|
2021-01-02 19:28:37 +00:00
|
|
|
s.db.Close()
|
|
|
|
for i := range s.snapshots {
|
|
|
|
s.snapshots[i].snapshot.Close()
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) UpdateSnapshots(buckets []string, snapshotKV RoKV, done chan struct{}) {
|
|
|
|
sd := snapshotData{
|
|
|
|
buckets: buckets,
|
|
|
|
snapshot: snapshotKV,
|
|
|
|
}
|
|
|
|
|
|
|
|
toClose := []RoKV{}
|
|
|
|
var (
|
|
|
|
snData snapshotData
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
s.mtx.Lock()
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
snData, ok = s.snapshots[bucket]
|
|
|
|
if ok {
|
|
|
|
toClose = append(toClose, snData.snapshot)
|
|
|
|
}
|
|
|
|
s.snapshots[bucket] = sd
|
|
|
|
}
|
|
|
|
s.mtx.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(toClose))
|
|
|
|
|
|
|
|
for i := range toClose {
|
|
|
|
i := i
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
toClose[i].Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SnapshotKV) WriteDB() RwKV {
|
|
|
|
return s.db
|
|
|
|
}
|
|
|
|
func (s *SnapshotKV) SnapshotKV(bucket string) RoKV {
|
|
|
|
return s.snapshots[bucket].snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SnapshotKV) CollectMetrics() {
|
2021-03-09 06:34:13 +00:00
|
|
|
s.db.CollectMetrics()
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) BeginRo(ctx context.Context) (Tx, error) {
|
2021-04-03 06:26:00 +00:00
|
|
|
dbTx, err := s.db.BeginRo(ctx)
|
2021-03-21 13:15:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
return &snTX{
|
2021-03-21 13:15:25 +00:00
|
|
|
dbTX: dbTx,
|
2021-05-08 08:45:40 +00:00
|
|
|
snapshots: s.copySnapshots(),
|
2021-03-21 13:15:25 +00:00
|
|
|
snTX: map[string]Tx{},
|
|
|
|
}, nil
|
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) copySnapshots() map[string]snapshotData {
|
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
mp := make(map[string]snapshotData, len(s.snapshots))
|
|
|
|
for i := range s.snapshots {
|
|
|
|
mp[i] = s.snapshots[i]
|
|
|
|
}
|
|
|
|
return mp
|
|
|
|
}
|
2021-03-21 13:15:25 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) BeginRw(ctx context.Context) (RwTx, error) {
|
2021-03-21 13:15:25 +00:00
|
|
|
dbTx, err := s.db.BeginRw(ctx)
|
2021-01-02 19:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
return &snTX{
|
2021-01-02 19:28:37 +00:00
|
|
|
dbTX: dbTx,
|
2021-05-08 08:45:40 +00:00
|
|
|
snapshots: s.copySnapshots(),
|
2021-01-02 19:28:37 +00:00
|
|
|
snTX: map[string]Tx{},
|
|
|
|
}, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *SnapshotKV) AllBuckets() dbutils.BucketsCfg {
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.db.AllBuckets()
|
2020-11-14 13:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
var ErrUnavailableSnapshot = errors.New("unavailable snapshot")
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
type snTX struct {
|
2021-01-02 19:28:37 +00:00
|
|
|
dbTX Tx
|
|
|
|
snapshots map[string]snapshotData
|
|
|
|
snTX map[string]Tx
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
type DBTX interface {
|
|
|
|
DBTX() RwTx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snTX) DBTX() RwTx {
|
|
|
|
return s.dbTX.(RwTx)
|
|
|
|
}
|
|
|
|
func (s *snTX) RwCursor(bucket string) (RwCursor, error) {
|
|
|
|
tx, err := s.getSnapshotTX(bucket)
|
|
|
|
if err != nil && !errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
//process only db buckets
|
|
|
|
if errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
return s.dbTX.(RwTx).RwCursor(bucket)
|
|
|
|
}
|
|
|
|
dbCursor, err := s.dbTX.(RwTx).RwCursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
snCursor2, err := tx.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &snCursor{
|
|
|
|
dbCursor: dbCursor,
|
|
|
|
snCursor: snCursor2,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snTX) DropBucket(bucket string) error {
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.dbTX.(BucketMigrator).DropBucket(bucket)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) CreateBucket(bucket string) error {
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.dbTX.(BucketMigrator).CreateBucket(bucket)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) ExistsBucket(bucket string) bool {
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.dbTX.(BucketMigrator).ExistsBucket(bucket)
|
2020-11-28 15:08:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) ClearBucket(bucket string) error {
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.dbTX.(BucketMigrator).ClearBucket(bucket)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) ExistingBuckets() ([]string, error) {
|
|
|
|
return s.dbTX.(BucketMigrator).ExistingBuckets()
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) Cursor(bucket string) (Cursor, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
tx, err := s.getSnapshotTX(bucket)
|
|
|
|
if err != nil && !errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
//process only db buckets
|
|
|
|
if errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
return s.dbTX.Cursor(bucket)
|
|
|
|
}
|
2021-04-02 06:36:49 +00:00
|
|
|
dbCursor, err := s.dbTX.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
snCursor2, err := tx.Cursor(bucket)
|
2021-04-02 06:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
return &snCursor{
|
2021-04-02 06:36:49 +00:00
|
|
|
dbCursor: dbCursor,
|
2021-05-08 08:45:40 +00:00
|
|
|
snCursor: snCursor2,
|
2021-04-02 06:36:49 +00:00
|
|
|
}, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) CursorDupSort(bucket string) (CursorDupSort, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
tx, err := s.getSnapshotTX(bucket)
|
|
|
|
if err != nil && !errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
//process only db buckets
|
|
|
|
if errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
return s.dbTX.CursorDupSort(bucket)
|
|
|
|
}
|
2021-04-02 06:36:49 +00:00
|
|
|
dbc, err := s.dbTX.CursorDupSort(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sncbc, err := tx.CursorDupSort(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-08 08:45:40 +00:00
|
|
|
return &snCursorDup{
|
|
|
|
snCursor{
|
2021-01-02 19:28:37 +00:00
|
|
|
dbCursor: dbc,
|
|
|
|
snCursor: sncbc,
|
|
|
|
},
|
|
|
|
dbc,
|
|
|
|
sncbc,
|
2021-04-02 06:36:49 +00:00
|
|
|
}, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) RwCursorDupSort(bucket string) (RwCursorDupSort, error) {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := s.CursorDupSort(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.(RwCursorDupSort), nil
|
2021-03-21 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) GetOne(bucket string, key []byte) (val []byte, err error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
v, err := s.dbTX.GetOne(bucket, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(v) == 0 {
|
|
|
|
snTx, innerErr := s.getSnapshotTX(bucket)
|
|
|
|
if innerErr != nil && !errors.Is(innerErr, ErrUnavailableSnapshot) {
|
|
|
|
return nil, innerErr
|
|
|
|
}
|
|
|
|
//process only db buckets
|
|
|
|
if errors.Is(innerErr, ErrUnavailableSnapshot) {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
v, err = snTx.GetOne(bucket, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if bytes.Equal(v, DeletedValue) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return v, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
return v, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-04-02 06:36:49 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) Put(bucket string, k, v []byte) error {
|
2021-04-02 06:36:49 +00:00
|
|
|
return s.dbTX.(RwTx).Put(bucket, k, v)
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) Delete(bucket string, k, v []byte) error {
|
|
|
|
return s.dbTX.(RwTx).Put(bucket, k, DeletedValue)
|
2021-04-02 06:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) CollectMetrics() {
|
2021-04-27 08:32:41 +00:00
|
|
|
s.dbTX.CollectMetrics()
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) getSnapshotTX(bucket string) (Tx, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
tx, ok := s.snTX[bucket]
|
|
|
|
if ok {
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
sn, ok := s.snapshots[bucket]
|
2020-10-13 12:56:16 +00:00
|
|
|
if !ok {
|
2021-01-02 19:28:37 +00:00
|
|
|
return nil, fmt.Errorf("%s %w", bucket, ErrUnavailableSnapshot)
|
|
|
|
}
|
|
|
|
var err error
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err = sn.snapshot.BeginRo(context.TODO())
|
2021-01-02 19:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
s.snTX[bucket] = tx
|
|
|
|
return tx, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) Has(bucket string, key []byte) (bool, error) {
|
2021-04-03 12:04:58 +00:00
|
|
|
v, err := s.dbTX.Has(bucket, key)
|
2021-01-02 19:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
if !v {
|
|
|
|
snTx, err := s.getSnapshotTX(bucket)
|
|
|
|
if err != nil && !errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
//process only db buckets
|
|
|
|
if errors.Is(err, ErrUnavailableSnapshot) {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := snTx.GetOne(bucket, key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if bytes.Equal(v, DeletedValue) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
return true, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
return v, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:49:33 +00:00
|
|
|
func (s *snTX) ForEach(bucket string, fromPrefix []byte, walker func(k, v []byte) error) error {
|
|
|
|
c, err := s.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
for k, v, err := c.Seek(fromPrefix); k != nil; k, v, err = c.Next() {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := walker(k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snTX) ForPrefix(bucket string, prefix []byte, walker func(k, v []byte) error) error {
|
|
|
|
c, err := s.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
for k, v, err := c.Seek(prefix); k != nil; k, v, err = c.Next() {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.HasPrefix(k, prefix) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := walker(k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) Commit() error {
|
2021-01-02 19:28:37 +00:00
|
|
|
for i := range s.snTX {
|
|
|
|
defer s.snTX[i].Rollback()
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-04-03 06:26:00 +00:00
|
|
|
return s.dbTX.Commit()
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) Rollback() {
|
2021-01-02 19:28:37 +00:00
|
|
|
for i := range s.snTX {
|
|
|
|
defer s.snTX[i].Rollback()
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
s.dbTX.Rollback()
|
|
|
|
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) BucketSize(name string) (uint64, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) Comparator(bucket string) dbutils.CmpFunc {
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.dbTX.Comparator(bucket)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) IncrementSequence(bucket string, amount uint64) (uint64, error) {
|
|
|
|
return s.dbTX.(RwTx).IncrementSequence(bucket, amount)
|
2021-03-20 14:12:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) ReadSequence(bucket string) (uint64, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) CHandle() unsafe.Pointer {
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.dbTX.CHandle()
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) BucketExists(bucket string) (bool, error) {
|
|
|
|
return s.dbTX.(BucketsMigrator).BucketExists(bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snTX) ClearBuckets(buckets ...string) error {
|
|
|
|
return s.dbTX.(BucketsMigrator).ClearBuckets(buckets...)
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snTX) DropBuckets(buckets ...string) error {
|
|
|
|
return s.dbTX.(BucketsMigrator).DropBuckets(buckets...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var DeletedValue = []byte{0}
|
|
|
|
|
|
|
|
type snCursor struct {
|
2021-01-02 19:28:37 +00:00
|
|
|
dbCursor Cursor
|
|
|
|
snCursor Cursor
|
|
|
|
|
|
|
|
currentKey []byte
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) First() ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
var err error
|
|
|
|
lastDBKey, lastDBVal, err := s.dbCursor.First()
|
2020-10-13 12:56:16 +00:00
|
|
|
if err != nil {
|
2021-01-02 19:28:37 +00:00
|
|
|
return nil, nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
lastSNDBKey, lastSNDBVal, err := s.snCursor.First()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
cmp, br := common.KeyCmp(lastDBKey, lastSNDBKey)
|
|
|
|
if br {
|
|
|
|
return nil, nil, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
if cmp <= 0 {
|
|
|
|
s.saveCurrent(lastDBKey)
|
|
|
|
return lastDBKey, lastDBVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
s.saveCurrent(lastSNDBKey)
|
|
|
|
return lastSNDBKey, lastSNDBVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Seek(seek []byte) ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
dbKey, dbVal, err := s.dbCursor.Seek(seek)
|
|
|
|
if err != nil && !errors.Is(err, ErrKeyNotFound) {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
sndbKey, sndbVal, err := s.snCursor.Seek(seek)
|
|
|
|
if err != nil && !errors.Is(err, ErrKeyNotFound) {
|
|
|
|
return nil, nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
if bytes.Equal(dbKey, seek) && dbVal != nil {
|
|
|
|
return dbKey, dbVal, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
if bytes.Equal(sndbKey, seek) && sndbVal != nil {
|
|
|
|
return sndbKey, sndbVal, err
|
|
|
|
}
|
|
|
|
cmp, _ := common.KeyCmp(dbKey, sndbKey)
|
|
|
|
if cmp <= 0 {
|
|
|
|
s.saveCurrent(dbKey)
|
|
|
|
return dbKey, dbVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
s.saveCurrent(sndbKey)
|
|
|
|
return sndbKey, sndbVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) SeekExact(key []byte) ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
k, v, err := s.dbCursor.SeekExact(key)
|
2020-10-13 12:56:16 +00:00
|
|
|
if err != nil {
|
2021-01-02 19:28:37 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if bytes.Equal(v, DeletedValue) {
|
|
|
|
return nil, nil, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
if v == nil {
|
|
|
|
k, v, err = s.snCursor.SeekExact(key)
|
|
|
|
s.saveCurrent(k)
|
|
|
|
return k, v, err
|
|
|
|
}
|
|
|
|
s.saveCurrent(k)
|
|
|
|
return k, v, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) iteration(dbNextElement func() ([]byte, []byte, error), sndbNextElement func() ([]byte, []byte, error), cmpFunc func(kdb, ksndb []byte) (int, bool)) ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
var err error
|
2021-05-18 18:32:04 +00:00
|
|
|
var noDBNext, noSnDBNext bool
|
2021-01-02 19:28:37 +00:00
|
|
|
//current returns error on empty bucket
|
|
|
|
lastDBKey, lastDBVal, err := s.dbCursor.Current()
|
2020-10-13 12:56:16 +00:00
|
|
|
if err != nil {
|
2021-01-02 19:28:37 +00:00
|
|
|
var innerErr error
|
|
|
|
lastDBKey, lastDBVal, innerErr = dbNextElement()
|
|
|
|
if innerErr != nil {
|
|
|
|
return nil, nil, fmt.Errorf("get current from db %w inner %v", err, innerErr)
|
|
|
|
}
|
2021-05-18 18:32:04 +00:00
|
|
|
noDBNext = true
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
lastSNDBKey, lastSNDBVal, err := s.snCursor.Current()
|
|
|
|
if err != nil {
|
2021-05-18 18:32:04 +00:00
|
|
|
var innerErr error
|
|
|
|
lastSNDBKey, lastSNDBVal, innerErr = sndbNextElement()
|
|
|
|
if innerErr != nil {
|
|
|
|
return nil, nil, fmt.Errorf("get current from snapshot %w inner %v", err, innerErr)
|
|
|
|
}
|
|
|
|
noSnDBNext = true
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
cmp, br := cmpFunc(lastDBKey, lastSNDBKey)
|
|
|
|
if br {
|
|
|
|
return nil, nil, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
//todo Seek fastpath
|
|
|
|
if cmp > 0 {
|
2021-05-18 18:32:04 +00:00
|
|
|
if !noSnDBNext {
|
|
|
|
lastSNDBKey, lastSNDBVal, err = sndbNextElement()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentKeyCmp, _ := common.KeyCmp(s.currentKey, lastDBKey); len(lastSNDBKey) == 0 && currentKeyCmp >= 0 && len(s.currentKey) > 0 {
|
|
|
|
lastDBKey, lastDBVal, err = dbNextElement()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//current receives last acceptable key. If it is empty
|
|
|
|
if cmp < 0 {
|
2021-05-18 18:32:04 +00:00
|
|
|
if !noDBNext {
|
|
|
|
lastDBKey, lastDBVal, err = dbNextElement()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if currentKeyCmp, _ := common.KeyCmp(s.currentKey, lastSNDBKey); len(lastDBKey) == 0 && currentKeyCmp >= 0 && len(s.currentKey) > 0 {
|
|
|
|
lastSNDBKey, lastSNDBVal, err = sndbNextElement()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if cmp == 0 {
|
2021-05-18 18:32:04 +00:00
|
|
|
if !noDBNext {
|
|
|
|
lastDBKey, lastDBVal, err = dbNextElement()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
2021-05-18 18:32:04 +00:00
|
|
|
if !noSnDBNext {
|
|
|
|
lastSNDBKey, lastSNDBVal, err = sndbNextElement()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmp, br = cmpFunc(lastDBKey, lastSNDBKey)
|
|
|
|
if br {
|
|
|
|
return nil, nil, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
if cmp <= 0 {
|
|
|
|
return lastDBKey, lastDBVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
return lastSNDBKey, lastSNDBVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Next() ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
k, v, err := s.iteration(s.dbCursor.Next, s.snCursor.Next, common.KeyCmp) //f(s.dbCursor.Next, s.snCursor.Next)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
for bytes.Equal(v, DeletedValue) {
|
|
|
|
k, v, err = s.iteration(s.dbCursor.Next, s.snCursor.Next, common.KeyCmp) // f(s.dbCursor.Next, s.snCursor.Next)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
|
|
|
s.saveCurrent(k)
|
|
|
|
return k, v, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Prev() ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
k, v, err := s.iteration(s.dbCursor.Prev, s.snCursor.Prev, func(kdb, ksndb []byte) (int, bool) {
|
|
|
|
cmp, br := KeyCmpBackward(kdb, ksndb)
|
|
|
|
return -1 * cmp, br
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
for cmp, _ := KeyCmpBackward(k, s.currentKey); bytes.Equal(v, DeletedValue) || cmp >= 0; cmp, _ = KeyCmpBackward(k, s.currentKey) {
|
|
|
|
k, v, err = s.iteration(s.dbCursor.Prev, s.snCursor.Prev, func(kdb, ksndb []byte) (int, bool) {
|
|
|
|
cmp, br := KeyCmpBackward(kdb, ksndb)
|
|
|
|
return -1 * cmp, br
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
s.saveCurrent(k)
|
|
|
|
return k, v, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Last() ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
var err error
|
|
|
|
lastSNDBKey, lastSNDBVal, err := s.snCursor.Last()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
lastDBKey, lastDBVal, err := s.dbCursor.Last()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
cmp, br := KeyCmpBackward(lastDBKey, lastSNDBKey)
|
|
|
|
if br {
|
|
|
|
return nil, nil, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
|
|
|
|
if cmp >= 0 {
|
|
|
|
s.saveCurrent(lastDBKey)
|
|
|
|
return lastDBKey, lastDBVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
s.saveCurrent(lastSNDBKey)
|
|
|
|
return lastSNDBKey, lastSNDBVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Current() ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
k, v, err := s.dbCursor.Current()
|
|
|
|
if bytes.Equal(k, s.currentKey) {
|
|
|
|
return k, v, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
return s.snCursor.Current()
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Put(k, v []byte) error {
|
2021-03-21 13:15:25 +00:00
|
|
|
return s.dbCursor.(RwCursor).Put(k, v)
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Append(k []byte, v []byte) error {
|
2021-03-21 13:15:25 +00:00
|
|
|
return s.dbCursor.(RwCursor).Append(k, v)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Delete(k, v []byte) error {
|
2021-03-21 13:15:25 +00:00
|
|
|
return s.dbCursor.(RwCursor).Put(k, DeletedValue)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) DeleteCurrent() error {
|
2020-10-13 12:56:16 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Count() (uint64, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) Close() {
|
2021-01-02 19:28:37 +00:00
|
|
|
s.dbCursor.Close()
|
|
|
|
s.snCursor.Close()
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
type snCursorDup struct {
|
|
|
|
snCursor
|
2021-01-02 19:28:37 +00:00
|
|
|
dbCursorDup CursorDupSort
|
|
|
|
sndbCursorDup CursorDupSort
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) SeekBothExact(key, value []byte) ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
k, v, err := c.dbCursorDup.SeekBothExact(key, value)
|
2020-10-13 12:56:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
if v == nil {
|
|
|
|
k, v, err = c.sndbCursorDup.SeekBothExact(key, value)
|
|
|
|
c.saveCurrent(k)
|
|
|
|
return k, v, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
c.saveCurrent(k)
|
|
|
|
return k, v, err
|
2020-10-13 12:56:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) SeekBothRange(key, value []byte) ([]byte, error) {
|
2021-03-19 07:45:01 +00:00
|
|
|
dbVal, err := c.dbCursorDup.SeekBothRange(key, value)
|
2020-10-13 12:56:16 +00:00
|
|
|
if err != nil {
|
2021-03-19 07:45:01 +00:00
|
|
|
return nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-03-19 07:45:01 +00:00
|
|
|
snDBVal, err := c.sndbCursorDup.SeekBothRange(key, value)
|
2020-10-13 12:56:16 +00:00
|
|
|
if err != nil {
|
2021-03-19 07:45:01 +00:00
|
|
|
return nil, err
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 07:45:01 +00:00
|
|
|
if dbVal == nil {
|
|
|
|
c.saveCurrent(key)
|
|
|
|
return dbVal, nil
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
2021-03-19 07:45:01 +00:00
|
|
|
|
|
|
|
return snDBVal, nil
|
2021-01-02 19:28:37 +00:00
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) FirstDup() ([]byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) NextDup() ([]byte, []byte, error) {
|
2020-10-13 12:56:16 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) NextNoDup() ([]byte, []byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) LastDup() ([]byte, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) CountDuplicates() (uint64, error) {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) DeleteCurrentDuplicates() error {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (c *snCursorDup) AppendDup(key, value []byte) error {
|
2021-01-02 19:28:37 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
func (s *snCursor) saveCurrent(k []byte) {
|
2021-01-02 19:28:37 +00:00
|
|
|
if k != nil {
|
|
|
|
s.currentKey = common.CopyBytes(k)
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
|
|
|
|
func KeyCmpBackward(key1, key2 []byte) (int, bool) {
|
|
|
|
switch {
|
|
|
|
case len(key1) == 0 && len(key2) == 0:
|
|
|
|
return 0, true
|
|
|
|
case len(key1) == 0 && len(key2) != 0:
|
|
|
|
return -1, false
|
|
|
|
case len(key1) != 0 && len(key2) == 0:
|
|
|
|
return 1, false
|
|
|
|
default:
|
|
|
|
return bytes.Compare(key1, key2), false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type KvData struct {
|
|
|
|
K []byte
|
|
|
|
V []byte
|
|
|
|
}
|
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
func GenStateData(data []KvData) (RwKV, error) {
|
2021-05-20 11:12:07 +00:00
|
|
|
snapshot := NewMDBX().WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
|
2021-01-02 19:28:37 +00:00
|
|
|
return dbutils.BucketsCfg{
|
|
|
|
dbutils.PlainStateBucket: dbutils.BucketConfigItem{},
|
|
|
|
}
|
|
|
|
}).InMem().MustOpen()
|
|
|
|
|
2021-03-21 13:15:25 +00:00
|
|
|
err := snapshot.Update(context.Background(), func(tx RwTx) error {
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := tx.RwCursor(dbutils.PlainStateBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-02 19:28:37 +00:00
|
|
|
for i := range data {
|
|
|
|
innerErr := c.Put(data[i].K, data[i].V)
|
|
|
|
if innerErr != nil {
|
|
|
|
return innerErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return snapshot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//type cursorSnapshotDupsort struct {
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) First() ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Seek(seek []byte) ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) SeekExact(key []byte) ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Next() ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Prev() ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Last() ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Current() ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Put(k, v []byte) error {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Append(k []byte, v []byte) error {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Delete(k, v []byte) error {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) DeleteCurrent() error {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Count() (uint64, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) Close() {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//
|
|
|
|
////dupsort
|
|
|
|
//func (c *cursorSnapshotDupsort) SeekBothExact(key, value []byte) ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) SeekBothRange(key, value []byte) ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) FirstDup() ([]byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) NextDup() ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) NextNoDup() ([]byte, []byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) LastDup(k []byte) ([]byte, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) CountDuplicates() (uint64, error) {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) DeleteCurrentDuplicates() error {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (c *cursorSnapshotDupsort) AppendDup(key, value []byte) error {
|
|
|
|
// panic("implement me")
|
|
|
|
//}
|