leveldb-go示例,文档

huangapple go评论79阅读模式
英文:

leveldb-go example, docs

问题

LevelDB-Go 是用Go语言实现的LevelDB的移植版本。
LevelDB-Go通常被称为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?

答案1

得分: 6

我玩了一下leveldb,这是我目前得到的结果。这应该能帮你入门。

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("\n\n###### 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
}
英文:

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("\n\n###### 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

得分: 1

如果我记得没错的话,LevelDB-Go目前还不完整。这可能解释了缺乏文档和示例的原因。你可以使用项目的问题跟踪器来请求文档,或者联系作者询问是否已经可以使用。

英文:

If I recall from the mailing lists at this time LevelDB-Go is not complete. That likely explains the lack of documentation and examples. You could use the projects issue tracker to request documentation and/or ping the authors to see if it's ready for use or not.

答案3

得分: 1

LevelDB-Go尚未完成,但有一个名为levigo的leveldb包装器可用。
您也可以查阅其文档

英文:

LevelDB-Go is unfinished, but there is wrapper for leveldb available, named levigo.
You can consult its documentation too.

huangapple
  • 本文由 发表于 2012年9月29日 05:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/12647378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定