mirror of
https://github.com/typesense/typesense.git
synced 2025-05-20 13:42:26 +08:00
API for deleting a document by ID.
This commit is contained in:
parent
56c539d0df
commit
3478aef573
2
TODO.md
2
TODO.md
@ -33,10 +33,12 @@
|
||||
- ~~Desc/Asc ordering with tests~~
|
||||
- ~~Found count is wrong~~
|
||||
- ~~Filter query in the API~~
|
||||
- Handle store-get() not finding a key
|
||||
- Fix API response codes
|
||||
- Test for search without any sort_by given
|
||||
- Test for asc/desc upper/lower casing
|
||||
- Test for collection creation validation
|
||||
- Test for delete document
|
||||
- Proper pagination
|
||||
- Deprecate old split function
|
||||
- Prevent string copy during indexing
|
||||
|
@ -114,7 +114,7 @@ public:
|
||||
const std::vector<sort_field> & sort_fields, const int num_typos,
|
||||
const size_t num_results, const token_ordering token_order = FREQUENCY, const bool prefix = false);
|
||||
|
||||
void remove(std::string id);
|
||||
Option<std::string> remove(std::string id);
|
||||
|
||||
void score_results(const std::vector<sort_field> & sort_fields, const int & token_rank, Topster<100> &topster,
|
||||
const std::vector<art_leaf *> & query_suggestion, const uint32_t *result_ids,
|
||||
|
@ -47,6 +47,11 @@ struct http_res {
|
||||
status_code = 500;
|
||||
body = res_body;
|
||||
}
|
||||
|
||||
void send(uint32_t code, const std::string & message) {
|
||||
status_code = code;
|
||||
body = "{\"message\": \"" + message + "\"}";
|
||||
}
|
||||
};
|
||||
|
||||
struct http_req {
|
||||
@ -97,7 +102,7 @@ public:
|
||||
|
||||
void put(const std::string & path, void (*handler)(http_req &, http_res &));
|
||||
|
||||
void del();
|
||||
void del(const std::string & path, void (*handler)(http_req &, http_res &));
|
||||
|
||||
int run();
|
||||
};
|
@ -24,6 +24,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
enum StoreStatus {
|
||||
FOUND,
|
||||
OK,
|
||||
NOT_FOUND,
|
||||
ERROR
|
||||
};
|
||||
|
||||
/*
|
||||
* Abstraction for underlying KV store (RocksDB)
|
||||
*/
|
||||
@ -72,9 +79,18 @@ public:
|
||||
return status.ok() && !status.IsNotFound();
|
||||
}
|
||||
|
||||
bool get(const std::string& key, std::string& value) {
|
||||
StoreStatus get(const std::string& key, std::string& value) {
|
||||
rocksdb::Status status = db->Get(rocksdb::ReadOptions(), key, &value);
|
||||
return status.ok();
|
||||
|
||||
if(status.IsNotFound()) {
|
||||
return StoreStatus::NOT_FOUND;
|
||||
}
|
||||
|
||||
if(!status.ok()) {
|
||||
return StoreStatus::ERROR;
|
||||
}
|
||||
|
||||
return StoreStatus::FOUND;
|
||||
}
|
||||
|
||||
bool remove(const std::string& key) {
|
||||
|
48
src/api.cpp
48
src/api.cpp
@ -194,47 +194,27 @@ void post_add_document(http_req & req, http_res & res) {
|
||||
|
||||
Option<std::string> inserted_id_op = collection->add(req.body);
|
||||
|
||||
nlohmann::json json_response;
|
||||
static h2o_generator_t generator = {NULL, NULL};
|
||||
|
||||
if(!inserted_id_op.ok()) {
|
||||
json_response["message"] = inserted_id_op.error();
|
||||
res.send_500(json_response.dump());
|
||||
|
||||
res.send(inserted_id_op.code(), inserted_id_op.error());
|
||||
} else {
|
||||
nlohmann::json json_response;
|
||||
json_response["id"] = inserted_id_op.get();
|
||||
res.send_201(json_response.dump());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
int del_remove_document(h2o_handler_t *self, h2o_req_t *req) {
|
||||
h2o_iovec_t query = req->query_at != SIZE_MAX ?
|
||||
h2o_iovec_init(req->path.base + req->query_at, req->path.len - req->query_at) :
|
||||
h2o_iovec_init(H2O_STRLIT(""));
|
||||
|
||||
std::string query_str(query.base, query.len);
|
||||
std::map<std::string, std::string> req.params = parse_query(query_str);
|
||||
|
||||
void del_remove_document(http_req & req, http_res & res) {
|
||||
std::string doc_id = req.params["id"];
|
||||
|
||||
auto begin = std::chrono::high_resolution_clock::now();
|
||||
collection->remove(doc_id);
|
||||
long long int time_micro = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
std::chrono::high_resolution_clock::now() - begin).count();
|
||||
std::cout << "Time taken: " << time_micro << "us" << std::endl;
|
||||
CollectionManager & collectionManager = CollectionManager::get_instance();
|
||||
Collection* collection = collectionManager.get_collection(req.params["collection"]);
|
||||
Option<std::string> deleted_id_op = collection->remove(doc_id);
|
||||
|
||||
nlohmann::json json_response;
|
||||
json_response["id"] = doc_id;
|
||||
json_response["status"] = "SUCCESS";
|
||||
|
||||
static h2o_generator_t generator = {NULL, NULL};
|
||||
req->res.status = 200;
|
||||
req->res.reason = "OK";
|
||||
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, H2O_STRLIT("application/json; charset=utf-8"));
|
||||
h2o_start_response(req, &generator);
|
||||
h2o_iovec_t body = h2o_strdup(&req->pool, json_response.dump().c_str(), SIZE_MAX);
|
||||
h2o_send(req, &body, 1, 1);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
if(!deleted_id_op.ok()) {
|
||||
res.send(deleted_id_op.code(), deleted_id_op.error());
|
||||
} else {
|
||||
nlohmann::json json_response;
|
||||
json_response["id"] = deleted_id_op.get();
|
||||
res.send_200(json_response.dump());
|
||||
}
|
||||
}
|
@ -1002,9 +1002,15 @@ void Collection::remove_and_shift_offset_index(sorted_array &offset_index, const
|
||||
delete[] new_array;
|
||||
}
|
||||
|
||||
void Collection::remove(std::string id) {
|
||||
Option<std::string> Collection::remove(std::string id) {
|
||||
nlohmann::json result = nlohmann::json::object();
|
||||
|
||||
std::string seq_id_str;
|
||||
store->get(get_doc_id_key(id), seq_id_str);
|
||||
StoreStatus status = store->get(get_doc_id_key(id), seq_id_str);
|
||||
|
||||
if(status == StoreStatus::NOT_FOUND) {
|
||||
return Option<std::string>(404, "Could not find a document with id: " + id);
|
||||
}
|
||||
|
||||
uint32_t seq_id = (uint32_t) std::stol(seq_id_str);
|
||||
|
||||
@ -1052,6 +1058,8 @@ void Collection::remove(std::string id) {
|
||||
|
||||
store->remove(get_doc_id_key(id));
|
||||
store->remove(get_seq_id_key(seq_id));
|
||||
|
||||
return Option<std::string>(id);
|
||||
}
|
||||
|
||||
std::string Collection::get_next_seq_id_key(std::string collection_name) {
|
||||
|
@ -203,8 +203,11 @@ void HttpServer::put(const std::string & path, void (*handler)(http_req &, http_
|
||||
routes.push_back(rpath);
|
||||
}
|
||||
|
||||
void HttpServer::del() {
|
||||
|
||||
void HttpServer::del(const std::string & path, void (*handler)(http_req &, http_res &)) {
|
||||
std::vector<std::string> path_parts;
|
||||
StringUtils::split(path, path_parts, "/");
|
||||
route_path rpath = {"DELETE", path_parts, handler};
|
||||
routes.push_back(rpath);
|
||||
}
|
||||
|
||||
HttpServer::~HttpServer() {
|
||||
|
@ -18,6 +18,7 @@ int main(int argc, char **argv) {
|
||||
server.post("/collection", post_create_collection);
|
||||
server.post("/collection/:collection", post_add_document);
|
||||
server.get("/collection/:collection/search", get_search);
|
||||
server.del("/collection/:collection/:id", del_remove_document);
|
||||
|
||||
server.run();
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user