概述
leveldb是key-value类型的数据库,与redis,mongodb同为nosql数据库类型。
多用于比特币、以太坊、iot等场景。 数据库如果遭到破坏,可以根据日志进行修复。
源码:
源码目录:
main.go:
package main
import (
"fmt"
"tt/database/leveldb"
)
func main() {
fmt.Println("vim-go")
defer func() {
if r := recover(); r != nil {
fmt.Printf("捕获到运行时错误:%sn", r)
}
}()
db, err := leveldb.NewLevelDB("./data_dir")
if err != nil {
panic(err)
}
defer db.Close()
pd := func(key string) {
val, err := db.GetString(key)
if err != nil {
panic(err)
}
fmt.Println(val)
}
key := "key-TangYiMo"
err = db.PutString(key, "henan come on, zhengzhou come on")
if err != nil {
panic(err)
}
pd(key)
if f, err := db.HasString(key); err == nil && f {
fmt.Println("leveldb has value of key:", key)
}
db.DeleteSring(key)
if f, err := db.HasString(key); err == nil && f {
fmt.Println("leveldb has value of key:", key)
}
// 批量提交
patch := db.NewBatch()
patch.Put([]byte(key), []byte{'h', 'e', 'l', 'l', 'o'})
//patch.Rollback()
patch.Commit()
fmt.Println("leveldb has value of key:", key)
pd(key)
}
db接口 database/database.go:
package database
type Database interface {
Close()
Put(key []byte, value []byte) error
Get(key []byte) ([]byte, error)
GetString(key string) (string, error)
PutString(key string, value string) error
Has(key []byte) (ret bool, err error)
HasString(key string) (ret bool, err error)
Delete(key []byte) error
DeleteSring(key string) error
NewBatch() Batch
}
type Batch interface {
Put(key []byte, value []byte)
Delete(key []byte)
Commit() error
Rollback()
}
事务和回滚 database/leveldb/batch.go:
package leveldb
import (
"github.com/syndtr/goleveldb/leveldb"
)
type Batch struct {
leveldb *leveldb.DB
batch *leveldb.Batch
}
func (b *Batch) Put(key []byte, value []byte) {
b.batch.Put(key, value)
}
func (b *Batch) Delete(key []byte) {
b.batch.Delete(key)
}
func (b *Batch) Commit() error {
return b.leveldb.Write(b.batch, nil)
}
func (b *Batch) Rollback() {
b.batch.Reset()
}
leveldb api封装database/leveldb/leveldb.go:
package leveldb
import (
"tt/database"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
)
var (
ErrEmptyKey = errors.New("key could not be empty")
)
type LevelDB struct {
db *leveldb.DB
quitChan chan struct{}
}
func NewLevelDB(path string) (database.Database, error) {
db, err := leveldb.OpenFile(path, nil)
if _, corrupted := err.(*errors.ErrCorrupted); corrupted {
db, err = leveldb.RecoverFile(path, nil)
}
if err != nil {
return nil, err
}
result := &LevelDB{
db: db,
quitChan: make(chan struct{}),
}
return result, nil
}
func (db *LevelDB) Close() {
close(db.quitChan)
db.db.Close()
}
func (db *LevelDB) GetString(key string) (string, error) {
value, err := db.Get([]byte(key))
return string(value), err
}
func (db *LevelDB) Get(key []byte) ([]byte, error) {
return db.db.Get(key, nil)
}
func (db *LevelDB) Put(key []byte, value []byte) error {
if len(key) < 1 {
return ErrEmptyKey
}
return db.db.Put(key, value, nil)
}
func (db *LevelDB) PutString(key string, value string) error {
return db.Put([]byte(key), []byte(value))
}
func (db *LevelDB) Has(key []byte) (ret bool, err error) {
return db.db.Has(key, nil)
}
func (db *LevelDB) HasString(key string) (ret bool, err error) {
return db.Has([]byte(key))
}
func (db *LevelDB) Delete(key []byte) error {
return db.db.Delete(key, nil)
}
func (db *LevelDB) DeleteSring(key string) error {
return db.Delete([]byte(key))
}
func (db *LevelDB) NewBatch() database.Batch {
batch := &Batch{
leveldb: db.db,
batch: new(leveldb.Batch),
}
return batch
}
执行:
root@jack-VirtualBox:~/test/leveldb# go mod init tt
root@jack-VirtualBox:~/test/leveldb# go mod tidy
root@jack-VirtualBox:~/test/leveldb# ./tt
vim-go
henan come on, zhengzhou come on
leveldb has value of key: key-TangYiMo
leveldb has value of key: key-TangYiMo
hello
root@jack-VirtualBox:~/test/leveldb#
最后
以上就是阳光美女为你收集整理的golang 封装leveldb的全部内容,希望文章能够帮你解决golang 封装leveldb所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复