Go中多线程LevelDB的合适方法是什么?

I have implemented the levigo wrapper in my project so I can use LevelDB. The declaration is fairly boilerplate, like so:

func NewLeveldbStorage(dbPath string) *leveldbStorage {
     opts := levigo.NewOptions()
     opts.SetCache(levigo.NewLRUCache(3<<30))
     opts.SetCreateIfMissing(true)
     log.Debugf("Entering Open")
     db, err := levigo.Open(dbPath, opts); if err != nil {
         log.Fatal("BOOM %v", err)
     }
     log.Debugf("Finished calling open")
     opts.Close()
     return &leveldbStorage{db:db}
 }

Here is the struct returned:

type leveldbStorage struct {
     db *levigo.DB
}

I then made a few simple GET and STORE commands on the struct that essentially just use s.db.Get and s.db.Put. This works fine in my tests, but when I run the following benchmark:

 func BenchmarkLeviDbGet(b *testing.B) {
     s := storage.NewLeveldbStorage("/path/to/db")
     value := "value"
     uid,_ := s.Store(value)

     b.ResetTimer()
     for i := 0; i < b.N; i++ {
         s.Get(uid)
     }

This benchmark, when run, returns:

 2014/10/12 21:17:09 BOOM %vIO error: lock /path/to/db/LOCK: already held by process

Is there an appropriate way to use levigo/leveldb to enable multithreaded reading? What about writing? I would not be surprised if multithreaded writing is not possible, but multithreaded reading seems like it should be. What am I doing wrong here?

duanshan2988
duanshan2988 在BenchmarkLeviDbGet之后关闭数据库吗?
大约 6 年之前 回复

1个回答



您需要关闭数据库文件或对其使用全局实例,不能多次打开该文件,可以 但是,可以从多个goroutine中访问同一实例。</ p>
</ div>

展开原文

原文

You either need to close the database file or use a global instance to it, you can't open the file multiple times, you can however access the same instance from multiple goroutines.

doudula1974
doudula1974 是的,我误解了基准设置; 我以为它将继续在for循环中运行事物,但实际上它会连续运行整个过程。
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