start with doc store

This commit is contained in:
2023-10-29 02:21:40 +02:00
commit 0078850db4
6 changed files with 209 additions and 0 deletions

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);
};