Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix bulk loading during collection init.

Kishore Nallan5856263a0871
diff --git a/include/collection_manager.h b/include/collection_manager.h
index c3a692a6..0d2d7648 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -37,6 +37,16 @@ private:
~CollectionManager() = default;
+ Option<std::string> get_first_index_error(const std::vector<index_result> & items) {
+ for(const auto & item: items) {
+ if(!item.index_op.ok()) {
+ return Option<std::string>(item.index_op.error());
+ }
+ }
+
+ return Option<std::string>(404, "Not found");
+ }
+
public:
static CollectionManager & get_instance() {
static CollectionManager instance;
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index d3e57e48..47331a92 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -112,10 +112,12 @@ Option<bool> CollectionManager::init(Store *store,
const uint32_t seq_id = Collection::get_seq_id_key(iter->key().ToString());
nlohmann::json document;
- Option<uint32_t> seq_id_doc_op = collection->to_doc(iter->value().ToString(), document);
- if(!seq_id_doc_op.ok()) {
- return Option<bool>(500, "Error while parsing document."); // FIXME: populate error
+ try {
+ document = nlohmann::json::parse(iter->value().ToString());
+ } catch(const std::exception& e) {
+ LOG(ERR) << "JSON error: " << e.what();
+ return Option<bool>(false, "Bad JSON.");
}
iter_batch[seq_id % collection->get_num_indices()].push_back(
@@ -129,9 +131,12 @@ Option<bool> CollectionManager::init(Store *store,
iter_batch[i].clear();
}
- if(res.num_indexed != iter_batch.size()) {
+ if(res.num_indexed != res.items.size()) {
delete iter;
- return Option<bool>(false, "Error while loading records."); // FIXME: populate actual record error
+ const Option<std::string> & index_error_op = get_first_index_error(res.items);
+ if(index_error_op.ok()) {
+ return Option<bool>(false, index_error_op.get());
+ }
}
}
@@ -143,8 +148,11 @@ Option<bool> CollectionManager::init(Store *store,
batch_index_result res;
collection->par_index_in_memory(iter_batch, res);
- if(res.num_indexed != iter_batch.size()) {
- return Option<bool>(false, "Error while loading records."); // FIXME: populate actual record error
+ if(res.num_indexed != res.items.size()) {
+ const Option<std::string> & index_error_op = get_first_index_error(res.items);
+ if(index_error_op.ok()) {
+ return Option<bool>(false, index_error_op.get());
+ }
}
add_to_collections(collection);