golang 封装leveldb

leveldb 是 key-value类型的数据库,与 redis,mongodb 同为nosql数据库类型。多用于比特币、以太坊、iot等场景。

leveldb  数据库如果遭到破坏,可以根据日志进行修复。

1. leveldb 项目结构

在这里插入图片描述

2. leveldb 操作源码

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
}

3. 执行结果

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#

1 简介哈希算法 (Hash Algorithm) 是将任意长度的数据映射为固定长度数据的算法,也称为消息摘要。一般情况下,哈希算法有两个特点:原始数据的细微变化(比如一个位翻转)会导致结果产生巨大差距运算过程 ...