1 Star 0 Fork 0

艾鸥科技 / go-aiou

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
storage.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
张卓 提交于 2020-05-10 14:55 . init
package kv
import (
"gitee.com/aiou-official/go-aiou/db/kv/leveldb"
)
// StorageType is the type of storage, include leveldb and rocksdb
type StorageType uint8
// Storage type constant
const (
_ StorageType = iota
LevelDBStorage
)
// StorageBackend is the storage backend interface
type StorageBackend interface {
Get(key []byte) ([]byte, error)
Put(key []byte, value []byte) error
Has(key []byte) (bool, error)
Delete(key []byte) error
Keys(prefix []byte) ([][]byte, error)
KeysByRange(from []byte, to []byte, limit int) ([][]byte, error)
BeginBatch() error
CommitBatch() error
Size() (int64, error)
Close() error
NewIteratorByPrefix(prefix []byte) interface{}
}
// Storage is a kv database
type Storage struct {
StorageBackend
}
// NewStorage return the storage of the specify type
func NewStorage(path string, t StorageType) (*Storage, error) {
switch t {
case LevelDBStorage:
sb, err := leveldb.NewDB(path)
if err != nil {
return nil, err
}
return &Storage{StorageBackend: sb}, nil
default:
sb, err := leveldb.NewDB(path)
if err != nil {
return nil, err
}
return &Storage{StorageBackend: sb}, nil
}
}
// NewIteratorByPrefix returns a new iterator by prefix
func (s *Storage) NewIteratorByPrefix(prefix []byte) *Iterator {
ib := s.StorageBackend.NewIteratorByPrefix(prefix).(IteratorBackend)
return &Iterator{
IteratorBackend: ib,
}
}
// IteratorBackend is the storage iterator backend
type IteratorBackend interface {
Next() bool
Key() []byte
Value() []byte
Error() error
Release()
}
// Iterator is the storage iterator
type Iterator struct {
IteratorBackend
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiou-official/go-aiou.git
git@gitee.com:aiou-official/go-aiou.git
aiou-official
go-aiou
go-aiou
376a44096468

搜索帮助