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

Test for dropping a collection.

Kishore Nallan22816ea0a03f
diff --git a/include/collection_manager.h b/include/collection_manager.h
index b8afc7d9..ab6f9104 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -49,4 +49,6 @@ public:
Collection* get_collection(std::string collection_name);
bool drop_collection(std::string collection_name);
+
+ uint32_t get_next_collection_id();
};
\ No newline at end of file
diff --git a/include/store.h b/include/store.h
index c9ad3d59..45efa254 100644
--- a/include/store.h
+++ b/include/store.h
@@ -85,6 +85,11 @@ public:
return iter;
}
+ rocksdb::Iterator* get_iterator() {
+ rocksdb::Iterator* it = db->NewIterator(rocksdb::ReadOptions());
+ return it;
+ };
+
void scan_fill(const std::string & prefix, std::vector<std::string> & values) {
rocksdb::Iterator *iter = db->NewIterator(rocksdb::ReadOptions());
for (iter->Seek(prefix); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 61ff76f7..060238e9 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -127,6 +127,7 @@ bool CollectionManager::drop_collection(std::string collection_name) {
store->remove(Collection::get_meta_key(collection_name));
store->remove(Collection::get_next_seq_id_key(collection_name));
+ store->remove(NEXT_COLLECTION_ID_KEY);
const std::string &collection_id_str = std::to_string(collection->get_collection_id());
rocksdb::Iterator* iter = store->scan(collection_id_str);
@@ -144,3 +145,7 @@ bool CollectionManager::drop_collection(std::string collection_name) {
return true;
}
+
+uint32_t CollectionManager::get_next_collection_id() {
+ return next_collection_id;
+}
\ No newline at end of file
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 2d12d9e1..7409341a 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -54,8 +54,51 @@ TEST(CollectionManagerTest, RestoreRecordsOnRestart) {
results = collection1->search("thomas", search_fields, 0, 10, FREQUENCY, false);
ASSERT_EQ(4, results["hits"].size());
+
+ delete store;
}
TEST(CollectionManagerTest, DropCollectionCleanly) {
+ const std::string state_dir_path = "/tmp/typesense_test/coll_manager_test_db";
+
+ std::cout << "Truncating and creating: " << state_dir_path << std::endl;
+ system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
+
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+ Store* store = new Store(state_dir_path);
+ collectionManager.init(store);
+
+ // Summary: create a collection, add documents, destroy it, try reopening the collection to query
+ Collection *collection1;
+ std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");
+ std::vector<field> fields = {field("title", field_types::STRING), field("starring", field_types::STRING)};
+ std::vector<std::string> rank_fields = {"points"};
+
+ collection1 = collectionManager.get_collection("collection1");
+ if(collection1 == nullptr) {
+ collection1 = collectionManager.create_collection("collection1", fields, rank_fields);
+ }
+
+ std::string json_line;
+
+ while (std::getline(infile, json_line)) {
+ collection1->add(json_line);
+ }
+
+ infile.close();
+
+ collectionManager.drop_collection("collection1");
+
+ rocksdb::Iterator* it = store->get_iterator();
+ for (it->SeekToFirst(); it->Valid(); it->Next()) {
+ ASSERT_TRUE(false);
+ }
+
+ assert(it->status().ok());
+
+ ASSERT_EQ(nullptr, collectionManager.get_collection("collection1"));
+ ASSERT_EQ(1, collectionManager.get_next_collection_id());
+ delete it;
+ delete store;
}
\ No newline at end of file