2018-09-24 15:57:49 +03:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// table is a wrapper around a database that prefixes each key access with a pre-
|
|
|
|
// configured string.
|
|
|
|
type table struct {
|
|
|
|
db ethdb.Database
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTable returns a database object that prefixes all keys with a given string.
|
|
|
|
func NewTable(db ethdb.Database, prefix string) ethdb.Database {
|
|
|
|
return &table{
|
|
|
|
db: db,
|
|
|
|
prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is a noop to implement the Database interface.
|
|
|
|
func (t *table) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has retrieves if a prefixed version of a key is present in the database.
|
|
|
|
func (t *table) Has(key []byte) (bool, error) {
|
|
|
|
return t.db.Has(append([]byte(t.prefix), key...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves the given prefixed key if it's present in the database.
|
|
|
|
func (t *table) Get(key []byte) ([]byte, error) {
|
|
|
|
return t.db.Get(append([]byte(t.prefix), key...))
|
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 22:59:48 +08:00
|
|
|
// HasAncient is a noop passthrough that just forwards the request to the underlying
|
|
|
|
// database.
|
|
|
|
func (t *table) HasAncient(kind string, number uint64) (bool, error) {
|
|
|
|
return t.db.HasAncient(kind, number)
|
|
|
|
}
|
|
|
|
|
2019-03-08 15:56:20 +02:00
|
|
|
// Ancient is a noop passthrough that just forwards the request to the underlying
|
|
|
|
// database.
|
|
|
|
func (t *table) Ancient(kind string, number uint64) ([]byte, error) {
|
|
|
|
return t.db.Ancient(kind, number)
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:24:27 +02:00
|
|
|
// AncientRange is a noop passthrough that just forwards the request to the underlying
|
2021-08-13 10:51:01 +02:00
|
|
|
// database.
|
2021-10-25 16:24:27 +02:00
|
|
|
func (t *table) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
|
|
|
|
return t.db.AncientRange(kind, start, count, maxBytes)
|
2021-08-13 10:51:01 +02:00
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 22:59:48 +08:00
|
|
|
// Ancients is a noop passthrough that just forwards the request to the underlying
|
|
|
|
// database.
|
|
|
|
func (t *table) Ancients() (uint64, error) {
|
|
|
|
return t.db.Ancients()
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:37:23 +08:00
|
|
|
// Tail is a noop passthrough that just forwards the request to the underlying
|
|
|
|
// database.
|
|
|
|
func (t *table) Tail() (uint64, error) {
|
|
|
|
return t.db.Tail()
|
|
|
|
}
|
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 22:07:44 +08:00
|
|
|
// AncientSize is a noop passthrough that just forwards the request to the underlying
|
|
|
|
// database.
|
|
|
|
func (t *table) AncientSize(kind string) (uint64, error) {
|
|
|
|
return t.db.AncientSize(kind)
|
|
|
|
}
|
|
|
|
|
2021-09-07 12:31:17 +02:00
|
|
|
// ModifyAncients runs an ancient write operation on the underlying database.
|
|
|
|
func (t *table) ModifyAncients(fn func(ethdb.AncientWriteOp) error) (int64, error) {
|
|
|
|
return t.db.ModifyAncients(fn)
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 22:59:48 +08:00
|
|
|
}
|
|
|
|
|
2022-05-06 19:28:42 +08:00
|
|
|
func (t *table) ReadAncients(fn func(reader ethdb.AncientReaderOp) error) (err error) {
|
2021-10-25 16:24:27 +02:00
|
|
|
return t.db.ReadAncients(fn)
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:37:23 +08:00
|
|
|
// TruncateHead is a noop passthrough that just forwards the request to the underlying
|
|
|
|
// database.
|
2023-08-01 20:17:32 +08:00
|
|
|
func (t *table) TruncateHead(items uint64) (uint64, error) {
|
2022-03-10 16:37:23 +08:00
|
|
|
return t.db.TruncateHead(items)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TruncateTail is a noop passthrough that just forwards the request to the underlying
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 22:59:48 +08:00
|
|
|
// database.
|
2023-08-01 20:17:32 +08:00
|
|
|
func (t *table) TruncateTail(items uint64) (uint64, error) {
|
2022-03-10 16:37:23 +08:00
|
|
|
return t.db.TruncateTail(items)
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 22:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync is a noop passthrough that just forwards the request to the underlying
|
|
|
|
// database.
|
|
|
|
func (t *table) Sync() error {
|
|
|
|
return t.db.Sync()
|
|
|
|
}
|
|
|
|
|
2022-03-23 20:57:32 +01:00
|
|
|
// MigrateTable processes the entries in a given table in sequence
|
|
|
|
// converting them to a new format if they're of an old format.
|
|
|
|
func (t *table) MigrateTable(kind string, convert convertLegacyFn) error {
|
|
|
|
return t.db.MigrateTable(kind, convert)
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:28:42 +08:00
|
|
|
// AncientDatadir returns the ancient datadir of the underlying database.
|
|
|
|
func (t *table) AncientDatadir() (string, error) {
|
|
|
|
return t.db.AncientDatadir()
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:57:49 +03:00
|
|
|
// Put inserts the given value into the database at a prefixed version of the
|
|
|
|
// provided key.
|
|
|
|
func (t *table) Put(key []byte, value []byte) error {
|
|
|
|
return t.db.Put(append([]byte(t.prefix), key...), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the given prefixed key from the database.
|
|
|
|
func (t *table) Delete(key []byte) error {
|
|
|
|
return t.db.Delete(append([]byte(t.prefix), key...))
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:08:53 +02:00
|
|
|
// NewIterator creates a binary-alphabetical iterator over a subset
|
|
|
|
// of database content with a particular key prefix, starting at a particular
|
|
|
|
// initial key (or after, if it does not exist).
|
|
|
|
func (t *table) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
|
|
|
|
innerPrefix := append([]byte(t.prefix), prefix...)
|
|
|
|
iter := t.db.NewIterator(innerPrefix, start)
|
2020-03-18 20:15:49 +08:00
|
|
|
return &tableIterator{
|
|
|
|
iter: iter,
|
|
|
|
prefix: t.prefix,
|
|
|
|
}
|
2018-09-24 15:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a particular internal stat of the database.
|
|
|
|
func (t *table) Stat(property string) (string, error) {
|
|
|
|
return t.db.Stat(property)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compact flattens the underlying data store for the given key range. In essence,
|
|
|
|
// deleted and overwritten versions are discarded, and the data is rearranged to
|
|
|
|
// reduce the cost of operations needed to access them.
|
|
|
|
//
|
|
|
|
// A nil start is treated as a key before all keys in the data store; a nil limit
|
|
|
|
// is treated as a key after all keys in the data store. If both is nil then it
|
|
|
|
// will compact entire data store.
|
|
|
|
func (t *table) Compact(start []byte, limit []byte) error {
|
|
|
|
// If no start was specified, use the table prefix as the first value
|
|
|
|
if start == nil {
|
|
|
|
start = []byte(t.prefix)
|
2021-05-21 16:33:59 +08:00
|
|
|
} else {
|
|
|
|
start = append([]byte(t.prefix), start...)
|
2018-09-24 15:57:49 +03:00
|
|
|
}
|
|
|
|
// If no limit was specified, use the first element not matching the prefix
|
|
|
|
// as the limit
|
|
|
|
if limit == nil {
|
|
|
|
limit = []byte(t.prefix)
|
|
|
|
for i := len(limit) - 1; i >= 0; i-- {
|
|
|
|
// Bump the current character, stopping if it doesn't overflow
|
|
|
|
limit[i]++
|
|
|
|
if limit[i] > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Character overflown, proceed to the next or nil if the last
|
|
|
|
if i == 0 {
|
|
|
|
limit = nil
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 16:33:59 +08:00
|
|
|
} else {
|
|
|
|
limit = append([]byte(t.prefix), limit...)
|
2018-09-24 15:57:49 +03:00
|
|
|
}
|
|
|
|
// Range correctly calculated based on table prefix, delegate down
|
|
|
|
return t.db.Compact(start, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBatch creates a write-only database that buffers changes to its host db
|
|
|
|
// until a final write is called, each operation prefixing all keys with the
|
|
|
|
// pre-configured string.
|
|
|
|
func (t *table) NewBatch() ethdb.Batch {
|
|
|
|
return &tableBatch{t.db.NewBatch(), t.prefix}
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:15:13 +08:00
|
|
|
// NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
|
|
|
|
func (t *table) NewBatchWithSize(size int) ethdb.Batch {
|
|
|
|
return &tableBatch{t.db.NewBatchWithSize(size), t.prefix}
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:35:22 +08:00
|
|
|
// NewSnapshot creates a database snapshot based on the current state.
|
|
|
|
// The created snapshot will not be affected by all following mutations
|
|
|
|
// happened on the database.
|
|
|
|
func (t *table) NewSnapshot() (ethdb.Snapshot, error) {
|
|
|
|
return t.db.NewSnapshot()
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:57:49 +03:00
|
|
|
// tableBatch is a wrapper around a database batch that prefixes each key access
|
|
|
|
// with a pre-configured string.
|
|
|
|
type tableBatch struct {
|
|
|
|
batch ethdb.Batch
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put inserts the given value into the batch for later committing.
|
|
|
|
func (b *tableBatch) Put(key, value []byte) error {
|
|
|
|
return b.batch.Put(append([]byte(b.prefix), key...), value)
|
|
|
|
}
|
|
|
|
|
2023-09-29 09:52:22 +02:00
|
|
|
// Delete inserts a key removal into the batch for later committing.
|
2018-09-24 15:57:49 +03:00
|
|
|
func (b *tableBatch) Delete(key []byte) error {
|
|
|
|
return b.batch.Delete(append([]byte(b.prefix), key...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValueSize retrieves the amount of data queued up for writing.
|
|
|
|
func (b *tableBatch) ValueSize() int {
|
|
|
|
return b.batch.ValueSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write flushes any accumulated data to disk.
|
|
|
|
func (b *tableBatch) Write() error {
|
|
|
|
return b.batch.Write()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the batch for reuse.
|
|
|
|
func (b *tableBatch) Reset() {
|
|
|
|
b.batch.Reset()
|
|
|
|
}
|
2019-03-26 15:48:31 +01:00
|
|
|
|
2020-03-18 20:15:49 +08:00
|
|
|
// tableReplayer is a wrapper around a batch replayer which truncates
|
|
|
|
// the added prefix.
|
|
|
|
type tableReplayer struct {
|
|
|
|
w ethdb.KeyValueWriter
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put implements the interface KeyValueWriter.
|
|
|
|
func (r *tableReplayer) Put(key []byte, value []byte) error {
|
|
|
|
trimmed := key[len(r.prefix):]
|
|
|
|
return r.w.Put(trimmed, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete implements the interface KeyValueWriter.
|
|
|
|
func (r *tableReplayer) Delete(key []byte) error {
|
|
|
|
trimmed := key[len(r.prefix):]
|
|
|
|
return r.w.Delete(trimmed)
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:48:31 +01:00
|
|
|
// Replay replays the batch contents.
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 22:59:48 +08:00
|
|
|
func (b *tableBatch) Replay(w ethdb.KeyValueWriter) error {
|
2020-03-18 20:15:49 +08:00
|
|
|
return b.batch.Replay(&tableReplayer{w: w, prefix: b.prefix})
|
|
|
|
}
|
|
|
|
|
|
|
|
// tableIterator is a wrapper around a database iterator that prefixes each key access
|
|
|
|
// with a pre-configured string.
|
|
|
|
type tableIterator struct {
|
|
|
|
iter ethdb.Iterator
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next moves the iterator to the next key/value pair. It returns whether the
|
|
|
|
// iterator is exhausted.
|
|
|
|
func (iter *tableIterator) Next() bool {
|
|
|
|
return iter.iter.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns any accumulated error. Exhausting all the key/value pairs
|
|
|
|
// is not considered to be an error.
|
|
|
|
func (iter *tableIterator) Error() error {
|
|
|
|
return iter.iter.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key returns the key of the current key/value pair, or nil if done. The caller
|
|
|
|
// should not modify the contents of the returned slice, and its contents may
|
|
|
|
// change on the next call to Next.
|
|
|
|
func (iter *tableIterator) Key() []byte {
|
|
|
|
key := iter.iter.Key()
|
|
|
|
if key == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return key[len(iter.prefix):]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value of the current key/value pair, or nil if done. The
|
|
|
|
// caller should not modify the contents of the returned slice, and its contents
|
|
|
|
// may change on the next call to Next.
|
|
|
|
func (iter *tableIterator) Value() []byte {
|
|
|
|
return iter.iter.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release releases associated resources. Release should always succeed and can
|
|
|
|
// be called multiple times without causing error.
|
|
|
|
func (iter *tableIterator) Release() {
|
|
|
|
iter.iter.Release()
|
2019-03-26 15:48:31 +01:00
|
|
|
}
|