Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice committed Feb 24, 2024
1 parent 0f70aa0 commit fd26e03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/search/indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Status IndexUpdater::UpdateIndex(const std::string &field, std::string_view key,

auto s = storage->Write(storage->DefaultWriteOptions(), batch->GetWriteBatch());
if (!s.ok()) return {Status::NotOK, s.ToString()};
} else if ([[maybe_unused]] auto numeric = dynamic_cast<SearchNumericFieldMetadata *>(metadata)) {
} else if (auto numeric [[maybe_unused]] = dynamic_cast<SearchNumericFieldMetadata *>(metadata)) {
auto batch = storage->GetWriteBatchBase();

if (!original.empty()) {
Expand Down Expand Up @@ -180,17 +180,37 @@ Status IndexUpdater::UpdateIndex(const std::string &field, std::string_view key,
return Status::OK();
}

Status IndexUpdater::Update(const FieldValues &original, std::string_view key, const std::string &ns) {
auto current = GET_OR_RET(Record(key, ns));

for (const auto &[field, _] : fields) {
std::string_view original_val, current_val;

if (auto it = original.find(field); it != original.end()) {
original_val = it->second;
}
if (auto it = current.find(field); it != current.end()) {
current_val = it->second;
}

GET_OR_RET(UpdateIndex(field, key, original_val, current_val, ns));
}

return Status::OK();
}

void GlobalIndexer::Add(IndexUpdater updater) {
auto &up = updaters.emplace_back(std::move(updater));
for (const auto &prefix : up.prefixes) {
prefix_map.emplace(prefix, &up);
}
}

StatusOr<IndexUpdater::FieldValues> GlobalIndexer::Record(std::string_view key, const std::string &ns) {
StatusOr<GlobalIndexer::RecordResult> GlobalIndexer::Record(std::string_view key, const std::string &ns) {
auto iter = prefix_map.longest_prefix(key);
if (iter != prefix_map.end()) {
return iter.value()->Record(key, ns);
auto updater = iter.value();
return std::make_pair(updater, GET_OR_RET(updater->Record(key, ns)));
}

return {Status::NoPrefixMatched};
Expand Down
6 changes: 5 additions & 1 deletion src/search/indexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ struct IndexUpdater {
StatusOr<FieldValues> Record(std::string_view key, const std::string &ns);
Status UpdateIndex(const std::string &field, std::string_view key, std::string_view original,
std::string_view current, const std::string &ns);
Status Update(const FieldValues &original, std::string_view key, const std::string &ns);
};

struct GlobalIndexer {
using FieldValues = IndexUpdater::FieldValues;
using RecordResult = std::pair<IndexUpdater *, FieldValues>;

std::deque<IndexUpdater> updaters;
tsl::htrie_map<char, IndexUpdater *> prefix_map;

Expand All @@ -89,7 +93,7 @@ struct GlobalIndexer {
explicit GlobalIndexer(engine::Storage *storage) : storage(storage) {}

void Add(IndexUpdater updater);
StatusOr<IndexUpdater::FieldValues> Record(std::string_view key, const std::string &ns);
StatusOr<RecordResult> Record(std::string_view key, const std::string &ns);
};

} // namespace redis

0 comments on commit fd26e03

Please sign in to comment.