golang 封装leveldb

golang 封装leveldb,第1张

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("捕获到运行时错误:%s\n", 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#

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/1298632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存