mirror of
https://github.com/Green-Sky/crdt_tests.git
synced 2025-07-05 17:46:45 +02:00
rename stuff
This commit is contained in:
247
version0/green_crdt/v0/list.hpp
Normal file
247
version0/green_crdt/v0/list.hpp
Normal file
@ -0,0 +1,247 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace GreenCRDT::V0 {
|
||||
|
||||
template<typename ValueType, typename AgentType>
|
||||
struct List {
|
||||
struct ListID {
|
||||
AgentType id;
|
||||
uint64_t seq{0}; // strictly increasing for that agent
|
||||
|
||||
bool operator<(const ListID& rhs) const {
|
||||
if (seq < rhs.seq) {
|
||||
return true;
|
||||
} else if (seq > rhs.seq) {
|
||||
return false;
|
||||
} else { // ==
|
||||
return id < rhs.id;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const ListID& rhs) const {
|
||||
return seq == rhs.seq && id == rhs.id;
|
||||
}
|
||||
|
||||
bool operator!=(const ListID& rhs) const {
|
||||
return seq != rhs.seq || id != rhs.id;
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: move ops up?
|
||||
// almost the same as entry
|
||||
struct OpAdd {
|
||||
ListID id;
|
||||
|
||||
std::optional<ListID> parent_left;
|
||||
std::optional<ListID> parent_right;
|
||||
|
||||
ValueType value;
|
||||
};
|
||||
|
||||
struct OpDel {
|
||||
ListID id;
|
||||
};
|
||||
|
||||
// TODO: replace with SoA
|
||||
struct Entry {
|
||||
ListID id;
|
||||
|
||||
// Yjs
|
||||
std::optional<ListID> parent_left;
|
||||
std::optional<ListID> parent_right;
|
||||
|
||||
// might be deleted (yes, *sigh*, crtds need tombstones)
|
||||
std::optional<ValueType> value;
|
||||
};
|
||||
|
||||
// TODO: use something better, edit: this seems fine
|
||||
std::vector<Entry> list;
|
||||
|
||||
// number of not deleted entries
|
||||
size_t doc_size {0};
|
||||
|
||||
std::map<AgentType, uint64_t> last_seen_seq;
|
||||
|
||||
std::optional<size_t> findIdx(const ListID& list_id) const {
|
||||
//verify(); // too expensive
|
||||
for (size_t i = 0; i < list.size(); i++) {
|
||||
if (list[i].id == list_id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// returns false if missing OPs
|
||||
// based on YjsMod https://github.com/josephg/reference-crdts/blob/9f4f9c3a97b497e2df8ae4473d1e521d3c3bf2d2/crdts.ts#L293-L348
|
||||
// which is a modified Yjs(YATA) algo
|
||||
bool add(const ListID& list_id, const ValueType& value, const std::optional<ListID>& parent_left, const std::optional<ListID>& parent_right) {
|
||||
verify();
|
||||
// check agent op order
|
||||
if (!last_seen_seq.count(list_id.id)) {
|
||||
// we dont know this agent yet, first seq needs to be 0
|
||||
if (list_id.seq != 0) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// making sure we dont skip operations by that agent
|
||||
if (list_id.seq != last_seen_seq.at(list_id.id) + 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
size_t insert_idx = 0;
|
||||
if (list.empty()) {
|
||||
if (parent_left.has_value() || parent_right.has_value()) {
|
||||
// empty, missing parents
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// find left
|
||||
std::optional<size_t> left_idx = std::nullopt;
|
||||
if (parent_left.has_value()) {
|
||||
left_idx = findIdx(parent_left.value());
|
||||
if (!left_idx.has_value()) {
|
||||
// missing parent left
|
||||
return false;
|
||||
}
|
||||
|
||||
// we insert before the it, so we need to go past the left parent
|
||||
insert_idx = left_idx.value() + 1;
|
||||
} // else insert_idx = 0
|
||||
|
||||
// find right
|
||||
size_t right_idx = list.size();
|
||||
if (parent_right.has_value()) {
|
||||
auto tmp_right = findIdx(parent_right.value());
|
||||
if (!tmp_right.has_value()) {
|
||||
return false;
|
||||
}
|
||||
right_idx = tmp_right.value();
|
||||
}
|
||||
|
||||
bool scanning {false};
|
||||
|
||||
for(size_t i = insert_idx;; i++) {
|
||||
if (!scanning) {
|
||||
insert_idx = i;
|
||||
}
|
||||
// if right parent / end of doc, insert
|
||||
if (insert_idx == right_idx) {
|
||||
break;
|
||||
}
|
||||
// we ran past right o.o ?
|
||||
if (insert_idx == list.size()) {
|
||||
break;
|
||||
}
|
||||
|
||||
const Entry& at_i = list[i];
|
||||
// parents left and right
|
||||
std::optional<size_t> i_left_idx {std::nullopt};
|
||||
if (at_i.parent_left.has_value()) {
|
||||
i_left_idx = findIdx(at_i.parent_left.value());
|
||||
if (!i_left_idx.has_value()) {
|
||||
assert(false && "item in list with unknown parent left!!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// possibility map
|
||||
//
|
||||
// | ir < r | ir == r | ir > r
|
||||
// -------------------------------------
|
||||
// il < l | insert | insert | insert
|
||||
// il == l | ? | agentfallback | ?
|
||||
// il > l | skip | skip | skip
|
||||
|
||||
if (i_left_idx < left_idx) {
|
||||
break;
|
||||
} else if (i_left_idx == left_idx) {
|
||||
// get i parent_right
|
||||
size_t i_right_idx = list.size();
|
||||
if (at_i.parent_right.has_value()) {
|
||||
auto tmp_right = findIdx(at_i.parent_right.value());
|
||||
if (!tmp_right.has_value()) {
|
||||
assert(false && "item in list with unknown parent right!!");
|
||||
return false;
|
||||
}
|
||||
i_right_idx = tmp_right.value();
|
||||
}
|
||||
|
||||
if (i_right_idx < right_idx) {
|
||||
scanning = true;
|
||||
} else if (i_right_idx == right_idx) {
|
||||
// agent id tie breaker
|
||||
if (list_id.id < at_i.id.id) {
|
||||
break;
|
||||
} else {
|
||||
scanning = false;
|
||||
}
|
||||
} else { // i_right_idx > right_idx
|
||||
scanning = false;
|
||||
}
|
||||
} else { // il > l
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list.emplace(list.begin() + insert_idx, Entry{
|
||||
list_id,
|
||||
parent_left,
|
||||
parent_right,
|
||||
value
|
||||
});
|
||||
|
||||
doc_size++;
|
||||
last_seen_seq[list_id.id] = list_id.seq;
|
||||
verify();
|
||||
return true;
|
||||
}
|
||||
|
||||
// returns false if not found
|
||||
bool del(const ListID& id) {
|
||||
verify();
|
||||
for (auto& it : list) {
|
||||
if (it.id == id) {
|
||||
if (it.value.has_value()) {
|
||||
it.value.reset();
|
||||
|
||||
doc_size--;
|
||||
verify();
|
||||
return true;
|
||||
} else {
|
||||
verify();
|
||||
return false; // TODO: allow double deletes?,,,, need ids
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
verify();
|
||||
// not found
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: only in debug?
|
||||
void verify(void) const {
|
||||
size_t actual_size = 0;
|
||||
for (const auto& it : list) {
|
||||
if (it.value.has_value()) {
|
||||
actual_size++;
|
||||
}
|
||||
}
|
||||
assert(doc_size == actual_size);
|
||||
}
|
||||
};
|
||||
|
||||
} // GreenCRDT::V0
|
||||
|
285
version0/green_crdt/v0/text_document.hpp
Normal file
285
version0/green_crdt/v0/text_document.hpp
Normal file
@ -0,0 +1,285 @@
|
||||
#pragma once
|
||||
|
||||
#include "./list.hpp"
|
||||
|
||||
#include <variant>
|
||||
|
||||
//#include <iostream> // debug
|
||||
|
||||
namespace GreenCRDT::V0 {
|
||||
|
||||
template<typename AgentType>
|
||||
struct TextDocument {
|
||||
// TODO: determine if char is the best
|
||||
using ListType = List<char, AgentType>;
|
||||
|
||||
using Op = std::variant<typename ListType::OpAdd, typename ListType::OpDel>;
|
||||
|
||||
// TODO: implement
|
||||
struct Cursor {
|
||||
AgentType who;
|
||||
typename ListType::ListID pos;
|
||||
};
|
||||
|
||||
AgentType local_agent;
|
||||
|
||||
ListType state;
|
||||
|
||||
[[nodiscard]] std::string getText(void) const {
|
||||
std::string text;
|
||||
|
||||
for (const auto& it : state.list) {
|
||||
if (it.value.has_value()) {
|
||||
text += it.value.value();
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
bool apply(const Op& op) {
|
||||
if(std::holds_alternative<typename ListType::OpAdd>(op)) {
|
||||
const auto& add_op = std::get<typename ListType::OpAdd>(op);
|
||||
//std::cout << "a:" << add_op.id.id << " s:" << add_op.id.seq << " v:" << add_op.value << "\n";
|
||||
return state.add(add_op.id, add_op.value, add_op.parent_left, add_op.parent_right);
|
||||
} else if (std::holds_alternative<typename ListType::OpDel>(op)) {
|
||||
const auto& del_op = std::get<typename ListType::OpDel>(op);
|
||||
return state.del(del_op.id);
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
bool apply(const std::vector<Op>& ops) {
|
||||
for (const auto& op : ops) {
|
||||
if (!apply(op)) {
|
||||
// this is not ideal, since we might have applyed some, and dont report which/howmany
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static std::vector<Op> text2adds(
|
||||
const AgentType& agent, uint64_t seq, // seq is the first seq
|
||||
std::optional<typename ListType::ListID> parent_left,
|
||||
std::optional<typename ListType::ListID> parent_right,
|
||||
std::string_view text
|
||||
) {
|
||||
std::vector<Op> ops;
|
||||
for (size_t i = 0; i < text.size(); i++) {
|
||||
typename ListType::ListID new_id {agent, seq++};
|
||||
|
||||
ops.emplace_back(typename ListType::OpAdd{
|
||||
new_id,
|
||||
parent_left,
|
||||
parent_right,
|
||||
text[i]
|
||||
});
|
||||
|
||||
parent_left = new_id;
|
||||
}
|
||||
|
||||
return ops;
|
||||
}
|
||||
|
||||
// adds in tast with specified parents
|
||||
// returns generated ops
|
||||
std::vector<Op> addText(
|
||||
std::optional<typename ListType::ListID> parent_left,
|
||||
std::optional<typename ListType::ListID> parent_right,
|
||||
std::string_view text
|
||||
) {
|
||||
// TODO: look up typesystem and fix (move? decltype?)
|
||||
std::vector<Op> ops = text2adds(
|
||||
local_agent, state.last_seen_seq.count(local_agent) ? state.last_seen_seq[local_agent]+1u : 0u,
|
||||
parent_left,
|
||||
parent_right,
|
||||
text
|
||||
);
|
||||
|
||||
// TODO: make this better
|
||||
// and apply
|
||||
for (const auto& op : ops) {
|
||||
if(std::holds_alternative<typename ListType::OpAdd>(op)) {
|
||||
const auto& add_op = std::get<typename ListType::OpAdd>(op);
|
||||
//std::cout << "a:" << add_op.id.id << " s:" << add_op.id.seq << " v:" << add_op.value << "\n";
|
||||
bool r = state.add(add_op.id, add_op.value, add_op.parent_left, add_op.parent_right);
|
||||
assert(r);
|
||||
} else if (std::holds_alternative<typename ListType::OpDel>(op)) {
|
||||
const auto& del_op = std::get<typename ListType::OpDel>(op);
|
||||
state.del(del_op.id);
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
return ops; // TODO: move?
|
||||
}
|
||||
|
||||
// deletes everything in range [first, last)
|
||||
// returns generated ops
|
||||
std::vector<Op> delRange(
|
||||
std::optional<typename ListType::ListID> left,
|
||||
std::optional<typename ListType::ListID> right
|
||||
) {
|
||||
size_t first_idx = 0;
|
||||
if (left.has_value()) {
|
||||
auto res = state.findIdx(left.value());
|
||||
if (!res.has_value()) {
|
||||
assert(false && "cant find left");
|
||||
return {};
|
||||
}
|
||||
first_idx = res.value();
|
||||
}
|
||||
|
||||
size_t last_idx = state.list.size();
|
||||
if (right.has_value()) {
|
||||
auto res = state.findIdx(right.value());
|
||||
if (!res.has_value()) {
|
||||
assert(false && "cant find right");
|
||||
return {};
|
||||
}
|
||||
last_idx = res.value();
|
||||
}
|
||||
|
||||
std::vector<Op> ops;
|
||||
|
||||
for (size_t i = first_idx; i < last_idx; i++) {
|
||||
if (!state.list.at(i).value.has_value()) {
|
||||
// allready deleted
|
||||
continue;
|
||||
}
|
||||
|
||||
ops.emplace_back(typename ListType::OpDel{
|
||||
state.list.at(i).id
|
||||
});
|
||||
|
||||
// TODO: do delets get a seq?????
|
||||
|
||||
state.del(state.list[i].id);
|
||||
}
|
||||
|
||||
return ops;
|
||||
}
|
||||
|
||||
// generates ops from the difference
|
||||
// note: rn it only creates 1 diff patch
|
||||
std::vector<Op> merge(std::string_view text) {
|
||||
if (text.empty()) {
|
||||
if (state.list.empty() || state.doc_size == 0) {
|
||||
// no op
|
||||
return {};
|
||||
} else {
|
||||
// delete all
|
||||
return delRange(std::nullopt, std::nullopt);
|
||||
}
|
||||
}
|
||||
// text not empty
|
||||
|
||||
if (state.list.empty()) {
|
||||
return addText(
|
||||
std::nullopt,
|
||||
std::nullopt,
|
||||
text
|
||||
);
|
||||
}
|
||||
// neither empty
|
||||
|
||||
// find start and end of changes
|
||||
// start
|
||||
size_t list_start = 0;
|
||||
size_t list_start_counted = 0;
|
||||
size_t text_start = 0;
|
||||
bool differ = false;
|
||||
for (; list_start < state.list.size() && text_start < text.size();) {
|
||||
// jump over tombstones
|
||||
if (!state.list[list_start].value.has_value()) {
|
||||
list_start++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state.list[list_start].value.value() != text[text_start]) {
|
||||
differ = true;
|
||||
break;
|
||||
}
|
||||
|
||||
list_start++;
|
||||
text_start++;
|
||||
list_start_counted++;
|
||||
}
|
||||
|
||||
// doc and text dont differ
|
||||
if (!differ && list_start == state.list.size() && text_start == text.size()) {
|
||||
return {};
|
||||
}
|
||||
//std::cout << "list.size: " << state.list.size() << "(" << getText().size() << ")" << " text.size: " << text.size() << "\n";
|
||||
//std::cout << "list_start: " << list_start << " text_start: " << text_start << "\n";
|
||||
|
||||
// +1 so i can have unsigned
|
||||
size_t list_end = state.list.size();
|
||||
size_t text_end = text.size();
|
||||
//for (; list_end > 0 && text_end > 0 && list_end >= list_start && text_end >= text_start;) {
|
||||
//while (list_end >= list_start && text_end >= text_start) {
|
||||
size_t list_end_counted = 0;
|
||||
differ = false; // var reuse
|
||||
//while (list_start_counted - list_end_counted > state.doc_size && text_end >= text_start) {
|
||||
while (state.doc_size - list_start_counted > list_end_counted && text_end >= text_start) {
|
||||
// jump over tombstones
|
||||
if (!state.list[list_end-1].value.has_value()) {
|
||||
list_end--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state.list[list_end-1].value.value() != text[text_end-1]) {
|
||||
differ = true;
|
||||
break;
|
||||
}
|
||||
|
||||
list_end--;
|
||||
text_end--;
|
||||
list_end_counted++;
|
||||
}
|
||||
|
||||
if (!differ && text_start == text_end+1) {
|
||||
// we ran into eachother without seeing the different char
|
||||
// TODO: do we need to increment list_end? text_end?
|
||||
list_end++;
|
||||
}
|
||||
|
||||
//std::cout << "list_end: " << list_end << " text_end: " << text_end << "\n";
|
||||
//std::cout << "substring before: " << text.substr(text_start, text.size() - state.doc_size) << "\n";
|
||||
|
||||
std::vector<Op> ops;
|
||||
|
||||
// 1. clear range (del all list_start - list_end)
|
||||
if (list_start <= list_end && list_start < state.list.size()) {
|
||||
//list_end += list_start == list_end;
|
||||
ops = delRange(
|
||||
state.list[list_start].id,
|
||||
list_end < state.list.size() ? std::make_optional(state.list[list_end].id) : std::nullopt
|
||||
);
|
||||
//std::cout << "deleted: " << ops.size() << "\n";
|
||||
}
|
||||
|
||||
//std::cout << "text between: " << getText() << "\n";
|
||||
//std::cout << "substring between: " << text.substr(text_start, text.size() - state.doc_size) << "\n";
|
||||
|
||||
// 2. add range (add all text_start - text_end)
|
||||
if (state.doc_size < text.size()) {
|
||||
auto tmp_add_ops = addText(
|
||||
list_start == 0 ? std::nullopt : std::make_optional(state.list[list_start-1].id),
|
||||
list_start == state.list.size() ? std::nullopt :std::make_optional(state.list.at(list_start).id),
|
||||
text.substr(text_start, text.size() - state.doc_size)
|
||||
);
|
||||
//std::cout << "added: " << tmp_add_ops.size() << "\n";
|
||||
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());
|
||||
}
|
||||
|
||||
return ops;
|
||||
}
|
||||
};
|
||||
|
||||
} // GreenCRDT::V0
|
||||
|
Reference in New Issue
Block a user