Skip to content

Commit

Permalink
🔥 new design C.U.D. working
Browse files Browse the repository at this point in the history
  • Loading branch information
enzo418 committed Oct 5, 2022
1 parent 3e71a26 commit 68b623a
Show file tree
Hide file tree
Showing 101 changed files with 3,369 additions and 2,102 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
!*.h
!README.md
!CMakeLists.txt
!*.cmake

!vendor/
!vendor/*
!vendor/**/
!LICENSE*

!queries/
!queries/*.sql
!queries/*.sql

!*.plantuml
!docs/
!docs/**/
!docs/**/*.svg
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/noLiteDb",
"args": [],
"preLaunchTask": "build",
"cwd": "${workspaceFolder}"
"cwd": "${workspaceFolder}/build"
},
{
"type": "lldb",
Expand All @@ -20,7 +20,7 @@
"program": "${workspaceFolder}/build/tests",
"args": [],
"preLaunchTask": "build",
"cwd": "${workspaceFolder}"
"cwd": "${workspaceFolder}/build"
}
]
}
88 changes: 42 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,64 @@ project(noLiteDb)

file(GLOB_RECURSE SOURCES src/*.cpp)

# REMOVE MAIN so we can don't need to compile the code again for the tests
# REMOVE MAIN from sources
# Note: this way we don't need to compile the code again for the tests
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)


# Set cmake cxx flags
# Set std version
# Note: I do it this way because otherwise export compile commands doesn't export it
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")

# clangd:
# clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE DEBUG)

include_directories(src/include)
include_directories(vendor/sqlite)
include_directories(vendor/logger/include)
include_directories(vendor/lrucache11)
include_directories(vendor/others)

# --- YAMLCPP
# i thought to use yaml-cpp but doesn't have an easy way to get the data type
# and that was crucial for this library
include(Dependecy.cmake)

# --- json (nlohmann)
set(JSON_BuildTests OFF CACHE INTERNAL "")
set(JSON_Install OFF CACHE INTERNAL "")
add_subdirectory(vendor/json)
message(STATUS "NLDB dependencies: ${DEPENDENCY_LIST}")
message(STATUS "NLDB dependency libs: ${DEPENDENCY_LIBS}")
message(STATUS "NLDB dependency lib includes: ${DEPENDENCY_INCLUDE_DIR}")

# --- SQLITE library
add_library(c_sqllite3 vendor/sqlite/sqlite/sqlite3.c)
include_directories(src/include)
include_directories(src)

# --- Add nolitedb library
# --------------------- NOLITE --------------------- #
add_library(noLitedb_lb ${SOURCES})
target_link_libraries(noLitedb_lb c_sqllite3 nlohmann_json::nlohmann_json)

set(CMAKE_BUILD_TYPE DEBUG)
add_dependencies(noLitedb_lb ${DEPENDENCY_LIST})

target_include_directories(noLitedb_lb PUBLIC ${DEPENDENCY_INCLUDE_DIR} ${DEPENDENCY_INCLUDE_LIST})
target_link_libraries(noLitedb_lb PUBLIC ${DEPENDENCY_LIBS})
target_link_directories(noLitedb_lb PUBLIC ${DEPENDENCY_LIB_DIR})

# --- Add nolitedb executable
add_executable(noLiteDb src/main.cpp)
target_link_libraries(noLiteDb noLitedb_lb)
# ------------------ NOLITE EXECUTABLE ----------------- #
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} noLitedb_lb)

# --- TESTS
add_subdirectory(vendor/googletest)
include_directories(vendor/googletest/googletest/include)
# ------------------------ TESTS ----------------------- #
# add_subdirectory(vendor/googletest)
# include_directories(vendor/googletest/googletest/include)

enable_testing()
# enable_testing()

file(GLOB TESTS_SRC tests/*.cpp)
# file(GLOB TESTS_SRC tests/*.cpp)

add_executable(
tests
${TESTS_SRC}
)
# add_executable(
# tests
# ${TESTS_SRC}
# )

target_link_libraries(
tests
noLitedb_lb
GTest::gtest
)
# target_link_libraries(
# tests
# noLitedb_lb
# GTest::gtest
# )

if (CMAKE_BUILD_TYPE MATCHES "DEBUG")
add_definitions( -D LOGGER_MAX_LOG_LEVEL_PRINTED=6 ) # trace
else ()
add_definitions( -D LOGGER_MAX_LOG_LEVEL_PRINTED=1 ) # error
endif()
# if (CMAKE_BUILD_TYPE MATCHES "DEBUG")
# add_definitions( -D LOGGER_MAX_LOG_LEVEL_PRINTED=6 ) # trace
# else ()
# add_definitions( -D LOGGER_MAX_LOG_LEVEL_PRINTED=1 ) # error
# endif()

include(GoogleTest)
gtest_discover_tests(tests)
# include(GoogleTest)
# gtest_discover_tests(tests)
64 changes: 64 additions & 0 deletions Dependecy.cmake
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)
36 changes: 0 additions & 36 deletions queries/create.sql

This file was deleted.

134 changes: 7 additions & 127 deletions src/Collection.cpp
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
Loading

0 comments on commit 68b623a

Please sign in to comment.