mirror of
https://github.com/typesense/typesense.git
synced 2025-05-16 19:55:21 +08:00
Store the created timestamp as part of collection metadata.
This commit is contained in:
parent
3c98931c0e
commit
35b7ba720b
@ -80,6 +80,7 @@ file(APPEND ${DEP_ROOT_DIR}/libs.txt "${DEP_ROOT_DIR}/${FOR_NAME}")
|
||||
file(APPEND ${DEP_ROOT_DIR}/libs.txt " ${DEP_ROOT_DIR}/${H2O_NAME}/build")
|
||||
file(APPEND ${DEP_ROOT_DIR}/libs.txt " ${DEP_ROOT_DIR}/${ROCKSDB_NAME}")
|
||||
file(APPEND ${DEP_ROOT_DIR}/libs.txt " ${DEP_ROOT_DIR}/${G3LOG_NAME}/build")
|
||||
file(APPEND ${DEP_ROOT_DIR}/libs.txt " ${DEP_ROOT_DIR}/${ICONV_NAME}/lib/.libs")
|
||||
|
||||
add_executable(typesense-server ${SRC_FILES} src/main/typesense_server.cpp)
|
||||
add_executable(search ${SRC_FILES} src/main/main.cpp)
|
||||
|
@ -39,6 +39,8 @@ private:
|
||||
|
||||
uint32_t collection_id;
|
||||
|
||||
uint64_t created_at;
|
||||
|
||||
size_t num_documents;
|
||||
|
||||
std::vector<Index*> indices;
|
||||
@ -75,8 +77,9 @@ private:
|
||||
public:
|
||||
Collection() = delete;
|
||||
|
||||
Collection(const std::string name, const uint32_t collection_id, const uint32_t next_seq_id, Store *store,
|
||||
const std::vector<field> & fields, const std::string & default_sorting_field, const size_t num_indices=4);
|
||||
Collection(const std::string name, const uint32_t collection_id, const uint64_t created_at,
|
||||
const uint32_t next_seq_id, Store *store, const std::vector<field> & fields,
|
||||
const std::string & default_sorting_field, const size_t num_indices=4);
|
||||
|
||||
~Collection();
|
||||
|
||||
@ -88,6 +91,8 @@ public:
|
||||
|
||||
std::string get_name();
|
||||
|
||||
uint64_t get_created_at();
|
||||
|
||||
size_t get_num_documents();
|
||||
|
||||
uint32_t get_collection_id();
|
||||
|
@ -26,6 +26,7 @@ private:
|
||||
static constexpr const char* COLLECTION_ID_KEY = "id";
|
||||
static constexpr const char* COLLECTION_SEARCH_FIELDS_KEY = "fields";
|
||||
static constexpr const char* COLLECTION_DEFAULT_SORTING_FIELD_KEY = "default_sorting_field";
|
||||
static constexpr const char* COLLECTION_CREATED = "created_at";
|
||||
|
||||
std::string auth_key;
|
||||
std::string search_only_auth_key;
|
||||
@ -57,7 +58,8 @@ public:
|
||||
bool search_only_auth_key_matches(std::string auth_key_sent);
|
||||
|
||||
Option<Collection*> create_collection(const std::string name, const std::vector<field> & fields,
|
||||
const std::string & default_sorting_field);
|
||||
const std::string & default_sorting_field,
|
||||
const uint64_t created_at = static_cast<uint64_t>(std::time(nullptr)));
|
||||
|
||||
Collection* get_collection(const std::string & collection_name);
|
||||
|
||||
|
@ -30,9 +30,9 @@ struct match_index_t {
|
||||
}
|
||||
};
|
||||
|
||||
Collection::Collection(const std::string name, const uint32_t collection_id, const uint32_t next_seq_id, Store *store,
|
||||
const std::vector<field> &fields, const std::string & default_sorting_field,
|
||||
const size_t num_indices):
|
||||
Collection::Collection(const std::string name, const uint32_t collection_id, const uint64_t created_at,
|
||||
const uint32_t next_seq_id, Store *store, const std::vector<field> &fields,
|
||||
const std::string & default_sorting_field, const size_t num_indices):
|
||||
name(name), collection_id(collection_id), next_seq_id(next_seq_id), store(store),
|
||||
fields(fields), default_sorting_field(default_sorting_field), num_indices(num_indices) {
|
||||
|
||||
@ -56,7 +56,8 @@ Collection::Collection(const std::string name, const uint32_t collection_id, con
|
||||
index_threads.push_back(thread);
|
||||
}
|
||||
|
||||
num_documents = 0;
|
||||
this->created_at = created_at;
|
||||
this->num_documents = 0;
|
||||
}
|
||||
|
||||
Collection::~Collection() {
|
||||
@ -868,6 +869,10 @@ std::string Collection::get_name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
uint64_t Collection::get_created_at() {
|
||||
return created_at;
|
||||
}
|
||||
|
||||
size_t Collection::get_num_documents() {
|
||||
return num_documents;
|
||||
}
|
||||
|
@ -19,9 +19,12 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
|
||||
}
|
||||
|
||||
std::string default_sorting_field = collection_meta[COLLECTION_DEFAULT_SORTING_FIELD_KEY].get<std::string>();
|
||||
uint64_t created_at = collection_meta.find((const char*)COLLECTION_CREATED) != collection_meta.end() ?
|
||||
collection_meta[COLLECTION_CREATED].get<uint64_t>() : 0;
|
||||
|
||||
Collection* collection = new Collection(this_collection_name,
|
||||
collection_meta[COLLECTION_ID_KEY].get<uint32_t>(),
|
||||
created_at,
|
||||
collection_next_seq_id,
|
||||
store,
|
||||
fields,
|
||||
@ -150,7 +153,8 @@ bool CollectionManager::search_only_auth_key_matches(std::string auth_key_sent)
|
||||
}
|
||||
|
||||
Option<Collection*> CollectionManager::create_collection(const std::string name, const std::vector<field> & fields,
|
||||
const std::string & default_sorting_field) {
|
||||
const std::string & default_sorting_field,
|
||||
const uint64_t created_at) {
|
||||
if(store->contains(Collection::get_meta_key(name))) {
|
||||
return Option<Collection*>(409, std::string("A collection with name `") + name + "` already exists.");
|
||||
}
|
||||
@ -176,8 +180,10 @@ Option<Collection*> CollectionManager::create_collection(const std::string name,
|
||||
collection_meta[COLLECTION_ID_KEY] = next_collection_id;
|
||||
collection_meta[COLLECTION_SEARCH_FIELDS_KEY] = fields_json;
|
||||
collection_meta[COLLECTION_DEFAULT_SORTING_FIELD_KEY] = default_sorting_field;
|
||||
collection_meta[COLLECTION_CREATED] = created_at;
|
||||
|
||||
Collection* new_collection = new Collection(name, next_collection_id, 0, store, fields, default_sorting_field);
|
||||
Collection* new_collection = new Collection(name, next_collection_id, created_at, 0, store, fields,
|
||||
default_sorting_field);
|
||||
next_collection_id++;
|
||||
|
||||
rocksdb::WriteBatch batch;
|
||||
|
@ -30,7 +30,7 @@ protected:
|
||||
};
|
||||
|
||||
sort_fields = { sort_by("points", "DESC") };
|
||||
collection1 = collectionManager.create_collection("collection1", search_fields, "points").get();
|
||||
collection1 = collectionManager.create_collection("collection1", search_fields, "points", 12345).get();
|
||||
}
|
||||
|
||||
virtual void SetUp() {
|
||||
@ -80,14 +80,42 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
|
||||
ASSERT_EQ(3, num_keys);
|
||||
// we already call `collection1->get_next_seq_id` above, which is side-effecting
|
||||
ASSERT_EQ(1, StringUtils::deserialize_uint32_t(next_seq_id));
|
||||
ASSERT_EQ("{\"default_sorting_field\":\"points\",\"fields\":[{\"facet\":false,\"name\":\"title\",\"type\":\"string\"},"
|
||||
"{\"facet\":false,\"name\":\"starring\",\"type\":\"string\"},"
|
||||
"{\"facet\":true,\"name\":\"cast\",\"type\":\"string[]\"},"
|
||||
"{\"facet\":false,\"name\":\"points\",\"type\":\"int32\"}"
|
||||
"],\"id\":0,\"name\":\"collection1\"}", collection_meta_json);
|
||||
ASSERT_EQ("{\"created_at\":12345,\"default_sorting_field\":\"points\","
|
||||
"\"fields\":[{\"facet\":false,\"name\":\"title\",\"type\":\"string\"},"
|
||||
"{\"facet\":false,\"name\":\"starring\",\"type\":\"string\"},"
|
||||
"{\"facet\":true,\"name\":\"cast\",\"type\":\"string[]\"},"
|
||||
"{\"facet\":false,\"name\":\"points\",\"type\":\"int32\"}],\"id\":0,\"name\":\"collection1\"}",
|
||||
collection_meta_json);
|
||||
ASSERT_EQ("1", next_collection_id);
|
||||
}
|
||||
|
||||
TEST_F(CollectionManagerTest, ShouldInitCollection) {
|
||||
nlohmann::json collection_meta1 =
|
||||
nlohmann::json::parse("{\"name\": \"foobar\", \"id\": 100, \"fields\": [{\"name\": \"org\", \"type\": "
|
||||
"\"string\", \"facet\": false}], \"default_sorting_field\": \"foo\"}");
|
||||
|
||||
Collection *collection = collectionManager.init_collection(collection_meta1, 100);
|
||||
ASSERT_EQ("foobar", collection->get_name());
|
||||
ASSERT_EQ(100, collection->get_collection_id());
|
||||
ASSERT_EQ(1, collection->get_fields().size());
|
||||
ASSERT_EQ("foo", collection->get_default_sorting_field());
|
||||
ASSERT_EQ(0, collection->get_created_at());
|
||||
|
||||
delete collection;
|
||||
|
||||
// with created_at ts
|
||||
|
||||
nlohmann::json collection_meta2 =
|
||||
nlohmann::json::parse("{\"name\": \"foobar\", \"id\": 100, \"fields\": [{\"name\": \"org\", \"type\": "
|
||||
"\"string\", \"facet\": false}], \"created_at\": 12345,"
|
||||
"\"default_sorting_field\": \"foo\"}");
|
||||
|
||||
collection = collectionManager.init_collection(collection_meta2, 100);
|
||||
ASSERT_EQ(12345, collection->get_created_at());
|
||||
|
||||
delete collection;
|
||||
}
|
||||
|
||||
TEST_F(CollectionManagerTest, GetAllCollections) {
|
||||
std::vector<Collection*> collection_vec = collectionManager.get_collections();
|
||||
ASSERT_EQ(1, collection_vec.size());
|
||||
|
Loading…
x
Reference in New Issue
Block a user