英文:
level-db golang implementation writing over existing values?
问题
我正在尝试使用leveldb-g实现,并遇到一些问题。
这是我的实现(基于这里的另一个答案here):
package propertyData
import (
"code.google.com/p/leveldb-go/leveldb/db"
"code.google.com/p/leveldb-go/leveldb/table"
"log"
"runtime"
)
const (
DBFILE = "./admin.db"
)
var DBFS = db.DefaultFileSystem
func AddDataToProperty(property, value string) {
Connection, e := DBFS.Create(DBFILE)
Check(e)
w := table.NewWriter(Connection, nil)
defer w.Close()
e = w.Set([]byte(property), []byte(value), nil)
}
func GetDataFromProperty(property string) string {
v := findOne([]byte(property))
return string(v)
}
func findOne(k []byte) []byte {
Connection, e := DBFS.Open(DBFILE)
Check(e)
r := table.NewReader(Connection, nil)
v1, err := r.Get([]byte(k), nil)
if err != nil {
log.Fatalf("An error occurred finding one", err.Error())
}
return v1
}
func Check(e error) {
if e != nil {
_, file, line, _ := runtime.Caller(1)
log.Fatalf("Bad Happened: %s, %s", file, line)
}
}
以及一个测试:
package propertyData
import (
"com.levelsbeyond/admin/propertyData"
"log"
"os"
"testing"
)
func TestAddProperty(t *testing.T) {
os.RemoveAll("./admin.db")
propertyData.AddDataToProperty("test.property", "one")
propertyData.AddDataToProperty("test.property", "two")
propertyData.AddDataToProperty("test.property", "three")
propertyValue := propertyData.GetDataFromProperty("test.property")
log.Println(propertyValue)
propertyData.AddDataToProperty("test.different", "four")
propertyValue = propertyData.GetDataFromProperty("test.different")
log.Println(propertyValue)
propertyValue = propertyData.GetDataFromProperty("test.property")
log.Println(propertyValue)
}
输出结果为:
=== RUN TestAddProperty
2013/09/16 10:47:50 three
2013/09/16 10:47:50 four
2013/09/16 10:47:50
--- PASS: TestAddProperty (0.00 seconds)
好像写入第二个属性("property.different")会覆盖我已经存在的值。我确定我做了一些愚蠢的事情,非常感谢任何帮助。
编辑
我在findOne函数中添加了一些错误处理(感谢 @miltonb),实际上在那里遇到了一个错误,尽管我不确定该如何处理:
=== RUN TestAddProperty
2013/09/16 15:36:34 three
2013/09/16 15:36:34 four
2013/09/16 15:36:34 An error occurred finding one%!(EXTRA string=leveldb/db: not found)
exit status 1
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
package propertyData
import (
"code.google.com/p/leveldb-go/leveldb/db"
"code.google.com/p/leveldb-go/leveldb/table"
"log"
"runtime"
)
const (
DBFILE = "./admin.db"
)
var DBFS = db.DefaultFileSystem
func AddDataToProperty(property, value string) {
Connection, e := DBFS.Create(DBFILE)
Check(e)
w := table.NewWriter(Connection, nil)
defer w.Close()
e = w.Set([]byte(property), []byte(value), nil)
}
func GetDataFromProperty(property string) string {
v := findOne([]byte(property))
return string(v)
}
func findOne(k []byte) []byte {
Connection, e := DBFS.Open(DBFILE)
Check(e)
r := table.NewReader(Connection, nil)
v1, err := r.Get([]byte(k), nil)
if err != nil {
log.Fatalf("An error occurred finding one", err.Error())
}
return v1
}
func Check(e error) {
if e != nil {
_, file, line, _ := runtime.Caller(1)
log.Fatalf("Bad Happened: %s, %s", file, line)
}
}
and a test:
package propertyData
import (
"com.levelsbeyond/admin/propertyData"
"log"
"os"
"testing"
)
func TestAddProperty(t *testing.T) {
os.RemoveAll("./admin.db")
propertyData.AddDataToProperty("test.property", "one")
propertyData.AddDataToProperty("test.property", "two")
propertyData.AddDataToProperty("test.property", "three")
propertyValue := propertyData.GetDataFromProperty("test.property")
log.Println(propertyValue)
propertyData.AddDataToProperty("test.different", "four")
propertyValue = propertyData.GetDataFromProperty("test.different")
log.Println(propertyValue)
propertyValue = propertyData.GetDataFromProperty("test.property")
log.Println(propertyValue)
}
Which outputs:
=== RUN TestAddProperty
2013/09/16 10:47:50 three
2013/09/16 10:47:50 four
2013/09/16 10:47:50
--- 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:
=== RUN TestAddProperty
2013/09/16 15:36:34 three
2013/09/16 15:36:34 four
2013/09/16 15:36:34 An error occurred finding one%!(EXTRA string=leveldb/db: not found)
exit status 1
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论