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

Symlink name must not conflict with a collection name.

kishorenc8845917020c2
diff --git a/include/collection_manager.h b/include/collection_manager.h
index c059bd57..22d72c5f 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -100,9 +100,9 @@ public:
spp::sparse_hash_map<std::string, std::string> & get_symlinks();
- bool upsert_symlink(const std::string & symlink_name, const std::string & collection_name);
+ Option<bool> upsert_symlink(const std::string & symlink_name, const std::string & collection_name);
- bool delete_symlink(const std::string & symlink_name);
+ Option<bool> delete_symlink(const std::string & symlink_name);
static constexpr const char* NEXT_COLLECTION_ID_KEY = "$CI";
static constexpr const char* SYMLINK_PREFIX = "$SL";
diff --git a/src/collection.cpp b/src/collection.cpp
index adf4ea8c..58cc49e6 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -71,9 +71,15 @@ Collection::~Collection() {
index->terminate = true;
index->cv.notify_one();
t->join();
+
delete t;
delete indices[i];
+ t = nullptr;
+ indices[i] = nullptr;
}
+
+ indices.clear();
+ index_threads.clear();
}
uint32_t Collection::get_next_seq_id() {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index d4d8cd89..02488a3b 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -372,22 +372,28 @@ Option<std::string> CollectionManager::resolve_symlink(const std::string & symli
return Option<std::string>(404, "Not found.");
}
-bool CollectionManager::upsert_symlink(const std::string & symlink_name, const std::string & collection_name) {
+Option<bool> CollectionManager::upsert_symlink(const std::string & symlink_name, const std::string & collection_name) {
+ if(collections.count(symlink_name) != 0) {
+ return Option<bool>(500, "Name `" + symlink_name + "` conflicts with an existing collection name.");
+ }
+
bool inserted = store->insert(get_symlink_key(symlink_name), collection_name);
if(!inserted) {
- return false;
+ return Option<bool>(500, "Unable to insert into store.");
}
- collection_symlinks[symlink_name] = collection_name;
- return true;
+
+ collection_symlinks[symlink_name] = collection_name;
+ return Option<bool>(true);
}
-bool CollectionManager::delete_symlink(const std::string & symlink_name) {
+Option<bool> CollectionManager::delete_symlink(const std::string & symlink_name) {
bool removed = store->remove(get_symlink_key(symlink_name));
if(!removed) {
- return false;
+ return Option<bool>(500, "Unable to delete from store.");
}
+
collection_symlinks.erase(symlink_name);
- return true;
+ return Option<bool>(true);
}
Store* CollectionManager::get_store() {
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 9fb21068..16b15ba7 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -290,11 +290,16 @@ TEST_F(CollectionManagerTest, Symlinking) {
ASSERT_EQ(0, cmanager.get_symlinks().size());
+ // symlink name cannot be the same as an existing collection
+ Option<bool> inserted = cmanager.upsert_symlink("collection1", "collection_2018");
+ ASSERT_FALSE(inserted.ok());
+ ASSERT_STREQ("Name `collection1` conflicts with an existing collection name.", inserted.error().c_str());
+
// insert a symlink
- bool inserted = cmanager.upsert_symlink("collection", "collection_2018");
- ASSERT_TRUE(inserted);
+ inserted = cmanager.upsert_symlink("collection_link", "collection_2018");
+ ASSERT_TRUE(inserted.ok());
- collection_option = cmanager.resolve_symlink("collection");
+ collection_option = cmanager.resolve_symlink("collection_link");
ASSERT_TRUE(collection_option.ok());
ASSERT_EQ("collection_2018", collection_option.get());
@@ -308,13 +313,27 @@ TEST_F(CollectionManagerTest, Symlinking) {
// update existing symlink
inserted = cmanager.upsert_symlink("company", "company_2019");
- ASSERT_TRUE(inserted);
+ ASSERT_TRUE(inserted.ok());
collection_option = cmanager.resolve_symlink("company");
ASSERT_TRUE(collection_option.ok());
ASSERT_EQ("company_2019", collection_option.get());
+ // add and update a symlink against an existing collection
+ inserted = cmanager.upsert_symlink("collection1_link", "collection1");
+ ASSERT_TRUE(inserted.ok());
+ collection_option = cmanager.resolve_symlink("collection1_link");
+ ASSERT_TRUE(collection_option.ok());
+ ASSERT_EQ("collection1", collection_option.get());
+
+ inserted = cmanager.upsert_symlink("collection1_link", "collection2");
+ ASSERT_TRUE(inserted.ok());
+ collection_option = cmanager.resolve_symlink("collection1_link");
+ ASSERT_TRUE(collection_option.ok());
+ ASSERT_EQ("collection2", collection_option.get());
+
// remove link
- cmanager.delete_symlink("collection");
+ Option<bool> deleted = cmanager.delete_symlink("collection");
+ ASSERT_TRUE(deleted.ok());
collection_option = cmanager.resolve_symlink("collection");
ASSERT_FALSE(collection_option.ok());
ASSERT_EQ(404, collection_option.code());
@@ -340,4 +359,4 @@ TEST_F(CollectionManagerTest, Symlinking) {
collection_option = cmanager2.resolve_symlink("company_3");
ASSERT_TRUE(collection_option.ok());
ASSERT_EQ("company_2020", collection_option.get());
-}
\ No newline at end of file
+}