Skip to content

Commit

Permalink
Move custom code from VBucket::Attach to PersistTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHogan committed Oct 13, 2021
1 parent 8331bda commit dba58bf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 64 deletions.
49 changes: 30 additions & 19 deletions src/api/traits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ FileMappingTrait::FileMappingTrait(
const std::string &filename,
std::unordered_map<std::string, u64> &offset_map,
FILE *fh,
TraitCallback flush_cb, TraitCallback load_cb)
OnLinkCallback flush_cb, OnLinkCallback load_cb)
: Trait(HERMES_FILE_TRAIT, TraitIdArray(), TraitType::FILE_MAPPING),
flush_cb(flush_cb),
load_cb(load_cb),
Expand All @@ -52,30 +52,30 @@ FileMappingTrait::FileMappingTrait(
std::placeholders::_3);
}

void FileMappingTrait::onAttach(HermesPtr hermes, TraitInput &input,
Trait *trait) {
if (load_cb) {
load_cb(hermes, input, trait);
// TODO(hari): @errorhandling Check if load was successful
}
void FileMappingTrait::onAttach(HermesPtr hermes, VBucketID id, Trait *trait) {
(void)hermes;
(void)id;
(void)trait;
}

void FileMappingTrait::onDetach(HermesPtr hermes, TraitInput &input,
Trait *trait) {
if (flush_cb) {
flush_cb(hermes, input, trait);
// TODO(hari): @errorhandling Check if flush was successful
}
void FileMappingTrait::onDetach(HermesPtr hermes, VBucketID id, Trait *trait) {
(void)hermes;
(void)id;
(void)trait;
}

void FileMappingTrait::onLink(HermesPtr hermes, TraitInput &input,
Trait *trait) {
onAttach(hermes, input, trait);
(void)hermes;
(void)input;
(void)trait;
}

void FileMappingTrait::onUnlink(HermesPtr hermes, TraitInput &input,
Trait *trait) {
onDetach(hermes, input, trait);
(void)hermes;
(void)input;
(void)trait;
}

PersistTrait::PersistTrait(FileMappingTrait mapping, bool synchronous)
Expand All @@ -95,13 +95,24 @@ PersistTrait::PersistTrait(FileMappingTrait mapping, bool synchronous)
std::placeholders::_3);
}

