level-db golang implementation writing over existing values?

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

level-db golang implementation writing over existing values?

问题

我正在尝试使用leveldb-g实现,并遇到一些问题。

这是我的实现(基于这里的另一个答案here):

  1. package propertyData
  2. import (
  3. "code.google.com/p/leveldb-go/leveldb/db"
  4. "code.google.com/p/leveldb-go/leveldb/table"
  5. "log"
  6. "runtime"
  7. )
  8. const (
  9. DBFILE = "./admin.db"
  10. )
  11. var DBFS = db.DefaultFileSystem
  12. func AddDataToProperty(property, value string) {
  13. Connection, e := DBFS.Create(DBFILE)
  14. Check(e)
  15. w := table.NewWriter(Connection, nil)
  16. defer w.Close()
  17. e = w.Set([]byte(property), []byte(value), nil)
  18. }
  19. func GetDataFromProperty(property string) string {
  20. v := findOne([]byte(property))
  21. return string(v)
  22. }
  23. func findOne(k []byte) []byte {
  24. Connection, e := DBFS.Open(DBFILE)
  25. Check(e)
  26. r := table.NewReader(Connection, nil)
  27. v1, err := r.Get([]byte(k), nil)
  28. if err != nil {
  29. log.Fatalf("An error occurred finding one", err.Error())
  30. }
  31. return v1
  32. }
  33. func Check(e error) {
  34. if e != nil {
  35. _, file, line, _ := runtime.Caller(1)
  36. log.Fatalf("Bad Happened: %s, %s", file, line)
  37. }
  38. }

以及一个测试:

  1. package propertyData
  2. import (
  3. "com.levelsbeyond/admin/propertyData"
  4. "log"
  5. "os"
  6. "testing"
  7. )
  8. func TestAddProperty(t *testing.T) {
  9. os.RemoveAll("./admin.db")
  10. propertyData.AddDataToProperty("test.property", "one")
  11. propertyData.AddDataToProperty("test.property", "two")
  12. propertyData.AddDataToProperty("test.property", "three")
  13. propertyValue := propertyData.GetDataFromProperty("test.property")
  14. log.Println(propertyValue)
  15. propertyData.AddDataToProperty("test.different", "four")
  16. propertyValue = propertyData.GetDataFromProperty("test.different")
  17. log.Println(propertyValue)
  18. propertyValue = propertyData.GetDataFromProperty("test.property")
  19. log.Println(propertyValue)
  20. }

输出结果为:

  1. === RUN TestAddProperty
  2. 2013/09/16 10:47:50 three
  3. 2013/09/16 10:47:50 four
  4. 2013/09/16 10:47:50
  5. --- PASS: TestAddProperty (0.00 seconds)

好像写入第二个属性("property.different")会覆盖我已经存在的值。我确定我做了一些愚蠢的事情,非常感谢任何帮助。

编辑

我在findOne函数中添加了一些错误处理(感谢 @miltonb),实际上在那里遇到了一个错误,尽管我不确定该如何处理:

  1. === RUN TestAddProperty
  2. 2013/09/16 15:36:34 three
  3. 2013/09/16 15:36:34 four
  4. 2013/09/16 15:36:34 An error occurred finding one%!(EXTRA string=leveldb/db: not found)
  5. exit status 1
  6. FAIL command-line-arguments 0.018s
英文:

I am trying to use the leveldb-g implementation and having some issues.

Here is my implementaation (based on another answer here

  1. package propertyData
  2. import (
  3. "code.google.com/p/leveldb-go/leveldb/db"
  4. "code.google.com/p/leveldb-go/leveldb/table"
  5. "log"
  6. "runtime"
  7. )
  8. const (
  9. DBFILE = "./admin.db"
  10. )
  11. var DBFS = db.DefaultFileSystem
  12. func AddDataToProperty(property, value string) {
  13. Connection, e := DBFS.Create(DBFILE)
  14. Check(e)
  15. w := table.NewWriter(Connection, nil)
  16. defer w.Close()
  17. e = w.Set([]byte(property), []byte(value), nil)
  18. }
  19. func GetDataFromProperty(property string) string {
  20. v := findOne([]byte(property))
  21. return string(v)
  22. }
  23. func findOne(k []byte) []byte {
  24. Connection, e := DBFS.Open(DBFILE)
  25. Check(e)
  26. r := table.NewReader(Connection, nil)
  27. v1, err := r.Get([]byte(k), nil)
  28. if err != nil {
  29. log.Fatalf("An error occurred finding one", err.Error())
  30. }
  31. return v1
  32. }
  33. func Check(e error) {
  34. if e != nil {
  35. _, file, line, _ := runtime.Caller(1)
  36. log.Fatalf("Bad Happened: %s, %s", file, line)
  37. }
  38. }

and a test:

  1. package propertyData
  2. import (
  3. "com.levelsbeyond/admin/propertyData"
  4. "log"
  5. "os"
  6. "testing"
  7. )
  8. func TestAddProperty(t *testing.T) {
  9. os.RemoveAll("./admin.db")
  10. propertyData.AddDataToProperty("test.property", "one")
  11. propertyData.AddDataToProperty("test.property", "two")
  12. propertyData.AddDataToProperty("test.property", "three")
  13. propertyValue := propertyData.GetDataFromProperty("test.property")
  14. log.Println(propertyValue)
  15. propertyData.AddDataToProperty("test.different", "four")
  16. propertyValue = propertyData.GetDataFromProperty("test.different")
  17. log.Println(propertyValue)
  18. propertyValue = propertyData.GetDataFromProperty("test.property")
  19. log.Println(propertyValue)
  20. }

Which outputs:

  1. === RUN TestAddProperty
  2. 2013/09/16 10:47:50 three
  3. 2013/09/16 10:47:50 four
  4. 2013/09/16 10:47:50
  5. --- PASS: TestAddProperty (0.00 seconds)

It's like writing the second property ("property.different") Overwrites the values I already have in there. I'm sure I'm doing something dumb, any help would be greatly appreciated.

EDIT

I added some error handling in the findOne function (thanks @miltonb) and I actually am getting an error there, though I'm not sure what to make of it:

  1. === RUN TestAddProperty
  2. 2013/09/16 15:36:34 three
  3. 2013/09/16 15:36:34 four
  4. 2013/09/16 15:36:34 An error occurred finding one%!(EXTRA string=leveldb/db: not found)
  5. exit status 1
  6. FAIL command-line-arguments 0.018s

答案1

得分: 2

将切换到另一个实现leveldb的版本,该版本似乎有更好的文档,例如levidb(http://godoc.org/github.com/jmhodges/levigo)。

英文:

Change to another implementation of leveldb with what looks to be better documentation such as levidb http://godoc.org/github.com/jmhodges/levigo.

huangapple
  • 本文由 发表于 2013年9月17日 00:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/18833310.html
匿名

发表评论

匿名网友

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

确定