Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Aug 21, 2024
1 parent 7b6ce96 commit bcd9ffa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
49 changes: 21 additions & 28 deletions src/fpindex/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ void Index::AddSegment(std::shared_ptr<BaseSegment> segment) {
}
}
info_->add_segments()->CopyFrom(current_segment_->info());
auto snapshot = std::make_shared<IndexSnapshot>(info_, segments_);
snapshot_.store(snapshot);
snapshot_.store(std::make_shared<IndexSnapshot>(info_, segments_));
}

int Index::GetNextSegmentId() {
Expand All @@ -149,9 +148,20 @@ int Index::GetNextSegmentId() {
return 0;
}

std::shared_ptr<SegmentBuilder> Index::GetCurrentSegment() {
bool Index::Update(IndexUpdate&& update) {
std::lock_guard<std::mutex> lock(mutex_);

if (!oplog_ || !oplog_->IsReady()) {
LOG_ERROR() << "oplog is not open";
return false;
}

auto last_oplog_id = oplog_->GetLastId();
if (!last_oplog_id) {
LOG_ERROR() << "failed to get last oplog id";
return false;
}

if (!current_segment_) {
current_segment_ = std::make_shared<SegmentBuilder>(GetNextSegmentId());
AddSegment(current_segment_);
Expand All @@ -167,44 +177,25 @@ std::shared_ptr<SegmentBuilder> Index::GetCurrentSegment() {
writer_cv_.notify_one();
}

return current_segment_;
}

bool Index::Update(IndexUpdate&& update) {
auto current_segment = GetCurrentSegment();

std::lock_guard<std::mutex> lock(mutex_);

if (!oplog_ || !oplog_->IsReady()) {
LOG_ERROR() << "oplog is not open";
return false;
}

auto last_oplog_id = oplog_->GetLastId();
if (!last_oplog_id) {
LOG_ERROR() << "failed to get last oplog id";
return false;
}

if (current_segment->max_oplog_id() != last_oplog_id) {
if (current_segment_->max_oplog_id() != last_oplog_id) {
LOG_ERROR() << "current_segment is not up-to-date";
return false;
}

auto check_update = [=](const auto& entries) {
return current_segment->CheckUpdate(entries);
auto check_update = [this](const auto& entries) {
return current_segment_->CheckUpdate(entries);
};
auto entries = update.Finish();
if (!oplog_->Write(entries, check_update)) {
LOG_ERROR() << "failed to write oplog";
return false;
}

current_segment->Update(entries);
current_segment_->Update(entries);

for (auto i = info_->segments_size() - 1; i >= 0; i--) {
if (info_->segments(i).id() == current_segment->id()) {
info_->mutable_segments(i)->CopyFrom(current_segment->info());
if (info_->segments(i).id() == current_segment_->id()) {
info_->mutable_segments(i)->CopyFrom(current_segment_->info());
break;
}
}
Expand All @@ -216,6 +207,8 @@ bool Index::Update(IndexUpdate&& update) {
}
}

snapshot_.store(std::make_shared<IndexSnapshot>(info_, segments_));

return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/fpindex/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class IndexSnapshot {
bool Search(const std::vector<uint32_t> &hashes, std::vector<SearchResult> *results);

private:
std::mutex mutex_;
std::shared_ptr<IndexInfo> info_;
std::map<uint32_t, std::shared_ptr<BaseSegment>> segments_;
};
Expand All @@ -42,7 +43,7 @@ class Index {
bool Search(const std::vector<uint32_t> &hashes, std::vector<SearchResult> *results);

private:
std::shared_ptr<SegmentBuilder> GetCurrentSegment();
void EnsureWritable();
void Writer();

void AddSegment(std::shared_ptr<BaseSegment> segment);
Expand Down

0 comments on commit bcd9ffa

Please sign in to comment.