dongshui2254 2012-09-28 21:05
浏览 65
已采纳

leveldb-go示例,文档

LevelDB-Go is port of LevelDB in Go language. LevelDB-Go often referred as native alternative for Go apps. Website has no examples and no documentation.

Should I learn it by reading source code? or there is another website with examples and docs?

Does library support concurrency?

  • 写回答

3条回答 默认 最新

  • dongshuzhuo5659 2012-10-02 08:42
    关注

    I played around a little with leveldb Here is what I got so far. This should get you started.

    package main
    
    import (
        "code.google.com/p/leveldb-go/leveldb/db"
        "code.google.com/p/leveldb-go/leveldb/table"
        "fmt"
        "runtime"
    )
    
    type kv struct {
        K []byte
        V []byte
    }
    
    type kvs struct {
        items map[int]kv
    }
    
    func (p *kv) PutKV(k []byte, v []byte) {
        p.K = k
        p.V = v
    }
    
    func (items *kvs) PutKVs() {
        fmt.Println(items)
    }
    
    func (p *kv) GetKV() (key []byte, value []byte) {
        key = p.K
        value = p.V
        return
    }
    
    func Check(e error) {
        if e != nil {
            _, file, line, _ := runtime.Caller(1)
        }
    }
    
    func p(r []byte, e error) {
        if e != nil {
            return e
        }
        println(string(r))
    }
    
    const (
        DBFILE = "/tmp/leveldb2.db"
    )
    
    var DBFS = db.DefaultFileSystem
    
    func main() {
        Connection, e := DBFS.Create(DBFILE)
        Check(e)
        w := table.NewWriter(Connection, nil)
        defer w.Close()
    
        e = w.Set([]byte("1"), []byte("red"), nil)
        Check(e)
        e = w.Set([]byte("2"), []byte("yellow"), nil)
        Check(e)
        e = w.Set([]byte("3"), []byte("blue"), nil)
        Check(e)
        e = w.Close()
        Check(e)
        w = nil
    
        count()
    
        fmt.Println("Printing # KV")
        itemsKV := readByte()
        fmt.Println(itemsKV[0])
        fmt.Println(itemsKV[1])
        fmt.Println(itemsKV[2])
        println("Done Printing # KV")
    
        Connection, e = DBFS.Create(DBFILE)
        Check(e)
        w = table.NewWriter(Connection, nil)
        defer w.Close()
        e = w.Set([]byte("4"), []byte("green"), nil)
        Check(e)
        e = w.Set([]byte("5"), []byte("white"), nil)
        Check(e)
        e = w.Set([]byte("6"), []byte("black"), nil)
        Check(e)
        e = w.Close()
        Check(e)
    }
    
    func count() {
        Connection, e := DBFS.Open(DBFILE)
        Check(e)
        b := []byte("0")
        r := table.NewReader(Connection, nil)
    
        println("
    
    ###### Counting ###### ")
    
        iter, n := r.Find(b, nil), 0
        for iter.Next() {
            n++
            println("Count # ", n)
        }
    
        e = r.Close()
        Check(e)
        println("#####Total: ", n)
    }
    
    func read() map[int64]string {
        Connection, e := DBFS.Open(DBFILE)
        Check(e)
        b := []byte("0")
        r := table.NewReader(Connection, nil)
    
        items := map[int64]string{}
        iter, _ := r.Find(b, nil), 0
        for iter.Next() {
            k := iter.Key()
            v := iter.Value()
            items[int64(k[0])] = string(v)
        }
    
        e = r.Close()
        Check(e)
        return items
    }
    
    func readByte() map[int]kv {
        Connection, e := DBFS.Open(DBFILE)
        Check(e)
        c := 0
        b := []byte("0")
        r := table.NewReader(Connection, nil)
    
        //items := map[int64]kv{}
        item := new(kv)
        items := map[int]kv{}
        iter, _ := r.Find(b, nil), 0
        for iter.Next() {
            k := iter.Key()
            v := iter.Value()
            item.PutKV(k, v)
            items[c] = *item
            c++
        }
    
        e = r.Close()
        Check(e)
        return items
    }
    
    func findOne(k []byte) []byte {
        Connection, e := DBFS.Open(DBFILE)
        Check(e)
        b := []byte("0")
        r := table.NewReader(Connection, nil)
    
        iter, _ := r.Find(b, nil), 0
        k = iter.Key()
        v := iter.Value()
    
        e = r.Close()
        Check(e)
        return v
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?