Documentation
¶
Index ¶
- Constants
- Variables
- func BackupDb(Path string) error
- func BatchBackupDb(Path string, batchSize ...int) error
- func DeriveKey(password, salt []byte, iterations int) ([]byte, error)
- func GetMaxBatchSize() int
- func GetScenarioOptions(scenario string) *opt.Options
- func SetMaxBatchSize(size int)
- type AESGCMEncryptor
- type Batch
- type Config
- type ConfigManager
- type DBManager
- func (dm *DBManager) CloseDB() error
- func (dm *DBManager) GetDB() Store
- func (dm *DBManager) NewLevelDBStore(Path string, opts *opt.Options, encryptConfig ...*EncryptionConfig) (Store, error)
- func (dm *DBManager) OpenDB(path string, encryptConfig ...*EncryptionConfig) (Store, error)
- func (dm *DBManager) OpenDBWithScenario(path string, scenario string, encryptConfig ...*EncryptionConfig) (Store, error)
- func (dm *DBManager) SetDB(store Store)
- type EncryptedStoreWrapper
- func (es *EncryptedStoreWrapper) Close() error
- func (es *EncryptedStoreWrapper) Delete(key []byte) error
- func (es *EncryptedStoreWrapper) Get(key []byte) ([]byte, error)
- func (es *EncryptedStoreWrapper) GetBatch() Batch
- func (es *EncryptedStoreWrapper) GetEncryptionConfig() *EncryptionConfig
- func (es *EncryptedStoreWrapper) Iterator(start, limit []byte) Iterator
- func (es *EncryptedStoreWrapper) Put(key, value []byte) error
- func (es *EncryptedStoreWrapper) ReEncrypt(newKey []byte) error
- func (es *EncryptedStoreWrapper) Snapshot() (Snapshot, error)
- func (es *EncryptedStoreWrapper) SwitchToDB() error
- func (es *EncryptedStoreWrapper) SwitchToSnapshot() error
- func (es *EncryptedStoreWrapper) WriteBatch(batch Batch, writeOpts ...*opt.WriteOptions) error
- type EncryptionConfig
- type Encryptor
- type Error
- type FunIter
- type Iterator
- type LevelDBGetter
- type LevelDBStore
- func (s *LevelDBStore) Close() error
- func (s *LevelDBStore) Delete(key []byte) error
- func (s *LevelDBStore) Get(key []byte) ([]byte, error)
- func (s *LevelDBStore) GetBatch() Batch
- func (s *LevelDBStore) GetDB() *leveldb.DB
- func (s *LevelDBStore) Iterator(start, limit []byte) Iterator
- func (s *LevelDBStore) Put(key []byte, value []byte) error
- func (s *LevelDBStore) Release() error
- func (s *LevelDBStore) Snapshot() (Snapshot, error)
- func (s *LevelDBStore) SwitchToDB() error
- func (s *LevelDBStore) SwitchToSnapshot() error
- func (s *LevelDBStore) WriteBatch(batch Batch, writeOpts ...*opt.WriteOptions) error
- type Snapshot
- type Store
Constants ¶
const ( DefaultWriteBuffer = 64 * 1024 * 1024 // 默认写入缓冲区大小,64MB DefaultOpenFilesCacheCapacity = 200 // 默认打开文件缓存容量,200 DefaultBlockCacheCapacity = 128 * 1024 * 1024 // 默认块缓存容量,128MB DefaultMaxOpenFiles = 200 // 默认最大打开文件数 EmbeddedWriteBuffer = 2 * 1024 * 1024 // 嵌入式场景写入缓冲区大小,2MB EmbeddedOpenFilesCacheCapacity = 10 // 嵌入式场景打开文件缓存容量,10 EmbeddedBlockCacheCapacity = 4 * 1024 * 1024 // 嵌入式场景块缓存容量,4MB EmbeddedMaxOpenFiles = 20 // 嵌入式场景最大打开文件数 IoTWriteBuffer = 4 * 1024 * 1024 // 物联网场景写入缓冲区大小,4MB IoTOpenFilesCacheCapacity = 10 // 物联网场景打开文件缓存容量,10 IoTBlockCacheCapacity = 8 * 1024 * 1024 // 物联网场景块缓存容量,8MB IoTMaxOpenFiles = 30 // 物联网场景最大打开文件数 EdgeWriteBuffer = 16 * 1024 * 1024 // 边缘场景写入缓冲区大小,16MB EdgeOpenFilesCacheCapacity = 50 // 边缘场景打开文件缓存容量,50 EdgeBlockCacheCapacity = 32 * 1024 * 1024 // 边缘场景块缓存容量,32MB EdgeMaxOpenFiles = 100 // 边缘场景最大打开文件数 GameWriteBuffer = 64 * 1024 * 1024 // 游戏场景写入缓冲区大小,64MB GameOpenFilesCacheCapacity = 200 GameBlockCacheCapacity = 128 * 1024 * 1024 // 游戏场景块缓存容量,128MB GameMaxOpenFiles = 200 // 游戏场景最大打开文件数 // 极限生存模式(128MB 内存设备) ExtremeWriteBuffer = 2 * 1024 * 1024 // 极限模式写入缓冲区,2MB ExtremeOpenFilesCacheCapacity = 5 // 极限模式打开文件缓存,5 ExtremeBlockCacheCapacity = 4 * 1024 * 1024 // 极限模式块缓存,4MB ExtremeMaxOpenFiles = 20 // 极限模式最大打开文件数 )
const ( ScenarioEmbedded = "embedded" // 嵌入式场景,默认配置 ScenarioIoT = "iot" // 物联网场景 ScenarioEdge = "edge" // 边缘场景 ScenarioGame = "game" // 游戏场景 ScenarioDefault = "default" // 默认场景 ScenarioExtreme = "extreme" // 极限生存模式(128MB内存设备) )
Variables ¶
var ( ErrInvalidKeyLength = NewError("invalid key length") ErrInvalidAlgorithm = NewError("invalid algorithm") ErrEncryptionFailed = NewError("encryption failed") ErrDecryptionFailed = NewError("decryption failed") )
定义加密相关错误
var ( ErrNotFound = NewError("key not found") ErrInvalidKey = NewError("invalid key") ErrInvalidValue = NewError("invalid value") ErrStoreClosed = NewError("store is closed") )
Error definitions
var ( SetConfig = configManager.SetConfig // 设置配置 GetConfig = configManager.GetConfig // 获取配置 )
var LdbBatchPool = &batchPool{ pool: sync.Pool{ New: func() any { return new(leveldb.Batch) }, }, }
批处理对象池
var LdbSnapshotPool = &snapshotPool{ pool: sync.Pool{ New: func() any { return new(LevelDBStore) }, }, }
快照对象池
var ( // MaxBatchSize 批处理对象大小阈值(字节),超过此值的批处理对象不会被缓存 MaxBatchSize = 1024 * 1024 // 1MB )
批处理对象池配置
Functions ¶
func BatchBackupDb ¶ added in v1.9.3
BatchBackupDb 批量备份数据库 Path: 备份路径 batchSize: 批量操作的大小阈值
func GetScenarioOptions ¶
获取指定场景的自定义配置,根据GetScenarioConfig返回的配置创建opt.Options
func SetMaxBatchSize ¶
func SetMaxBatchSize(size int)
SetMaxBatchSize 设置批处理对象大小阈值(字节) size: 批处理对象大小阈值(字节)
Types ¶
type AESGCMEncryptor ¶
type AESGCMEncryptor struct {
// contains filtered or unexported fields
}
AESGCMEncryptor AES-256-GCM加密器实现
func NewAESGCMEncryptor ¶
func NewAESGCMEncryptor(key []byte) (*AESGCMEncryptor, error)
NewAESGCMEncryptor 创建新的AES-GCM加密器
type Batch ¶
type Batch interface {
// Put 添加put操作
Put(key []byte, value []byte)
// Delete 添加delete操作
Delete(key []byte)
// Len 获取批量操作数量
Len() int
// Reset 重置批量操作
Reset()
}
Batch 批量操作接口
type Config ¶
type ConfigManager ¶
type ConfigManager struct {
// contains filtered or unexported fields
}
type DBManager ¶
type DBManager struct {
// contains filtered or unexported fields
}
DBManager 管理数据库实例的创建、打开和关闭
func (*DBManager) NewLevelDBStore ¶ added in v1.9.6
func (dm *DBManager) NewLevelDBStore(Path string, opts *opt.Options, encryptConfig ...*EncryptionConfig) (Store, error)
NewLevelDBStore 创建新的LevelDB存储实例
func (*DBManager) OpenDB ¶
func (dm *DBManager) OpenDB(path string, encryptConfig ...*EncryptionConfig) (Store, error)
OpenDB 打开存储数据库,使用管理器内部的db变量,保证全局唯一实例
func (*DBManager) OpenDBWithScenario ¶ added in v1.9.6
func (dm *DBManager) OpenDBWithScenario(path string, scenario string, encryptConfig ...*EncryptionConfig) (Store, error)
OpenDBWithScenario 根据场景打开存储数据库,使用管理器内部的db变量,保证全局唯一实例
type EncryptedStoreWrapper ¶
type EncryptedStoreWrapper struct {
// contains filtered or unexported fields
}
EncryptedStoreWrapper 加密存储包装器
func NewEncryptedStoreWrapper ¶
func NewEncryptedStoreWrapper(underlyingStore Store, config *EncryptionConfig) (*EncryptedStoreWrapper, error)
NewEncryptedStoreWrapper 创建新的加密存储包装器
func (*EncryptedStoreWrapper) Delete ¶
func (es *EncryptedStoreWrapper) Delete(key []byte) error
Delete 删除指定key
func (*EncryptedStoreWrapper) Get ¶
func (es *EncryptedStoreWrapper) Get(key []byte) ([]byte, error)
Get 获取并解密数据
func (*EncryptedStoreWrapper) GetBatch ¶
func (es *EncryptedStoreWrapper) GetBatch() Batch
GetBatch 创建批量操作对象
func (*EncryptedStoreWrapper) GetEncryptionConfig ¶
func (es *EncryptedStoreWrapper) GetEncryptionConfig() *EncryptionConfig
GetEncryptionConfig 获取加密配置(返回副本,避免外部修改)
func (*EncryptedStoreWrapper) Iterator ¶
func (es *EncryptedStoreWrapper) Iterator(start, limit []byte) Iterator
Iterator 创建迭代器
func (*EncryptedStoreWrapper) Put ¶
func (es *EncryptedStoreWrapper) Put(key, value []byte) error
Put 加密存储数据
func (*EncryptedStoreWrapper) ReEncrypt ¶
func (es *EncryptedStoreWrapper) ReEncrypt(newKey []byte) error
ReEncrypt 重新加密所有数据
func (*EncryptedStoreWrapper) Snapshot ¶
func (es *EncryptedStoreWrapper) Snapshot() (Snapshot, error)
Snapshot 创建快照
func (*EncryptedStoreWrapper) SwitchToDB ¶
func (es *EncryptedStoreWrapper) SwitchToDB() error
SwitchToDB 切换到数据库模式
func (*EncryptedStoreWrapper) SwitchToSnapshot ¶
func (es *EncryptedStoreWrapper) SwitchToSnapshot() error
SwitchToSnapshot 切换到快照模式
func (*EncryptedStoreWrapper) WriteBatch ¶
func (es *EncryptedStoreWrapper) WriteBatch(batch Batch, writeOpts ...*opt.WriteOptions) error
WriteBatch 执行批量操作
type EncryptionConfig ¶
type EncryptionConfig struct {
// 是否启用加密
Enabled bool `json:"enabled"`
// 加密算法,默认AES-256-GCM
Algorithm string `json:"algorithm"`
// 主密钥,可以是直接的密钥或密码派生
MasterKey []byte `json:"master_key,omitempty"`
// 密码,用于派生密钥
Password string `json:"password,omitempty"`
// 盐值,用于密码派生
Salt []byte `json:"salt,omitempty"`
// 迭代次数,用于密码派生
Iterations int `json:"iterations,omitempty"`
}
EncryptionConfig 加密配置
type Encryptor ¶
type Encryptor interface {
// 加密数据
Encrypt(plaintext []byte) ([]byte, error)
// 解密数据
Decrypt(ciphertext []byte) ([]byte, error)
// 获取加密算法
Algorithm() string
}
Encryptor 加密器接口
type FunIter ¶
type FunIter func(start, limit []byte) Iterator
// Iterator 创建迭代器
func (s *LevelDBStore) Iterator1(slice *util.Range) Iterator {
return s.ldb.NewIterator(slice, nil)
}
type Iterator ¶
type Iterator interface {
// First 移动到第一个元素
First() bool
// Last 移动到最后一个元素
Last() bool
// Seek 移动到大于等于指定key的位置
Seek(key []byte) bool
// Next 移动到下一个元素
Next() bool
// Prev 移动到前一个元素
Prev() bool
// Key 获取当前元素的key
Key() []byte
// Value 获取当前元素的value
Value() []byte
// Valid 检查迭代器是否有效
Valid() bool
// Release 释放迭代器资源
Release()
}
Iterator 迭代器接口
type LevelDBGetter ¶
type LevelDBGetter interface {
// Get 获取指定key的值
Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
// NewIterator 创建迭代器
NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
}
LevelDBGetter 定义LevelDB读取操作的公共接口,适用于DB和Snapshot
type LevelDBStore ¶
type LevelDBStore struct {
// contains filtered or unexported fields
}
LevelDBStore LevelDB存储实现,支持快照功能
func (*LevelDBStore) GetBatch ¶
func (s *LevelDBStore) GetBatch() Batch
GetBatch 获取批处理操作对象
返回值:
Batch - 批处理操作对象,可用于执行多个写操作
说明:
- 从批处理对象池中获取一个批处理对象,实现了对象复用
- 即使在快照模式下也返回有效的批处理对象
- 批处理对象的写操作会通过WriteBatch方法使用originalDB执行
- 如果对象池获取失败,会创建一个新的批处理对象
func (*LevelDBStore) GetDB ¶ added in v1.9.3
func (s *LevelDBStore) GetDB() *leveldb.DB
GetDB 获取底层的 LevelDB 实例 注意:这个方法应该谨慎使用,仅用于需要直接操作 LevelDB 的场景
func (*LevelDBStore) Iterator ¶
func (s *LevelDBStore) Iterator(start, limit []byte) Iterator
Iterator 创建迭代器
func (*LevelDBStore) Put ¶
func (s *LevelDBStore) Put(key []byte, value []byte) error
Put 设置key-value对
func (*LevelDBStore) Release ¶
func (s *LevelDBStore) Release() error
Release 释放快照资源 对于普通数据库实例,Release方法不做任何事情 对于快照实例,Release方法会释放快照资源
func (*LevelDBStore) SwitchToDB ¶
func (s *LevelDBStore) SwitchToDB() error
SwitchToDB 将当前实例切换回数据库模式 如果当前已是数据库模式,则返回错误 如果当前是快照模式,会先释放快照资源
func (*LevelDBStore) SwitchToSnapshot ¶
func (s *LevelDBStore) SwitchToSnapshot() error
SwitchToSnapshot 将当前实例切换到快照模式 如果当前已是快照模式,则返回错误
func (*LevelDBStore) WriteBatch ¶
func (s *LevelDBStore) WriteBatch(batch Batch, writeOpts ...*opt.WriteOptions) error
WriteBatch 执行批量写入操作 batch: 批量操作对象 writeOpts: 可选的写入选项(用于 IoT 等特殊场景)
type Snapshot ¶
type Snapshot interface {
// Get 从快照中获取指定key的值
Get(key []byte) ([]byte, error)
// Iterator 从快照中创建迭代器
Iterator(start, limit []byte) Iterator
// Release 释放快照资源
Release() error
}
Snapshot 快照接口
type Store ¶
type Store interface {
// Get 获取指定key的值
Get(key []byte) ([]byte, error)
// Put 设置key-value对
Put(key []byte, value []byte) error
// Delete 删除指定key
Delete(key []byte) error
// Batch 创建批量操作对象
GetBatch() Batch
// WriteBatch 执行批量操作
WriteBatch(batch Batch, writeOpts ...*opt.WriteOptions) error
// Iterator 创建迭代器
//Iterator(slice *util.Range) Iterator
Iterator(start, limit []byte) Iterator
// Snapshot 创建快照
Snapshot() (Snapshot, error)
// SwitchToSnapshot 切换到数据库模式
SwitchToSnapshot() error
// SwitchToDB 切换到数据库模式
SwitchToDB() error
// Close 关闭存储
Close() error
}
Store KV存储接口