2018-09-24 15:57:49 +03:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
2015-07-22 18:48:40 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 02:54:22 +02:00
|
|
|
//
|
2015-07-23 18:35:11 +02:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 02:54:22 +02:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 18:48:40 +02:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 02:54:22 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 18:48:40 +02:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 02:54:22 +02:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 18:48:40 +02:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 02:54:22 +02:00
|
|
|
|
2015-09-14 09:35:57 +02:00
|
|
|
package ethdb
|
2014-02-14 23:56:09 +01:00
|
|
|
|
2019-11-11 17:02:37 +01:00
|
|
|
import (
|
2020-09-29 00:18:36 +07:00
|
|
|
"context"
|
2019-11-11 17:02:37 +01:00
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2020-02-06 11:53:09 +01:00
|
|
|
// DESCRIBED: For info on database buckets see docs/programmers_guide/db_walkthrough.MD
|
2019-11-13 17:43:26 +01:00
|
|
|
|
2019-11-11 17:02:37 +01:00
|
|
|
// ErrKeyNotFound is returned when key isn't found in the database.
|
|
|
|
var ErrKeyNotFound = errors.New("db: key not found")
|
|
|
|
|
2021-04-03 09:26:00 +03:00
|
|
|
type TxFlags uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
RW TxFlags = 0x00 // default
|
|
|
|
RO TxFlags = 0x02
|
|
|
|
)
|
2018-09-24 15:57:49 +03:00
|
|
|
|
2021-04-05 16:04:58 +03:00
|
|
|
type DatabaseReader interface {
|
|
|
|
KVGetter
|
2021-04-03 15:04:58 +03:00
|
|
|
|
2019-11-11 17:02:37 +01:00
|
|
|
// Get returns the value for a given key if it's present.
|
2020-08-11 06:55:32 +07:00
|
|
|
Get(bucket string, key []byte) ([]byte, error)
|
2021-04-05 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getter wraps the database read operations.
|
|
|
|
type Getter interface {
|
|
|
|
DatabaseReader
|
2019-11-07 15:55:21 +01:00
|
|
|
|
2019-11-13 17:43:26 +01:00
|
|
|
// Walk iterates over entries with keys greater or equal to startkey.
|
|
|
|
// Only the keys whose first fixedbits match those of startkey are iterated over.
|
|
|
|
// walker is called for each eligible entry.
|
|
|
|
// If walker returns false or an error, the walk stops.
|
2020-08-11 06:55:32 +07:00
|
|
|
Walk(bucket string, startkey []byte, fixedbits int, walker func(k, v []byte) (bool, error)) error
|
2019-05-27 14:51:49 +01:00
|
|
|
}
|
2018-09-24 15:57:49 +03:00
|
|
|
|
2021-03-30 12:53:54 +03:00
|
|
|
type GetterTx interface {
|
|
|
|
Getter
|
|
|
|
|
|
|
|
Rollback()
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetterBeginner interface {
|
|
|
|
Getter
|
|
|
|
|
2021-04-03 09:26:00 +03:00
|
|
|
BeginGetter(ctx context.Context) (GetterTx, error)
|
2021-03-30 12:53:54 +03:00
|
|
|
}
|
|
|
|
|
2020-04-26 19:02:38 +03:00
|
|
|
type GetterPutter interface {
|
|
|
|
Getter
|
|
|
|
Putter
|
|
|
|
}
|
|
|
|
|
2019-05-27 14:51:49 +01:00
|
|
|
// Database wraps all database operations. All methods are safe for concurrent use.
|
|
|
|
type Database interface {
|
2021-03-30 12:53:54 +03:00
|
|
|
GetterBeginner
|
2019-05-27 14:51:49 +01:00
|
|
|
Putter
|
|
|
|
Deleter
|
2020-08-15 20:21:30 +02:00
|
|
|
Closer
|
2019-11-21 16:12:38 +01:00
|
|
|
|
|
|
|
// MultiPut inserts or updates multiple entries.
|
|
|
|
// Entries are passed as an array:
|
|
|
|
// bucket0, key0, val0, bucket1, key1, val1, ...
|
2019-05-27 14:51:49 +01:00
|
|
|
MultiPut(tuples ...[]byte) (uint64, error)
|
2020-08-24 18:07:59 +07:00
|
|
|
|
|
|
|
// NewBatch - starts in-mem batch
|
|
|
|
//
|
|
|
|
// Common pattern:
|
|
|
|
//
|
|
|
|
// batch := db.NewBatch()
|
|
|
|
// defer batch.Rollback()
|
|
|
|
// ... some calculations on `batch`
|
|
|
|
// batch.Commit()
|
|
|
|
//
|
2020-10-24 13:55:43 +07:00
|
|
|
NewBatch() DbWithPendingMutations //
|
2020-10-25 15:38:55 +07:00
|
|
|
Begin(ctx context.Context, flags TxFlags) (DbWithPendingMutations, error) // starts db transaction
|
2020-08-12 10:49:52 +07:00
|
|
|
Last(bucket string) ([]byte, []byte, error)
|
2019-11-21 16:12:38 +01:00
|
|
|
|
2019-05-27 14:51:49 +01:00
|
|
|
Keys() ([][]byte, error)
|
2019-11-21 16:12:38 +01:00
|
|
|
|
2020-09-01 07:48:25 +01:00
|
|
|
Append(bucket string, key, value []byte) error
|
2020-11-28 21:24:47 +07:00
|
|
|
AppendDup(bucket string, key, value []byte) error
|
2021-03-20 17:12:54 +03:00
|
|
|
IncrementSequence(bucket string, amount uint64) (uint64, error)
|
|
|
|
ReadSequence(bucket string) (uint64, error)
|
2014-02-14 23:56:09 +01:00
|
|
|
}
|
2018-09-24 15:57:49 +03:00
|
|
|
|
2019-11-06 11:24:48 +01:00
|
|
|
// MinDatabase is a minimalistic version of the Database interface.
|
|
|
|
type MinDatabase interface {
|
2020-08-11 06:55:32 +07:00
|
|
|
Get(bucket string, key []byte) ([]byte, error)
|
|
|
|
Put(bucket string, key, value []byte) error
|
2020-10-29 20:19:31 +07:00
|
|
|
Delete(bucket string, k, v []byte) error
|
2019-11-05 13:28:36 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 18:33:01 +01:00
|
|
|
// DbWithPendingMutations is an extended version of the Database,
|
|
|
|
// where all changes are first made in memory.
|
|
|
|
// Later they can either be committed to the database or rolled back.
|
|
|
|
type DbWithPendingMutations interface {
|
2019-05-27 14:51:49 +01:00
|
|
|
Database
|
2020-08-24 18:07:59 +07:00
|
|
|
|
|
|
|
// Commit - commits transaction (or flush data into underlying db object in case of `mutation`)
|
|
|
|
//
|
|
|
|
// Common pattern:
|
|
|
|
//
|
|
|
|
// tx := db.Begin()
|
|
|
|
// defer tx.Rollback()
|
|
|
|
// ... some calculations on `tx`
|
|
|
|
// tx.Commit()
|
|
|
|
//
|
2021-03-22 19:41:52 +07:00
|
|
|
Commit() error
|
2020-08-24 18:07:59 +07:00
|
|
|
|
|
|
|
// CommitAndBegin - commits and starts new transaction inside same db object.
|
|
|
|
// useful for periodical commits implementation.
|
|
|
|
//
|
|
|
|
// Common pattern:
|
|
|
|
//
|
|
|
|
// tx := db.Begin()
|
|
|
|
// defer tx.Rollback()
|
|
|
|
// for {
|
|
|
|
// ... some calculations on `tx`
|
|
|
|
// tx.CommitAndBegin()
|
|
|
|
// // defer here - is not useful, because 'tx' object is reused and first `defer` will work perfectly
|
|
|
|
// }
|
|
|
|
// tx.Commit()
|
|
|
|
//
|
2020-09-29 00:18:36 +07:00
|
|
|
CommitAndBegin(ctx context.Context) error
|
2021-03-23 16:00:07 +07:00
|
|
|
RollbackAndBegin(ctx context.Context) error
|
2019-05-27 14:51:49 +01:00
|
|
|
Rollback()
|
|
|
|
BatchSize() int
|
2018-09-24 15:57:49 +03:00
|
|
|
}
|
2019-11-21 16:12:38 +01:00
|
|
|
|
2021-03-30 12:53:54 +03:00
|
|
|
type HasRwKV interface {
|
|
|
|
RwKV() RwKV
|
|
|
|
SetRwKV(kv RwKV)
|
2020-04-04 14:18:10 +07:00
|
|
|
}
|
|
|
|
|
2020-08-26 13:02:10 +07:00
|
|
|
type HasTx interface {
|
|
|
|
Tx() Tx
|
|
|
|
}
|
|
|
|
|
2020-03-20 17:06:14 +07:00
|
|
|
type HasNetInterface interface {
|
|
|
|
DB() Database
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:39:43 +07:00
|
|
|
type BucketsMigrator interface {
|
2020-08-11 06:55:32 +07:00
|
|
|
BucketExists(bucket string) (bool, error) // makes them empty
|
2020-10-25 15:38:55 +07:00
|
|
|
ClearBuckets(buckets ...string) error // makes them empty
|
|
|
|
DropBuckets(buckets ...string) error // drops them, use of them after drop will panic
|
2020-07-17 12:52:09 +07:00
|
|
|
}
|
|
|
|
|
2021-04-05 16:04:58 +03:00
|
|
|
func getOneWrapper(dat []byte, err error) ([]byte, error) {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if dat == nil {
|
|
|
|
return nil, ErrKeyNotFound
|
|
|
|
}
|
|
|
|
return dat, nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:12:38 +01:00
|
|
|
var errNotSupported = errors.New("not supported")
|