leveldb-go示例,文档

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

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,这是我目前得到的结果。这应该能帮你入门。

  1. package main
  2. import (
  3. "code.google.com/p/leveldb-go/leveldb/db"
  4. "code.google.com/p/leveldb-go/leveldb/table"
  5. "fmt"
  6. "runtime"
  7. )
  8. type kv struct {
  9. K []byte
  10. V []byte
  11. }
  12. type kvs struct {
  13. items map[int]kv
  14. }
  15. func (p *kv) PutKV(k []byte, v []byte) {
  16. p.K = k
  17. p.V = v
  18. }
  19. func (items *kvs) PutKVs() {
  20. fmt.Println(items)
  21. }
  22. func (p *kv) GetKV() (key []byte, value []byte) {
  23. key = p.K
  24. value = p.V
  25. return
  26. }
  27. func Check(e error) {
  28. if e != nil {
  29. _, file, line, _ := runtime.Caller(1)
  30. }
  31. }
  32. func p(r []byte, e error) {
  33. if e != nil {
  34. return e
  35. }
  36. println(string(r))
  37. }
  38. const (
  39. DBFILE = "/tmp/leveldb2.db"
  40. )
  41. var DBFS = db.DefaultFileSystem
  42. func main() {
  43. Connection, e := DBFS.Create(DBFILE)
  44. Check(e)
  45. w := table.NewWriter(Connection, nil)
  46. defer w.Close()
  47. e = w.Set([]byte("1"), []byte("red"), nil)
  48. Check(e)
  49. e = w.Set([]byte("2"), []byte("yellow"), nil)
  50. Check(e)
  51. e = w.Set([]byte("3"), []byte("blue"), nil)
  52. Check(e)
  53. e = w.Close()
  54. Check(e)
  55. w = nil
  56. count()
  57. fmt.Println("Printing # KV")
  58. itemsKV := readByte()
  59. fmt.Println(itemsKV[0])
  60. fmt.Println(itemsKV[1])
  61. fmt.Println(itemsKV[2])
  62. println("Done Printing # KV")
  63. Connection, e = DBFS.Create(DBFILE)
  64. Check(e)
  65. w = table.NewWriter(Connection, nil)
  66. defer w.Close()
  67. e = w.Set([]byte("4"), []byte("green"), nil)
  68. Check(e)
  69. e = w.Set([]byte("5"), []byte("white"), nil)
  70. Check(e)
  71. e = w.Set([]byte("6"), []byte("black"), nil)
  72. Check(e)
  73. e = w.Close()
  74. Check(e)
  75. }
  76. func count() {
  77. Connection, e := DBFS.Open(DBFILE)
  78. Check(e)
  79. b := []byte("0")
  80. r := table.NewReader(Connection, nil)
  81. println("\n\n###### Counting ###### ")
  82. iter, n := r.Find(b, nil), 0
  83. for iter.Next() {
  84. n++
  85. println("Count # ", n)
  86. }
  87. e = r.Close()
  88. Check(e)
  89. println("#####Total: ", n)
  90. }
  91. func read() map[int64]string {
  92. Connection, e := DBFS.Open(DBFILE)
  93. Check(e)
  94. b := []byte("0")
  95. r := table.NewReader(Connection, nil)
  96. items := map[int64]string{}
  97. iter, _ := r.Find(b, nil), 0
  98. for iter.Next() {
  99. k := iter.Key()
  100. v := iter.Value()
  101. items[int64(k[0])] = string(v)
  102. }
  103. e = r.Close()
  104. Check(e)
  105. return items
  106. }
  107. func readByte() map[int]kv {
  108. Connection, e := DBFS.Open(DBFILE)
  109. Check(e)
  110. c := 0
  111. b := []byte("0")
  112. r := table.NewReader(Connection, nil)
  113. //items := map[int64]kv{}
  114. item := new(kv)
  115. items := map[int]kv{}
  116. iter, _ := r.Find(b, nil), 0
  117. for iter.Next() {
  118. k := iter.Key()
  119. v := iter.Value()
  120. item.PutKV(k, v)
  121. items[c] = *item
  122. c++
  123. }
  124. e = r.Close()
  125. Check(e)
  126. return items
  127. }
  128. func findOne(k []byte) []byte {
  129. Connection, e := DBFS.Open(DBFILE)
  130. Check(e)
  131. b := []byte("0")
  132. r := table.NewReader(Connection, nil)
  133. iter, _ := r.Find(b, nil), 0
  134. k = iter.Key()
  135. v := iter.Value()
  136. e = r.Close()
  137. Check(e)
  138. return v
  139. }
