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

Pull index count into configs.

Kishore Nallan2f0f3f9c9aad
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 55e7d7c3..0fffd5ad 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -31,6 +31,8 @@ private:
std::string auth_key;
std::string search_only_auth_key;
+ size_t default_num_indices;
+
CollectionManager();
~CollectionManager() = default;
@@ -44,7 +46,10 @@ public:
CollectionManager(CollectionManager const&) = delete;
void operator=(CollectionManager const&) = delete;
- Option<bool> init(Store *store, const std::string & auth_key, const std::string & search_only_auth_key);
+ Option<bool> init(Store *store,
+ const size_t default_num_indices,
+ const std::string & auth_key,
+ const std::string & search_only_auth_key);
// frees in-memory data structures when server is shutdown - helps us run a memory leak detecter properly
void dispose();
diff --git a/include/config.h b/include/config.h
index ec7e9cf2..01e3bd49 100644
--- a/include/config.h
+++ b/include/config.h
@@ -23,6 +23,8 @@ private:
bool enable_cors;
+ size_t indices_per_collection;
+
std::string config_file;
int config_file_validity;
@@ -32,6 +34,7 @@ public:
this->listen_address = "0.0.0.0";
this->listen_port = 8108;
this->enable_cors = false;
+ this->indices_per_collection = 4;
}
// setters
@@ -75,6 +78,10 @@ public:
void set_enable_cors(bool enable_cors) {
this->enable_cors = enable_cors;
}
+
+ void set_indices_per_collection(size_t indices_per_collection) {
+ this->indices_per_collection = indices_per_collection;
+ }
// getters
@@ -118,6 +125,10 @@ public:
return this->enable_cors;
}
+ size_t get_indices_per_collection() {
+ return indices_per_collection;
+ }
+
// loaders
std::string get_env(const char *name) {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 01047b38..6af74123 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -29,7 +29,8 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
collection_next_seq_id,
store,
fields,
- default_sorting_field);
+ default_sorting_field,
+ default_num_indices);
return collection;
}
@@ -39,11 +40,14 @@ void CollectionManager::add_to_collections(Collection* collection) {
collection_id_names.emplace(collection->get_collection_id(), collection->get_name());
}
-Option<bool> CollectionManager::init(Store *store, const std::string & auth_key,
+Option<bool> CollectionManager::init(Store *store,
+ const size_t default_num_indices,
+ const std::string & auth_key,
const std::string & search_only_auth_key) {
this->store = store;
this->auth_key = auth_key;
this->search_only_auth_key = search_only_auth_key;
+ this->default_num_indices = default_num_indices;
std::string next_collection_id_str;
StoreStatus next_coll_id_status = store->get(NEXT_COLLECTION_ID_KEY, next_collection_id_str);
@@ -196,7 +200,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string name,
collection_meta[COLLECTION_CREATED] = created_at;
Collection* new_collection = new Collection(name, next_collection_id, created_at, 0, store, fields,
- default_sorting_field);
+ default_sorting_field, this->default_num_indices);
next_collection_id++;
rocksdb::WriteBatch batch;
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index b1556aaf..fd6dc85e 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -83,7 +83,9 @@ int run_server(Config & config, void (*master_server_routes)(), void (*replica_s
LOG(INFO) << "Loading collections from disk...";
CollectionManager & collectionManager = CollectionManager::get_instance();
- Option<bool> init_op = collectionManager.init(&store, config.get_api_key(),
+ Option<bool> init_op = collectionManager.init(&store,
+ config.get_indices_per_collection(),
+ config.get_api_key(),
config.get_search_only_api_key());
if(init_op.ok()) {
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 4e538d2d..a9e2170d 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -20,7 +20,7 @@ protected:
system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
store = new Store(state_dir_path);
- collectionManager.init(store, "auth_key", "search_auth_key");
+ collectionManager.init(store, 4, "auth_key", "search_auth_key");
search_fields = {
field("title", field_types::STRING, false),
@@ -153,7 +153,7 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
// create a new collection manager to ensure that it restores the records from the disk backed store
CollectionManager & collectionManager2 = CollectionManager::get_instance();
- collectionManager2.init(store, "auth_key", "search_auth_key");
+ collectionManager2.init(store, 4, "auth_key", "search_auth_key");
collection1 = collectionManager2.get_collection("collection1");
ASSERT_NE(nullptr, collection1);
@@ -210,7 +210,7 @@ TEST_F(CollectionManagerTest, Symlinking) {
std::string state_dir_path = "/tmp/typesense_test/cmanager_test_db";
system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
Store *store = new Store(state_dir_path);
- cmanager.init(store, "auth_key", "search_auth_key");
+ cmanager.init(store, 4, "auth_key", "search_auth_key");
// try resolving on a blank slate
Option<std::string> collection_option = cmanager.resolve_symlink("collection");
@@ -256,7 +256,7 @@ TEST_F(CollectionManagerTest, Symlinking) {
// should be able to restore state on init
CollectionManager & cmanager2 = CollectionManager::get_instance();
- cmanager2.init(store, "auth_key", "search_auth_key");
+ cmanager2.init(store, 4, "auth_key", "search_auth_key");
collection_option = cmanager2.resolve_symlink("company");
ASSERT_TRUE(collection_option.ok());
ASSERT_EQ("company_2019", collection_option.get());
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index ae255659..55f1fb68 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -21,7 +21,7 @@ protected:
system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
store = new Store(state_dir_path);
- collectionManager.init(store, "auth_key", "search_auth_key");
+ collectionManager.init(store, 4, "auth_key", "search_auth_key");
std::ifstream infile(std::string(ROOT_DIR)+"test/documents.jsonl");
std::vector<field> search_fields = {