crdt_tests/version0/crdt/text_document.hpp

286 lines
7.4 KiB
C++
Raw Normal View History

2022-12-13 19:15:38 +01:00
#pragma once
2022-12-13 18:17:34 +01:00
#include "./list.hpp"
#include <variant>
2022-12-16 16:21:28 +01:00
//#include <iostream> // debug
2022-12-14 22:06:23 +01:00
2022-12-13 18:17:34 +01:00
namespace GreenCRDT {
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>;
2022-12-16 16:21:28 +01:00
// TODO: implement
2022-12-13 18:17:34 +01:00
struct Cursor {
AgentType who;
typename ListType::ListID pos;
};
AgentType local_agent;
2022-12-13 18:17:34 +01:00
ListType state;
[[nodiscard]] std::string getText(void) const {
2022-12-13 18:17:34 +01:00
std::string text;
for (const auto& it : state.list) {
if (it.value.has_value()) {
2022-12-13 18:17:34 +01:00
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(
2022-12-14 22:06:23 +01:00
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++) {
2022-12-14 22:06:23 +01:00
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) {
2022-12-14 22:06:23 +01:00
if(std::holds_alternative<typename ListType::OpAdd>(op)) {
const auto& add_op = std::get<typename ListType::OpAdd>(op);
2022-12-14 22:06:23 +01:00
//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++) {
2022-12-17 02:50:37 +01:00
if (!state.list.at(i).value.has_value()) {
// allready deleted
continue;
}
ops.emplace_back(typename ListType::OpDel{
2022-12-17 02:50:37 +01:00
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
2022-12-14 22:06:23 +01:00
std::vector<Op> merge(std::string_view text) {
if (text.empty()) {
2022-12-17 02:50:37 +01:00
if (state.list.empty() || state.doc_size == 0) {
// no op
2022-12-14 22:06:23 +01:00
return {};
} else {
// delete all
return delRange(std::nullopt, std::nullopt);
2022-12-14 22:06:23 +01:00
}
}
2022-12-14 22:06:23 +01:00
// text not empty
if (state.list.empty()) {
return addText(
std::nullopt,
std::nullopt,
2022-12-14 22:06:23 +01:00
text
);
}
2022-12-14 22:06:23 +01:00
// neither empty
// find start and end of changes
// start
2022-12-14 22:06:23 +01:00
size_t list_start = 0;
2022-12-17 02:50:37 +01:00
size_t list_start_counted = 0;
2022-12-14 22:06:23 +01:00
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]) {
2022-12-14 22:06:23 +01:00
differ = true;
break;
}
list_start++;
text_start++;
2022-12-17 02:50:37 +01:00
list_start_counted++;
2022-12-14 22:06:23 +01:00
}
// doc and text dont differ
if (!differ && list_start == state.list.size() && text_start == text.size()) {
2022-12-14 22:06:23 +01:00
return {};
}
2022-12-16 16:21:28 +01:00
//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";
2022-12-14 22:06:23 +01:00
// +1 so i can have unsigned
size_t list_end = state.list.size();
size_t text_end = text.size();
2022-12-17 02:50:37 +01:00
//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) {
2022-12-14 22:06:23 +01:00
// 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;
2022-12-14 22:06:23 +01:00
break;
}
list_end--;
text_end--;
2022-12-17 02:50:37 +01:00
list_end_counted++;
2022-12-14 22:06:23 +01:00
}
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++;
}
2022-12-16 16:21:28 +01:00
//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
);
2022-12-16 16:21:28 +01:00
//std::cout << "deleted: " << ops.size() << "\n";
}
2022-12-16 16:21:28 +01:00
//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)
);
2022-12-16 16:21:28 +01:00
//std::cout << "added: " << tmp_add_ops.size() << "\n";
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());
}
return ops;
}
2022-12-13 18:17:34 +01:00
};
} // GreenCRDT