fix + random doc id

This commit is contained in:
Green Sky 2023-11-03 15:51:32 +01:00
parent a1bf480b82
commit 7fff379d88
No known key found for this signature in database
3 changed files with 19 additions and 4 deletions

View File

@ -1,6 +1,8 @@
#include "./crdtnotes.hpp" #include "./crdtnotes.hpp"
CRDTNotes::CRDTNotes(void) { CRDTNotes::CRDTNotes(void) {
_rng.seed(std::random_device{}());
_rng.discard(707);
} }
CRDTNotes::~CRDTNotes(void) { CRDTNotes::~CRDTNotes(void) {
@ -37,3 +39,13 @@ CRDTNotes::Doc* CRDTNotes::addDoc(const CRDTAgent& self_agent, const DocID& id)
return &doc; return &doc;
} }
CRDTNotes::Doc* CRDTNotes::addDoc(const CRDTAgent& self_agent) {
DocID new_id;
for (auto& it : new_id) {
// TODO: this discards alot
it = static_cast<uint8_t>(_rng());
}
return addDoc(self_agent, new_id);
}

View File

@ -6,6 +6,7 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <unordered_map> #include <unordered_map>
#include <random>
using ID32 = std::array<uint8_t, 32>; using ID32 = std::array<uint8_t, 32>;
@ -36,6 +37,7 @@ class CRDTNotes {
private: private:
// TODO: add metadata to docs // TODO: add metadata to docs
std::unordered_map<DocID, Doc> _docs; std::unordered_map<DocID, Doc> _docs;
std::default_random_engine _rng;
public: public:
@ -49,5 +51,8 @@ class CRDTNotes {
Doc* getDoc(const DocID& id); Doc* getDoc(const DocID& id);
Doc* addDoc(const CRDTAgent& self_agent, const DocID& doc); Doc* addDoc(const CRDTAgent& self_agent, const DocID& doc);
// new doc with random id
Doc* addDoc(const CRDTAgent& self_agent);
}; };

View File

@ -39,7 +39,7 @@ namespace detail {
std::string to_hex(const Container& data) { std::string to_hex(const Container& data) {
std::string res; std::string res;
for (const uint8_t it : data) { for (const uint8_t it : data) {
res += nib_to_hex(it << 4); res += nib_to_hex(it >> 4);
res += nib_to_hex(it & 0x0f); res += nib_to_hex(it & 0x0f);
} }
return res; return res;
@ -74,9 +74,7 @@ float CRDTNotesImGui::render(void) {
_notes.addDoc( _notes.addDoc(
// tox id (id from self) // tox id (id from self)
self_agent_id, self_agent_id
// random 32bytes?
{}
); );
// and open the doc // and open the doc