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

Use rocksdb batch put for atomic insertion.

Kishore Nallan13f4c7282734
diff --git a/TODO.md b/TODO.md
index ad069658..dc413cb5 100644
--- a/TODO.md
+++ b/TODO.md
@@ -87,6 +87,7 @@
- ~~clean special chars before indexing~~
- ~~Add docs/explanation around ranking calc~~
- ~~UTF-8 normalization~~
+- ~~Use rocksdb batch put for atomic insertion~~
- NOT operator support
- > INT32_MAX validation for float field
- Proper logging
@@ -95,7 +96,6 @@
- test for float int field deletion during doc deletion
- Test for snippets
- Test for replication
-- Use rocksdb batch put for atomic insertion
- Query token ids should match query token ordering
- ID should not have "/"
- Group results by field
diff --git a/include/store.h b/include/store.h
index de410e30..8ca371d2 100644
--- a/include/store.h
+++ b/include/store.h
@@ -7,6 +7,7 @@
#include <memory>
#include <option.h>
#include <rocksdb/db.h>
+#include <rocksdb/write_batch.h>
#include <rocksdb/options.h>
#include <rocksdb/merge_operator.h>
#include <rocksdb/transaction_log.h>
@@ -81,6 +82,11 @@ public:
return status.ok();
}
+ bool batch_write(rocksdb::WriteBatch& batch) {
+ rocksdb::Status status = db->Write(rocksdb::WriteOptions(), &batch);
+ return status.ok();
+ }
+
bool contains(const std::string& key) const {
std::string value;
rocksdb::Status status = db->Get(rocksdb::ReadOptions(), key, &value);
diff --git a/src/collection.cpp b/src/collection.cpp
index 38c65756..62b8600b 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -8,6 +8,7 @@
#include <art.h>
#include <thread>
#include <chrono>
+#include <rocksdb/write_batch.h>
Collection::Collection(const std::string name, const uint32_t collection_id, const uint32_t next_seq_id, Store *store,
const std::vector<field> &fields, const std::string & token_ranking_field):
@@ -95,8 +96,14 @@ Option<nlohmann::json> Collection::add(const std::string & json_str) {
return Option<nlohmann::json>(index_memory_op.code(), index_memory_op.error());
}
- store->insert(get_doc_id_key(document["id"]), seq_id_str);
- store->insert(get_seq_id_key(seq_id), document.dump());
+ rocksdb::WriteBatch batch;
+ batch.Put(get_doc_id_key(document["id"]), seq_id_str);
+ batch.Put(get_seq_id_key(seq_id), document.dump());
+ bool write_ok = store->batch_write(batch);
+
+ if(!write_ok) {
+ return Option<nlohmann::json>(500, "Could not write to on-disk storage.");
+ }
return Option<nlohmann::json>(document);
}
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index e407f128..6c52cb12 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -143,13 +143,19 @@ Option<Collection*> CollectionManager::create_collection(std::string name, const
Collection* new_collection = new Collection(name, next_collection_id, 0, store, fields, token_ranking_field);
next_collection_id++;
- store->insert(Collection::get_next_seq_id_key(name), std::to_string(0));
- store->insert(Collection::get_meta_key(name), collection_meta.dump());
- store->insert(NEXT_COLLECTION_ID_KEY, std::to_string(next_collection_id));
+ rocksdb::WriteBatch batch;
+ batch.Put(Collection::get_next_seq_id_key(name), std::to_string(0));
+ batch.Put(Collection::get_meta_key(name), collection_meta.dump());
+ batch.Put(NEXT_COLLECTION_ID_KEY, std::to_string(next_collection_id));
+ bool write_ok = store->batch_write(batch);
+
+ if(!write_ok) {
+ return Option<Collection*>(500, "Could not write to on-disk storage.");
+ }
add_to_collections(new_collection);
- return new_collection;
+ return Option<Collection*>(new_collection);
}
Collection* CollectionManager::get_collection(const std::string & collection_name) {
diff --git a/test/art_test.cpp b/test/art_test.cpp
index f971e8e4..7de01547 100644
--- a/test/art_test.cpp
+++ b/test/art_test.cpp
@@ -1005,14 +1005,16 @@ TEST(ArtTest, test_encode_int64) {
ASSERT_EQ(chars_100K[i], chars[i]);
}
- unsigned char chars_large_num[8] = {255, 255, 255, 255, 128, 0, 0, 199};
- encode_int64((int64_t)(std::numeric_limits<std::int32_t>::max())+200, chars);
+ unsigned char chars_large_num[8] = {0, 0, 0, 0, 128, 0, 0, 199};
+ int64_t large_num = (int64_t)(std::numeric_limits<std::int32_t>::max()) + 200;
+ std::cout << std::endl << large_num << std::endl;
+ encode_int64(large_num, chars);
for(uint32_t i = 0; i < 8; i++) {
ASSERT_EQ(chars_large_num[i], chars[i]);
}
- unsigned char chars_large_neg_num[8] = {0, 0, 0, 0, 127, 255, 255, 57};
- encode_int64(-1 * ((int64_t)(std::numeric_limits<std::int32_t>::max())+200), chars);
+ unsigned char chars_large_neg_num[8] = {255, 255, 255, 255, 127, 255, 255, 57};
+ encode_int64(-1 * large_num, chars);
for(uint32_t i = 0; i < 8; i++) {
ASSERT_EQ(chars_large_neg_num[i], chars[i]);
}