2018-09-24 12:57:49 +00:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-09-14 07:35:57 +00:00
|
|
|
package ethdb
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2019-11-11 16:02:37 +00:00
|
|
|
import (
|
2020-09-28 17:18:36 +00:00
|
|
|
"context"
|
2019-11-11 16:02:37 +00:00
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2020-02-06 10:53:09 +00:00
|
|
|
// DESCRIBED: For info on database buckets see docs/programmers_guide/db_walkthrough.MD
|
2019-11-13 16:43:26 +00:00
|
|
|
|
2019-11-11 16:02:37 +00:00
|
|
|
// ErrKeyNotFound is returned when key isn't found in the database.
|
|
|
|
var ErrKeyNotFound = errors.New("db: key not found")
|
|
|
|
|
2019-11-05 12:28:36 +00:00
|
|
|
// Putter wraps the database write operations.
|
2019-05-27 13:51:49 +00:00
|
|
|
type Putter interface {
|
2019-11-05 12:28:36 +00:00
|
|
|
// Put inserts or updates a single entry.
|
2020-08-10 23:55:32 +00:00
|
|
|
Put(bucket string, key, value []byte) error
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2018-09-24 12:57:49 +00:00
|
|
|
|
2019-11-05 12:28:36 +00:00
|
|
|
// Getter wraps the database read operations.
|
2019-05-27 13:51:49 +00:00
|
|
|
type Getter interface {
|
2019-11-11 16:02:37 +00:00
|
|
|
// Get returns the value for a given key if it's present.
|
2020-08-10 23:55:32 +00:00
|
|
|
Get(bucket string, key []byte) ([]byte, error)
|
2019-11-07 14:55:21 +00:00
|
|
|
|
2020-04-20 10:35:33 +00:00
|
|
|
// Get returns prober chunk of index or error if index is not created.
|
|
|
|
// Key must contain 8byte inverted block number in the end.
|
2020-08-10 23:55:32 +00:00
|
|
|
GetIndexChunk(bucket string, key []byte, timestamp uint64) ([]byte, error)
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2019-11-12 10:40:46 +00:00
|
|
|
// Has indicates whether a key exists in the database.
|
2020-08-10 23:55:32 +00:00
|
|
|
Has(bucket string, key []byte) (bool, error)
|
2019-11-12 10:40:46 +00:00
|
|
|
|
2019-11-13 16:43:26 +00: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-10 23:55:32 +00:00
|
|
|
Walk(bucket string, startkey []byte, fixedbits int, walker func(k, v []byte) (bool, error)) error
|
2019-11-14 12:00:38 +00:00
|
|
|
|
2019-11-21 15:12:38 +00:00
|
|
|
// MultiWalk is similar to multiple Walk calls folded into one.
|
2020-08-10 23:55:32 +00:00
|
|
|
MultiWalk(bucket string, startkeys [][]byte, fixedbits []int, walker func(int, []byte, []byte) error) error
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2018-09-24 12:57:49 +00:00
|
|
|
|
2020-04-26 16:02:38 +00:00
|
|
|
type GetterPutter interface {
|
|
|
|
Getter
|
|
|
|
Putter
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:28:36 +00:00
|
|
|
// Deleter wraps the database delete operations.
|
2019-05-27 13:51:49 +00:00
|
|
|
type Deleter interface {
|
2019-11-05 12:28:36 +00:00
|
|
|
// Delete removes a single entry.
|
2020-10-29 13:19:31 +00:00
|
|
|
Delete(bucket string, k, v []byte) error
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2018-09-24 12:57:49 +00:00
|
|
|
|
2020-08-15 18:21:30 +00:00
|
|
|
type Closer interface {
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// Database wraps all database operations. All methods are safe for concurrent use.
|
|
|
|
type Database interface {
|
|
|
|
Getter
|
|
|
|
Putter
|
|
|
|
Deleter
|
2020-08-15 18:21:30 +00:00
|
|
|
Closer
|
2019-11-21 15:12:38 +00:00
|
|
|
|
|
|
|
// MultiPut inserts or updates multiple entries.
|
|
|
|
// Entries are passed as an array:
|
|
|
|
// bucket0, key0, val0, bucket1, key1, val1, ...
|
2019-05-27 13:51:49 +00:00
|
|
|
MultiPut(tuples ...[]byte) (uint64, error)
|
2020-08-24 11:07:59 +00:00
|
|
|
|
|
|
|
// NewBatch - starts in-mem batch
|
|
|
|
//
|
|
|
|
// Common pattern:
|
|
|
|
//
|
|
|
|
// batch := db.NewBatch()
|
|
|
|
// defer batch.Rollback()
|
|
|
|
// ... some calculations on `batch`
|
|
|
|
// batch.Commit()
|
|
|
|
//
|
2020-10-24 06:55:43 +00:00
|
|
|
NewBatch() DbWithPendingMutations //
|
2020-10-25 08:38:55 +00:00
|
|
|
Begin(ctx context.Context, flags TxFlags) (DbWithPendingMutations, error) // starts db transaction
|
2020-08-12 03:49:52 +00:00
|
|
|
Last(bucket string) ([]byte, []byte, error)
|
2019-11-21 15:12:38 +00:00
|
|
|
|
|
|
|
// IdealBatchSize defines the size of the data batches should ideally add in one write.
|
|
|
|
IdealBatchSize() int
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
Keys() ([][]byte, error)
|
2019-11-21 15:12:38 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// [TURBO-GETH] Freezer support (minimum amount that is actually used)
|
|
|
|
// FIXME: implement support if needed
|
|
|
|
Ancients() (uint64, error)
|
|
|
|
TruncateAncients(items uint64) error
|
2020-09-01 06:48:25 +00:00
|
|
|
Append(bucket string, key, value []byte) error
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
2018-09-24 12:57:49 +00:00
|
|
|
|
2019-11-06 10:24:48 +00:00
|
|
|
// MinDatabase is a minimalistic version of the Database interface.
|
|
|
|
type MinDatabase interface {
|
2020-08-10 23:55:32 +00:00
|
|
|
Get(bucket string, key []byte) ([]byte, error)
|
|
|
|
Put(bucket string, key, value []byte) error
|
2020-10-29 13:19:31 +00:00
|
|
|
Delete(bucket string, k, v []byte) error
|
2019-11-05 12:28:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:33:01 +00: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 13:51:49 +00:00
|
|
|
Database
|
2020-08-24 11:07:59 +00: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()
|
|
|
|
//
|
2019-05-27 13:51:49 +00:00
|
|
|
Commit() (uint64, error)
|
2020-08-24 11:07:59 +00: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-28 17:18:36 +00:00
|
|
|
CommitAndBegin(ctx context.Context) error
|
2019-05-27 13:51:49 +00:00
|
|
|
Rollback()
|
|
|
|
BatchSize() int
|
2020-09-28 17:18:36 +00:00
|
|
|
|
|
|
|
Reserve(bucket string, key []byte, i int) ([]byte, error)
|
2018-09-24 12:57:49 +00:00
|
|
|
}
|
2019-11-21 15:12:38 +00:00
|
|
|
|
2020-03-24 02:12:55 +00:00
|
|
|
type HasKV interface {
|
2020-06-05 09:25:33 +00:00
|
|
|
KV() KV
|
2020-10-28 03:18:10 +00:00
|
|
|
SetKV(kv KV)
|
2020-04-04 07:18:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 06:02:10 +00:00
|
|
|
type HasTx interface {
|
|
|
|
Tx() Tx
|
|
|
|
}
|
|
|
|
|
2020-03-20 10:06:14 +00:00
|
|
|
type HasNetInterface interface {
|
|
|
|
DB() Database
|
|
|
|
}
|
|
|
|
|
2020-09-08 19:39:43 +00:00
|
|
|
type BucketsMigrator interface {
|
2020-08-10 23:55:32 +00:00
|
|
|
BucketExists(bucket string) (bool, error) // makes them empty
|
2020-10-25 08:38:55 +00:00
|
|
|
ClearBuckets(buckets ...string) error // makes them empty
|
|
|
|
DropBuckets(buckets ...string) error // drops them, use of them after drop will panic
|
2020-07-17 05:52:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:12:38 +00:00
|
|
|
var errNotSupported = errors.New("not supported")
|