ren
ren

Reputation: 3993

leveldb writebatch concurrency issue

If I have

using (WriteBatch batch = new WriteBatch())
{
    for(int i=0; i<100; i++)
    {
        batch.Put(i.ToString(), i.ToString())
    }
    leveld_db.Write(batch);
}

from one thread and

using (WriteBatch batch = new WriteBatch())
{
    for(int i=0; i<100; i++)
    {
        batch.Put(i.ToString(), (i+1000).ToString())
    }
    leveld_db.Write(batch);
}

from another concurrent thread, is there a guarantee that I won't end up with data like {10, 10} {11, 1011} ?

Upvotes: 1

Views: 563

Answers (1)

sel-fish
sel-fish

Reputation: 4476

Assume that you are wondering the thread-safe of WriteBatch operation in leveldb.
The answer is yes, this operation is thread-safe. In fact, every operation in leveldb is implemented using WriteBatch:

Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
  WriteBatch batch;
  batch.Put(key, value);
  return Write(opt, &batch);
}

And the write ops is queued to avoid racing:

  writers_.push_back(&w);
  while (!w.done && &w != writers_.front()) {
    w.cv.Wait();
  }
  if (w.done) {
    return w.status;
  }

Upvotes: 2

Related Questions