Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix removing attachments with numeric keys, [PR-113](https://github.com/reductstore/reduct-cpp/pull/113)
- Fix removing entry attachments whose keys start with `$`, #114

## 1.18.0 - 2026-02-04

Expand Down
10 changes: 9 additions & 1 deletion src/reduct/bucket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,23 @@ class Bucket : public IBucket {
const std::set<std::string>& attachment_keys) const noexcept override {
QueryOptions options;
if (!attachment_keys.empty()) {
const auto escape_attachment_key = [](const std::string& key) {
if (!key.empty() && key.front() == '$') {
return fmt::format("${}", key);
}
return key;
};

nlohmann::json when;
when["$in"] = nlohmann::json::array();
when["$in"].push_back({{"&key", {{"$cast", "string"}}}});
for (const auto& key : attachment_keys) {
when["$in"].push_back(key);
when["$in"].push_back(escape_attachment_key(key));
}
options.when = when.dump();
}


Batch remove_batch;
const auto meta_entry = fmt::format("{}/$meta", entry_name);
auto query_err = QueryV2({meta_entry}, std::nullopt, std::nullopt, std::move(options),
Expand Down
21 changes: 21 additions & 0 deletions tests/reduct/entry_api_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,27 @@ TEST_CASE("reduct::IBucket should remove entry attachments with numeric keys", "
REQUIRE(stored_after.empty());
}

TEST_CASE("reduct::IBucket should remove entry attachments with reserved keys", "[entry_api][1_19]") {
Fixture ctx;
auto [bucket, _] = ctx.client->CreateBucket(kBucketName);
REQUIRE(bucket);

IBucket::AttachmentMap attachments{
{"meta-1", R"({"value":1})"},
{"$system", R"({"value":"test"})"},
{"meta-2", R"({"value":2})"},
};

REQUIRE(bucket->WriteAttachments("entry-1", attachments) == Error::kOk);
REQUIRE(bucket->RemoveAttachments("entry-1", std::set<std::string>{"meta-1", "$system"}) == Error::kOk);

auto [stored, err] = bucket->ReadAttachments("entry-1");
REQUIRE(err == Error::kOk);
REQUIRE(stored.size() == 1);
REQUIRE(stored.contains("meta-2"));
REQUIRE(nlohmann::json::parse(stored.at("meta-2")) == nlohmann::json::parse(attachments.at("meta-2")));
}

TEST_CASE("Batch should slice data", "[batch]") {
auto batch = IBucket::Batch();

Expand Down
Loading