void PersistTrait::onAttach(HermesPtr hermes, TraitInput &input, Trait *trait) {
onLink(hermes, input, trait);
void PersistTrait::onAttach(HermesPtr hermes, VBucketID id, Trait *trait) {
SharedMemoryContext *context = &hermes->context_;
RpcContext *rpc = &hermes->rpc_;

auto blob_ids =
GetBlobsFromVBucketInfo(context, rpc, id);
for (const auto& blob_id : blob_ids) {
TraitInput input;
auto bucket_id = GetBucketIdFromBlobId(context, rpc, blob_id);
input.bucket_name = GetBucketNameById(context, rpc, bucket_id);
input.blob_name = GetBlobNameFromId(context, rpc, blob_id);
onLink(hermes, input, trait);
}
}

void PersistTrait::onDetach(HermesPtr hermes, TraitInput &input, Trait *trait) {
void PersistTrait::onDetach(HermesPtr hermes, VBucketID id, Trait *trait) {
(void)hermes;
(void)input;
(void)id;
(void)trait;
}

Expand Down
26 changes: 13 additions & 13 deletions src/api/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ typedef BlobInfo TraitInput;
struct Trait;
using HermesPtr = std::shared_ptr<Hermes>;

typedef
std::function<void(HermesPtr, TraitInput &, Trait *)> TraitCallback;
typedef std::function<void(HermesPtr, TraitInput &, Trait *)> OnLinkCallback;
typedef std::function<void(HermesPtr, VBucketID, Trait *)> OnAttachCallback;

struct Trait {
TraitID id;
TraitIdArray conflict_traits;
TraitType type;
TraitCallback onAttachFn;
TraitCallback onDetachFn;
TraitCallback onLinkFn;
TraitCallback onUnlinkFn;
OnAttachCallback onAttachFn;
OnAttachCallback onDetachFn;
OnLinkCallback onLinkFn;
OnLinkCallback onUnlinkFn;

Trait() {}
Trait(TraitID id, TraitIdArray conflict_traits, TraitType type);
Expand All @@ -50,18 +50,18 @@ struct Trait {
#define HERMES_PERSIST_TRAIT 11

struct FileMappingTrait : public Trait {
TraitCallback flush_cb;
TraitCallback load_cb;
OnLinkCallback flush_cb;
OnLinkCallback load_cb;
std::string filename;
std::unordered_map<std::string, u64> offset_map;
FILE *fh;

FileMappingTrait() {}
FileMappingTrait(const std::string &filename,
std::unordered_map<std::string, u64> &offset_map, FILE *fh,
TraitCallback flush_cb, TraitCallback load_cb);
void onAttach(HermesPtr hermes, TraitInput &blob, Trait *trait);
void onDetach(HermesPtr hermes, TraitInput &blob, Trait *trait);
OnLinkCallback flush_cb, OnLinkCallback load_cb);
void onAttach(HermesPtr hermes, VBucketID id, Trait *trait);
void onDetach(HermesPtr hermes, VBucketID id, Trait *trait);
void onLink(HermesPtr hermes, TraitInput &blob, Trait *trait);
void onUnlink(HermesPtr hermes, TraitInput &blob, Trait *trait);
};
Expand All @@ -74,8 +74,8 @@ struct PersistTrait : public Trait {
explicit PersistTrait(FileMappingTrait mapping,
bool synchronous = false);

void onAttach(HermesPtr hermes, TraitInput &blob, Trait *trait);
void onDetach(HermesPtr hermes, TraitInput &blob, Trait *trait);
void onAttach(HermesPtr hermes, VBucketID id, Trait *trait);
void onDetach(HermesPtr hermes, VBucketID id, Trait *trait);
void onLink(HermesPtr hermes, TraitInput &blob, Trait *trait);
void onUnlink(HermesPtr hermes, TraitInput &blob, Trait *trait);
};
Expand Down
42 changes: 11 additions & 31 deletions src/api/vbucket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,12 @@ Status VBucket::Attach(Trait* trait, Context& ctx) {
}
}
if (!selected_trait) {
auto blob_ids =
GetBlobsFromVBucketInfo(&hermes_->context_, &hermes_->rpc_, id_);
for (const auto& blob_id : blob_ids) {
Trait* t = static_cast<Trait*>(trait);
TraitInput input;
auto bucket_id =
GetBucketIdFromBlobId(&hermes_->context_, &hermes_->rpc_, blob_id);
input.bucket_name =
GetBucketNameById(&hermes_->context_, &hermes_->rpc_, bucket_id);
input.blob_name =
GetBlobNameFromId(&hermes_->context_, &hermes_->rpc_, blob_id);
if (t->onAttachFn != nullptr) {
t->onAttachFn(hermes_, input, trait);
// TODO(hari): @errorhandling Check if attach was successful
}
Trait* t = static_cast<Trait*>(trait);
if (t->onAttachFn != nullptr) {
t->onAttachFn(hermes_, id_, trait);
// TODO(hari): @errorhandling Check if attach was successful
}

attached_traits_.push_back(trait);
} else {
ret = TRAIT_EXISTS_ALREADY;
Expand Down Expand Up @@ -232,22 +222,12 @@ Status VBucket::Detach(Trait* trait, Context& ctx) {
}
}
if (selected_trait) {
auto blob_ids =
GetBlobsFromVBucketInfo(&hermes_->context_, &hermes_->rpc_, id_);
for (const auto& blob_id : blob_ids) {
Trait* t = static_cast<Trait*>(trait);
TraitInput input;
auto bucket_id =
GetBucketIdFromBlobId(&hermes_->context_, &hermes_->rpc_, blob_id);
input.bucket_name =
GetBucketNameById(&hermes_->context_, &hermes_->rpc_, bucket_id);
input.blob_name =
GetBlobNameFromId(&hermes_->context_, &hermes_->rpc_, blob_id);
if (t->onDetachFn != nullptr) {
t->onDetachFn(hermes_, input, trait);
// TODO(hari): @errorhandling Check if detach was successful
}
Trait* t = static_cast<Trait*>(trait);
if (t->onDetachFn != nullptr) {
t->onDetachFn(hermes_, id_, trait);
// TODO(hari): @errorhandling Check if detach was successful
}

attached_traits_.erase(selected_trait_iter);
} else {
ret = TRAIT_NOT_VALID;
Expand Down Expand Up @@ -341,7 +321,7 @@ Status VBucket::Destroy(Context& ctx) {
for (const auto& t : attached_traits_) {
if (t->onDetachFn != nullptr) {
TraitInput input = {};
t->onDetachFn(hermes_, input, t);
t->onDetachFn(hermes_, id_, t);
// TODO(hari): @errorhandling Check if detach was successful
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/bucket_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int compress_blob(HermesPtr hermes, hapi::TraitInput &input,
struct MyTrait : public hapi::Trait {
int compress_level;
MyTrait() : Trait(10001, hermes::TraitIdArray(), hapi::TraitType::META) {
onAttachFn =
onLinkFn =
std::bind(&compress_blob, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3);
}
Expand Down

0 comments on commit dba58bf

Please sign in to comment.