dqve65954 2014-10-12 21:33
浏览 155
已采纳

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?

  • 写回答

1条回答 默认 最新

  • ds122455 2014-10-13 00:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器