diff --git a/include/field.h b/include/field.h index ca29fa43..0b6307a2 100644 --- a/include/field.h +++ b/include/field.h @@ -9,6 +9,7 @@ #include #include #include "json.hpp" +#include namespace field_types { // first field value indexed will determine the type @@ -278,11 +279,17 @@ struct field { const std::string & default_sorting_field, nlohmann::json& fields_json) { bool found_default_sorting_field = false; + const std::regex sequence_id_pattern(".*_sequence_id$"); // Check for duplicates in field names std::map> unique_fields; for(const field & field: fields) { + if (std::regex_match(field.name, sequence_id_pattern)) { + // Don't add foo_sequence_id field. + continue; + } + unique_fields[field.name].push_back(&field); if(field.name == "id") { diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp index 0fb4d5be..f1540932 100644 --- a/src/collection_manager.cpp +++ b/src/collection_manager.cpp @@ -80,6 +80,10 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection } fields.push_back(f); + + if (!f.reference.empty()) { + fields.emplace_back(field(f.name + "_sequence_id", "string", false, f.optional, true)); + } } std::string default_sorting_field = collection_meta[Collection::COLLECTION_DEFAULT_SORTING_FIELD_KEY].get(); diff --git a/src/field.cpp b/src/field.cpp index a48c4637..4275577a 100644 --- a/src/field.cpp +++ b/src/field.cpp @@ -700,7 +700,7 @@ Option field::json_field_to_field(bool enable_nested_fields, nlohmann::jso if (!field_json[fields::reference].get().empty()) { the_fields.emplace_back( field(field_json[fields::name].get() + "_sequence_id", "string", false, - false, true) + field_json[fields::optional], true) ); } diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp index 81a311bb..f57895d4 100644 --- a/test/collection_join_test.cpp +++ b/test/collection_join_test.cpp @@ -92,10 +92,10 @@ TEST_F(CollectionJoinTest, SchemaReferenceField) { ASSERT_EQ(schema.at("customer_name").reference, ""); ASSERT_EQ(schema.at("product_id").reference, "Products.product_id"); - ASSERT_EQ(schema.count("product_id_sequence_id"), 1); - auto field = schema.at("product_id_sequence_id"); - ASSERT_TRUE(field.index); + // Index a `foo_sequence_id` field for `foo` reference field. + ASSERT_EQ(schema.count("product_id_sequence_id"), 1); + ASSERT_TRUE(schema.at("product_id_sequence_id").index); collectionManager.drop_collection("Customers"); } \ No newline at end of file diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp index 882f92fd..dd4debd2 100644 --- a/test/collection_manager_test.cpp +++ b/test/collection_manager_test.cpp @@ -221,7 +221,7 @@ TEST_F(CollectionManagerTest, CollectionCreation) { "locale":"", "name":"product_id", "nested":false, - "optional":false, + "optional":true, "sort":false, "type":"string", "reference":"Products.product_id" @@ -351,7 +351,11 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) { std::string json_line; while (std::getline(infile, json_line)) { - collection1->add(json_line); + auto op = collection1->add(json_line); + if (!op.ok()) { + LOG(INFO) << op.error(); + } + ASSERT_TRUE(op.ok()); } infile.close(); @@ -434,6 +438,7 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) { ASSERT_EQ(4, results["hits"].size()); tsl::htrie_map schema = collection1->get_schema(); + ASSERT_EQ(schema.count("product_id_sequence_id"), 1); // recreate collection manager to ensure that it restores the records from the disk backed store collectionManager.dispose(); @@ -472,6 +477,7 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) { ASSERT_TRUE(restored_schema.at("person").nested); ASSERT_EQ(2, restored_schema.at("person").nested_array); ASSERT_EQ(128, restored_schema.at("vec").num_dim); + ASSERT_EQ(restored_schema.count("product_id_sequence_id"), 1); ASSERT_TRUE(collection1->get_enable_nested_fields());