-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
101 changed files
with
3,369 additions
and
2,102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
include(ExternalProject) | ||
|
||
set(DEPENDENCY_INSTALL_DIR ${PROJECT_BINARY_DIR}/install) | ||
set(DEPENDENCY_INCLUDE_DIR ${DEPENDENCY_INSTALL_DIR}/include) | ||
set(DEPENDENCY_LIB_DIR ${DEPENDENCY_INSTALL_DIR}/lib) | ||
|
||
function(add_dep dep) | ||
set(DEPENDENCY_LIST ${DEPENDENCY_LIST} ${dep} PARENT_SCOPE) | ||
endfunction() | ||
|
||
function(add_include include_dir) | ||
set(DEPENDENCY_INCLUDE_LIST ${DEPENDENCY_INCLUDE_LIST} ${include_dir} PARENT_SCOPE) | ||
endfunction() | ||
|
||
function(add_lib lib) | ||
set(DEPENDENCY_LIBS ${DEPENDENCY_LIBS} ${lib} PARENT_SCOPE) | ||
endfunction(add_lib) | ||
|
||
# ----------------------- SPDLOG ----------------------- # | ||
ExternalProject_Add( | ||
dep-spdlog | ||
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/spdlog | ||
UPDATE_COMMAND "" | ||
PATCH_COMMAND "" | ||
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${DEPENDENCY_INSTALL_DIR} | ||
TEST_COMMAND "" | ||
) | ||
|
||
add_dep(dep-spdlog) | ||
add_include(${CMAKE_CURRENT_SOURCE_DIR}/vendor/spdlog/include) | ||
add_lib(spdlog) | ||
|
||
# ----------------------- SQLITE3 ---------------------- # | ||
add_library(xsqllite3 ${CMAKE_CURRENT_SOURCE_DIR}/vendor/sqlite/sqlite/sqlite3.c) | ||
|
||
add_include(${CMAKE_CURRENT_SOURCE_DIR}/vendor/sqlite) | ||
add_lib(xsqllite3) | ||
|
||
# -------------------- NLOHMANN JSON ------------------- # | ||
ExternalProject_Add( | ||
dep-json | ||
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/json | ||
UPDATE_COMMAND "" | ||
PATCH_COMMAND "" | ||
CMAKE_ARGS | ||
-DCMAKE_INSTALL_PREFIX=${DEPENDENCY_INSTALL_DIR} | ||
-DNLOHMANN_JSON_INCLUDE_INSTALL_DIR=${DEPENDENCY_INSTALL_DIR} | ||
-DJSON_Install=ON | ||
-DJSON_BuildTests=OFF | ||
TEST_COMMAND "" | ||
) | ||
|
||
add_dep(dep-json) | ||
add_include(${CMAKE_CURRENT_SOURCE_DIR}/vendor/json/include) | ||
# add_lib(nlohmann_json) # dependecy adds the .Targets file and handles the link_library | ||
|
||
# --------------------- MAGIC ENUM --------------------- # | ||
add_include(${CMAKE_CURRENT_SOURCE_DIR}/vendor/magic_enum/include) | ||
|
||
# ---------------------- LRU CACHE --------------------- # | ||
add_include(${CMAKE_CURRENT_SOURCE_DIR}/vendor/lrucache11) | ||
|
||
# ----------------------- OTHERS ----------------------- # | ||
add_include(${CMAKE_CURRENT_SOURCE_DIR}/vendor/others) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,10 @@ | ||
#include "Collection.hpp" | ||
#include "nldb/Collection.hpp" | ||
|
||
#include <optional> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
namespace nldb { | ||
Collection::Collection(int id, const std::string& name) | ||
: id(id), name(name) {} | ||
|
||
#include "Enums.hpp" | ||
#include "PropertyRep.hpp" | ||
#include "SqlExpression.hpp" | ||
#include "dbwrapper/IDB.hpp" | ||
#include "logger/Logger.h" | ||
// max to 20 collections | ||
lru11::Cache<int, std::unordered_map<std::string, PropertyRep>> | ||
Collection::propertyCache(20); | ||
int Collection::getId() const { return id; } | ||
|
||
Collection::Collection(IDB* pCtx, int pId, const std::string& pName) | ||
: ctx(pCtx), id(pId), name(pName) {} | ||
|
||
int Collection::getID() { return this->id; } | ||
|
||
bool Collection::hasProperty(const std::string& key) { | ||
return tryGetProperty(key).has_value(); | ||
} | ||
|
||
bool Collection::addProperty(const std::string& key, PropertyType type) { | ||
const std::string sql = | ||
"insert into property (coll_id, name, type) values (@colid, @name, " | ||
"@type);"; | ||
|
||
(void)ctx->executeOneStep( | ||
sql, {{"@colid", this->id}, {"@name", key}, {"@type", (int)type}}); | ||
|
||
int id = ctx->getLastInsertedRowId(); | ||
|
||
if (id >= 0) updatePropCache(key, PropertyRep(key, id, type)); | ||
|
||
return id >= 0; | ||
} | ||
|
||
std::optional<PropertyRep> Collection::tryGetProperty(const std::string& key) { | ||
// check cache | ||
if (propertyCache.contains(this->id)) { | ||
if (propertyCache.get(this->id).contains(key)) { | ||
return propertyCache.get(this->id).at(key); | ||
} | ||
} | ||
|
||
LogDebug("Cache miss with key '%s'", key.c_str()); | ||
|
||
// else check db | ||
std::optional<int> id; | ||
auto property = PropertyRep::find(ctx, this->id, key); | ||
if (property.has_value()) { | ||
id = property.value().getId(); | ||
updatePropCache(key, property.value()); | ||
} | ||
|
||
return property; | ||
} | ||
|
||
PropertyRep Collection::getProperty(const std::string& key) { | ||
if (key == "id") { | ||
return PropertyRep("id", -1, PropertyType::ID); | ||
} | ||
|
||
auto prop = this->tryGetProperty(key); | ||
if (prop.has_value()) { | ||
return prop.value(); | ||
} else { | ||
LogError("Property '%s' was requested but wasn't found.", key.c_str()); | ||
|
||
throw std::runtime_error("Missing property"); | ||
} | ||
} | ||
|
||
std::vector<PropertyRep> Collection::getAllTheProperties() { | ||
const std::string sql = | ||
"select id, name, type from property where coll_id = @id;"; | ||
|
||
auto reader = ctx->executeReader(sql, {{"@id", this->id}}); | ||
|
||
std::vector<PropertyRep> props = {PropertyRep("id", -1, PropertyType::ID)}; | ||
std::shared_ptr<IDBRowReader> row; | ||
while (reader->readRow(row)) { | ||
props.push_back(PropertyRep(row->readString(1), row->readInt64(0), | ||
(PropertyType)row->readInt64(2))); | ||
} | ||
|
||
return std::move(props); | ||
} | ||
|
||
Collection Collection::find(IDB* ctx, const std::string& name) { | ||
const std::string sql = "SELECT id FROM collection where name = @name;"; | ||
|
||
auto id = ctx->executeAndGetFirstInt(sql, {{"@name", name}}); | ||
|
||
if (id.has_value()) { | ||
LogTrace("Collection %s found", name.c_str()); | ||
return Collection(ctx, id.value(), name); | ||
} else { | ||
LogTrace("Collection %s not found, creating it", name.c_str()); | ||
return Collection(ctx, create(*ctx, name), name); | ||
} | ||
} | ||
|
||
int Collection::create(IDB& ctx, const std::string& name) { | ||
const std::string sql = "insert into collection (name) values (@name);"; | ||
|
||
(void)ctx.executeOneStep(sql, {{"@name", name}}); | ||
|
||
return ctx.getLastInsertedRowId(); | ||
} | ||
|
||
int Collection::documentExists(int doc_id) { | ||
const auto sql = | ||
"select d.id from document as d where coll_id=@coll_id and d.id = @id;"; | ||
|
||
auto id = ctx->executeAndGetFirstInt( | ||
sql, {{"@coll_id", this->id}, {"@id", doc_id}}); | ||
|
||
return id.has_value(); | ||
} | ||
|
||
void Collection::updatePropCache(const std::string& key, PropertyRep prop) { | ||
std::unordered_map<std::string, PropertyRep> map; | ||
propertyCache.tryGet(this->id, map); | ||
map.insert({key, prop}); | ||
propertyCache.insert(this->id, map); // updates the position | ||
} | ||
std::string Collection::getName() const { return name; } | ||
} // namespace nldb |
Oops, something went wrong.