From 296245d23932557d025409733603e9c6e279d321 Mon Sep 17 00:00:00 2001 From: CCG Date: Thu, 19 Mar 2026 19:42:50 -0300 Subject: [PATCH 1/2] Remove fully concrete match + test_db_loader better split --- src/atomdb/inmemorydb/InMemoryDB.cc | 3 ++- src/atomdb/redis_mongodb/RedisMongoDB.cc | 3 ++- src/tests/main/tests_db_loader.cc | 14 +++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/atomdb/inmemorydb/InMemoryDB.cc b/src/atomdb/inmemorydb/InMemoryDB.cc index ac5772de..054f8ca2 100644 --- a/src/atomdb/inmemorydb/InMemoryDB.cc +++ b/src/atomdb/inmemorydb/InMemoryDB.cc @@ -716,7 +716,8 @@ vector> InMemoryDB::index_entries_combinations(unsigned int arity vector> index_entries; unsigned int total = 1 << arity; // 2^arity - for (unsigned int mask = 0; mask < total; ++mask) { + // Skip mask == 0 (all concrete): identical to the link's own handle; no separate pattern index. + for (unsigned int mask = 1; mask < total; ++mask) { vector index_entry; for (unsigned int i = 0; i < arity; ++i) { if (mask & (1 << i)) diff --git a/src/atomdb/redis_mongodb/RedisMongoDB.cc b/src/atomdb/redis_mongodb/RedisMongoDB.cc index e6d5a8c8..29fd6ecd 100644 --- a/src/atomdb/redis_mongodb/RedisMongoDB.cc +++ b/src/atomdb/redis_mongodb/RedisMongoDB.cc @@ -1307,7 +1307,8 @@ vector> RedisMongoDB::index_entries_combinations(unsigned int ari vector> index_entries; unsigned int total = 1 << arity; // 2^arity - for (unsigned int mask = 0; mask < total; ++mask) { + // Skip mask == 0 (all concrete): identical to the link's own handle; no separate pattern index. + for (unsigned int mask = 1; mask < total; ++mask) { vector index_entry; for (unsigned int i = 0; i < arity; ++i) { if (mask & (1 << i)) diff --git a/src/tests/main/tests_db_loader.cc b/src/tests/main/tests_db_loader.cc index 862d6310..0da6b66a 100644 --- a/src/tests/main/tests_db_loader.cc +++ b/src/tests/main/tests_db_loader.cc @@ -124,13 +124,13 @@ int main(int argc, char* argv[]) { atomic total_atoms_processed(0); for (int i = 0; i < num_threads; i++) { - size_t start_line = i * lines_per_thread; - size_t end_line = start_line + lines_per_thread; - - // Remainder must be added to the last thread - if (i == num_threads - 1) { - end_line++; - } + // Even split: thread i gets [start_line, end_line). Using integer division avoids + // the bug where "last thread gets +1" was always applied (OOB when remainder==0, + // e.g. 5 lines / 1 thread => end_line 6 and lines[5] is undefined). + size_t start_line = + (static_cast(i) * lines.size()) / static_cast(num_threads); + size_t end_line = + (static_cast(i + 1) * lines.size()) / static_cast(num_threads); threads.emplace_back([&, start_line, end_line, i]() -> void { auto thread_atomdb = AtomDBSingleton::get_instance(); From 079e2ba141f059612fa865d2af2adcf61f3c8ea1 Mon Sep 17 00:00:00 2001 From: CCG Date: Fri, 20 Mar 2026 08:07:39 -0300 Subject: [PATCH 2/2] Add default_pattern_index_schema() --- src/atomdb/redis_mongodb/RedisMongoDB.cc | 29 ++++++++++++------------ src/atomdb/redis_mongodb/RedisMongoDB.h | 1 + 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/atomdb/redis_mongodb/RedisMongoDB.cc b/src/atomdb/redis_mongodb/RedisMongoDB.cc index 29fd6ecd..ae4f25ac 100644 --- a/src/atomdb/redis_mongodb/RedisMongoDB.cc +++ b/src/atomdb/redis_mongodb/RedisMongoDB.cc @@ -1242,34 +1242,35 @@ void RedisMongoDB::load_pattern_index_schema() { LOG_INFO( "WARNING: No pattern_index_schema found, all possible patterns will be created during link " "insertion!"); + this->pattern_index_schema_map = default_pattern_index_schema(); } } -vector RedisMongoDB::match_pattern_index_schema(const Link* link) { - vector pattern_handles; - auto local_map = this->pattern_index_schema_map; - - if (local_map.size() == 0) { - vector tokens = {"LINK_TEMPLATE", "Expression", to_string(link->arity())}; - for (unsigned int i = 0; i < link->arity(); i++) { +map, vector>>> RedisMongoDB::default_pattern_index_schema() { + map, vector>>> default_map; + for (unsigned int arity = 1; arity <= 4; arity++) { + vector tokens = {"LINK_TEMPLATE", "Expression", to_string(arity)}; + for (unsigned int i = 0; i < arity; i++) { tokens.push_back("VARIABLE"); tokens.push_back("v" + to_string(i + 1)); } - - auto link_schema = LinkSchema(tokens); - auto index_entries = index_entries_combinations(link->arity()); - - local_map[1] = make_tuple(move(tokens), move(index_entries)); + auto index_entries = index_entries_combinations(arity); + default_map[arity] = make_tuple(move(tokens), move(index_entries)); } + return default_map; +} + +vector RedisMongoDB::match_pattern_index_schema(const Link* link) { + vector pattern_handles; vector sorted_keys; - for (const auto& pair : local_map) { + for (const auto& pair : this->pattern_index_schema_map) { sorted_keys.push_back(pair.first); } std::sort(sorted_keys.begin(), sorted_keys.end(), std::greater()); for (const auto& priority : sorted_keys) { - auto value = local_map[priority]; + const auto& value = this->pattern_index_schema_map.at(priority); auto link_schema = LinkSchema(get<0>(value)); auto index_entries = get<1>(value); Assignment assignment; diff --git a/src/atomdb/redis_mongodb/RedisMongoDB.h b/src/atomdb/redis_mongodb/RedisMongoDB.h index 7491210c..19b2f701 100644 --- a/src/atomdb/redis_mongodb/RedisMongoDB.h +++ b/src/atomdb/redis_mongodb/RedisMongoDB.h @@ -193,6 +193,7 @@ class RedisMongoDB : public AtomDB { void update_incoming_set(const string& key, const string& value); void load_pattern_index_schema(); + map, vector>>> default_pattern_index_schema(); vector match_pattern_index_schema(const Link* link); vector> index_entries_combinations(unsigned int arity);