LevelDB介绍:
官网:http://leveldb.org/
levelDB是Google开源的KeyValue文件数据库,levelDB支持按照文件大小切分文件的功能.
特点
- 1.key和value都是任意长度的字节数组;
- 2.entry(即一条k-v记录)默认是按照key的字典顺序存储的,开发者开源重载这个排序函数;
- 3.提供了一些基本接口
- 4.支持批量操作以原子操作进行
- 5.开源创建数据全景的snapshot(快照),并允许在快照中查询
- 6.开源通过向前(后)迭代器遍历数据(迭代器隐含的创建一个snapshot)
- 7.自动使用Snappy压缩数据;
- 8.可移植性.
限制
- 1.NoSQL,不支持sql语句,也不支持索引;
- 2.一次只允许一个进程访问一个特定的数据库;
- 3.没有内置的C/S架构,开发者需要使用LevelDB库自己封装一个server;
以下内容摘自百度百科(维基现在不能翻墙,后面翻墙了再补充吧~但愿记得):
介绍:LevelDB是一个非常高效的kv数据库,1.2-version就能够支持billion级的数据量了,在这个数量级下还有着非常高的性能,主要归功于它的良好设计,特别是LSM算法.
特点:LevelDB是单进程的服务,性能非常之高,在一台4核Q6600的CPU机器上,每秒写数据超过40w,而随机数的性能每秒钟超过10w.(此处随机读是完全命中内存的速度),如果不命中 速度大大下降.
LSM-Tree介绍
这里待以后有时间再详细了解,补齐
goleveldb
这里介绍的使用go语言实现的leveldb(在以太坊中使用的)
github网址:https://github.com/syndtr/goleveldb
This is an implementation of the LevelDB key/value database in the Go programming language.
[图片上传失败...(image-b033d9-1542464775771)]
Installation
go get github.com/syndtr/goleveldb/leveldb
Requirements
- Need at least
go1.5
or newer.
基础代码实现
这里先介绍一些最基础的代码:其它的后续在更新
1.基本方法:
- 获取db实例;
- 添加数据;
- 删除数据;
- 获取db快照
相关注意事项
package main
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
)
func main() {
levelDB()
}
func levelDB() {
//定义字符数组key value
key := []byte("hello")
value := []byte("hi i'm levelDB by go")
//定义字符串的键值对
k1 := "hhh"
v1 := "heiheiheihei"
// The returned DB instance is safe for concurrent use.
// The DB must be closed after use, by calling Close method.
//文件夹位置,获取db实际
db, err := leveldb.OpenFile("path/to/db", nil)
//延迟关闭db流,必须的操作
defer db.Close()
if err != nil {
fmt.Println(err)
}
//向db中插入键值对
e := db.Put(key, value, nil)
//将字符串转换为byte数组
e = db.Put([]byte(k1), []byte(v1), nil)
fmt.Println(e)//
/**
|| 注意:查看路径下的db文件,可知道文件都是自动切分为一些小文件.
*/
//根据key值查询value
data, _ := db.Get(key, nil)
fmt.Println(data)//[104 105 32 105 39 109 32 108 101 118 101 108 68 66 32 98 121 32 103 111]
fmt.Printf("%c\n",data)//[h i i ' m l e v e l D B b y g o]
data, _ = db.Get([]byte(k1), nil)
fmt.Println(data)//[104 101 105 104 101 105 104 101 105 104 101 105]
fmt.Printf("%c\n",data)//[h e i h e i h e i h e i]
// It is safe to modify the contents of the arguments after Delete returns but
// not before.
//根据key进行删除操作
i := db.Delete(key, nil)
fmt.Println(i)//
data, _ = db.Get(key, nil)
fmt.Println(data)//[]
//获取db快照
snapshot, i := db.GetSnapshot()
fmt.Println(snapshot)//leveldb.Snapshot{22}
fmt.Println(i)//
//注意: The snapshot must be released after use, by calling Release method.
//也就是说snapshot在使用之后,必须使用它的Release方法释放!
snapshot.Release()
}
2.迭代
调用的迭代方法源码
// The iterator must be released after use, by calling Release method.
// Also read Iterator documentation of the leveldb/iterator package.
func (db *DB) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator {
if err := db.ok(); err != nil {
return iterator.NewEmptyIterator(err)
}
se := db.acquireSnapshot()
defer db.releaseSnapshot(se)
// Iterator holds 'version' lock, 'version' is immutable so snapshot
// can be released after iterator created.
return db.newIterator(nil, nil, se.seq, slice, ro)
}
type Range struct {
// Start of the key range, include in the range.
Start []byte
// Limit of the key range, not include in the range.
Limit []byte
}
// ReadOptions holds the optional parameters for 'read operation'. The
// 'read operation' includes Get, Find and NewIterator.
type ReadOptions struct {
// DontFillCache defines whether block reads for this 'read operation'
// should be cached. If false then the block will be cached. This does
// not affects already cached block.
//
// The default value is false.
DontFillCache bool
// Strict will be OR'ed with global DB 'strict level' unless StrictOverride
// is present. Currently only StrictReader that has effect here.
Strict Strict
}
// NewEmptyIterator creates an empty iterator. The err parameter can be
// nil, but if not nil the given err will be returned by Error method.
func NewEmptyIterator(err error) Iterator {
return &emptyIterator{err: err}
}
Iterate over database content:迭代数据库内容
iter := db.NewIterator(nil, nil)
for iter.Next() {
// Remember that the contents of the returned slice should not be modified, and
// only valid until the next call to Next.
key := iter.Key()
value := iter.Value()
...
}
iter.Release()
err = iter.Error()
...
Seek-then-Iterate:
查找然后迭代
iter := db.NewIterator(nil, nil)
for ok := iter.Seek(key); ok; ok = iter.Next() {
// Use key/value.
...
}
iter.Release()
err = iter.Error()
...
terate over subset of database content:
迭代数据库内容的子集:
iter := db.NewIterator(&util.Range{Start: []byte("foo"), Limit: []byte("xoo")}, nil)
for iter.Next() {
// Use key/value.
...
}
iter.Release()
err = iter.Error()
...
Iterate over subset of database content with a particular prefix:
使用特定前缀迭代数据库内容的子集:
iter := db.NewIterator(util.BytesPrefix([]byte("foo-")), nil)
for iter.Next() {
// Use key/value.
...
}
iter.Release()
err = iter.Error()
...
Batch writes:
批量写入
batch := new(leveldb.Batch)
batch.Put([]byte("foo"), []byte("value"))
batch.Put([]byte("bar"), []byte("another value"))
batch.Delete([]byte("baz"))
err = db.Write(batch, nil)
Use bloom filter:
使用bloom过滤器
o := &opt.Options{
Filter: filter.NewBloomFilter(10),
}
db, err := leveldb.OpenFile("path/to/db", o)
...
defer db.Close()
...