Skip to content

Commit

Permalink
fix issue 9 (#9).
Browse files Browse the repository at this point in the history
like associateMergeOperator, remove `AllowSingleOperand` and `ShouldMerge` solve this problem
  • Loading branch information
cocalele committed Oct 24, 2022
1 parent eecd063 commit a06eb55
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
6 changes: 4 additions & 2 deletions include/vn_data_merge.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using ROCKSDB_NAMESPACE::MergeOperator;
using ROCKSDB_NAMESPACE::Slice;
using ROCKSDB_NAMESPACE::Logger;
using ROCKSDB_NAMESPACE::AssociativeMergeOperator;

//#define USE_ASSOCIATE_MERGE 1
#ifdef USE_ASSOCIATE_MERGE
class ViveDataMergeOperator : public AssociativeMergeOperator
{
Expand Down Expand Up @@ -59,8 +61,8 @@ class ViveDataMergeOperator : public MergeOperator
// Logger* logger) const;
virtual bool FullMergeV2(const MergeOperationInput& merge_in,
MergeOperationOutput* merge_out) const override;
virtual bool AllowSingleOperand() const override { return true; }
virtual bool ShouldMerge(const std::vector<Slice>& /*operands*/) const override { return true; }
//virtual bool AllowSingleOperand() const override { return true; }
//virtual bool ShouldMerge(const std::vector<Slice>& /*operands*/) const override { return true; }


virtual const char* Name() const {
Expand Down
44 changes: 15 additions & 29 deletions src/vn_data_merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,6 @@ static inline int is_bit_set(int16_t& bmp, int index)
// }
// return true;
//}
bool ViveDataMergeOperator::PartialMerge(const Slice& key,
const Slice& left_operand,
const Slice& right_operand,
std::string* new_value,
Logger* logger) const
{
const struct pfs_extent_head* left_ext_head = (const struct pfs_extent_head* )left_operand.data();
const char* left_data_buf = ((const char*)left_ext_head) + sizeof(struct pfs_extent_head);
const struct pfs_extent_head* right_ext_head = (const struct pfs_extent_head*)right_operand.data();
char* right_data_buf = ((char*)right_ext_head) + sizeof(struct pfs_extent_head);
const struct pfs_extent_key* ext_key = (const struct pfs_extent_key*)key.data();
S5LOG_DEBUG("PartialMerge extent:%s with left:%d + right:%d bytes", ext_key->to_string(),
left_operand.size() - sizeof(struct pfs_extent_head),
right_operand.size() - sizeof(struct pfs_extent_head));


size_t ext_begin = min(left_ext_head->merge_off, right_ext_head->merge_off);
size_t ext_end = max(left_ext_head->merge_off + left_operand.size() - sizeof(struct pfs_extent_head),
right_ext_head->merge_off + right_operand.size() - sizeof(struct pfs_extent_head));

new_value->resize(ext_end - ext_begin + sizeof(struct pfs_extent_head));
struct pfs_extent_head* new_ext_head = (struct pfs_extent_head* )new_value->data();
char* new_data_buf = ((char*)new_ext_head) + sizeof(struct pfs_extent_head);
memcpy(new_data_buf + (left_ext_head->merge_off - ext_begin), left_data_buf, left_operand.size() - sizeof(struct pfs_extent_head));
memcpy(new_data_buf + (right_ext_head->merge_off - ext_begin), right_data_buf, right_operand.size() - sizeof(struct pfs_extent_head));
new_ext_head->merge_off = (int16_t)ext_begin;
return true;
}


//bool ViveDataMergeOperator::PartialMergeMulti(const Slice& key,
Expand Down Expand Up @@ -144,6 +116,19 @@ static bool _merge(const Slice& key,
new_ext_head->merge_off = (int16_t)ext_begin;
return true;
}
bool ViveDataMergeOperator::PartialMerge(const Slice& key,
const Slice& left_operand,
const Slice& right_operand,
std::string* new_value,
Logger* logger) const
{

//这个函数实现和AssociativeMergeOperator 不同,导致了bug?
return _merge(key, &left_operand, right_operand, new_value, logger);

}


bool ViveDataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in,
MergeOperationOutput* merge_out) const
{
Expand All @@ -170,7 +155,7 @@ bool ViveDataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in,

// The result will be in *new_value. All merges succeeded.
return true;
#endif
#else

const struct pfs_extent_key* ext_key = (const struct pfs_extent_key*)merge_in.key.data();
S5LOG_DEBUG("FullMergeV2 extent:%s with %d operand", ext_key->to_string(), merge_in.operand_list.size());
Expand Down Expand Up @@ -204,6 +189,7 @@ bool ViveDataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in,
}

return true;
#endif
}
#else
bool ViveDataMergeOperator::Merge(const Slice& key,
Expand Down
18 changes: 10 additions & 8 deletions src/vn_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ ViveFsContext* vn_mount(const char* db_path) {
ctx->root_inode.i_links_count = 2;
ctx->root_inode.i_size = 4096;
ctx->root_inode.i_atime = ctx->root_inode.i_ctime = time(NULL);
ctx->root_inode.ref_cnt = 1;

string v;
s = ctx->db->Get(ctx->read_opt, ctx->meta_cf, INODE_SEED_KEY, &v);
Expand Down Expand Up @@ -156,6 +157,7 @@ ViveFsContext* vn_mount(const char* db_path) {
return NULL;
}
f->i_no = VN_ROOT_INO;
vn_add_inode_ref(&ctx->root_inode);
f->inode = &ctx->root_inode;
f->file_name = "/";
ctx->root_file.reset(f);
Expand All @@ -177,14 +179,14 @@ int vn_umount(ViveFsContext* ctx)
cfs.push_back(ctx->default_cf);

//TODO: the following Get may cause segfault. reason still unknown
PinnableSlice v;
Status s = ctx->db->Get(ctx->read_opt, ctx->meta_cf, INODE_SEED_KEY, &v);
if (!s.ok()) {
S5LOG_ERROR("Failed GET key:%s, %s", INODE_SEED_KEY, s.ToString().c_str());
}
else {
fprintf(stderr, "Succeed get key:%s\n", INODE_SEED_KEY);
}
//PinnableSlice v;
//Status s = ctx->db->Get(ctx->read_opt, ctx->meta_cf, INODE_SEED_KEY, &v);
//if (!s.ok()) {
// S5LOG_ERROR("Failed GET key:%s, %s", INODE_SEED_KEY, s.ToString().c_str());
//}
//else {
// fprintf(stderr, "Succeed get key:%s\n", INODE_SEED_KEY);
//}
ctx->db->Flush(FlushOptions(), cfs);
ctx->db->SyncWAL();
CancelAllBackgroundWork(ctx->db, true);
Expand Down
4 changes: 2 additions & 2 deletions testing/vntest_1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ assert_equal $? 0

info "Now umount and shutdown"
assert umount $MNT_DIR
kill -s 2 $NFSD_PID
kill $NFSD_PID
assert wait $NFSD_PID

info "Restart nfsd"
Expand All @@ -66,7 +66,7 @@ assert_equal "$str2" "$(cat $MNT_DIR/dd/f2.txt)"

info "Now umount and shutdown"
assert umount $MNT_DIR
kill -s 2 $NFSD_PID
kill $NFSD_PID
assert wait $NFSD_PID

info "Test OK"

0 comments on commit a06eb55

Please sign in to comment.