mirror of
https://github.com/luxfi/coreth.git
synced 2026-10-30 17:00:51 +00:00
96 lines
3.2 KiB
Go
96 lines
3.2 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package database
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/luxfi/database"
|
|
"github.com/luxfi/geth/ethdb"
|
|
)
|
|
|
|
var (
|
|
_ ethdb.KeyValueStore = (*ethDbWrapper)(nil)
|
|
|
|
ErrSnapshotNotSupported = errors.New("snapshot is not supported")
|
|
)
|
|
|
|
// ethDbWrapper implements ethdb.Database
|
|
type ethDbWrapper struct{ database.Database }
|
|
|
|
func WrapDatabase(db database.Database) ethdb.KeyValueStore { return ethDbWrapper{db} }
|
|
|
|
// Stat implements ethdb.Database
|
|
func (db ethDbWrapper) Stat() (string, error) { return "", database.ErrNotFound }
|
|
|
|
// DeleteRange implements ethdb.KeyValueStore
|
|
func (db ethDbWrapper) DeleteRange(start, end []byte) error {
|
|
// Note: The underlying database doesn't support DeleteRange operation
|
|
// This is a limitation we need to handle at a higher level
|
|
return errors.New("DeleteRange not supported")
|
|
}
|
|
|
|
// SyncKeyValue implements ethdb.KeyValueStore
|
|
func (db ethDbWrapper) SyncKeyValue() error {
|
|
// No-op for now as the underlying database handles sync differently
|
|
return nil
|
|
}
|
|
|
|
// NewBatch implements ethdb.Database
|
|
func (db ethDbWrapper) NewBatch() ethdb.Batch { return wrappedBatch{db.Database.NewBatch()} }
|
|
|
|
// NewBatchWithSize implements ethdb.Database
|
|
// TODO: propagate size through luxd Database interface
|
|
func (db ethDbWrapper) NewBatchWithSize(size int) ethdb.Batch {
|
|
return wrappedBatch{db.Database.NewBatch()}
|
|
}
|
|
|
|
// Snapshot is a minimal implementation that satisfies the interface
|
|
type Snapshot struct{}
|
|
|
|
func (s Snapshot) Has(key []byte) (bool, error) { return false, ErrSnapshotNotSupported }
|
|
func (s Snapshot) Get(key []byte) ([]byte, error) { return nil, ErrSnapshotNotSupported }
|
|
func (s Snapshot) Release() {}
|
|
|
|
func (db ethDbWrapper) NewSnapshot() (Snapshot, error) {
|
|
return Snapshot{}, ErrSnapshotNotSupported
|
|
}
|
|
|
|
// NewIterator implements ethdb.Database
|
|
//
|
|
// Note: This method assumes that the prefix is NOT part of the start, so there's
|
|
// no need for the caller to prepend the prefix to the start.
|
|
func (db ethDbWrapper) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
|
|
// luxd's database implementation assumes that the prefix is part of the
|
|
// start, so it is added here (if it is provided).
|
|
if len(prefix) > 0 {
|
|
newStart := make([]byte, len(prefix)+len(start))
|
|
copy(newStart, prefix)
|
|
copy(newStart[len(prefix):], start)
|
|
start = newStart
|
|
}
|
|
return db.Database.NewIteratorWithStartAndPrefix(start, prefix)
|
|
}
|
|
|
|
// NewIteratorWithStart implements ethdb.Database
|
|
func (db ethDbWrapper) NewIteratorWithStart(start []byte) ethdb.Iterator {
|
|
return db.Database.NewIteratorWithStart(start)
|
|
}
|
|
|
|
// wrappedBatch implements ethdb.wrappedBatch
|
|
type wrappedBatch struct{ database.Batch }
|
|
|
|
// ValueSize implements ethdb.Batch
|
|
func (batch wrappedBatch) ValueSize() int { return batch.Batch.Size() }
|
|
|
|
// Replay implements ethdb.Batch
|
|
func (batch wrappedBatch) Replay(w ethdb.KeyValueWriter) error { return batch.Batch.Replay(w) }
|
|
|
|
// DeleteRange implements ethdb.Batch
|
|
func (batch wrappedBatch) DeleteRange(start, end []byte) error {
|
|
// Note: The underlying batch doesn't support DeleteRange operation
|
|
// This is a limitation we need to handle at a higher level
|
|
return errors.New("DeleteRange not supported in batch")
|
|
}
|