Skip to content

Commit db23413

Browse files
committed
[CF] WriteBatch to take in ColumnFamilyHandle
Summary: Client doesn't need to know anything about ColumnFamily ID. By making WriteBatch take ColumnFamilyHandle as a parameter, we can eliminate method GetID() from ColumnFamilyHandle Test Plan: column_family_test Reviewers: haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D16887
1 parent f0e1e3e commit db23413

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

db/column_family.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ColumnFamilyHandleImpl : public ColumnFamilyHandle {
4747
virtual ~ColumnFamilyHandleImpl();
4848
virtual ColumnFamilyData* cfd() const { return cfd_; }
4949

50-
virtual uint32_t GetID() const override;
50+
virtual uint32_t GetID() const;
5151

5252
private:
5353
ColumnFamilyData* cfd_;

db/column_family_test.cc

+8-5
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ TEST(ColumnFamilyTest, DontReuseColumnFamilyID) {
289289
Open();
290290
CreateColumnFamilies({"one", "two", "three"});
291291
for (size_t i = 0; i < handles_.size(); ++i) {
292-
ASSERT_EQ(i, handles_[i]->GetID());
292+
auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(handles_[i]);
293+
ASSERT_EQ(i, cfh->GetID());
293294
}
294295
if (iter == 1) {
295296
Reopen();
@@ -303,7 +304,8 @@ TEST(ColumnFamilyTest, DontReuseColumnFamilyID) {
303304
}
304305
CreateColumnFamilies({"three2"});
305306
// ID 3 that was used for dropped column family "three" should not be reused
306-
ASSERT_EQ(4, handles_[3]->GetID());
307+
auto cfh3 = reinterpret_cast<ColumnFamilyHandleImpl*>(handles_[3]);
308+
ASSERT_EQ(4, cfh3->GetID());
307309
Close();
308310
Destroy();
309311
}
@@ -362,12 +364,13 @@ TEST(ColumnFamilyTest, DropTest) {
362364

363365
TEST(ColumnFamilyTest, WriteBatchFailure) {
364366
Open();
367+
CreateColumnFamiliesAndReopen({"one", "two"});
365368
WriteBatch batch;
366-
batch.Put(1, Slice("non-existing"), Slice("column-family"));
369+
batch.Put(handles_[1], Slice("non-existing"), Slice("column-family"));
370+
ASSERT_OK(db_->Write(WriteOptions(), &batch));
371+
DropColumnFamilies({1});
367372
Status s = db_->Write(WriteOptions(), &batch);
368373
ASSERT_TRUE(s.IsInvalidArgument());
369-
CreateColumnFamilies({"one"});
370-
ASSERT_OK(db_->Write(WriteOptions(), &batch));
371374
Close();
372375
}
373376

db/db_impl.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -3992,21 +3992,21 @@ Status DB::Put(const WriteOptions& opt, ColumnFamilyHandle* column_family,
39923992
// 8 bytes are taken by header, 4 bytes for count, 1 byte for type,
39933993
// and we allocate 11 extra bytes for key length, as well as value length.
39943994
WriteBatch batch(key.size() + value.size() + 24);
3995-
batch.Put(column_family->GetID(), key, value);
3995+
batch.Put(column_family, key, value);
39963996
return Write(opt, &batch);
39973997
}
39983998

39993999
Status DB::Delete(const WriteOptions& opt, ColumnFamilyHandle* column_family,
40004000
const Slice& key) {
40014001
WriteBatch batch;
4002-
batch.Delete(column_family->GetID(), key);
4002+
batch.Delete(column_family, key);
40034003
return Write(opt, &batch);
40044004
}
40054005

40064006
Status DB::Merge(const WriteOptions& opt, ColumnFamilyHandle* column_family,
40074007
const Slice& key, const Slice& value) {
40084008
WriteBatch batch;
4009-
batch.Merge(column_family->GetID(), key, value);
4009+
batch.Merge(column_family, key, value);
40104010
return Write(opt, &batch);
40114011
}
40124012

db/write_batch.cc

+28-4
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,14 @@ void WriteBatchInternal::SetSequence(WriteBatch* b, SequenceNumber seq) {
173173
EncodeFixed64(&b->rep_[0], seq);
174174
}
175175

176-
void WriteBatch::Put(uint32_t column_family_id, const Slice& key,
176+
void WriteBatch::Put(ColumnFamilyHandle* column_family, const Slice& key,
177177
const Slice& value) {
178+
uint32_t column_family_id = 0;
179+
if (column_family != nullptr) {
180+
auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family);
181+
column_family_id = cfh->GetID();
182+
}
183+
178184
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
179185
if (column_family_id == 0) {
180186
rep_.push_back(static_cast<char>(kTypeValue));
@@ -186,8 +192,14 @@ void WriteBatch::Put(uint32_t column_family_id, const Slice& key,
186192
PutLengthPrefixedSlice(&rep_, value);
187193
}
188194

189-
void WriteBatch::Put(uint32_t column_family_id, const SliceParts& key,
195+
void WriteBatch::Put(ColumnFamilyHandle* column_family, const SliceParts& key,
190196
const SliceParts& value) {
197+
uint32_t column_family_id = 0;
198+
if (column_family != nullptr) {
199+
auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family);
200+
column_family_id = cfh->GetID();
201+
}
202+
191203
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
192204
if (column_family_id == 0) {
193205
rep_.push_back(static_cast<char>(kTypeValue));
@@ -199,7 +211,13 @@ void WriteBatch::Put(uint32_t column_family_id, const SliceParts& key,
199211
PutLengthPrefixedSliceParts(&rep_, value);
200212
}
201213

202-
void WriteBatch::Delete(uint32_t column_family_id, const Slice& key) {
214+
void WriteBatch::Delete(ColumnFamilyHandle* column_family, const Slice& key) {
215+
uint32_t column_family_id = 0;
216+
if (column_family != nullptr) {
217+
auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family);
218+
column_family_id = cfh->GetID();
219+
}
220+
203221
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
204222
if (column_family_id == 0) {
205223
rep_.push_back(static_cast<char>(kTypeDeletion));
@@ -210,8 +228,14 @@ void WriteBatch::Delete(uint32_t column_family_id, const Slice& key) {
210228
PutLengthPrefixedSlice(&rep_, key);
211229
}
212230

213-
void WriteBatch::Merge(uint32_t column_family_id, const Slice& key,
231+
void WriteBatch::Merge(ColumnFamilyHandle* column_family, const Slice& key,
214232
const Slice& value) {
233+
uint32_t column_family_id = 0;
234+
if (column_family != nullptr) {
235+
auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family);
236+
column_family_id = cfh->GetID();
237+
}
238+
215239
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
216240
if (column_family_id == 0) {
217241
rep_.push_back(static_cast<char>(kTypeMerge));

include/rocksdb/db.h

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ using std::unique_ptr;
2727
class ColumnFamilyHandle {
2828
public:
2929
virtual ~ColumnFamilyHandle() {}
30-
31-
virtual uint32_t GetID() const = 0;
3230
};
3331
extern const std::string default_column_family_name;
3432

include/rocksdb/write_batch.h

+11-8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
namespace rocksdb {
3232

3333
class Slice;
34+
class ColumnFamilyHandle;
3435
struct SliceParts;
3536

3637
class WriteBatch {
@@ -39,31 +40,33 @@ class WriteBatch {
3940
~WriteBatch();
4041

4142
// Store the mapping "key->value" in the database.
42-
void Put(uint32_t column_family_id, const Slice& key, const Slice& value);
43+
void Put(ColumnFamilyHandle* column_family, const Slice& key,
44+
const Slice& value);
4345
void Put(const Slice& key, const Slice& value) {
44-
Put(0, key, value);
46+
Put(nullptr, key, value);
4547
}
4648

4749
// Variant of Put() that gathers output like writev(2). The key and value
4850
// that will be written to the database are concatentations of arrays of
4951
// slices.
50-
void Put(uint32_t column_family_id, const SliceParts& key,
52+
void Put(ColumnFamilyHandle* column_family, const SliceParts& key,
5153
const SliceParts& value);
5254
void Put(const SliceParts& key, const SliceParts& value) {
53-
Put(0, key, value);
55+
Put(nullptr, key, value);
5456
}
5557

5658
// Merge "value" with the existing value of "key" in the database.
5759
// "key->merge(existing, value)"
58-
void Merge(uint32_t column_family_id, const Slice& key, const Slice& value);
60+
void Merge(ColumnFamilyHandle* column_family, const Slice& key,
61+
const Slice& value);
5962
void Merge(const Slice& key, const Slice& value) {
60-
Merge(0, key, value);
63+
Merge(nullptr, key, value);
6164
}
6265

6366
// If the database contains a mapping for "key", erase it. Else do nothing.
64-
void Delete(uint32_t column_family_id, const Slice& key);
67+
void Delete(ColumnFamilyHandle* column_family, const Slice& key);
6568
void Delete(const Slice& key) {
66-
Delete(0, key);
69+
Delete(nullptr, key);
6770
}
6871

6972
// Append a blob of arbitrary size to the records in this batch. The blob will

0 commit comments

Comments
 (0)