Documentation ¶
Overview ¶
Package shardingdb provides a sharding db based on goleveldb
Index ¶
- func Migration(dbReaders []LevelDbHandle, sdb *ShardingDb) error
- func MurmurSharding(key []byte, max uint16) uint16
- func Sha256Sharding(key []byte, max uint16) uint16
- func XorSharding(key []byte, max uint8) uint8
- func XorSharding16(key []byte, max uint16) uint16
- type AESCryptor
- type Batch
- type CommonDbHandle
- type DbOption
- type Encryptor
- type LevelDbHandle
- type Logger
- type ShardingBatch
- type ShardingDb
- func (sdb *ShardingDb) Close() error
- func (sdb *ShardingDb) CompactRange(r util.Range) error
- func (sdb *ShardingDb) Debugf(msg string, a ...interface{})
- func (sdb *ShardingDb) Delete(key []byte, wo *opt.WriteOptions) error
- func (sdb *ShardingDb) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
- func (sdb *ShardingDb) GetProperty(name string) (value string, err error)
- func (sdb *ShardingDb) GetSnapshot() (Snapshot, error)
- func (sdb *ShardingDb) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
- func (sdb *ShardingDb) Infof(msg string, a ...interface{})
- func (sdb *ShardingDb) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
- func (sdb *ShardingDb) OpenTransaction() (Transaction, error)
- func (sdb *ShardingDb) Put(key, value []byte, wo *opt.WriteOptions) error
- func (sdb *ShardingDb) Resharding() error
- func (sdb *ShardingDb) SetReadOnly() error
- func (sdb *ShardingDb) ShardCount() uint16
- func (sdb *ShardingDb) SizeOf(ranges []util.Range) (leveldb.Sizes, error)
- func (sdb *ShardingDb) Stats(s *leveldb.DBStats) error
- func (sdb *ShardingDb) Write(batch *leveldb.Batch, wo *opt.WriteOptions) error
- type ShardingDbHandle
- type ShardingFunc
- type ShardingSnapshot
- func (s ShardingSnapshot) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
- func (s ShardingSnapshot) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
- func (s ShardingSnapshot) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
- func (s ShardingSnapshot) Release()
- func (s ShardingSnapshot) String() string
- type ShardingTransaction
- func (s ShardingTransaction) Commit() error
- func (s ShardingTransaction) Delete(key []byte, wo *opt.WriteOptions) error
- func (s ShardingTransaction) Discard()
- func (s ShardingTransaction) Get(key []byte, ro *opt.ReadOptions) ([]byte, error)
- func (s ShardingTransaction) Has(key []byte, ro *opt.ReadOptions) (bool, error)
- func (s ShardingTransaction) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
- func (s ShardingTransaction) Put(key, value []byte, wo *opt.WriteOptions) error
- func (s ShardingTransaction) Write(b *leveldb.Batch, wo *opt.WriteOptions) error
- type Snapshot
- type Transaction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Migration ¶
func Migration(dbReaders []LevelDbHandle, sdb *ShardingDb) error
Migration changed leveldb count, reorganize all data to the new leveldb @param dbReaders @param sdb @return error
func MurmurSharding ¶
MurmurSharding sharding function @param key @param max @return uint16
func Sha256Sharding ¶
Sha256Sharding sharding function @param key @param max @return int
func XorSharding ¶ added in v1.1.0
XorSharding sharding function use XOR
func XorSharding16 ¶ added in v1.1.0
XorSharding16 sharding function,but actually max is uint8 @param key @param max @return uint16
Types ¶
type AESCryptor ¶ added in v1.1.0
type AESCryptor struct {
// contains filtered or unexported fields
}
AESCryptor is a encryptor using AES
func NewAESCryptor ¶ added in v1.1.0
func NewAESCryptor(key []byte) *AESCryptor
NewAESCryptor creates a new AESCryptor
type Batch ¶
type Batch interface { // Put sets the value for the given key. // @param key // @param value Put(key, value []byte) // Delete deletes the value for the given key. // @param key Delete(key []byte) // Dump returns the serialized representation of the batch. // @return []byte Dump() []byte // Load loads the batch from the serialized representation returned by Dump. // @param data // @return error Load(data []byte) error // Replay replays the batch contents into the given handler. // @param r // @return error Replay(r leveldb.BatchReplay) error // Len returns the number of updates in the batch. // @return int Len() int // Reset resets the batch contents. Reset() }
Batch is the interface that wraps the basic methods of a leveldb.Batch
type CommonDbHandle ¶ added in v1.1.0
type CommonDbHandle interface { // Get returns the value for the given key. // @param key // @param ro // @return value // @return err Get(key []byte, ro *opt.ReadOptions) (value []byte, err error) // Has returns whether the DB does contains the given key. // @param key // @param ro // @return ret // @return err Has(key []byte, ro *opt.ReadOptions) (ret bool, err error) // NewIterator returns an iterator for the latest snapshot of the DB. // @param slice // @param ro // @return iterator.Iterator NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator // GetProperty returns the value of the given property for the DB. // @param name // @return value // @return err GetProperty(name string) (value string, err error) // Stats returns the DB's leveldb.DBStats. // @param s // @return error Stats(s *leveldb.DBStats) error // SizeOf returns the approximate file system space used by keys in the given ranges. // @param ranges // @return leveldb.Sizes // @return error SizeOf(ranges []util.Range) (leveldb.Sizes, error) // Close closes the DB. // @return error Close() error // Write writes the given batch to the DB. // @param batch // @param wo // @return error Write(batch *leveldb.Batch, wo *opt.WriteOptions) error // Put sets the value for the given key. // @param key // @param value // @param wo // @return error Put(key, value []byte, wo *opt.WriteOptions) error // Delete deletes the value for the given key. // @param key // @param wo // @return error Delete(key []byte, wo *opt.WriteOptions) error // CompactRange manually compacts the underlying DB for the given key range. // @param r // @return error CompactRange(r util.Range) error // SetReadOnly sets the DB to read-only mode. // @return error SetReadOnly() error }
CommonDbHandle is the interface that wraps the basic methods of a leveldb.DB
type DbOption ¶ added in v1.1.0
type DbOption func(db *ShardingDb)
DbOption is used to set options for ShardingDb
func WithDbHandles ¶ added in v1.1.0
func WithDbHandles(dbHandles ...LevelDbHandle) DbOption
WithDbHandles sets dbHandles for ShardingDb
func WithDbPaths ¶ added in v1.1.0
WithDbPaths sets dbHandles for ShardingDb
func WithEncryptor ¶ added in v1.1.0
WithEncryptor sets encryptor for ShardingDb
func WithLogger ¶ added in v1.1.0
WithLogger sets logger for ShardingDb
func WithShardingFunc ¶ added in v1.1.0
func WithShardingFunc(f ShardingFunc) DbOption
WithShardingFunc sets shardingFunc for ShardingDb
type Encryptor ¶ added in v1.1.0
type Encryptor interface { // Encrypt encrypts the given data. Encrypt(data []byte) ([]byte, error) // Decrypt decrypts the given data. Decrypt(data []byte) ([]byte, error) }
Encryptor is the interface that wraps the basic methods of a encryptor
type LevelDbHandle ¶
type LevelDbHandle interface { CommonDbHandle // GetSnapshot returns a new snapshot of the DB. GetSnapshot() (*leveldb.Snapshot, error) // OpenTransaction opens a transaction. OpenTransaction() (*leveldb.Transaction, error) }
LevelDbHandle is the interface that wraps the basic LevelDB methods.
type Logger ¶
type Logger interface { // Debug logs a debug message. Debug(msg string) // Info logs an info message. Info(msg string) }
Logger is the interface that wraps the basic methods of a logger
type ShardingBatch ¶
type ShardingBatch struct {
// contains filtered or unexported fields
}
ShardingBatch is a batch of multiple db
func NewShardingBatch ¶
func NewShardingBatch(len uint16, shardingFunc ShardingFunc, e Encryptor) *ShardingBatch
NewShardingBatch returns a new ShardingBatch
func (*ShardingBatch) Delete ¶
func (s *ShardingBatch) Delete(key []byte)
Delete deletes the value for the given key
func (*ShardingBatch) GetSplitBatch ¶
func (s *ShardingBatch) GetSplitBatch() map[uint16]*leveldb.Batch
GetSplitBatch returns a map of db index to batch
func (*ShardingBatch) Put ¶
func (s *ShardingBatch) Put(key, value []byte)
Put sets the value for the given key
type ShardingDb ¶
type ShardingDb struct {
// contains filtered or unexported fields
}
ShardingDb is a db of multiple db
func NewShardingDb ¶
func NewShardingDb(options ...DbOption) (*ShardingDb, error)
NewShardingDb creates a new ShardingDb @param shardingFunc @param txHandles @return *ShardingDb
func OpenFile ¶
func OpenFile(path []string, o *opt.Options) (db *ShardingDb, err error)
OpenFile opens multi db,looks like leveldb.OpenFile @param path @param o @return db @return err
func (*ShardingDb) CompactRange ¶
func (sdb *ShardingDb) CompactRange(r util.Range) error
CompactRange compact range @param r @return error
func (*ShardingDb) Debugf ¶
func (sdb *ShardingDb) Debugf(msg string, a ...interface{})
Debugf log debug @param msg @param a
func (*ShardingDb) Delete ¶
func (sdb *ShardingDb) Delete(key []byte, wo *opt.WriteOptions) error
Delete delete key @param key @param wo @return error Delete removes the given key from the database. If there are multiple replicas, it removes the key from all replicas concurrently and waits for all of them to complete.
func (*ShardingDb) Get ¶
func (sdb *ShardingDb) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
Get get value by key @param key @param ro @return value @return err
func (*ShardingDb) GetProperty ¶
func (sdb *ShardingDb) GetProperty(name string) (value string, err error)
GetProperty get property @param name @return value @return err
func (*ShardingDb) GetSnapshot ¶
func (sdb *ShardingDb) GetSnapshot() (Snapshot, error)
GetSnapshot get snapshot @return Snapshot @return error
func (*ShardingDb) Has ¶
func (sdb *ShardingDb) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
Has checks if the given key exists in the database. If there are multiple replicas, it checks for the key in all replicas concurrently and returns true if any replica contains the key. @param key @param ro @return ret @return err
func (*ShardingDb) Infof ¶
func (sdb *ShardingDb) Infof(msg string, a ...interface{})
Infof log info @param msg @param a
func (*ShardingDb) NewIterator ¶
func (sdb *ShardingDb) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
NewIterator create a new iterator @param slice @param ro @return iterator.Iterator
func (*ShardingDb) OpenTransaction ¶
func (sdb *ShardingDb) OpenTransaction() (Transaction, error)
OpenTransaction open transaction @return Transaction @return error
func (*ShardingDb) Put ¶
func (sdb *ShardingDb) Put(key, value []byte, wo *opt.WriteOptions) error
Put put key value @param key @param value @param wo @return error Put writes the given key-value pair to the database. If there are multiple replicas, it writes the key-value pair to all replicas concurrently and waits for all of them to complete.
func (*ShardingDb) Resharding ¶
func (sdb *ShardingDb) Resharding() error
Resharding changed leveldb count, reorganize all data in the original leveldb @return error
func (*ShardingDb) SetReadOnly ¶
func (sdb *ShardingDb) SetReadOnly() error
SetReadOnly set read only @return error
func (*ShardingDb) ShardCount ¶ added in v1.1.0
func (sdb *ShardingDb) ShardCount() uint16
ShardCount returns the number of shards
func (*ShardingDb) SizeOf ¶
SizeOf get size of ranges @param ranges @return leveldb.Sizes @return error
func (*ShardingDb) Stats ¶
func (sdb *ShardingDb) Stats(s *leveldb.DBStats) error
Stats get stats @param s @return error
func (*ShardingDb) Write ¶
func (sdb *ShardingDb) Write(batch *leveldb.Batch, wo *opt.WriteOptions) error
Write write batch @param batch @param wo @return error Write applies the given batch to the database. If there are multiple replicas, it applies the batch to all replicas concurrently and waits for all of them to complete.
type ShardingDbHandle ¶ added in v1.1.0
type ShardingDbHandle interface { CommonDbHandle // GetSnapshot returns a new snapshot of the DB. // @return Snapshot // @return error GetSnapshot() (Snapshot, error) // OpenTransaction opens a transaction. // @return Transaction // @return error OpenTransaction() (Transaction, error) // Resharding resharding the DB. // @return error Resharding() error // ShardCount returns the shard count of the DB. ShardCount() uint16 }
ShardingDbHandle is the interface that wraps the basic methods of a leveldb.DB
type ShardingFunc ¶ added in v1.1.0
ShardingFunc is a function to calculate the index of db
type ShardingSnapshot ¶
type ShardingSnapshot struct {
// contains filtered or unexported fields
}
ShardingSnapshot is a snapshot of multiple db
func (ShardingSnapshot) Get ¶
func (s ShardingSnapshot) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
Get returns the value for the given key
func (ShardingSnapshot) Has ¶
func (s ShardingSnapshot) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error)
Has returns whether the DB does contains the given key
func (ShardingSnapshot) NewIterator ¶
func (s ShardingSnapshot) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
NewIterator returns an iterator for the latest snapshot of the DB
func (ShardingSnapshot) String ¶
func (s ShardingSnapshot) String() string
String returns a string representation of the snapshot
type ShardingTransaction ¶
type ShardingTransaction struct {
// contains filtered or unexported fields
}
ShardingTransaction is a transaction of multiple db
func (ShardingTransaction) Commit ¶
func (s ShardingTransaction) Commit() error
Commit commits the transaction.
func (ShardingTransaction) Delete ¶
func (s ShardingTransaction) Delete(key []byte, wo *opt.WriteOptions) error
Delete deletes the value for the given key.
func (ShardingTransaction) Discard ¶
func (s ShardingTransaction) Discard()
Discard discards the transaction.
func (ShardingTransaction) Get ¶
func (s ShardingTransaction) Get(key []byte, ro *opt.ReadOptions) ([]byte, error)
Get returns the value for the given key
func (ShardingTransaction) Has ¶
func (s ShardingTransaction) Has(key []byte, ro *opt.ReadOptions) (bool, error)
Has returns whether the DB does contains the given key
func (ShardingTransaction) NewIterator ¶
func (s ShardingTransaction) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
NewIterator returns an iterator for the latest snapshot of the DB.
func (ShardingTransaction) Put ¶
func (s ShardingTransaction) Put(key, value []byte, wo *opt.WriteOptions) error
Put sets the value for the given key.
func (ShardingTransaction) Write ¶
func (s ShardingTransaction) Write(b *leveldb.Batch, wo *opt.WriteOptions) error
Write writes the given batch to the DB.
type Snapshot ¶
type Snapshot interface { // String returns a string representation of the snapshot String() string // Get returns the value for the given key Get(key []byte, ro *opt.ReadOptions) (value []byte, err error) // Has returns whether the DB does contains the given key Has(key []byte, ro *opt.ReadOptions) (ret bool, err error) // NewIterator returns an iterator for the latest snapshot of the DB NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator // Release releases the snapshot Release() }
Snapshot is the interface that wraps the basic methods of a leveldb.Snapshot
type Transaction ¶
type Transaction interface { // Get returns the value for the given key. Get(key []byte, ro *opt.ReadOptions) ([]byte, error) // Has returns whether the DB does contains the given key. Has(key []byte, ro *opt.ReadOptions) (bool, error) // NewIterator returns an iterator for the latest snapshot of the DB. NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator // Put sets the value for the given key. Put(key, value []byte, wo *opt.WriteOptions) error // Delete deletes the value for the given key. Delete(key []byte, wo *opt.WriteOptions) error // Write writes the given batch to the DB. Write(b *leveldb.Batch, wo *opt.WriteOptions) error // Commit commits the transaction. Commit() error // Discard discards the transaction. Discard() }
Transaction is the interface that wraps the basic methods of a leveldb.Transaction