英文:

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

  1. package main
  2. import (
  3. "code.google.com/p/leveldb-go/leveldb/db"
  4. "code.google.com/p/leveldb-go/leveldb/table"
  5. "fmt"
  6. "runtime"
  7. )
  8. type kv struct {
  9. K []byte
  10. V []byte
  11. }
  12. type kvs struct {
  13. items map[int]kv
  14. }
  15. func (p *kv) PutKV(k []byte, v []byte) {
  16. p.K = k
  17. p.V = v
  18. }
  19. func (items *kvs) PutKVs() {
  20. fmt.Println(items)
  21. }
  22. func (p *kv) GetKV() (key []byte, value []byte) {
  23. key = p.K
  24. value = p.V
  25. return
  26. }
  27. func Check(e error) {
  28. if e != nil {
  29. _, file, line, _ := runtime.Caller(1)
  30. }
  31. }
  32. func p(r []byte, e error) {
  33. if e != nil {
  34. return e
  35. }
  36. println(string(r))
  37. }
  38. const (
  39. DBFILE = "/tmp/leveldb2.db"
  40. )
  41. var DBFS = db.DefaultFileSystem
  42. func main() {
  43. Connection, e := DBFS.Create(DBFILE)
  44. Check(e)
  45. w := table.NewWriter(Connection, nil)
  46. defer w.Close()
  47. e = w.Set([]byte("1"), []byte("red"), nil)
  48. Check(e)
  49. e = w.Set([]byte("2"), []byte("yellow"), nil)
  50. Check(e)
  51. e = w.Set([]byte("3"), []byte("blue"), nil)
  52. Check(e)
  53. e = w.Close()
  54. Check(e)
  55. w = nil
  56. count()
  57. fmt.Println("Printing # KV")
  58. itemsKV := readByte()
  59. fmt.Println(itemsKV[0])
  60. fmt.Println(itemsKV[1])
  61. fmt.Println(itemsKV[2])
  62. println("Done Printing # KV")
  63. Connection, e = DBFS.Create(DBFILE)
  64. Check(e)
  65. w = table.NewWriter(Connection, nil)
  66. defer w.Close()
  67. e = w.Set([]byte("4"), []byte("green"), nil)
  68. Check(e)
  69. e = w.Set([]byte("5"), []byte("white"), nil)
  70. Check(e)
  71. e = w.Set([]byte("6"), []byte("black"), nil)
  72. Check(e)
  73. e = w.Close()
  74. Check(e)
  75. }
  76. func count() {
  77. Connection, e := DBFS.Open(DBFILE)
  78. Check(e)
  79. b := []byte("0")
  80. r := table.NewReader(Connection, nil)
  81. println("\n\n###### Counting ###### ")
  82. iter, n := r.Find(b, nil), 0
  83. for iter.Next() {
  84. n++
  85. println("Count # ", n)
  86. }
  87. e = r.Close()
  88. Check(e)
  89. println("#####Total: ", n)
  90. }
  91. func read() map[int64]string {
  92. Connection, e := DBFS.Open(DBFILE)
  93. Check(e)
  94. b := []byte("0")
  95. r := table.NewReader(Connection, nil)
  96. items := map[int64]string{}
  97. iter, _ := r.Find(b, nil), 0
  98. for iter.Next() {
  99. k := iter.Key()
  100. v := iter.Value()
  101. items[int64(k[0])] = string(v)
  102. }
  103. e = r.Close()
  104. Check(e)
  105. return items
  106. }
  107. func readByte() map[int]kv {
  108. Connection, e := DBFS.Open(DBFILE)
  109. Check(e)
  110. c := 0
  111. b := []byte("0")
  112. r := table.NewReader(Connection, nil)
  113. //items := map[int64]kv{}
  114. item := new(kv)
  115. items := map[int]kv{}
  116. iter, _ := r.Find(b, nil), 0
  117. for iter.Next() {
  118. k := iter.Key()
  119. v := iter.Value()
  120. item.PutKV(k, v)
  121. items[c] = *item
  122. c++
  123. }
  124. e = r.Close()
  125. Check(e)
  126. return items
  127. }
  128. func findOne(k []byte) []byte {
  129. Connection, e := DBFS.Open(DBFILE)
  130. Check(e)
  131. b := []byte("0")
  132. r := table.NewReader(Connection, nil)
  133. iter, _ := r.Find(b, nil), 0
  134. k = iter.Key()
  135. v := iter.Value()
  136. e = r.Close()
  137. Check(e)
  138. return v
  139. }

答案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:

确定