Golang版本的rocksdb-对gorocksdb的封装

rocksdb的优秀特性不用多说,但是它是用c++语言写的,就是这一个特点就把很多人拦住了。虽然rocksdb官方也有Java版本,但是Golang的发展速度让人不容小觑,而且由于golang原生对高并发的高效性能,golang版本的rocksdb接口很是需要。

实例如下


 

package main

import (
    "github.com/leeyazhou/gorocksdb"
    "log"
    "errors"
    "strconv"
)

const (
    DB_PATH = "/tmp/gorocksdb"
)

func main() {
    db, err := OpenDB()
    if err != nil {
        log.Println("fail to open db,", nil, db)
    }

    readOptions := gorocksdb.NewDefaultReadOptions()
    readOptions.SetFillCache(true)

    writeOptions := gorocksdb.NewDefaultWriteOptions()
    writeOptions.SetSync(true)

    for i := 0; i < 10000; i++ {
        keyStr := "aa" + strconv.Itoa(i)
        var key []byte = []byte(keyStr)
        db.Put(writeOptions, key, key)
        log.Println(i, keyStr)
        slice, err2 := db.Get(readOptions, key)
        if err2 != nil {
            log.Println("获取数据异常:", key, err2)
            continue
        }
        log.Println("获取数据:", slice.Size(), string(slice.Data()))
    }

    //defer readOptions.Destroy()
    //defer writeOptions.Destroy()
}

func OpenDB() (*gorocksdb.DB, error) {
    options := gorocksdb.NewDefaultOptions()
    options.SetCreateIfMissing(true)

    bloomFilter := gorocksdb.NewBloomFilter(10)

    readOptions := gorocksdb.NewDefaultReadOptions()
    readOptions.SetFillCache(false)

    rateLimiter := gorocksdb.NewRateLimiter(10000000, 10000, 10)
    options.SetRateLimiter(rateLimiter)
    options.SetCreateIfMissing(true)
    options.EnableStatistics()
    options.SetWriteBufferSize(8 * gorocksdb.KB)
    options.SetMaxWriteBufferNumber(3)
    options.SetMaxBackgroundCompactions(10)
    options.SetCompression(gorocksdb.SnappyCompression)
    options.SetCompactionStyle(gorocksdb.UniversalCompactionStyle)

    options.SetHashSkipListRep(2000000, 4, 4)

    blockBasedTableOptions := gorocksdb.NewDefaultBlockBasedTableOptions()
    blockBasedTableOptions.SetBlockCache(gorocksdb.NewLRUCache(64 * gorocksdb.KB))
    blockBasedTableOptions.SetFilterPolicy(bloomFilter)
    blockBasedTableOptions.SetBlockSizeDeviation(5)
    blockBasedTableOptions.SetBlockRestartInterval(10)
    blockBasedTableOptions.SetBlockCacheCompressed(gorocksdb.NewLRUCache(64 * gorocksdb.KB))
    blockBasedTableOptions.SetCacheIndexAndFilterBlocks(true)
    blockBasedTableOptions.SetIndexType(gorocksdb.KHashSearchIndexType)

    options.SetBlockBasedTableFactory(blockBasedTableOptions)
    //log.Println(bloomFilter, readOptions)
    options.SetPrefixExtractor(gorocksdb.NewFixedPrefixTransform(3))

    options.SetAllowConcurrentMemtableWrites(false)

    db, err := gorocksdb.OpenDb(options, DB_PATH)

    if err != nil {
        log.Fatalln("OPEN DB error", db, err)
        db.Close()
        return nil, errors.New("fail to open db")
    } else {
        log.Println("OPEN DB success", db)
    }
    return db, nil
}

 

posted @ 2018-02-28 13:32  leeyazhou  阅读(3379)  评论(0编辑  收藏  举报