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

Added a dispose() method for freeing collection manager's in-memory data.

Kishore Nallan6ab58d18393a
diff --git a/docker/debugger.Dockerfile b/docker/debugger.Dockerfile
new file mode 100644
index 00000000..be92a5e6
--- /dev/null
+++ b/docker/debugger.Dockerfile
@@ -0,0 +1,7 @@
+FROM ubuntu:16.04
+
+RUN mkdir -p /opt
+COPY typesense-server /opt
+RUN chmod +x /opt/typesense-server
+EXPOSE 8108
+ENTRYPOINT ["/opt/typesense-server"]
\ No newline at end of file
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 8d081bfa..501073f5 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -44,6 +44,9 @@ public:
void init(Store *store, const std::string & auth_key);
+ // frees in-memory data structures when server is shutdown - helps us run a memory leak detecter properly
+ void dispose();
+
Collection* init_collection(const std::string & collection_meta_json);
void add_to_collections(Collection* collection);
diff --git a/src/collection.cpp b/src/collection.cpp
index b56a4386..b68b9897 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -36,16 +36,20 @@ Collection::Collection(const std::string name, const uint32_t collection_id, con
}
Collection::~Collection() {
- for(auto & name_field: search_schema) {
- art_tree *t = search_index.at(name_field.first);
- art_tree_destroy(t);
- t = nullptr;
+ for(auto & name_tree: search_index) {
+ art_tree_destroy(name_tree.second);
+ delete name_tree.second;
+ name_tree.second = nullptr;
}
+ search_index.clear();
+
for(auto & name_map: sort_index) {
delete name_map.second;
name_map.second = nullptr;
}
+
+ sort_index.clear();
}
uint32_t Collection::get_next_seq_id() {
@@ -759,6 +763,7 @@ Option<nlohmann::json> Collection::search(std::string query, const std::vector<s
}
delete [] filter_ids;
+ delete [] all_result_ids;
// All fields are sorted descending
std::sort(field_order_kvs.begin(), field_order_kvs.end(),
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 5d19157f..54a8a78b 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -96,6 +96,15 @@ void CollectionManager::init(Store *store, const std::string & auth_key) {
std::cout << "Finished restoring all collections from disk." << std::endl;
}
+void CollectionManager::dispose() {
+ for(auto & name_collection: collections) {
+ delete name_collection.second;
+ name_collection.second = nullptr;
+ }
+
+ collections.clear();
+}
+
bool CollectionManager::auth_key_matches(std::string auth_key_sent) {
return (auth_key == auth_key_sent);
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 9e5b1e83..44d5e3ef 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -274,6 +274,7 @@ void HttpServer::send_response(http_req* request, const http_res* response) {
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("application/json; charset=utf-8"));
h2o_start_response(req, &generator);
h2o_send(req, &body, 1, H2O_SEND_STATE_FINAL);
+ h2o_dispose_request(req);
delete request;
delete response;
@@ -319,7 +320,6 @@ void HttpServer::del(const std::string & path, void (*handler)(http_req &, http_
routes.push_back(rpath);
}
-
void HttpServer::on(const std::string & message, void (*handler)(void*)) {
message_handlers.emplace(message, handler);
}
diff --git a/src/main/main.cpp b/src/main/main.cpp
index 3468fc35..42c70e00 100644
--- a/src/main/main.cpp
+++ b/src/main/main.cpp
@@ -16,7 +16,9 @@ using namespace std;
int main(int argc, char* argv[]) {
const std::string state_dir_path = "/tmp/typesense-data";
- Store *store = new Store("/tmp/typesense-data");
+ system("rm -rf /tmp/typesense-data && mkdir -p /tmp/typesense-data");
+
+ Store *store = new Store(state_dir_path);
CollectionManager & collectionManager = CollectionManager::get_instance();
collectionManager.init(store, "abcd");
@@ -41,7 +43,6 @@ int main(int argc, char* argv[]) {
};
Collection *collection = collectionManager.get_collection("github_top1k");
-
if(collection == nullptr) {
collection = collectionManager.create_collection("github_top1k", fields_to_index, facet_fields_index, sort_fields);
}
@@ -96,7 +97,7 @@ int main(int argc, char* argv[]) {
std::cout << "Time taken for deletion: " << timeMillis << "ms" << std::endl;
}
- delete collection;
+ collectionManager.dispose();
delete store;
return 0;
}
\ No newline at end of file
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 6ddc4e81..d5e5ed11 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -11,15 +11,15 @@
HttpServer* server;
-void free_server() {
+void free_resources() {
+ CollectionManager::get_instance().dispose();
delete server;
}
void catch_interrupt(int sig) {
- std::cout << "Stopping Typesense server..." << std::endl;
+ std::cout << "Stopping Typesense server...1" << std::endl;
signal(sig, SIG_IGN); // ignore for now as we want to shut down elegantly
- CollectionManager & collectionManager = CollectionManager::get_instance();
- server->stop(&free_server);
+ server->stop(&free_resources);
}
int main(int argc, char **argv) {
@@ -27,7 +27,7 @@ int main(int argc, char **argv) {
options.add<std::string>("data-dir", 'd', "Directory where data will be stored.", true);
options.add<std::string>("api-auth-key", 'k', "Key for authenticating the API endpoints.", true);
options.add<std::string>("listen-address", 'a', "Address to which Typesense server binds.", false, "0.0.0.0");
- options.add<uint32_t>("listen-port", 'p', "Port on which Typesense server listens.", false, 8080);
+ options.add<uint32_t>("listen-port", 'p', "Port on which Typesense server listens.", false, 8108);
options.add<std::string>("master", 'm', "<master_address>:<master_port> combination to start the server as a "
"read-only replica.", false, "");
options.parse_check(argc, argv);