start with doc store

This commit is contained in:
Green Sky 2023-10-29 02:21:40 +02:00
commit 0078850db4
No known key found for this signature in database
6 changed files with 209 additions and 0 deletions

26
.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
.vs/
*.o
*.swp
~*
*~
.idea/
cmake-build-debug/
cmake-build-debugandtest/
cmake-build-release/
*.stackdump
*.coredump
compile_commands.json
/build*
/result*
.clangd
.cache
.DS_Store
.AppleDouble
.LSOverride
CMakeLists.txt.user*
CMakeCache.txt
*.tox
imgui.ini

67
CMakeLists.txt Normal file
View File

@ -0,0 +1,67 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
# cmake setup begin
project(solanaceae_crdtnotes)
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(SOLANACEAE_CRDTNOTES_STANDALONE ON)
# why the f do i need this >:(
set(NOT_SOLANACEAE_CRDTNOTES_STANDALONE OFF)
else()
set(SOLANACEAE_CRDTNOTES_STANDALONE OFF)
set(NOT_SOLANACEAE_CRDTNOTES_STANDALONE ON)
endif()
message("II SOLANACEAE_CRDTNOTES_STANDALONE " ${SOLANACEAE_CRDTNOTES_STANDALONE})
if (SOLANACEAE_CRDTNOTES_STANDALONE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# defaulting to debug mode, if not specified
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
# setup my vim ycm :D
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# more paths
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
endif()
# external libs
add_subdirectory(./external) # before increasing warn levels, sad :(
if (SOLANACEAE_CRDTNOTES_STANDALONE)
set(CMAKE_CXX_EXTENSIONS OFF)
# bump up warning levels appropriately for clang, gcc & msvc
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
add_compile_options(
-Wall -Wextra # Reasonable and standard
-Wpedantic # Warn if non-standard C++ is used
-Wunused # Warn on anything being unused
#-Wconversion # Warn on type conversions that may lose data
#-Wsign-conversion # Warn on sign conversions
-Wshadow # Warn if a variable declaration shadows one from a parent context
)
if (NOT WIN32)
#link_libraries(-fsanitize=address,undefined)
#link_libraries(-fsanitize=undefined)
endif()
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
endif()
endif()
# cmake setup end
add_subdirectory(./src)

12
external/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
include(FetchContent)
if (NOT TARGET crdt_version3)
FetchContent_Declare(crdt_version3
GIT_REPOSITORY https://github.com/Green-Sky/green_crdt.git
GIT_TAG master
)
FetchContent_MakeAvailable(crdt_version3)
endif()

15
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
add_library(solanaceae_crdtnotes
./solanaceae/crdtnotes/crdtnotes.hpp
./solanaceae/crdtnotes/crdtnotes.cpp
)
target_include_directories(solanaceae_crdtnotes PUBLIC .)
target_compile_features(solanaceae_crdtnotes PUBLIC cxx_std_17)
target_link_libraries(solanaceae_crdtnotes PUBLIC
crdt_version3
#solanaceae_util
)
########################################

View File

@ -0,0 +1,39 @@
#include "./crdtnotes.hpp"
CRDTNotes::CRDTNotes(void) {
}
CRDTNotes::~CRDTNotes(void) {
}
std::vector<CRDTNotes::DocID> CRDTNotes::getDocList(void) {
std::vector<CRDTNotes::DocID> list;
for (const auto& [id, doc] : _docs) {
list.push_back(id);
}
return list;
}
const CRDTNotes::Doc* CRDTNotes::getDoc(const DocID& id) const {
auto res = _docs.find(id);
return res != _docs.cend() ? &(res->second) : nullptr;
}
CRDTNotes::Doc* CRDTNotes::getDoc(const DocID& id) {
auto res = _docs.find(id);
return res != _docs.cend() ? &(res->second) : nullptr;
}
CRDTNotes::Doc* CRDTNotes::addDoc(const CRDTAgent& self_agent, const DocID& id) {
if (_docs.count(id)) {
// error exists
// noop?
return nullptr;
}
// create and set local_actor
auto& doc = _docs[id];
doc.local_actor = self_agent;
return &doc;
}

View File

@ -0,0 +1,50 @@
#pragma once
#include <green_crdt/v3/text_document.hpp>
#include <array>
#include <cstdint>
#include <functional>
#include <unordered_map>
using ID32 = std::array<uint8_t, 32>;
template<>
struct std::hash<ID32> {
std::size_t operator()(ID32 const& s) const noexcept {
static_assert(sizeof(size_t) == 8);
// TODO: maybe shuffle the indices a bit
return
(static_cast<size_t>(s[0]) << 8*0) |
(static_cast<size_t>(s[1]) << 8*1) |
(static_cast<size_t>(s[2]) << 8*2) |
(static_cast<size_t>(s[3]) << 8*3) |
(static_cast<size_t>(s[4]) << 8*4) |
(static_cast<size_t>(s[5]) << 8*5) |
(static_cast<size_t>(s[6]) << 8*6) |
(static_cast<size_t>(s[7]) << 8*7)
;
}
};
class CRDTNotes {
using CRDTAgent = ID32;
using DocID = ID32;
using Doc = GreenCRDT::V3::TextDocument<CRDTAgent>;
// TODO: add metadata to docs
std::unordered_map<DocID, Doc> _docs;
public:
// config?
CRDTNotes(void);
~CRDTNotes(void);
std::vector<DocID> getDocList(void);
const Doc* getDoc(const DocID& id) const;
Doc* getDoc(const DocID& id);
Doc* addDoc(const CRDTAgent& self_agent, const DocID& doc);
};