Compare commits

..

24 Commits

Author SHA1 Message Date
8d8fa2f5da add debug bench result 2022-12-23 15:34:54 +01:00
d85a2dc191 add text document to v3, planing v4 2022-12-23 03:16:38 +01:00
931436dc11 v3 simple SoA, not really an improvement bc index hints remove most find iterations 2022-12-23 00:40:08 +01:00
47f406b786 corredted v1 results, v2 using hint for seach and last insert index cache as hint 2022-12-22 23:51:59 +01:00
72d00f759d bench v1 2022-12-22 20:25:23 +01:00
be5485856f basic lightly tested v1, only change from v0 to v1 is actor cache (way lighter for large actor ids) 2022-12-22 19:49:34 +01:00
a597193529 add jpaper bench for v0 2022-12-22 18:18:56 +01:00
175042eb7c rename stuff 2022-12-22 15:20:32 +01:00
e486d79dc3 make vim faster :D 2022-12-22 15:06:11 +01:00
3725b76ce7 fix crdt -> vim not deleting lines 2022-12-21 02:20:05 +01:00
4aa2a152af send requests, handle requests, more gossip
requests loop sometimes o.o
2022-12-21 00:53:49 +01:00
d845baf804 minor bug fixing, now only need requests 2022-12-20 20:32:11 +01:00
05fd0940ea forgot to clear buffer 2022-12-20 19:15:32 +01:00
ed72b27808 handle more packets, fix major flaw in diffing algo (again) and add extra tests 2022-12-20 18:46:44 +01:00
cb0c2642f8 works, somewhat 2022-12-20 02:23:54 +01:00
a0c87e5fc5 debugging <.< and splitting commands to reduce size 2022-12-20 00:21:15 +01:00
e961b8aec3 gossip p1 works, command 2 buffer works if not too large 2022-12-19 21:33:14 +01:00
7177d90c44 better threading and allow more then 1 port 2022-12-19 17:31:08 +01:00
e0938690c7 main thread com 2022-12-19 17:23:45 +01:00
2c10e258c0 prep for networking 2022-12-19 14:17:25 +01:00
79db39c5d1 more tox, not working yet 2022-12-18 12:21:40 +01:00
2165ab2070 add toxcore and start on vim test2 2022-12-17 19:57:10 +01:00
a3d9211e5e minor vimscript fixes 2022-12-17 18:39:21 +01:00
3c7111f6c9 crdt -> vim 2022-12-17 18:22:35 +01:00
34 changed files with 6377 additions and 31 deletions

5
.gitmodules vendored Normal file
View File

@ -0,0 +1,5 @@
[submodule "external/toxcore/c-toxcore"]
path = external/toxcore/c-toxcore
url = https://github.com/TokTok/c-toxcore.git
shallow = true
ignore = dirty

View File

@ -20,6 +20,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
add_subdirectory(./external/json) add_subdirectory(./external/json)
add_subdirectory(./external/zed_net) add_subdirectory(./external/zed_net)
add_subdirectory(./external/toxcore)
# Bump up warning levels appropriately for clang, gcc & msvc # Bump up warning levels appropriately for clang, gcc & msvc
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
@ -37,6 +38,12 @@ endif()
add_subdirectory(./prototyping EXCLUDE_FROM_ALL) add_subdirectory(./prototyping EXCLUDE_FROM_ALL)
add_subdirectory(./version0) add_subdirectory(./version0)
add_subdirectory(./version1)
add_subdirectory(./version2)
add_subdirectory(./version3)
#add_subdirectory(./version4)
add_subdirectory(./bench)
add_subdirectory(./vim_research) add_subdirectory(./vim_research)

48
bench/CMakeLists.txt Normal file
View File

@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
project(crdt_bench CXX C)
########################################
add_executable(crdt_bench_jpaper_v0
./v0_jpaper.cpp
)
target_link_libraries(crdt_bench_jpaper_v0 PUBLIC
crdt_version0
nlohmann_json::nlohmann_json
)
########################################
add_executable(crdt_bench_jpaper_v1
./v1_jpaper.cpp
)
target_link_libraries(crdt_bench_jpaper_v1 PUBLIC
crdt_version1
nlohmann_json::nlohmann_json
)
########################################
add_executable(crdt_bench_jpaper_v2
./v2_jpaper.cpp
)
target_link_libraries(crdt_bench_jpaper_v2 PUBLIC
crdt_version2
nlohmann_json::nlohmann_json
)
########################################
add_executable(crdt_bench_jpaper_v3
./v3_jpaper.cpp
)
target_link_libraries(crdt_bench_jpaper_v3 PUBLIC
crdt_version3
nlohmann_json::nlohmann_json
)

63
bench/README.md Normal file
View File

@ -0,0 +1,63 @@
# Timings
all benches use the uncompressed .json from disk
all done with the "cool an breezy copy" preset
note: the .json is not a perfect fit, bc it is not designed for the Yjs algo (missing parent_right and ids are not perfect)
the json contains:
doc size (with tombstones): 182315
doc size: 104852
total inserts: 182315
total deletes: 77463
total ops: 259778
## baseline ( just walking through the json, no insertions )
- g++9 -g :
- 23.0s ~11294 ops/s
- 22.6s ~11494 ops/s
- 23.0s
- g++9 -O3 -DNDEBUG :
- 9.6s ~27060 ops/s
- 9.7s
- 9.7s
## version0
- g++9 -g -O2 :
- 10m35s ~409 ops/s
- g++9 -O3 -DNDEBUG :
- 8m7s ~533 ops/s
## version1 - actor index
- g++9 -g -O2 :
- 4m1s ~1077 ops/s
- g++9 -O3 -DNDEBUG :
- 4m5s ~1060 ops/s
## version2 - find with hint, cache last insert and use as hint
- g++9 -g -O2 :
- 3m38s ~1191 ops/s
- g++9 -O3 -DNDEBUG :
- 3m43s ~1164 ops/s
## version3 - SoA, 1 array only ids, 1 array rest (parents, data)
- g++9 -g :
- 4m19s ~1003 ops/s
- g++9 -g -O2 :
- 3m36s ~1202 ops/s
- g++9 -O3 -DNDEBUG :
- 3m44s ~1159 ops/s

198
bench/v0_jpaper.cpp Normal file
View File

@ -0,0 +1,198 @@
#define EXTRA_ASSERTS 0
//#include <green_crdt/v0/text_document.hpp>
#include <green_crdt/v0/list.hpp>
#include <nlohmann/json.hpp>
#include <unordered_map>
#include <string_view>
#include <fstream>
#include <iostream>
#include <cassert>
using ActorID = std::array<uint8_t, 32>;
//using Doc = GreenCRDT::V0::TextDocument<ActorID>;
using List = GreenCRDT::V0::List<char, ActorID>;
template<>
struct std::hash<ActorID> {
std::size_t operator()(ActorID 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)
;
}
};
// for dev, benching in debug is usefull, but only if the ammount of asserts is reasonable
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace detail {
uint8_t nib_from_hex(char c) {
extra_assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
if (c >= '0' && c <= '9') {
return static_cast<uint8_t>(c) - '0';
} else if (c >= 'a' && c <= 'f') {
return (static_cast<uint8_t>(c) - 'a') + 10u;
} else {
return 0u;
}
}
} // detail
static ActorID ActorIDFromStr(std::string_view str) {
extra_assert(str.size() == 32*2);
ActorID tmp;
for (size_t i = 0; i < tmp.size(); i++) {
tmp[i] = detail::nib_from_hex(str[i*2]) << 4 | detail::nib_from_hex(str[i*2+1]);
}
return tmp;
}
// seq@ID type format used in the json
struct JObj {
ActorID id;
uint64_t seq {0};
};
static JObj JObjFromStr(std::string_view str) {
extra_assert(str.size() > 32*2 + 1);
size_t at_pos = str.find_first_of('@');
auto seq_sv = str.substr(0, at_pos);
auto id_sv = str.substr(at_pos+1);
assert(seq_sv.size() != 0);
assert(id_sv.size() == 32*2);
uint64_t tmp_seq {0};
for (size_t i = 0; i < seq_sv.size(); i++) {
assert(seq_sv[i] >= '0' && seq_sv[i] <= '9');
tmp_seq *= 10;
tmp_seq += seq_sv[i] - '0';
}
return {ActorIDFromStr(id_sv), tmp_seq};
}
int main(void) {
List list;
std::ifstream file {"../res/paper.json"};
std::cout << "start reading...\n";
uint64_t g_total_inserts {0};
uint64_t g_total_deletes {0};
//uint64_t g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, uint64_t> g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, std::unordered_map<uint64_t, uint64_t>> map_seq; // maps json op_seq -> lits id seq
for (std::string line; std::getline(file, line); ) {
nlohmann::json j_entry = nlohmann::json::parse(line);
const ActorID actor = ActorIDFromStr(static_cast<const std::string&>(j_entry["actor"]));
uint64_t op_seq = j_entry["startOp"];
for (const auto& j_op : j_entry["ops"]) {
if (j_op["action"] == "set") {
const auto obj = JObjFromStr(static_cast<const std::string&>(j_op["obj"]));
if (obj.seq != 1) {
// skip all non text edits (create text doc, curser etc)
continue;
}
if (j_op["insert"]) {
const auto& j_parent = j_op["key"];
extra_assert(!j_parent.is_null());
if (j_parent == "_head") {
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
std::nullopt,
std::nullopt
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
} else { // we have a parrent
extra_assert(static_cast<const std::string&>(j_op["value"]).size() == 1);
// split parent into seq and actor
const auto parent_left = JObjFromStr(static_cast<const std::string&>(j_parent));
auto idx_opt = list.findIdx({parent_left.id, map_seq[parent_left.id][parent_left.seq]});
assert(idx_opt.has_value());
const auto parent_left_id = list.list.at(idx_opt.value()).id;
std::optional<List::ListID> parent_right_id;
if (idx_opt.value()+1 < list.list.size()) {
parent_right_id = list.list.at(idx_opt.value()+1).id;
}
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
parent_left_id,
parent_right_id
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
}
} else {
// i think this is curser movement
}
} else if (j_op["action"] == "del") {
const auto list_id = JObjFromStr(static_cast<const std::string&>(j_op["key"]));
bool r = list.del({list_id.id, map_seq[list_id.id][list_id.seq]});
assert(r);
g_total_deletes++;
} else if (j_op["action"] == "makeText") {
// doc.clear();
} else if (j_op["action"] == "makeMap") {
// no idea
} else {
std::cout << "op: " << j_op << "\n";
}
op_seq++;
}
}
std::cout << "\ndoc size (with tombstones): " << list.list.size() << "\n";
std::cout << "doc size: " << list.doc_size << "\n";
std::cout << "total inserts: " << g_total_inserts << "\n";
std::cout << "total deletes: " << g_total_deletes << "\n";
std::cout << "total ops: " << g_total_inserts + g_total_deletes << "\n";
// checked, looks correct
#if 0
std::cout << "doc text:\n";
// simple print
for (const auto& it : list.list) {
if (it.value) {
std::cout << it.value.value();
}
}
std::cout << "\n";
#endif
return 0;
}

203
bench/v1_jpaper.cpp Normal file
View File

@ -0,0 +1,203 @@
#define EXTRA_ASSERTS 0
#include <green_crdt/v1/list.hpp>
#include <nlohmann/json.hpp>
#include <unordered_map>
#include <string_view>
#include <fstream>
#include <iostream>
#include <cassert>
using ActorID = std::array<uint8_t, 32>;
//using Doc = GreenCRDT::V0::TextDocument<ActorID>;
using List = GreenCRDT::V1::List<char, ActorID>;
template<>
struct std::hash<ActorID> {
std::size_t operator()(ActorID 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)
;
}
};
// for dev, benching in debug is usefull, but only if the ammount of asserts is reasonable
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace detail {
uint8_t nib_from_hex(char c) {
extra_assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
if (c >= '0' && c <= '9') {
return static_cast<uint8_t>(c) - '0';
} else if (c >= 'a' && c <= 'f') {
return (static_cast<uint8_t>(c) - 'a') + 10u;
} else {
return 0u;
}
}
} // detail
static ActorID ActorIDFromStr(std::string_view str) {
extra_assert(str.size() == 32*2);
ActorID tmp;
for (size_t i = 0; i < tmp.size(); i++) {
tmp[i] = detail::nib_from_hex(str[i*2]) << 4 | detail::nib_from_hex(str[i*2+1]);
}
return tmp;
}
// seq@ID type format used in the json
struct JObj {
ActorID id;
uint64_t seq {0};
};
static JObj JObjFromStr(std::string_view str) {
extra_assert(str.size() > 32*2 + 1);
size_t at_pos = str.find_first_of('@');
auto seq_sv = str.substr(0, at_pos);
auto id_sv = str.substr(at_pos+1);
assert(seq_sv.size() != 0);
assert(id_sv.size() == 32*2);
uint64_t tmp_seq {0};
for (size_t i = 0; i < seq_sv.size(); i++) {
assert(seq_sv[i] >= '0' && seq_sv[i] <= '9');
tmp_seq *= 10;
tmp_seq += seq_sv[i] - '0';
}
return {ActorIDFromStr(id_sv), tmp_seq};
}
int main(void) {
List list;
std::ifstream file {"../res/paper.json"};
std::cout << "start reading...\n";
uint64_t g_total_inserts {0};
uint64_t g_total_deletes {0};
//uint64_t g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, uint64_t> g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, std::unordered_map<uint64_t, uint64_t>> map_seq; // maps json op_seq -> lits id seq
for (std::string line; std::getline(file, line); ) {
nlohmann::json j_entry = nlohmann::json::parse(line);
const ActorID actor = ActorIDFromStr(static_cast<const std::string&>(j_entry["actor"]));
uint64_t op_seq = j_entry["startOp"];
for (const auto& j_op : j_entry["ops"]) {
if (j_op["action"] == "set") {
const auto obj = JObjFromStr(static_cast<const std::string&>(j_op["obj"]));
if (obj.seq != 1) {
// skip all non text edits (create text doc, curser etc)
continue;
}
if (j_op["insert"]) {
const auto& j_parent = j_op["key"];
extra_assert(!j_parent.is_null());
if (j_parent == "_head") {
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
std::nullopt,
std::nullopt
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
} else { // we have a parrent
extra_assert(static_cast<const std::string&>(j_op["value"]).size() == 1);
// split parent into seq and actor
const auto parent_left = JObjFromStr(static_cast<const std::string&>(j_parent));
auto idx_opt = list.findIdx({parent_left.id, map_seq[parent_left.id][parent_left.seq]});
assert(idx_opt.has_value());
std::optional<List::ListID> parent_left_id;
{
const auto& tmp_parent_left_id = list.list.at(idx_opt.value()).id;
parent_left_id = {list._actors[tmp_parent_left_id.actor_idx], tmp_parent_left_id.seq};
}
std::optional<List::ListID> parent_right_id;
if (idx_opt.value()+1 < list.list.size()) {
const auto& tmp_parent_right_id = list.list.at(idx_opt.value()+1).id;
parent_right_id = {list._actors[tmp_parent_right_id.actor_idx], tmp_parent_right_id.seq};
}
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
parent_left_id,
parent_right_id
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
}
} else {
// i think this is curser movement
}
} else if (j_op["action"] == "del") {
const auto list_id = JObjFromStr(static_cast<const std::string&>(j_op["key"]));
bool r = list.del({list_id.id, map_seq[list_id.id][list_id.seq]});
assert(r);
g_total_deletes++;
} else if (j_op["action"] == "makeText") {
// doc.clear();
} else if (j_op["action"] == "makeMap") {
// no idea
} else {
std::cout << "op: " << j_op << "\n";
}
op_seq++;
}
}
std::cout << "\ndoc size (with tombstones): " << list.list.size() << "\n";
std::cout << "doc size: " << list.doc_size << "\n";
std::cout << "total inserts: " << g_total_inserts << "\n";
std::cout << "total deletes: " << g_total_deletes << "\n";
std::cout << "total ops: " << g_total_inserts + g_total_deletes << "\n";
// checked, looks correct
#if 0
std::cout << "doc text:\n";
// simple print
for (const auto& it : list.list) {
if (it.value) {
std::cout << it.value.value();
}
}
std::cout << "\n";
#endif
return 0;
}

211
bench/v2_jpaper.cpp Normal file
View File

@ -0,0 +1,211 @@
#define EXTRA_ASSERTS 0
#include <green_crdt/v2/list.hpp>
#include <nlohmann/json.hpp>
#include <unordered_map>
#include <string_view>
#include <fstream>
#include <iostream>
#include <cassert>
using ActorID = std::array<uint8_t, 32>;
using List = GreenCRDT::V2::List<char, ActorID>;
template<>
struct std::hash<ActorID> {
std::size_t operator()(ActorID 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)
;
}
};
// for dev, benching in debug is usefull, but only if the ammount of asserts is reasonable
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace detail {
uint8_t nib_from_hex(char c) {
extra_assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
if (c >= '0' && c <= '9') {
return static_cast<uint8_t>(c) - '0';
} else if (c >= 'a' && c <= 'f') {
return (static_cast<uint8_t>(c) - 'a') + 10u;
} else {
return 0u;
}
}
} // detail
static ActorID ActorIDFromStr(std::string_view str) {
extra_assert(str.size() == 32*2);
ActorID tmp;
for (size_t i = 0; i < tmp.size(); i++) {
tmp[i] = detail::nib_from_hex(str[i*2]) << 4 | detail::nib_from_hex(str[i*2+1]);
}
return tmp;
}
// seq@ID type format used in the json
struct JObj {
ActorID id;
uint64_t seq {0};
};
static JObj JObjFromStr(std::string_view str) {
extra_assert(str.size() > 32*2 + 1);
size_t at_pos = str.find_first_of('@');
auto seq_sv = str.substr(0, at_pos);
auto id_sv = str.substr(at_pos+1);
assert(seq_sv.size() != 0);
assert(id_sv.size() == 32*2);
uint64_t tmp_seq {0};
for (size_t i = 0; i < seq_sv.size(); i++) {
assert(seq_sv[i] >= '0' && seq_sv[i] <= '9');
tmp_seq *= 10;
tmp_seq += seq_sv[i] - '0';
}
return {ActorIDFromStr(id_sv), tmp_seq};
}
int main(void) {
List list;
std::ifstream file {"../res/paper.json"};
std::cout << "start reading...\n";
uint64_t g_total_inserts {0};
uint64_t g_total_deletes {0};
//uint64_t g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, uint64_t> g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, std::unordered_map<uint64_t, uint64_t>> map_seq; // maps json op_seq -> lits id seq
for (std::string line; std::getline(file, line); ) {
nlohmann::json j_entry = nlohmann::json::parse(line);
const ActorID actor = ActorIDFromStr(static_cast<const std::string&>(j_entry["actor"]));
const size_t actor_idx = list.findActor(actor).value_or(0u);
uint64_t op_seq = j_entry["startOp"];
for (const auto& j_op : j_entry["ops"]) {
if (j_op["action"] == "set") {
const auto obj = JObjFromStr(static_cast<const std::string&>(j_op["obj"]));
if (obj.seq != 1) {
// skip all non text edits (create text doc, curser etc)
continue;
}
if (j_op["insert"]) {
const auto& j_parent = j_op["key"];
extra_assert(!j_parent.is_null());
if (j_parent == "_head") {
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
std::nullopt,
std::nullopt
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
} else { // we have a parrent
extra_assert(static_cast<const std::string&>(j_op["value"]).size() == 1);
size_t hint_last_insert {0};
if (list.last_inserted_idx.count(actor_idx)) {
hint_last_insert = list.last_inserted_idx[actor_idx];
}
// split parent into seq and actor
const auto parent_left = JObjFromStr(static_cast<const std::string&>(j_parent));
auto idx_opt = list.findIdx({parent_left.id, map_seq[parent_left.id][parent_left.seq]}, hint_last_insert);
assert(idx_opt.has_value());
std::optional<List::ListID> parent_left_id;
{
const auto& tmp_parent_left_id = list.list.at(idx_opt.value()).id;
parent_left_id = {list._actors[tmp_parent_left_id.actor_idx], tmp_parent_left_id.seq};
}
std::optional<List::ListID> parent_right_id;
if (idx_opt.value()+1 < list.list.size()) {
const auto& tmp_parent_right_id = list.list.at(idx_opt.value()+1).id;
parent_right_id = {list._actors[tmp_parent_right_id.actor_idx], tmp_parent_right_id.seq};
}
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
parent_left_id,
parent_right_id
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
}
} else {
// i think this is curser movement
}
} else if (j_op["action"] == "del") {
const auto list_id = JObjFromStr(static_cast<const std::string&>(j_op["key"]));
bool r = list.del({list_id.id, map_seq[list_id.id][list_id.seq]});
assert(r);
g_total_deletes++;
} else if (j_op["action"] == "makeText") {
// doc.clear();
} else if (j_op["action"] == "makeMap") {
// no idea
} else {
std::cout << "op: " << j_op << "\n";
}
op_seq++;
}
}
std::cout << "\ndoc size (with tombstones): " << list.list.size() << "\n";
std::cout << "doc size: " << list.doc_size << "\n";
std::cout << "total inserts: " << g_total_inserts << "\n";
std::cout << "total deletes: " << g_total_deletes << "\n";
std::cout << "total ops: " << g_total_inserts + g_total_deletes << "\n";
//std::cout << "find_hint: " << list._stat_find_with_hint << "\n";
//std::cout << "find_hint_hit: " << list._stat_find_with_hint_hit << "\n";
// checked, looks correct
#if 0
std::cout << "doc text:\n";
// simple print
for (const auto& it : list.list) {
if (it.value) {
std::cout << it.value.value();
}
}
std::cout << "\n";
#endif
return 0;
}

211
bench/v3_jpaper.cpp Normal file
View File

@ -0,0 +1,211 @@
#define EXTRA_ASSERTS 0
#include <green_crdt/v3/list.hpp>
#include <nlohmann/json.hpp>
#include <unordered_map>
#include <string_view>
#include <fstream>
#include <iostream>
#include <cassert>
using ActorID = std::array<uint8_t, 32>;
using List = GreenCRDT::V3::List<char, ActorID>;
template<>
struct std::hash<ActorID> {
std::size_t operator()(ActorID 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)
;
}
};
// for dev, benching in debug is usefull, but only if the ammount of asserts is reasonable
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace detail {
uint8_t nib_from_hex(char c) {
extra_assert((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
if (c >= '0' && c <= '9') {
return static_cast<uint8_t>(c) - '0';
} else if (c >= 'a' && c <= 'f') {
return (static_cast<uint8_t>(c) - 'a') + 10u;
} else {
return 0u;
}
}
} // detail
static ActorID ActorIDFromStr(std::string_view str) {
extra_assert(str.size() == 32*2);
ActorID tmp;
for (size_t i = 0; i < tmp.size(); i++) {
tmp[i] = detail::nib_from_hex(str[i*2]) << 4 | detail::nib_from_hex(str[i*2+1]);
}
return tmp;
}
// seq@ID type format used in the json
struct JObj {
ActorID id;
uint64_t seq {0};
};
static JObj JObjFromStr(std::string_view str) {
extra_assert(str.size() > 32*2 + 1);
size_t at_pos = str.find_first_of('@');
auto seq_sv = str.substr(0, at_pos);
auto id_sv = str.substr(at_pos+1);
assert(seq_sv.size() != 0);
assert(id_sv.size() == 32*2);
uint64_t tmp_seq {0};
for (size_t i = 0; i < seq_sv.size(); i++) {
assert(seq_sv[i] >= '0' && seq_sv[i] <= '9');
tmp_seq *= 10;
tmp_seq += seq_sv[i] - '0';
}
return {ActorIDFromStr(id_sv), tmp_seq};
}
int main(void) {
List list;
std::ifstream file {"../res/paper.json"};
std::cout << "start reading...\n";
uint64_t g_total_inserts {0};
uint64_t g_total_deletes {0};
//uint64_t g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, uint64_t> g_seq_inserts {0}; // the opsec are not sequentially growing for inserts, so we sidestep
std::unordered_map<ActorID, std::unordered_map<uint64_t, uint64_t>> map_seq; // maps json op_seq -> lits id seq
for (std::string line; std::getline(file, line); ) {
nlohmann::json j_entry = nlohmann::json::parse(line);
const ActorID actor = ActorIDFromStr(static_cast<const std::string&>(j_entry["actor"]));
const size_t actor_idx = list.findActor(actor).value_or(0u);
uint64_t op_seq = j_entry["startOp"];
for (const auto& j_op : j_entry["ops"]) {
if (j_op["action"] == "set") {
const auto obj = JObjFromStr(static_cast<const std::string&>(j_op["obj"]));
if (obj.seq != 1) {
// skip all non text edits (create text doc, curser etc)
continue;
}
if (j_op["insert"]) {
const auto& j_parent = j_op["key"];
extra_assert(!j_parent.is_null());
if (j_parent == "_head") {
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
std::nullopt,
std::nullopt
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
} else { // we have a parrent
extra_assert(static_cast<const std::string&>(j_op["value"]).size() == 1);
size_t hint_last_insert {0};
if (list._last_inserted_idx.count(actor_idx)) {
hint_last_insert = list._last_inserted_idx[actor_idx];
}
// split parent into seq and actor
const auto parent_left = JObjFromStr(static_cast<const std::string&>(j_parent));
auto idx_opt = list.findIdx({parent_left.id, map_seq[parent_left.id][parent_left.seq]}, hint_last_insert);
assert(idx_opt.has_value());
std::optional<List::ListID> parent_left_id;
{
const auto& tmp_parent_left_id = list._list_ids.at(idx_opt.value());
parent_left_id = {list._actors[tmp_parent_left_id.actor_idx], tmp_parent_left_id.seq};
}
std::optional<List::ListID> parent_right_id;
if (idx_opt.value()+1 < list._list_ids.size()) {
const auto& tmp_parent_right_id = list._list_ids.at(idx_opt.value()+1);
parent_right_id = {list._actors[tmp_parent_right_id.actor_idx], tmp_parent_right_id.seq};
}
uint64_t tmp_seq {g_seq_inserts[actor]++};
bool r = list.add(
{actor, tmp_seq},
static_cast<const std::string&>(j_op["value"]).front(),
parent_left_id,
parent_right_id
);
assert(r);
map_seq[actor][op_seq] = tmp_seq;
g_total_inserts++;
}
} else {
// i think this is curser movement
}
} else if (j_op["action"] == "del") {
const auto list_id = JObjFromStr(static_cast<const std::string&>(j_op["key"]));
bool r = list.del({list_id.id, map_seq[list_id.id][list_id.seq]});
assert(r);
g_total_deletes++;
} else if (j_op["action"] == "makeText") {
// doc.clear();
} else if (j_op["action"] == "makeMap") {
// no idea
} else {
std::cout << "op: " << j_op << "\n";
}
op_seq++;
}
}
std::cout << "\ndoc size (with tombstones): " << list._list_ids.size() << "\n";
std::cout << "doc size: " << list.getDocSize() << "\n";
std::cout << "total inserts: " << g_total_inserts << "\n";
std::cout << "total deletes: " << g_total_deletes << "\n";
std::cout << "total ops: " << g_total_inserts + g_total_deletes << "\n";
//std::cout << "find_hint: " << list._stat_find_with_hint << "\n";
//std::cout << "find_hint_hit: " << list._stat_find_with_hint_hit << "\n";
// checked, looks correct
#if 0
std::cout << "doc text:\n";
// simple print
for (const auto& it : list.list) {
if (it.value) {
std::cout << it.value.value();
}
}
std::cout << "\n";
#endif
return 0;
}

7
external/toxcore/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(./toxcore.cmake)

1
external/toxcore/c-toxcore vendored Submodule

297
external/toxcore/cmake/Findsodium.cmake vendored Normal file
View File

@ -0,0 +1,297 @@
# Written in 2016 by Henrik Steffen Gaßmann <henrik@gassmann.onl>
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
#
# http://creativecommons.org/publicdomain/zero/1.0/
#
########################################################################
# Tries to find the local libsodium installation.
#
# On Windows the sodium_DIR environment variable is used as a default
# hint which can be overridden by setting the corresponding cmake variable.
#
# Once done the following variables will be defined:
#
# sodium_FOUND
# sodium_INCLUDE_DIR
# sodium_LIBRARY_DEBUG
# sodium_LIBRARY_RELEASE
#
#
# Furthermore an imported "sodium" target is created.
#
if (CMAKE_C_COMPILER_ID STREQUAL "GNU"
OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(_GCC_COMPATIBLE 1)
endif()
# static library option
if (NOT DEFINED sodium_USE_STATIC_LIBS)
option(sodium_USE_STATIC_LIBS "enable to statically link against sodium" OFF)
endif()
if(NOT (sodium_USE_STATIC_LIBS EQUAL sodium_USE_STATIC_LIBS_LAST))
unset(sodium_LIBRARY CACHE)
unset(sodium_LIBRARY_DEBUG CACHE)
unset(sodium_LIBRARY_RELEASE CACHE)
unset(sodium_DLL_DEBUG CACHE)
unset(sodium_DLL_RELEASE CACHE)
set(sodium_USE_STATIC_LIBS_LAST ${sodium_USE_STATIC_LIBS} CACHE INTERNAL "internal change tracking variable")
endif()
########################################################################
# UNIX
if (UNIX)
# import pkg-config
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
pkg_check_modules(sodium_PKG QUIET libsodium)
endif()
if(sodium_USE_STATIC_LIBS)
foreach(_libname ${sodium_PKG_STATIC_LIBRARIES})
if (NOT _libname MATCHES "^lib.*\\.a$") # ignore strings already ending with .a
list(INSERT sodium_PKG_STATIC_LIBRARIES 0 "lib${_libname}.a")
endif()
endforeach()
list(REMOVE_DUPLICATES sodium_PKG_STATIC_LIBRARIES)
# if pkgconfig for libsodium doesn't provide
# static lib info, then override PKG_STATIC here..
if (NOT sodium_PKG_STATIC_FOUND)
set(sodium_PKG_STATIC_LIBRARIES libsodium.a)
endif()
set(XPREFIX sodium_PKG_STATIC)
else()
if (NOT sodium_PKG_FOUND)
set(sodium_PKG_LIBRARIES sodium)
endif()
set(XPREFIX sodium_PKG)
endif()
find_path(sodium_INCLUDE_DIR sodium.h
HINTS ${${XPREFIX}_INCLUDE_DIRS}
)
find_library(sodium_LIBRARY_DEBUG NAMES ${${XPREFIX}_LIBRARIES}
HINTS ${${XPREFIX}_LIBRARY_DIRS}
)
find_library(sodium_LIBRARY_RELEASE NAMES ${${XPREFIX}_LIBRARIES}
HINTS ${${XPREFIX}_LIBRARY_DIRS}
)
########################################################################
# Windows
elseif (WIN32)
set(sodium_DIR "$ENV{sodium_DIR}" CACHE FILEPATH "sodium install directory")
mark_as_advanced(sodium_DIR)
find_path(sodium_INCLUDE_DIR sodium.h
HINTS ${sodium_DIR}
PATH_SUFFIXES include
)
if (MSVC)
# detect target architecture
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp" [=[
#if defined _M_IX86
#error ARCH_VALUE x86_32
#elif defined _M_X64
#error ARCH_VALUE x86_64
#endif
#error ARCH_VALUE unknown
]=])
try_compile(_UNUSED_VAR "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/arch.cpp"
OUTPUT_VARIABLE _COMPILATION_LOG
)
string(REGEX REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*" "\\1" _TARGET_ARCH "${_COMPILATION_LOG}")
# construct library path
if (_TARGET_ARCH STREQUAL "x86_32")
string(APPEND _PLATFORM_PATH "Win32")
elseif(_TARGET_ARCH STREQUAL "x86_64")
string(APPEND _PLATFORM_PATH "x64")
else()
message(FATAL_ERROR "the ${_TARGET_ARCH} architecture is not supported by Findsodium.cmake.")
endif()
string(APPEND _PLATFORM_PATH "/$$CONFIG$$")
if (MSVC_VERSION LESS 1900)
math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 60")
else()
math(EXPR _VS_VERSION "${MSVC_VERSION} / 10 - 50")
endif()
string(APPEND _PLATFORM_PATH "/v${_VS_VERSION}")
if (sodium_USE_STATIC_LIBS)
string(APPEND _PLATFORM_PATH "/static")
else()
string(APPEND _PLATFORM_PATH "/dynamic")
endif()
string(REPLACE "$$CONFIG$$" "Debug" _DEBUG_PATH_SUFFIX "${_PLATFORM_PATH}")
string(REPLACE "$$CONFIG$$" "Release" _RELEASE_PATH_SUFFIX "${_PLATFORM_PATH}")
find_library(sodium_LIBRARY_DEBUG libsodium.lib
HINTS ${sodium_DIR}
PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX}
)
find_library(sodium_LIBRARY_RELEASE libsodium.lib
HINTS ${sodium_DIR}
PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX}
)
if (NOT sodium_USE_STATIC_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES_BCK ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll")
find_library(sodium_DLL_DEBUG libsodium
HINTS ${sodium_DIR}
PATH_SUFFIXES ${_DEBUG_PATH_SUFFIX}
)
find_library(sodium_DLL_RELEASE libsodium
HINTS ${sodium_DIR}
PATH_SUFFIXES ${_RELEASE_PATH_SUFFIX}
)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_BCK})
endif()
elseif(_GCC_COMPATIBLE)
if (sodium_USE_STATIC_LIBS)
find_library(sodium_LIBRARY_DEBUG libsodium.a
HINTS ${sodium_DIR}
PATH_SUFFIXES lib
)
find_library(sodium_LIBRARY_RELEASE libsodium.a
HINTS ${sodium_DIR}
PATH_SUFFIXES lib
)
else()
find_library(sodium_LIBRARY_DEBUG libsodium.dll.a
HINTS ${sodium_DIR}
PATH_SUFFIXES lib
)
find_library(sodium_LIBRARY_RELEASE libsodium.dll.a
HINTS ${sodium_DIR}
PATH_SUFFIXES lib
)
file(GLOB _DLL
LIST_DIRECTORIES false
RELATIVE "${sodium_DIR}/bin"
"${sodium_DIR}/bin/libsodium*.dll"
)
find_library(sodium_DLL_DEBUG ${_DLL} libsodium
HINTS ${sodium_DIR}
PATH_SUFFIXES bin
)
find_library(sodium_DLL_RELEASE ${_DLL} libsodium
HINTS ${sodium_DIR}
PATH_SUFFIXES bin
)
endif()
else()
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake")
endif()
########################################################################
# unsupported
else()
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake")
endif()
########################################################################
# common stuff
# extract sodium version
if (sodium_INCLUDE_DIR)
set(_VERSION_HEADER "${_INCLUDE_DIR}/sodium/version.h")
if (EXISTS _VERSION_HEADER)
file(READ "${_VERSION_HEADER}" _VERSION_HEADER_CONTENT)
string(REGEX REPLACE ".*#[ \t]*define[ \t]*SODIUM_VERSION_STRING[ \t]*\"([^\n]*)\".*" "\\1"
sodium_VERSION "${_VERSION_HEADER_CONTENT}")
set(sodium_VERSION "${sodium_VERSION}" PARENT_SCOPE)
endif()
endif()
# communicate results
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
sodium # The name must be either uppercase or match the filename case.
REQUIRED_VARS
sodium_LIBRARY_RELEASE
sodium_LIBRARY_DEBUG
sodium_INCLUDE_DIR
VERSION_VAR
sodium_VERSION
)
if(Sodium_FOUND)
set(sodium_LIBRARIES
optimized ${sodium_LIBRARY_RELEASE} debug ${sodium_LIBRARY_DEBUG})
endif()
# mark file paths as advanced
mark_as_advanced(sodium_INCLUDE_DIR)
mark_as_advanced(sodium_LIBRARY_DEBUG)
mark_as_advanced(sodium_LIBRARY_RELEASE)
if (WIN32)
mark_as_advanced(sodium_DLL_DEBUG)
mark_as_advanced(sodium_DLL_RELEASE)
endif()
# create imported target
if(sodium_USE_STATIC_LIBS)
set(_LIB_TYPE STATIC)
else()
set(_LIB_TYPE SHARED)
endif()
if(NOT TARGET sodium)
add_library(sodium ${_LIB_TYPE} IMPORTED)
endif()
set_target_properties(sodium PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${sodium_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
)
if (sodium_USE_STATIC_LIBS)
set_target_properties(sodium PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "SODIUM_STATIC"
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}"
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}"
)
else()
if (UNIX)
set_target_properties(sodium PROPERTIES
IMPORTED_LOCATION "${sodium_LIBRARY_RELEASE}"
IMPORTED_LOCATION_DEBUG "${sodium_LIBRARY_DEBUG}"
)
elseif (WIN32)
set_target_properties(sodium PROPERTIES
IMPORTED_IMPLIB "${sodium_LIBRARY_RELEASE}"
IMPORTED_IMPLIB_DEBUG "${sodium_LIBRARY_DEBUG}"
)
if (NOT (sodium_DLL_DEBUG MATCHES ".*-NOTFOUND"))
set_target_properties(sodium PROPERTIES
IMPORTED_LOCATION_DEBUG "${sodium_DLL_DEBUG}"
)
endif()
if (NOT (sodium_DLL_RELEASE MATCHES ".*-NOTFOUND"))
set_target_properties(sodium PROPERTIES
IMPORTED_LOCATION_RELWITHDEBINFO "${sodium_DLL_RELEASE}"
IMPORTED_LOCATION_MINSIZEREL "${sodium_DLL_RELEASE}"
IMPORTED_LOCATION_RELEASE "${sodium_DLL_RELEASE}"
)
endif()
endif()
endif()

158
external/toxcore/toxcore.cmake vendored Normal file
View File

@ -0,0 +1,158 @@
set(TOX_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c-toxcore/")
# TODO: shared
add_library(toxcore STATIC
${TOX_DIR}third_party/cmp/cmp.c
${TOX_DIR}third_party/cmp/cmp.h
${TOX_DIR}toxcore/announce.c
${TOX_DIR}toxcore/announce.h
${TOX_DIR}toxcore/bin_pack.c
${TOX_DIR}toxcore/bin_pack.h
${TOX_DIR}toxcore/bin_unpack.c
${TOX_DIR}toxcore/bin_unpack.h
${TOX_DIR}toxcore/ccompat.c
${TOX_DIR}toxcore/ccompat.h
${TOX_DIR}toxcore/crypto_core.c
${TOX_DIR}toxcore/crypto_core.h
${TOX_DIR}toxcore/DHT.c
${TOX_DIR}toxcore/DHT.h
${TOX_DIR}toxcore/events/conference_connected.c
${TOX_DIR}toxcore/events/conference_invite.c
${TOX_DIR}toxcore/events/conference_message.c
${TOX_DIR}toxcore/events/conference_peer_list_changed.c
${TOX_DIR}toxcore/events/conference_peer_name.c
${TOX_DIR}toxcore/events/conference_title.c
${TOX_DIR}toxcore/events/events_alloc.c
${TOX_DIR}toxcore/events/events_alloc.h
${TOX_DIR}toxcore/events/file_chunk_request.c
${TOX_DIR}toxcore/events/file_recv.c
${TOX_DIR}toxcore/events/file_recv_chunk.c
${TOX_DIR}toxcore/events/file_recv_control.c
${TOX_DIR}toxcore/events/friend_connection_status.c
${TOX_DIR}toxcore/events/friend_lossless_packet.c
${TOX_DIR}toxcore/events/friend_lossy_packet.c
${TOX_DIR}toxcore/events/friend_message.c
${TOX_DIR}toxcore/events/friend_name.c
${TOX_DIR}toxcore/events/friend_read_receipt.c
${TOX_DIR}toxcore/events/friend_request.c
${TOX_DIR}toxcore/events/friend_status.c
${TOX_DIR}toxcore/events/friend_status_message.c
${TOX_DIR}toxcore/events/friend_typing.c
${TOX_DIR}toxcore/events/self_connection_status.c
${TOX_DIR}toxcore/forwarding.c
${TOX_DIR}toxcore/forwarding.h
${TOX_DIR}toxcore/friend_connection.c
${TOX_DIR}toxcore/friend_connection.h
${TOX_DIR}toxcore/friend_requests.c
${TOX_DIR}toxcore/friend_requests.h
${TOX_DIR}toxcore/group.c
${TOX_DIR}toxcore/group.h
${TOX_DIR}toxcore/group_announce.c
${TOX_DIR}toxcore/group_announce.h
${TOX_DIR}toxcore/group_moderation.c
${TOX_DIR}toxcore/group_moderation.h
${TOX_DIR}toxcore/group_chats.c
${TOX_DIR}toxcore/group_chats.h
${TOX_DIR}toxcore/group_common.h
${TOX_DIR}toxcore/group_connection.c
${TOX_DIR}toxcore/group_connection.h
${TOX_DIR}toxcore/group_onion_announce.c
${TOX_DIR}toxcore/group_onion_announce.h
${TOX_DIR}toxcore/group_pack.c
${TOX_DIR}toxcore/group_pack.h
${TOX_DIR}toxcore/LAN_discovery.c
${TOX_DIR}toxcore/LAN_discovery.h
${TOX_DIR}toxcore/list.c
${TOX_DIR}toxcore/list.h
${TOX_DIR}toxcore/logger.c
${TOX_DIR}toxcore/logger.h
${TOX_DIR}toxcore/Messenger.c
${TOX_DIR}toxcore/Messenger.h
${TOX_DIR}toxcore/mono_time.c
${TOX_DIR}toxcore/mono_time.h
${TOX_DIR}toxcore/net_crypto.c
${TOX_DIR}toxcore/net_crypto.h
${TOX_DIR}toxcore/network.c
${TOX_DIR}toxcore/network.h
${TOX_DIR}toxcore/onion_announce.c
${TOX_DIR}toxcore/onion_announce.h
${TOX_DIR}toxcore/onion.c
${TOX_DIR}toxcore/onion_client.c
${TOX_DIR}toxcore/onion_client.h
${TOX_DIR}toxcore/onion.h
${TOX_DIR}toxcore/ping_array.c
${TOX_DIR}toxcore/ping_array.h
${TOX_DIR}toxcore/ping.c
${TOX_DIR}toxcore/ping.h
${TOX_DIR}toxcore/shared_key_cache.c
${TOX_DIR}toxcore/shared_key_cache.h
${TOX_DIR}toxcore/state.c
${TOX_DIR}toxcore/state.h
${TOX_DIR}toxcore/TCP_client.c
${TOX_DIR}toxcore/TCP_client.h
${TOX_DIR}toxcore/TCP_common.c
${TOX_DIR}toxcore/TCP_common.h
${TOX_DIR}toxcore/TCP_connection.c
${TOX_DIR}toxcore/TCP_connection.h
${TOX_DIR}toxcore/TCP_server.c
${TOX_DIR}toxcore/TCP_server.h
${TOX_DIR}toxcore/timed_auth.c
${TOX_DIR}toxcore/timed_auth.h
${TOX_DIR}toxcore/tox_api.c
${TOX_DIR}toxcore/tox.c
${TOX_DIR}toxcore/tox_dispatch.c
${TOX_DIR}toxcore/tox_dispatch.h
${TOX_DIR}toxcore/tox_events.c
${TOX_DIR}toxcore/tox_events.h
${TOX_DIR}toxcore/tox.h
${TOX_DIR}toxcore/tox_private.c
${TOX_DIR}toxcore/tox_private.h
${TOX_DIR}toxcore/tox_unpack.c
${TOX_DIR}toxcore/tox_unpack.h
${TOX_DIR}toxcore/util.c
${TOX_DIR}toxcore/util.h
)
# HACK: "install" api headers into self
# this is dirty, should be binary dir
# TODO: add the others
configure_file(
${TOX_DIR}toxcore/tox.h
${TOX_DIR}tox/tox.h
@ONLY
)
target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore")
target_include_directories(toxcore PUBLIC "${TOX_DIR}")
target_compile_definitions(toxcore PUBLIC USE_IPV6=1)
target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_DEBUG)
find_package(unofficial-sodium CONFIG QUIET)
find_package(sodium QUIET)
if(unofficial-sodium_FOUND) # vcpkg
target_link_libraries(toxcore unofficial-sodium::sodium unofficial-sodium::sodium_config_public)
elseif(sodium_FOUND)
target_link_libraries(toxcore sodium)
else()
message(SEND_ERROR "missing libsodium")
endif()
if(WIN32)
target_link_libraries(toxcore ws2_32 iphlpapi)
endif()
find_package(Threads REQUIRED)
target_link_libraries(toxcore Threads::Threads)
add_executable(DHT_Bootstrap EXCLUDE_FROM_ALL
${TOX_DIR}other/DHT_bootstrap.c
${TOX_DIR}other/bootstrap_node_packets.h
${TOX_DIR}other/bootstrap_node_packets.c
${TOX_DIR}testing/misc_tools.h
${TOX_DIR}testing/misc_tools.c
)
target_link_libraries(DHT_Bootstrap toxcore)

View File

@ -10,17 +10,17 @@ target_include_directories(crdt_version0 INTERFACE "${PROJECT_SOURCE_DIR}")
######################################## ########################################
add_executable(test1 add_executable(v0_test1
./test1.cpp ./test1.cpp
) )
target_link_libraries(test1 PUBLIC crdt_version0) target_link_libraries(v0_test1 PUBLIC crdt_version0)
######################################## ########################################
add_executable(test2 add_executable(v0_test2
./test2.cpp ./test2.cpp
) )
target_link_libraries(test2 PUBLIC crdt_version0) target_link_libraries(v0_test2 PUBLIC crdt_version0)

View File

@ -8,7 +8,15 @@
#include <cassert> #include <cassert>
namespace GreenCRDT { #if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace GreenCRDT::V0 {
template<typename ValueType, typename AgentType> template<typename ValueType, typename AgentType>
struct List { struct List {
@ -71,7 +79,9 @@ struct List {
std::map<AgentType, uint64_t> last_seen_seq; std::map<AgentType, uint64_t> last_seen_seq;
std::optional<size_t> findIdx(const ListID& list_id) const { std::optional<size_t> findIdx(const ListID& list_id) const {
verify(); //#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
//verify(); // too expensive
//#endif
for (size_t i = 0; i < list.size(); i++) { for (size_t i = 0; i < list.size(); i++) {
if (list[i].id == list_id) { if (list[i].id == list_id) {
return i; return i;
@ -243,5 +253,5 @@ struct List {
} }
}; };
} // GreenCRDT } // GreenCRDT::V0

View File

@ -6,7 +6,7 @@
//#include <iostream> // debug //#include <iostream> // debug
namespace GreenCRDT { namespace GreenCRDT::V0 {
template<typename AgentType> template<typename AgentType>
struct TextDocument { struct TextDocument {
@ -214,7 +214,6 @@ struct TextDocument {
if (!differ && list_start == state.list.size() && text_start == text.size()) { if (!differ && list_start == state.list.size() && text_start == text.size()) {
return {}; return {};
} }
//std::cout << "list.size: " << state.list.size() << "(" << getText().size() << ")" << " text.size: " << text.size() << "\n"; //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"; //std::cout << "list_start: " << list_start << " text_start: " << text_start << "\n";
@ -224,7 +223,9 @@ struct TextDocument {
//for (; list_end > 0 && text_end > 0 && list_end >= list_start && text_end >= text_start;) { //for (; list_end > 0 && text_end > 0 && list_end >= list_start && text_end >= text_start;) {
//while (list_end >= list_start && text_end >= text_start) { //while (list_end >= list_start && text_end >= text_start) {
size_t list_end_counted = 0; size_t list_end_counted = 0;
while (list_start_counted - list_end_counted > state.doc_size && text_end >= text_start) { 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 // jump over tombstones
if (!state.list[list_end-1].value.has_value()) { if (!state.list[list_end-1].value.has_value()) {
list_end--; list_end--;
@ -232,6 +233,7 @@ struct TextDocument {
} }
if (state.list[list_end-1].value.value() != text[text_end-1]) { if (state.list[list_end-1].value.value() != text[text_end-1]) {
differ = true;
break; break;
} }
@ -240,20 +242,29 @@ struct TextDocument {
list_end_counted++; 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 << "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; std::vector<Op> ops;
// 1. clear range (del all list_start - list_end) // 1. clear range (del all list_start - list_end)
if (list_start <= list_end && list_start < state.list.size()) { if (list_start <= list_end && list_start < state.list.size()) {
//list_end += list_start == list_end;
ops = delRange( ops = delRange(
state.list[list_start].id, state.list[list_start].id,
(list_start == list_end ? list_end+1 : list_end) < state.list.size() ? std::make_optional(state.list[list_end].id) : std::nullopt list_end < state.list.size() ? std::make_optional(state.list[list_end].id) : std::nullopt
); );
//std::cout << "deleted: " << ops.size() << "\n"; //std::cout << "deleted: " << ops.size() << "\n";
} }
//std::cout << "text between: " << getText() << "\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) // 2. add range (add all text_start - text_end)
if (state.doc_size < text.size()) { if (state.doc_size < text.size()) {
@ -266,10 +277,9 @@ struct TextDocument {
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end()); ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());
} }
//assert(false && "implement me");
return ops; return ops;
} }
}; };
} // GreenCRDT } // GreenCRDT::V0

View File

@ -1,5 +1,5 @@
#include <crdt/list.hpp> #include <green_crdt/v0/list.hpp>
#include <crdt/text_document.hpp> #include <green_crdt/v0/text_document.hpp>
#include <numeric> #include <numeric>
#include <random> #include <random>
@ -8,7 +8,7 @@
// single letter agent, for testing only // single letter agent, for testing only
using Agent = char; using Agent = char;
using DocType = GreenCRDT::TextDocument<Agent>; using DocType = GreenCRDT::V0::TextDocument<Agent>;
using ListType = DocType::ListType; using ListType = DocType::ListType;
void testSingle1(void) { void testSingle1(void) {

View File

@ -1,4 +1,4 @@
#include <crdt/text_document.hpp> #include <green_crdt/v0/text_document.hpp>
#include <numeric> #include <numeric>
#include <optional> #include <optional>
@ -9,8 +9,8 @@
// single letter agent, for testing only // single letter agent, for testing only
using Agent = char; using Agent = char;
using Doc = GreenCRDT::TextDocument<Agent>; using Doc = GreenCRDT::V0::TextDocument<Agent>;
using Op = GreenCRDT::TextDocument<Agent>::Op; using Op = Doc::Op;
using ListType = Doc::ListType; using ListType = Doc::ListType;
// maybe switch it up? // maybe switch it up?
@ -418,8 +418,9 @@ void testBugDoubleDel(void) {
{ {
std::string_view new_text{"a"}; std::string_view new_text{"a"};
doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
{ {
@ -445,20 +446,23 @@ void testBugSameDel(void) {
{ {
std::string_view new_text{"a"}; std::string_view new_text{"a"};
doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
{ {
std::string_view new_text{"aa"}; std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
{ {
std::string_view new_text{"a"}; std::string_view new_text{"a"};
const auto ops = doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
} }
@ -468,32 +472,122 @@ void testBugSameDel2(void) {
{ {
std::string_view new_text{"a"}; std::string_view new_text{"a"};
doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
{ {
std::string_view new_text{"aa"}; std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
{ {
std::string_view new_text{"aaa"}; std::string_view new_text{"aaa"};
const auto ops = doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
{ {
std::string_view new_text{"aa"}; std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
} }
{ {
std::string_view new_text{"a"}; std::string_view new_text{"a"};
const auto ops = doc.merge(new_text); const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text); assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
}
void testMulti1(void) {
Doc docA;
docA.local_agent = 'A';
Doc docB;
docB.local_agent = 'B';
// state A
{
std::string_view new_text{"iiiiiii"};
const auto ops = docA.merge(new_text);
assert(docA.getText() == new_text);
assert(docB.apply(ops));
assert(docB.getText() == new_text);
assert(docB.state.doc_size == docA.state.doc_size);
assert(docB.state.list.size() == docA.state.list.size());
}
// now B inserts b
{
std::string_view new_text{"iiibiiii"};
const auto ops = docB.merge(new_text);
assert(docB.getText() == new_text);
assert(ops.size() == 1); // 1 new inserted char, nothing to delete
assert(docA.apply(ops));
assert(docA.getText() == new_text);
}
}
void testPaste1(void) {
Doc docA;
docA.local_agent = 'A';
{
std::string_view new_text{"iiiiiii"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"iiiiiii\n"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 1);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"iiiiiii\niiiiiii"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
}
void testPaste2(void) {
Doc docA;
docA.local_agent = 'A';
{
std::string_view new_text{"aiiiiib"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"aiiiiib\n"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 1);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"aiiiiib\naiiiiib"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
} }
} }
@ -580,6 +674,27 @@ int main(void) {
testBugSameDel2(); testBugSameDel2();
} }
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testMulti1:\n";
testMulti1();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testPaste1:\n";
testPaste1();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testPaste2:\n";
testPaste2();
}
return 0; return 0;
} }

26
version1/CMakeLists.txt Normal file
View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
project(crdt_version1 CXX C)
add_library(crdt_version1 INTERFACE)
target_compile_features(crdt_version1 INTERFACE cxx_std_17)
target_include_directories(crdt_version1 INTERFACE "${PROJECT_SOURCE_DIR}")
########################################
add_executable(v1_test1
./test1.cpp
)
target_link_libraries(v1_test1 PUBLIC crdt_version1)
########################################
#add_executable(v1_test2
#./test2.cpp
#)
#target_link_libraries(v1_test2 PUBLIC crdt_version1)

View File

@ -0,0 +1,328 @@
#pragma once
#include <cstdint>
#include <optional>
#include <vector>
#include <map>
#include <string>
#include <cassert>
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace GreenCRDT::V1 {
template<typename ValueType, typename ActorType>
struct List {
// for public interface
struct ListID {
ActorType id;
uint64_t seq{0}; // strictly increasing for that actor
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;
}
};
struct ListIDInternal {
size_t actor_idx{0};
uint64_t seq{0}; // strictly increasing for that actor
bool operator==(const ListIDInternal& rhs) const {
return seq == rhs.seq && actor_idx == rhs.actor_idx;
}
};
// internally the index into this array is used to refer to an actor
std::vector<ActorType> _actors;
// TODO: replace with SoA
struct Entry {
ListIDInternal id;
// Yjs
std::optional<ListIDInternal> parent_left;
std::optional<ListIDInternal> 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<size_t, uint64_t> last_seen_seq;
std::optional<size_t> findActor(const ActorType& actor) const {
for (size_t i = 0; i < _actors.size(); i++) {
if (_actors[i] == actor) {
return i;
}
}
return std::nullopt;
}
std::optional<size_t> findIdx(const ListIDInternal& list_id) const {
extra_assert(verify());
for (size_t i = 0; i < list.size(); i++) {
if (list[i].id == list_id) {
return i;
}
}
return std::nullopt;
}
std::optional<size_t> findIdx(const ListID& list_id) const {
extra_assert(verify());
const auto actor_idx_opt = findActor(list_id.id);
if (!actor_idx_opt.has_value()) {
return std::nullopt;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), list_id.seq};
for (size_t i = 0; i < list.size(); i++) {
if (list[i].id == tmp_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) {
extra_assert(verify());
size_t actor_idx {0};
{ // new actor?
// add, even if op fails
const auto actor_opt = findActor(list_id.id);
if (!actor_opt.has_value()) {
actor_idx = _actors.size();
_actors.push_back(list_id.id);
} else {
actor_idx = actor_opt.value();
}
}
// check actor op order
if (!last_seen_seq.count(actor_idx)) {
// we dont know this actor yet, first seq needs to be 0
if (list_id.seq != 0) {
return false;
}
} else {
// making sure we dont skip operations by that actor
if (list_id.seq != last_seen_seq.at(actor_idx) + 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) {
// actor id tie breaker
if (_actors[actor_idx] < _actors[at_i.id.actor_idx]) {
break;
} else {
scanning = false;
}
} else { // i_right_idx > right_idx
scanning = false;
}
} else { // il > l
// do nothing
}
}
}
{ // actual insert
Entry new_entry;
new_entry.id.actor_idx = actor_idx;
new_entry.id.seq = list_id.seq;
if (parent_left.has_value()) {
new_entry.parent_left = ListIDInternal{findActor(parent_left.value().id).value(), parent_left.value().seq};
}
if (parent_right.has_value()) {
new_entry.parent_right = ListIDInternal{findActor(parent_right.value().id).value(), parent_right.value().seq};
}
new_entry.value = value;
list.emplace(list.begin() + insert_idx, new_entry);
}
doc_size++;
last_seen_seq[actor_idx] = list_id.seq;
extra_assert(verify());
return true;
}
// returns false if not found
bool del(const ListID& id) {
extra_assert(verify());
auto actor_idx_opt = findActor(id.id);
if (!actor_idx_opt.has_value()) {
// we dont have anything with that actor
return false;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), id.seq};
for (auto& it : list) {
if (it.id == tmp_id) {
if (it.value.has_value()) {
it.value.reset();
doc_size--;
extra_assert(verify());
return true;
} else {
extra_assert(verify());
return false; // TODO: allow double deletes?,,,, need ids
}
}
}
extra_assert(verify());
return false;
}
std::vector<ValueType> getArray(void) const {
std::vector<ValueType> array;
for (const auto& e : list) {
if (e.value.has_value()) {
array.push_back(e.value.value());
}
}
return array;
}
// TODO: only in debug?
bool 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);
return doc_size == actual_size;
}
};
} // GreenCRDT::V1

214
version1/test1.cpp Normal file
View File

@ -0,0 +1,214 @@
#define EXTRA_ASSERTS 1
#include <green_crdt/v1/list.hpp>
#include <numeric>
#include <random>
#include <iostream>
#include <cassert>
#include <string_view>
#include <vector>
// single letter actor, for testing only
using Actor = char;
using ListType = GreenCRDT::V1::List<char, Actor>;
namespace std {
bool operator==(const std::vector<char>& lhs, const std::string_view& rhs) {
if (lhs.size() != rhs.size()) {
return false;
}
for (size_t i = 0; i < rhs.size(); i++) {
if (lhs[i] != rhs[i]) {
return false;
}
}
return true;
}
} // namespace std
void testSingle1(void) {
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'A', 1}, 'b', ListType::ListID{'A', 0u}, std::nullopt));
assert(list.getArray() == "ab");
}
void testConcurrent1(void) {
// agent_a < agent_b
// concurrent insert of first element
{ // variant 1, a then b
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
{ // variant 2, b then a
ListType list;
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
}
struct AddOp {
ListType::ListID id;
char value;
std::optional<ListType::ListID> parent_left;
std::optional<ListType::ListID> parent_right;
};
void randomAddPermutations(const std::vector<AddOp>& ops, const std::string& expected) {
// TODO: more then 1k?
for (size_t i = 0; i < 1000; i++) {
std::minstd_rand rng(1337 + i);
std::vector<size_t> ops_todo(ops.size());
std::iota(ops_todo.begin(), ops_todo.end(), 0u);
size_t attempts {0};
ListType list;
do {
size_t idx = rng() % ops_todo.size();
if (list.add(ops[ops_todo[idx]].id, ops[ops_todo[idx]].value, ops[ops_todo[idx]].parent_left, ops[ops_todo[idx]].parent_right)) {
// only remove if it was possible -> returned true;
ops_todo.erase(ops_todo.begin()+idx);
}
attempts++;
assert(attempts < 10'000); // in case we run into an endless loop
} while (!ops_todo.empty());
assert(list.getArray() == expected);
}
}
void testInterleave1(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', ListType::ListID{'A', 0u}, std::nullopt},
{{'A', 2u}, 'a', ListType::ListID{'A', 1u}, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', ListType::ListID{'B', 0u}, std::nullopt},
{{'B', 2u}, 'b', ListType::ListID{'B', 1u}, std::nullopt},
};
randomAddPermutations(ops, "aaabbb");
}
void testInterleave2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', std::nullopt, ListType::ListID{'A', 0u}},
{{'A', 2u}, 'a', std::nullopt, ListType::ListID{'A', 1u}},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', std::nullopt, ListType::ListID{'B', 0u}},
{{'B', 2u}, 'b', std::nullopt, ListType::ListID{'B', 1u}},
};
randomAddPermutations(ops, "aaabbb");
}
void testConcurrent2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'C', 0u}, 'c', std::nullopt, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'D', 0u}, 'd', ListType::ListID{'A', 0u}, ListType::ListID{'C', 0u}},
};
randomAddPermutations(ops, "adbc");
}
void testMain1(void) {
ListType list;
static_assert('0' < '1');
const std::vector<AddOp> a0_ops {
{{'0', 0u}, 'a', std::nullopt, std::nullopt},
{{'0', 1u}, 'b', ListType::ListID{'0', 0u}, std::nullopt},
{{'0', 2u}, 'c', ListType::ListID{'0', 1u}, std::nullopt},
{{'0', 3u}, 'd', ListType::ListID{'0', 1u}, ListType::ListID{'0', 2u}},
};
const std::vector<AddOp> a1_ops {
// knows of a0 up to {a0, 1}
{{'1', 0u}, 'z', ListType::ListID{'0', 0u}, ListType::ListID{'0', 1u}},
{{'1', 1u}, 'y', ListType::ListID{'0', 1u}, std::nullopt},
};
{ // the ez, in order stuff
// a0 insert first char, 'a', since its the first, we dont have any parents
assert(list.add(a0_ops[0].id, a0_ops[0].value, a0_ops[0].parent_left, a0_ops[0].parent_right));
assert(list.getArray() == "a");
// a0 insert secound char, 'b' after 'a', no parents to right
assert(list.add(a0_ops[1].id, a0_ops[1].value, a0_ops[1].parent_left, a0_ops[1].parent_right));
assert(list.getArray() == "ab");
// a0 insert 'c' after 'b', no parents to right
assert(list.add(a0_ops[2].id, a0_ops[2].value, a0_ops[2].parent_left, a0_ops[2].parent_right));
assert(list.getArray() == "abc");
// a0 insert 'd' after 'b', 'c' parent right
assert(list.add(a0_ops[3].id, a0_ops[3].value, a0_ops[3].parent_left, a0_ops[3].parent_right));
assert(list.getArray() == "abdc");
// a1 insert 'z' after 'a', 'b' parent right
assert(list.add(a1_ops[0].id, a1_ops[0].value, a1_ops[0].parent_left, a1_ops[0].parent_right));
assert(list.getArray() == "azbdc");
}
std::cout << "done with ez\n";
{ // a1 was not uptodate only had 0,1 of a0
// a1 insert 'y' after 'b', no parent right
assert(list.add(a1_ops[1].id, a1_ops[1].value, a1_ops[1].parent_left, a1_ops[1].parent_right));
assert(list.getArray() == "azbdcy");
}
std::cout << "\ndoc size (with tombstones): " << list.list.size() << "\n";
std::cout << "\ndoc size: " << list.doc_size << "\n";
std::cout << "doc text:\n";
const auto tmp_array = list.getArray();
std::cout << std::string_view(tmp_array.data(), tmp_array.size()) << "\n";
}
int main(void) {
std::cout << "testSingle1:\n";
testSingle1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent1:\n";
testConcurrent1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave1:\n";
testInterleave1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave2:\n";
testInterleave2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent2:\n";
testConcurrent2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testMain1:\n";
testMain1();
std::cout << std::string(40, '-') << "\n";
return 0;
}

26
version2/CMakeLists.txt Normal file
View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
project(crdt_version2 CXX C)
add_library(crdt_version2 INTERFACE)
target_compile_features(crdt_version2 INTERFACE cxx_std_17)
target_include_directories(crdt_version2 INTERFACE "${PROJECT_SOURCE_DIR}")
########################################
add_executable(v2_test1
./test1.cpp
)
target_link_libraries(v2_test1 PUBLIC crdt_version2)
########################################
#add_executable(v2_test2
#./test2.cpp
#)
#target_link_libraries(v2_test2 PUBLIC crdt_version2)

View File

@ -0,0 +1,385 @@
#pragma once
#include <cstdint>
#include <optional>
#include <unordered_map>
#include <vector>
#include <string>
#include <cassert>
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace GreenCRDT::V2 {
template<typename ValueType, typename ActorType>
struct List {
// for public interface
struct ListID {
ActorType id;
uint64_t seq{0}; // strictly increasing for that actor
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;
}
};
struct ListIDInternal {
size_t actor_idx{0};
uint64_t seq{0}; // strictly increasing for that actor
bool operator==(const ListIDInternal& rhs) const {
return seq == rhs.seq && actor_idx == rhs.actor_idx;
}
};
// internally the index into this array is used to refer to an actor
std::vector<ActorType> _actors;
// TODO: replace with SoA
struct Entry {
ListIDInternal id;
// Yjs
std::optional<ListIDInternal> parent_left;
std::optional<ListIDInternal> 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};
// TODO: actor index instead of map
std::unordered_map<size_t, uint64_t> last_seen_seq;
// caching only, contains the last index an actor inserted at
std::unordered_map<size_t, size_t> last_inserted_idx;
//size_t _stat_find_with_hint{0};
//size_t _stat_find_with_hint_hit{0};
std::optional<size_t> findActor(const ActorType& actor) const {
for (size_t i = 0; i < _actors.size(); i++) {
if (_actors[i] == actor) {
return i;
}
}
return std::nullopt;
}
std::optional<size_t> findIdx(const ListIDInternal& list_id) const {
extra_assert(verify());
for (size_t i = 0; i < list.size(); i++) {
if (list[i].id == list_id) {
return i;
}
}
return std::nullopt;
}
// search close to hint first
std::optional<size_t> findIdx(const ListIDInternal& list_id, size_t hint) const {
extra_assert(verify());
//_stat_find_with_hint++;
// TODO: find some good magic values here
// total: 364150
// 2-9 hits: 360164 (3m54)
// 1-9 hits: 360161 (3m53)
// 1-2 hits: 359800 (3m55s)
// 0-2 hits: 359763 (3m54s)
// changed from loop to single if:
// 1-2 hits: 359800 (3m50s)
// 1-4 hits: 359928 (3m51s) (after cond reorder: 3m49s)
static constexpr size_t c_hint_pre = 1;
static constexpr size_t c_hint_post = 4;
{ // go back 2, so we dont miss // TODO: is this really needed
//for (size_t i = 0; hint > 0 && i < c_hint_pre; hint--, i++) {}
if (hint >= c_hint_pre) {
hint -= c_hint_pre;
}
}
const size_t max_at_hint = hint + c_hint_post; // how many positions we check at hint, before falling back to full lookup
for (size_t i = hint; i <= max_at_hint && i < list.size(); i++) {
if (list[i].id == list_id) {
//_stat_find_with_hint_hit++;
return i;
}
}
// fall back to normal search
// TODO: in some cases we scan the list twice now!!
return findIdx(list_id);
}
std::optional<size_t> findIdx(const ListID& list_id) const {
extra_assert(verify());
const auto actor_idx_opt = findActor(list_id.id);
if (!actor_idx_opt.has_value()) {
return std::nullopt;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), list_id.seq};
return findIdx(tmp_id);
}
std::optional<size_t> findIdx(const ListID& list_id, size_t hint) const {
extra_assert(verify());
const auto actor_idx_opt = findActor(list_id.id);
if (!actor_idx_opt.has_value()) {
return std::nullopt;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), list_id.seq};
return findIdx(tmp_id, hint);
}
// 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
// TODO: idx_hint
bool add(const ListID& list_id, const ValueType& value, const std::optional<ListID>& parent_left, const std::optional<ListID>& parent_right) {
extra_assert(verify());
size_t actor_idx {0};
{ // new actor?
// add, even if op fails
const auto actor_opt = findActor(list_id.id);
if (!actor_opt.has_value()) {
actor_idx = _actors.size();
last_inserted_idx[_actors.size()] = 0; // hack
_actors.push_back(list_id.id);
} else {
actor_idx = actor_opt.value();
}
}
// check actor op order
if (!last_seen_seq.count(actor_idx)) {
// we dont know this actor yet, first seq needs to be 0
if (list_id.seq != 0) {
return false;
}
} else {
// making sure we dont skip operations by that actor
if (list_id.seq != last_seen_seq.at(actor_idx) + 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_opt = std::nullopt;
if (parent_left.has_value()) {
left_idx_opt = findIdx(parent_left.value(), last_inserted_idx[actor_idx]);
if (!left_idx_opt.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_opt.value() + 1;
} // else insert_idx = 0
const size_t left_idx_hint = insert_idx;
// find right
size_t right_idx = list.size();
if (parent_right.has_value()) {
auto tmp_right = findIdx(parent_right.value(), left_idx_hint);
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(), left_idx_hint);
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_opt) {
break;
} else if (i_left_idx == left_idx_opt) {
// 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(), insert_idx);
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) {
// actor id tie breaker
if (_actors[actor_idx] < _actors[at_i.id.actor_idx]) {
break;
} else {
scanning = false;
}
} else { // i_right_idx > right_idx
scanning = false;
}
} else { // il > l
// do nothing
}
}
}
{ // actual insert
Entry new_entry;
new_entry.id.actor_idx = actor_idx;
new_entry.id.seq = list_id.seq;
if (parent_left.has_value()) {
new_entry.parent_left = ListIDInternal{findActor(parent_left.value().id).value(), parent_left.value().seq};
}
if (parent_right.has_value()) {
new_entry.parent_right = ListIDInternal{findActor(parent_right.value().id).value(), parent_right.value().seq};
}
new_entry.value = value;
list.emplace(list.begin() + insert_idx, new_entry);
last_inserted_idx[actor_idx] = insert_idx;
}
doc_size++;
last_seen_seq[actor_idx] = list_id.seq;
extra_assert(verify());
return true;
}
// returns false if not found
bool del(const ListID& id) {
extra_assert(verify());
auto actor_idx_opt = findActor(id.id);
if (!actor_idx_opt.has_value()) {
// we dont have anything with that actor
return false;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), id.seq};
for (auto& it : list) {
if (it.id == tmp_id) {
if (it.value.has_value()) {
it.value.reset();
doc_size--;
extra_assert(verify());
return true;
} else {
extra_assert(verify());
return false; // TODO: allow double deletes?,,,, need ids
}
}
}
extra_assert(verify());
return false;
}
std::vector<ValueType> getArray(void) const {
std::vector<ValueType> array;
for (const auto& e : list) {
if (e.value.has_value()) {
array.push_back(e.value.value());
}
}
return array;
}
// TODO: only in debug?
bool 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);
return doc_size == actual_size;
}
};
} // GreenCRDT::V1

214
version2/test1.cpp Normal file
View File

@ -0,0 +1,214 @@
#define EXTRA_ASSERTS 1
#include <green_crdt/v2/list.hpp>
#include <numeric>
#include <random>
#include <iostream>
#include <cassert>
#include <string_view>
#include <vector>
// single letter actor, for testing only
using Actor = char;
using ListType = GreenCRDT::V2::List<char, Actor>;
namespace std {
bool operator==(const std::vector<char>& lhs, const std::string_view& rhs) {
if (lhs.size() != rhs.size()) {
return false;
}
for (size_t i = 0; i < rhs.size(); i++) {
if (lhs[i] != rhs[i]) {
return false;
}
}
return true;
}
} // namespace std
void testSingle1(void) {
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'A', 1}, 'b', ListType::ListID{'A', 0u}, std::nullopt));
assert(list.getArray() == "ab");
}
void testConcurrent1(void) {
// agent_a < agent_b
// concurrent insert of first element
{ // variant 1, a then b
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
{ // variant 2, b then a
ListType list;
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
}
struct AddOp {
ListType::ListID id;
char value;
std::optional<ListType::ListID> parent_left;
std::optional<ListType::ListID> parent_right;
};
void randomAddPermutations(const std::vector<AddOp>& ops, const std::string& expected) {
// TODO: more then 1k?
for (size_t i = 0; i < 1000; i++) {
std::minstd_rand rng(1337 + i);
std::vector<size_t> ops_todo(ops.size());
std::iota(ops_todo.begin(), ops_todo.end(), 0u);
size_t attempts {0};
ListType list;
do {
size_t idx = rng() % ops_todo.size();
if (list.add(ops[ops_todo[idx]].id, ops[ops_todo[idx]].value, ops[ops_todo[idx]].parent_left, ops[ops_todo[idx]].parent_right)) {
// only remove if it was possible -> returned true;
ops_todo.erase(ops_todo.begin()+idx);
}
attempts++;
assert(attempts < 10'000); // in case we run into an endless loop
} while (!ops_todo.empty());
assert(list.getArray() == expected);
}
}
void testInterleave1(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', ListType::ListID{'A', 0u}, std::nullopt},
{{'A', 2u}, 'a', ListType::ListID{'A', 1u}, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', ListType::ListID{'B', 0u}, std::nullopt},
{{'B', 2u}, 'b', ListType::ListID{'B', 1u}, std::nullopt},
};
randomAddPermutations(ops, "aaabbb");
}
void testInterleave2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', std::nullopt, ListType::ListID{'A', 0u}},
{{'A', 2u}, 'a', std::nullopt, ListType::ListID{'A', 1u}},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', std::nullopt, ListType::ListID{'B', 0u}},
{{'B', 2u}, 'b', std::nullopt, ListType::ListID{'B', 1u}},
};
randomAddPermutations(ops, "aaabbb");
}
void testConcurrent2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'C', 0u}, 'c', std::nullopt, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'D', 0u}, 'd', ListType::ListID{'A', 0u}, ListType::ListID{'C', 0u}},
};
randomAddPermutations(ops, "adbc");
}
void testMain1(void) {
ListType list;
static_assert('0' < '1');
const std::vector<AddOp> a0_ops {
{{'0', 0u}, 'a', std::nullopt, std::nullopt},
{{'0', 1u}, 'b', ListType::ListID{'0', 0u}, std::nullopt},
{{'0', 2u}, 'c', ListType::ListID{'0', 1u}, std::nullopt},
{{'0', 3u}, 'd', ListType::ListID{'0', 1u}, ListType::ListID{'0', 2u}},
};
const std::vector<AddOp> a1_ops {
// knows of a0 up to {a0, 1}
{{'1', 0u}, 'z', ListType::ListID{'0', 0u}, ListType::ListID{'0', 1u}},
{{'1', 1u}, 'y', ListType::ListID{'0', 1u}, std::nullopt},
};
{ // the ez, in order stuff
// a0 insert first char, 'a', since its the first, we dont have any parents
assert(list.add(a0_ops[0].id, a0_ops[0].value, a0_ops[0].parent_left, a0_ops[0].parent_right));
assert(list.getArray() == "a");
// a0 insert secound char, 'b' after 'a', no parents to right
assert(list.add(a0_ops[1].id, a0_ops[1].value, a0_ops[1].parent_left, a0_ops[1].parent_right));
assert(list.getArray() == "ab");
// a0 insert 'c' after 'b', no parents to right
assert(list.add(a0_ops[2].id, a0_ops[2].value, a0_ops[2].parent_left, a0_ops[2].parent_right));
assert(list.getArray() == "abc");
// a0 insert 'd' after 'b', 'c' parent right
assert(list.add(a0_ops[3].id, a0_ops[3].value, a0_ops[3].parent_left, a0_ops[3].parent_right));
assert(list.getArray() == "abdc");
// a1 insert 'z' after 'a', 'b' parent right
assert(list.add(a1_ops[0].id, a1_ops[0].value, a1_ops[0].parent_left, a1_ops[0].parent_right));
assert(list.getArray() == "azbdc");
}
std::cout << "done with ez\n";
{ // a1 was not uptodate only had 0,1 of a0
// a1 insert 'y' after 'b', no parent right
assert(list.add(a1_ops[1].id, a1_ops[1].value, a1_ops[1].parent_left, a1_ops[1].parent_right));
assert(list.getArray() == "azbdcy");
}
std::cout << "\ndoc size (with tombstones): " << list.list.size() << "\n";
std::cout << "\ndoc size: " << list.doc_size << "\n";
std::cout << "doc text:\n";
const auto tmp_array = list.getArray();
std::cout << std::string_view(tmp_array.data(), tmp_array.size()) << "\n";
}
int main(void) {
std::cout << "testSingle1:\n";
testSingle1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent1:\n";
testConcurrent1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave1:\n";
testInterleave1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave2:\n";
testInterleave2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent2:\n";
testConcurrent2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testMain1:\n";
testMain1();
std::cout << std::string(40, '-') << "\n";
return 0;
}

26
version3/CMakeLists.txt Normal file
View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
project(crdt_version3 CXX C)
add_library(crdt_version3 INTERFACE)
target_compile_features(crdt_version3 INTERFACE cxx_std_17)
target_include_directories(crdt_version3 INTERFACE "${PROJECT_SOURCE_DIR}")
########################################
add_executable(v3_test1
./test1.cpp
)
target_link_libraries(v3_test1 PUBLIC crdt_version3)
########################################
add_executable(v3_test2
./test2.cpp
)
target_link_libraries(v3_test2 PUBLIC crdt_version3)

View File

@ -0,0 +1,426 @@
#pragma once
#include <cstdint>
#include <optional>
#include <unordered_map>
#include <vector>
#include <string>
#include <cassert>
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace GreenCRDT::V3 {
template<typename ValueType, typename ActorType>
struct List {
// for public interface
struct ListID {
ActorType id;
uint64_t seq{0}; // strictly increasing for that actor
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;
}
};
struct ListIDInternal {
size_t actor_idx{0};
uint64_t seq{0}; // strictly increasing for that actor
bool operator==(const ListIDInternal& rhs) const {
return seq == rhs.seq && actor_idx == rhs.actor_idx;
}
};
// internally the index into this array is used to refer to an actor
std::vector<ActorType> _actors;
struct Entry_Data {
// Yjs
std::optional<ListIDInternal> parent_left;
std::optional<ListIDInternal> parent_right;
// might be deleted (yes, *sigh*, crtds need tombstones)
std::optional<ValueType> value;
};
std::vector<ListIDInternal> _list_ids;
std::vector<Entry_Data> _list_data;
// number of not deleted entries
size_t _doc_size {0};
// TODO: actor index instead of map
std::unordered_map<size_t, uint64_t> _last_seen_seq;
// caching only, contains the last index an actor inserted at
std::unordered_map<size_t, size_t> _last_inserted_idx;
//size_t _stat_find_with_hint{0};
//size_t _stat_find_with_hint_hit{0};
[[nodiscard]] std::optional<size_t> findActor(const ActorType& actor) const {
for (size_t i = 0; i < _actors.size(); i++) {
if (_actors[i] == actor) {
return i;
}
}
return std::nullopt;
}
[[nodiscard]] std::optional<size_t> findIdx(const ListIDInternal& list_id) const {
extra_assert(verify());
for (size_t i = 0; i < _list_ids.size(); i++) {
if (_list_ids[i] == list_id) {
return i;
}
}
return std::nullopt;
}
// search close to hint first
[[nodiscard]] std::optional<size_t> findIdx(const ListIDInternal& list_id, size_t hint) const {
extra_assert(verify());
//_stat_find_with_hint++;
// TODO: find some good magic values here
// total: 364150
// 2-9 hits: 360164 (3m54)
// 1-9 hits: 360161 (3m53)
// 1-2 hits: 359800 (3m55s)
// 0-2 hits: 359763 (3m54s)
// changed from loop to single if:
// 1-2 hits: 359800 (3m50s)
// 1-4 hits: 359928 (3m51s) (after cond reorder: 3m49s)
static constexpr size_t c_hint_pre = 1;
static constexpr size_t c_hint_post = 4;
{ // go back 2, so we dont miss // TODO: is this really needed
//for (size_t i = 0; hint > 0 && i < c_hint_pre; hint--, i++) {}
if (hint >= c_hint_pre) {
hint -= c_hint_pre;
}
}
const size_t max_at_hint = hint + c_hint_post; // how many positions we check at hint, before falling back to full lookup
for (size_t i = hint; i <= max_at_hint && i < _list_ids.size(); i++) {
if (_list_ids[i] == list_id) {
//_stat_find_with_hint_hit++;
return i;
}
}
// fall back to normal search
// TODO: in some cases we scan the list twice now!!
return findIdx(list_id);
}
[[nodiscard]] std::optional<size_t> findIdx(const ListID& list_id) const {
extra_assert(verify());
const auto actor_idx_opt = findActor(list_id.id);
if (!actor_idx_opt.has_value()) {
return std::nullopt;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), list_id.seq};
return findIdx(tmp_id);
}
[[nodiscard]] std::optional<size_t> findIdx(const ListID& list_id, size_t hint) const {
extra_assert(verify());
const auto actor_idx_opt = findActor(list_id.id);
if (!actor_idx_opt.has_value()) {
return std::nullopt;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), list_id.seq};
return findIdx(tmp_id, hint);
}
// 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
// TODO: idx_hint
bool add(const ListID& list_id, const ValueType& value, const std::optional<ListID>& parent_left, const std::optional<ListID>& parent_right) {
extra_assert(verify());
size_t actor_idx {0};
{ // new actor?
// add, even if op fails
const auto actor_opt = findActor(list_id.id);
if (!actor_opt.has_value()) {
actor_idx = _actors.size();
_last_inserted_idx[_actors.size()] = 0; // hack
_actors.push_back(list_id.id);
} else {
actor_idx = actor_opt.value();
}
}
// check actor op order
if (!_last_seen_seq.count(actor_idx)) {
// we dont know this actor yet, first seq needs to be 0
if (list_id.seq != 0) {
return false;
}
} else {
// making sure we dont skip operations by that actor
if (list_id.seq != _last_seen_seq.at(actor_idx) + 1) {
return false;
}
}
size_t insert_idx = 0;
if (_list_ids.empty()) {
if (parent_left.has_value() || parent_right.has_value()) {
// empty, missing parents
return false;
}
} else {
// find left
std::optional<size_t> left_idx_opt = std::nullopt;
if (parent_left.has_value()) {
left_idx_opt = findIdx(parent_left.value(), _last_inserted_idx[actor_idx]);
if (!left_idx_opt.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_opt.value() + 1;
} // else insert_idx = 0
const size_t left_idx_hint = insert_idx;
// find right
size_t right_idx = _list_ids.size();
if (parent_right.has_value()) {
auto tmp_right = findIdx(parent_right.value(), left_idx_hint);
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_ids.size()) {
break;
}
const Entry_Data& at_i = _list_data[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(), left_idx_hint);
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_opt) {
break;
} else if (i_left_idx == left_idx_opt) {
// get i parent_right
size_t i_right_idx = _list_ids.size();
if (at_i.parent_right.has_value()) {
auto tmp_right = findIdx(at_i.parent_right.value(), insert_idx);
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) {
// actor id tie breaker
if (_actors[actor_idx] < _actors[_list_ids[i].actor_idx]) {
break;
} else {
scanning = false;
}
} else { // i_right_idx > right_idx
scanning = false;
}
} else { // il > l
// do nothing
}
}
}
{ // actual insert
_list_ids.emplace(_list_ids.begin() + insert_idx, ListIDInternal{actor_idx, list_id.seq});
Entry_Data new_entry;
if (parent_left.has_value()) {
new_entry.parent_left = ListIDInternal{findActor(parent_left.value().id).value(), parent_left.value().seq};
}
if (parent_right.has_value()) {
new_entry.parent_right = ListIDInternal{findActor(parent_right.value().id).value(), parent_right.value().seq};
}
new_entry.value = value;
_list_data.emplace(_list_data.begin() + insert_idx, new_entry);
_last_inserted_idx[actor_idx] = insert_idx;
}
_doc_size++;
_last_seen_seq[actor_idx] = list_id.seq;
extra_assert(verify());
return true;
}
// returns false if not found
bool del(const ListID& id) {
extra_assert(verify());
auto actor_idx_opt = findActor(id.id);
if (!actor_idx_opt.has_value()) {
// we dont have anything with that actor
return false;
}
#if 0
const ListIDInternal tmp_id {actor_idx_opt.value(), id.seq};
for (auto& it : list) {
if (it.id == tmp_id) {
if (it.value.has_value()) {
it.value.reset();
doc_size--;
extra_assert(verify());
return true;
} else {
extra_assert(verify());
return false; // TODO: allow double deletes?,,,, need ids
}
}
}
#endif
// TODO: actually test deletes lol
const auto idx_opt = findIdx(id);
if (idx_opt.has_value()) {
auto& it = _list_data[idx_opt.value()];
if (it.value.has_value()) {
it.value.reset();
_doc_size--;
extra_assert(verify());
return true;
} else {
extra_assert(verify());
return false; // TODO: allow double deletes?,,,, need ids
}
}
// not found
extra_assert(verify());
return false;
}
[[nodiscard]] bool empty(void) const {
return _list_ids.empty();
}
[[nodiscard]] size_t size(void) const {
return _list_ids.size();
}
[[nodiscard]] ListIDInternal getIDInternal(size_t idx) const {
return _list_ids.at(idx);
}
[[nodiscard]] const ListID getID(size_t idx) const {
return {_actors.at(_list_ids.at(idx).actor_idx), _list_ids.at(idx).seq};
}
[[nodiscard]] const std::optional<ValueType>& getValue(size_t idx) const {
return _list_data.at(idx).value;
}
// returns the size of alive entries
[[nodiscard]] size_t getDocSize(void) const {
return _doc_size;
}
[[nodiscard]] std::vector<ValueType> getArray(void) const {
std::vector<ValueType> array;
for (const auto& e : _list_data) {
if (e.value.has_value()) {
array.push_back(e.value.value());
}
}
return array;
}
// TODO: only in debug?
bool verify(void) const {
if (_list_ids.size() != _list_data.size()) {
return false;
}
size_t actual_size = 0;
for (const auto& it : _list_data) {
if (it.value.has_value()) {
actual_size++;
}
}
//assert(doc_size == actual_size);
return _doc_size == actual_size;
}
};
} // GreenCRDT::V1

View File

@ -0,0 +1,305 @@
#pragma once
#include "./list.hpp"
#include <variant>
//#include <iostream> // debug
namespace GreenCRDT::V3 {
template<typename ActorType>
struct TextDocument {
// TODO: determine if char is the best
using ListType = List<char, ActorType>;
struct OpAdd {
typename ListType::ListID id;
std::optional<typename ListType::ListID> parent_left;
std::optional<typename ListType::ListID> parent_right;
char value;
};
struct OpDel {
typename ListType::ListID id;
};
using Op = std::variant<OpAdd, OpDel>;
//// TODO: implement
//struct Cursor {
//AgentType who;
//typename ListType::ListID pos;
//};
ActorType local_actor;
ListType state;
[[nodiscard]] std::string getText(void) const {
std::string text;
for (const auto& it : state._list_data) {
if (it.value.has_value()) {
text += it.value.value();
}
}
return text;
}
bool apply(const Op& op) {
if(std::holds_alternative<OpAdd>(op)) {
const auto& add_op = std::get<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<OpDel>(op)) {
const auto& del_op = std::get<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 ActorType& actor, 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 {actor, seq++};
ops.emplace_back(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: move actor setting to list
if (!state.findActor(local_actor).has_value()) {
state._actors.push_back(local_actor);
}
// TODO: look up typesystem and fix (move? decltype?)
std::vector<Op> ops = text2adds(
// TODO: abstract actors
local_actor, state._last_seen_seq.count(state.findActor(local_actor).value()) ? state._last_seen_seq[state.findActor(local_actor).value()]+1u : 0u,
parent_left,
parent_right,
text
);
// TODO: make this better
// and apply
for (const auto& op : ops) {
if(std::holds_alternative<OpAdd>(op)) {
const auto& add_op = std::get<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<OpDel>(op)) {
const auto& del_op = std::get<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.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.getValue(i).has_value()) {
// allready deleted
continue;
}
ops.emplace_back(OpDel{
//state.list.at(i).id
state.getID(i)
});
// TODO: do delets get a seq?????
state.del(state.getID(i));
}
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.empty() || state.getDocSize() == 0) {
// no op
return {};
} else {
// delete all
return delRange(std::nullopt, std::nullopt);
}
}
// text not empty
if (state.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.size() && text_start < text.size();) {
// jump over tombstones
if (!state.getValue(list_start).has_value()) {
list_start++;
continue;
}
if (state.getValue(list_start).value() != text[text_start]) {
differ = true;
break;
}
list_start++;
text_start++;
list_start_counted++;
}
// doc and text dont differ
if (!differ && list_start == state.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.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.getDocSize() - list_start_counted > list_end_counted && text_end >= text_start) {
// jump over tombstones
if (!state.getValue(list_end-1).has_value()) {
list_end--;
continue;
}
if (state.getValue(list_end-1).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.size()) {
//list_end += list_start == list_end;
ops = delRange(
state.getID(list_start),
list_end < state.size() ? std::make_optional(state.getID(list_end)) : 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.getDocSize() < text.size()) {
auto tmp_add_ops = addText(
list_start == 0 ? std::nullopt : std::make_optional(state.getID(list_start-1)),
list_start == state.size() ? std::nullopt :std::make_optional(state.getID(list_start)),
text.substr(text_start, text.size() - state.getDocSize())
);
//std::cout << "added: " << tmp_add_ops.size() << "\n";
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());
}
return ops;
}
};
} // GreenCRDT::V3

214
version3/test1.cpp Normal file
View File

@ -0,0 +1,214 @@
#define EXTRA_ASSERTS 1
#include <green_crdt/v3/list.hpp>
#include <numeric>
#include <random>
#include <iostream>
#include <cassert>
#include <string_view>
#include <vector>
// single letter actor, for testing only
using Actor = char;
using ListType = GreenCRDT::V3::List<char, Actor>;
namespace std {
bool operator==(const std::vector<char>& lhs, const std::string_view& rhs) {
if (lhs.size() != rhs.size()) {
return false;
}
for (size_t i = 0; i < rhs.size(); i++) {
if (lhs[i] != rhs[i]) {
return false;
}
}
return true;
}
} // namespace std
void testSingle1(void) {
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'A', 1}, 'b', ListType::ListID{'A', 0u}, std::nullopt));
assert(list.getArray() == "ab");
}
void testConcurrent1(void) {
// agent_a < agent_b
// concurrent insert of first element
{ // variant 1, a then b
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
{ // variant 2, b then a
ListType list;
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
}
struct AddOp {
ListType::ListID id;
char value;
std::optional<ListType::ListID> parent_left;
std::optional<ListType::ListID> parent_right;
};
void randomAddPermutations(const std::vector<AddOp>& ops, const std::string& expected) {
// TODO: more then 1k?
for (size_t i = 0; i < 1000; i++) {
std::minstd_rand rng(1337 + i);
std::vector<size_t> ops_todo(ops.size());
std::iota(ops_todo.begin(), ops_todo.end(), 0u);
size_t attempts {0};
ListType list;
do {
size_t idx = rng() % ops_todo.size();
if (list.add(ops[ops_todo[idx]].id, ops[ops_todo[idx]].value, ops[ops_todo[idx]].parent_left, ops[ops_todo[idx]].parent_right)) {
// only remove if it was possible -> returned true;
ops_todo.erase(ops_todo.begin()+idx);
}
attempts++;
assert(attempts < 10'000); // in case we run into an endless loop
} while (!ops_todo.empty());
assert(list.getArray() == expected);
}
}
void testInterleave1(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', ListType::ListID{'A', 0u}, std::nullopt},
{{'A', 2u}, 'a', ListType::ListID{'A', 1u}, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', ListType::ListID{'B', 0u}, std::nullopt},
{{'B', 2u}, 'b', ListType::ListID{'B', 1u}, std::nullopt},
};
randomAddPermutations(ops, "aaabbb");
}
void testInterleave2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', std::nullopt, ListType::ListID{'A', 0u}},
{{'A', 2u}, 'a', std::nullopt, ListType::ListID{'A', 1u}},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', std::nullopt, ListType::ListID{'B', 0u}},
{{'B', 2u}, 'b', std::nullopt, ListType::ListID{'B', 1u}},
};
randomAddPermutations(ops, "aaabbb");
}
void testConcurrent2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'C', 0u}, 'c', std::nullopt, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'D', 0u}, 'd', ListType::ListID{'A', 0u}, ListType::ListID{'C', 0u}},
};
randomAddPermutations(ops, "adbc");
}
void testMain1(void) {
ListType list;
static_assert('0' < '1');
const std::vector<AddOp> a0_ops {
{{'0', 0u}, 'a', std::nullopt, std::nullopt},
{{'0', 1u}, 'b', ListType::ListID{'0', 0u}, std::nullopt},
{{'0', 2u}, 'c', ListType::ListID{'0', 1u}, std::nullopt},
{{'0', 3u}, 'd', ListType::ListID{'0', 1u}, ListType::ListID{'0', 2u}},
};
const std::vector<AddOp> a1_ops {
// knows of a0 up to {a0, 1}
{{'1', 0u}, 'z', ListType::ListID{'0', 0u}, ListType::ListID{'0', 1u}},
{{'1', 1u}, 'y', ListType::ListID{'0', 1u}, std::nullopt},
};
{ // the ez, in order stuff
// a0 insert first char, 'a', since its the first, we dont have any parents
assert(list.add(a0_ops[0].id, a0_ops[0].value, a0_ops[0].parent_left, a0_ops[0].parent_right));
assert(list.getArray() == "a");
// a0 insert secound char, 'b' after 'a', no parents to right
assert(list.add(a0_ops[1].id, a0_ops[1].value, a0_ops[1].parent_left, a0_ops[1].parent_right));
assert(list.getArray() == "ab");
// a0 insert 'c' after 'b', no parents to right
assert(list.add(a0_ops[2].id, a0_ops[2].value, a0_ops[2].parent_left, a0_ops[2].parent_right));
assert(list.getArray() == "abc");
// a0 insert 'd' after 'b', 'c' parent right
assert(list.add(a0_ops[3].id, a0_ops[3].value, a0_ops[3].parent_left, a0_ops[3].parent_right));
assert(list.getArray() == "abdc");
// a1 insert 'z' after 'a', 'b' parent right
assert(list.add(a1_ops[0].id, a1_ops[0].value, a1_ops[0].parent_left, a1_ops[0].parent_right));
assert(list.getArray() == "azbdc");
}
std::cout << "done with ez\n";
{ // a1 was not uptodate only had 0,1 of a0
// a1 insert 'y' after 'b', no parent right
assert(list.add(a1_ops[1].id, a1_ops[1].value, a1_ops[1].parent_left, a1_ops[1].parent_right));
assert(list.getArray() == "azbdcy");
}
std::cout << "\ndoc size (with tombstones): " << list._list_ids.size() << "\n";
std::cout << "\ndoc size: " << list.getDocSize() << "\n";
std::cout << "doc text:\n";
const auto tmp_array = list.getArray();
std::cout << std::string_view(tmp_array.data(), tmp_array.size()) << "\n";
}
int main(void) {
std::cout << "testSingle1:\n";
testSingle1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent1:\n";
testConcurrent1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave1:\n";
testInterleave1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave2:\n";
testInterleave2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent2:\n";
testConcurrent2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testMain1:\n";
testMain1();
std::cout << std::string(40, '-') << "\n";
return 0;
}

701
version3/test2.cpp Normal file
View File

@ -0,0 +1,701 @@
#include <green_crdt/v3/text_document.hpp>
#include <numeric>
#include <optional>
#include <random>
#include <iostream>
#include <cassert>
#include <variant>
// single letter agent, for testing only
using Agent = std::string;
using Doc = GreenCRDT::V3::TextDocument<Agent>;
using Op = Doc::Op;
using ListType = Doc::ListType;
// maybe switch it up?
//using Rng = std::minstd_rand;
//using Rng = std::mt19937;
using Rng = std::ranlux24_base;
// 10*7 -> 70 permutations , ggwp
// | 1add | 1del | 1rep | 2add | 2del | 2rep | random add | random del | random rep | random
// empty doc | | 0 | 0 | | 0 | 0 | x | 0 | 0 |
// before 1 char | | | | | | | | | |
// after 1 char | | | | | | | | | |
// before 2 char | | | | | | | | | |
// in 2 char | | | | | | | | | |
// after 2 char | | | | | | | | | |
// random | | | | | | | | | |
static const std::vector<char> random_chars {
'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y',
'z',
'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y',
'Z',
};
std::ostream& operator<<(std::ostream& out, const std::optional<ListType::ListID>& id) {
if (id.has_value()) {
out << id.value().id << "-" << id.value().seq;
} else {
out << "null";
}
return out;
}
std::ostream& operator<<(std::ostream& out, const Doc::OpAdd& op) {
out
<< "{ id:" << op.id.id
<< "-" << op.id.seq
<< ", v:" << op.value
<< ", l:" << op.parent_left
<< ", r:" << op.parent_right
<< " }"
;
return out;
}
// genX() changes doc, uses local agent
Op genAdd(Rng& rng, Doc& doc) {
Doc::OpAdd op {
{doc.local_actor, 0u},
std::nullopt,
std::nullopt,
random_chars[rng()%random_chars.size()]
};
// TODO: move to list
// make sure actor index exists
if (!doc.state.findActor(doc.local_actor).has_value()) {
doc.state._actors.push_back(doc.local_actor);
}
// first id is 0
if (doc.state._last_seen_seq.count(doc.state.findActor(doc.local_actor).value())) {
op.id.seq = doc.state._last_seen_seq[doc.state.findActor(doc.local_actor).value()] + 1;
}
if (!doc.state.empty()) {
// gen parents
size_t li = rng()%(1+doc.state.size());
if (li != doc.state.size()) { // nullopt
op.parent_left = doc.state.getID(li);
}
//size_t r_range = 1+doc.state.list.size();
//if (li != doc.state.list.size()) {
//r_range -= li+1;
//}
//size_t ri = rng()%r_range;
//if (li != doc.state.list.size()) {
//ri += li+1;
//}
//if (ri != doc.state.list.size()) { // nullopt
//op.parent_right = doc.state.list[li].id;
//}
if (op.parent_left.has_value()) {
if (doc.state.size() != li + 1) { // left is not last
op.parent_right = doc.state.getID(li+1);
}
} else {
// left is before first, so right is first
op.parent_right = doc.state.getID(0);
}
} // else first char, both nullopt
//std::cout << "op: " << op << "\n";
{
bool r = doc.state.add(op.id, op.value, op.parent_left, op.parent_right);
if (!r) {
std::cout << "op: " << op << "\n";
}
assert(r);
}
return op;
}
Op genDel(Rng& rng, Doc& doc) {
if (doc.state.getDocSize() == 0) {
assert(false && "empty doc");
return {}; // empty
}
doc.state.verify();
Doc::OpDel op{};
// search for undelted entry
size_t idx = rng()%doc.state.size();
bool found = false;
for (size_t attempts = 0; attempts <= doc.state.size(); attempts++) {
//if (doc.state.list[idx].value.has_value()) {
if (doc.state.getValue(idx).has_value()) {
op.id = doc.state.getID(idx);
found = true;
break;
}
idx = (idx+1) % doc.state.size();
}
assert(found);
{
auto size_pre = doc.state.getDocSize();
bool r = doc.state.del(op.id);
assert(r);
assert(size_pre-1 == doc.state.getDocSize());
assert(doc.state.verify());
}
return op;
}
//genRep()
//genAddContRange()
//genDelContRange()
//genRepContRange()
//genRand()
//genRandRanges()
std::vector<Op> genRandAll(Rng& rng, Doc& doc) {
switch (rng() % 1) {
case 0:
return {genAdd(rng, doc)};
}
return {};
}
void testEmptyDocAdds(size_t seed) {
Rng rng(seed);
Doc doc; // empty
doc.local_actor = 'A';
std::string changed_text;
{
// for modifying
Doc doctmp = doc;
const size_t loop_count = (rng() % 55)+1;
for (size_t i = 0; i < loop_count; i++) {
genAdd(rng, doctmp);
}
changed_text = doctmp.getText();
}
assert(doc.getText() != changed_text);
std::cout << "changed_text: " << changed_text << "\n";
Doc otherdoc = doc;
assert(doc.getText().size() == doc.state.getDocSize());
const auto merge_ops = doc.merge(changed_text);
assert(doc.getText().size() == doc.state.getDocSize());
assert(doc.getText() == changed_text);
assert(otherdoc.apply(merge_ops));
assert(doc.getText() == otherdoc.getText());
}
void test1CharDocAdds(size_t seed) {
Rng rng(seed);
Doc doc;
doc.local_actor = 'A';
doc.addText(std::nullopt, std::nullopt, "0");
assert(doc.getText() == "0");
std::string changed_text;
{
// for modifying
Doc doctmp = doc;
const size_t loop_count = (rng() % 4)+1;
for (size_t i = 0; i < loop_count; i++) {
genAdd(rng, doctmp);
}
changed_text = doctmp.getText();
}
assert(doc.getText() != changed_text);
std::cout << "text: " << doc.getText() << "\n";
std::cout << "changed_text: " << changed_text << "\n";
Doc otherdoc = doc;
assert(doc.getText().size() == doc.state.getDocSize());
const auto merge_ops = doc.merge(changed_text);
assert(doc.getText().size() == doc.state.getDocSize());
std::cout << "text after merge: " << doc.getText() << "\n";
assert(doc.getText() == changed_text);
assert(otherdoc.apply(merge_ops));
assert(doc.getText() == otherdoc.getText());
}
void test1CharDocDels(size_t seed) {
Rng rng(seed);
Doc doc;
doc.local_actor = 'A';
assert(doc.getText().size() == doc.state.getDocSize());
doc.addText(std::nullopt, std::nullopt, "0123");
assert(doc.getText().size() == doc.state.getDocSize());
assert(doc.getText() == "0123");
std::string changed_text;
{
// for modifying
Doc doctmp = doc;
const size_t loop_count = (rng() % 4)+1;
std::cout << "going to delete: " << loop_count << "\n";
for (size_t i = 0; i < loop_count; i++) {
genDel(rng, doctmp);
}
changed_text = doctmp.getText();
assert(doctmp.getText().size() == doctmp.state.getDocSize());
if (loop_count == doc.state.getDocSize()) {
assert(doctmp.state.getDocSize() == 0);
assert(changed_text.size() == 0);
}
}
assert(doc.getText() != changed_text);
std::cout << "text: " << doc.getText() << "\n";
std::cout << "changed_text: " << changed_text << "\n";
Doc otherdoc = doc;
assert(doc.getText().size() == doc.state.getDocSize());
const auto merge_ops = doc.merge(changed_text);
assert(doc.getText().size() == doc.state.getDocSize());
std::cout << "text after merge: " << doc.getText() << "\n";
assert(doc.getText() == changed_text);
assert(otherdoc.apply(merge_ops));
assert(doc.getText() == otherdoc.getText());
}
void test2CharDocAdds(size_t seed) {
Rng rng(seed);
Doc doc;
doc.local_actor = 'A';
assert(doc.getText().size() == doc.state.getDocSize());
doc.addText(std::nullopt, std::nullopt, "012345");
assert(doc.getText().size() == doc.state.getDocSize());
assert(doc.getText() == "012345");
std::string changed_text;
{
// for modifying
Doc doctmp = doc;
const size_t loop_count = (rng() % 6)+1;
for (size_t i = 0; i < loop_count; i++) {
genAdd(rng, doctmp);
}
changed_text = doctmp.getText();
}
assert(doc.getText() != changed_text);
std::cout << "text: " << doc.getText() << "\n";
std::cout << "changed_text: " << changed_text << "\n";
Doc otherdoc = doc;
assert(doc.getText().size() == doc.state.getDocSize());
const auto merge_ops = doc.merge(changed_text);
assert(doc.getText().size() == doc.state.getDocSize());
std::cout << "text after merge: " << doc.getText() << "\n";
assert(doc.getText() == changed_text);
assert(otherdoc.apply(merge_ops));
assert(doc.getText() == otherdoc.getText());
}
void testChange1(size_t seed) {
Rng rng(seed);
Doc doc;
doc.local_actor = 'A';
assert(doc.getText().size() == doc.state.getDocSize());
doc.addText(std::nullopt, std::nullopt, "012345");
assert(doc.getText().size() == doc.state.getDocSize());
assert(doc.getText() == "012345");
std::string changed_text;
{
// for modifying
Doc doctmp = doc;
{ // dels
const size_t loop_count = (rng() % 6)+1;
for (size_t i = 0; i < loop_count; i++) {
genDel(rng, doctmp);
}
}
{ // adds
const size_t loop_count = (rng() % 6)+1;
for (size_t i = 0; i < loop_count; i++) {
genAdd(rng, doctmp);
}
}
changed_text = doctmp.getText();
}
assert(doc.getText() != changed_text);
std::cout << "text: " << doc.getText() << "\n";
std::cout << "changed_text: " << changed_text << "\n";
Doc otherdoc = doc;
assert(doc.getText().size() == doc.state.getDocSize());
const auto merge_ops = doc.merge(changed_text);
assert(doc.getText().size() == doc.state.getDocSize());
std::cout << "text after merge: " << doc.getText() << "\n";
assert(doc.getText() == changed_text);
assert(otherdoc.apply(merge_ops));
assert(doc.getText() == otherdoc.getText());
}
void testBugSame(void) {
Doc doc;
doc.local_actor = 'A';
std::string_view new_text1{"a"};
doc.merge(new_text1);
assert(doc.getText() == new_text1);
std::string_view new_text2{"aa"};
doc.merge(new_text2);
assert(doc.getText() == new_text2);
}
void testBugDoubleDel(void) {
Doc doc;
doc.local_actor = 'A';
{
std::string_view new_text{"a"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{""};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
assert(std::holds_alternative<Doc::OpDel>(ops.front()));
assert(std::get<Doc::OpDel>(ops.front()).id.seq == 0);
}
{
std::string_view new_text{""};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 0);
}
}
void testBugSameDel(void) {
Doc doc;
doc.local_actor = 'A';
{
std::string_view new_text{"a"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"a"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
}
void testBugSameDel2(void) {
Doc doc;
doc.local_actor = 'A';
{
std::string_view new_text{"a"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aaa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"aa"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
{
std::string_view new_text{"a"};
const auto ops = doc.merge(new_text);
assert(doc.getText() == new_text);
assert(ops.size() == 1);
}
}
void testMulti1(void) {
Doc docA;
docA.local_actor = 'A';
Doc docB;
docB.local_actor = 'B';
// state A
{
std::string_view new_text{"iiiiiii"};
const auto ops = docA.merge(new_text);
assert(docA.getText() == new_text);
assert(docB.apply(ops));
assert(docB.getText() == new_text);
assert(docB.state.getDocSize() == docA.state.getDocSize());
assert(docB.state.size() == docA.state.size());
}
// now B inserts b
{
std::string_view new_text{"iiibiiii"};
const auto ops = docB.merge(new_text);
assert(docB.getText() == new_text);
assert(ops.size() == 1); // 1 new inserted char, nothing to delete
assert(docA.apply(ops));
assert(docA.getText() == new_text);
}
}
void testPaste1(void) {
Doc docA;
docA.local_actor = 'A';
{
std::string_view new_text{"iiiiiii"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"iiiiiii\n"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 1);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"iiiiiii\niiiiiii"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
}
void testPaste2(void) {
Doc docA;
docA.local_actor = 'A';
{
std::string_view new_text{"aiiiiib"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"aiiiiib\n"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 1);
assert(docA.getText() == new_text);
}
{
std::string_view new_text{"aiiiiib\naiiiiib"};
const auto ops = docA.merge(new_text);
assert(ops.size() == 7);
assert(docA.getText() == new_text);
}
}
int main(void) {
const size_t loops = 1'000;
{
std::cout << "testEmptyDocAdds:\n";
for (size_t i = 0; i < loops; i++) {
std::cout << "i " << i << "\n";
testEmptyDocAdds(1337+i);
std::cout << std::string(40, '-') << "\n";
}
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "test1CharDocAdds:\n";
for (size_t i = 0; i < loops; i++) {
std::cout << "i " << i << "\n";
test1CharDocAdds(1337+i);
std::cout << std::string(40, '-') << "\n";
}
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "test1CharDocDels:\n";
for (size_t i = 0; i < loops; i++) {
std::cout << "i " << i << "\n";
test1CharDocDels(1337+i);
std::cout << std::string(40, '-') << "\n";
}
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "test2CharDocAdds:\n";
for (size_t i = 0; i < loops; i++) {
std::cout << "i " << i << "\n";
test2CharDocAdds(1337+i);
std::cout << std::string(40, '-') << "\n";
}
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testChange1:\n";
for (size_t i = 0; i < loops; i++) {
std::cout << "i " << i << "\n";
testChange1(1337+i);
std::cout << std::string(40, '-') << "\n";
}
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testBugSame:\n";
testBugSame();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testBugDoubleDel:\n";
testBugDoubleDel();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testBugSameDel:\n";
testBugSameDel();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testBugSameDel2:\n";
testBugSameDel2();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testMulti1:\n";
testMulti1();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testPaste1:\n";
testPaste1();
}
std::cout << std::string(40, '=') << "\n";
{
std::cout << "testPaste2:\n";
testPaste2();
}
return 0;
}

26
version4/CMakeLists.txt Normal file
View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
project(crdt_version4 CXX C)
add_library(crdt_version4 INTERFACE)
target_compile_features(crdt_version4 INTERFACE cxx_std_17)
target_include_directories(crdt_version4 INTERFACE "${PROJECT_SOURCE_DIR}")
########################################
add_executable(v4_test1
./test1.cpp
)
target_link_libraries(v4_test1 PUBLIC crdt_version4)
########################################
#add_executable(v4_test2
#./test2.cpp
#)
#target_link_libraries(v4_test2 PUBLIC crdt_version4)

View File

@ -0,0 +1,381 @@
#pragma once
#include <cstdint>
#include <optional>
#include <unordered_map>
#include <vector>
#include <string>
#include <cassert>
#if !defined(extra_assert)
#if defined(EXTRA_ASSERTS) && EXTRA_ASSERTS == 1
#define extra_assert(...) assert(__VA_ARGS__)
#else
#define extra_assert(...) void(0)
#endif
#endif
namespace GreenCRDT::V4 {
template<typename ValueType, typename ActorType>
struct List {
// for public interface
struct ListID {
ActorType id;
uint64_t seq{0}; // strictly increasing for that actor
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;
}
};
struct ListIDInternal {
size_t actor_idx{0};
uint64_t seq{0}; // strictly increasing for that actor
bool operator==(const ListIDInternal& rhs) const {
return seq == rhs.seq && actor_idx == rhs.actor_idx;
}
};
// internally the index into this array is used to refer to an actor
std::vector<ActorType> _actors;
// range
struct Entry {
ListIDInternal id;
std::vector<ValueType> values;
bool deleted {false};
// Yjs
std::optional<ListIDInternal> parent_left;
std::optional<ListIDInternal> parent_right;
};
// TODO: use something better, edit: this seems fine
std::vector<Entry> _list;
// number of not deleted entries
size_t _doc_size {0};
// TODO: actor index instead of map
std::unordered_map<size_t, uint64_t> _last_seen_seq;
// caching only, contains the last index an actor inserted at
std::unordered_map<size_t, size_t> _last_inserted_idx;
//size_t _stat_find_with_hint{0};
//size_t _stat_find_with_hint_hit{0};
std::optional<size_t> findActor(const ActorType& actor) const {
for (size_t i = 0; i < _actors.size(); i++) {
if (_actors[i] == actor) {
return i;
}
}
return std::nullopt;
}
std::optional<size_t> findIdx(const ListIDInternal& list_id) const {
extra_assert(verify());
for (size_t i = 0; i < _list.size(); i++) {
if (
_list[i].id.actor_idx == list_id && // same actor
list_id.seq >= _list[i].id.seq && // in range seen from left
list_id.seq < _list[i].id.seq + _list[i].values.size() // in range seen from right
) {
return i;
}
}
return std::nullopt;
}
// search close to hint first
std::optional<size_t> findIdx(const ListIDInternal& list_id, size_t hint) const {
extra_assert(verify());
//_stat_find_with_hint++;
// TODO: find NEW magic values
static constexpr size_t c_hint_pre = 1;
static constexpr size_t c_hint_post = 4;
if (hint >= c_hint_pre) {
hint -= c_hint_pre;
}
const size_t max_at_hint = hint + c_hint_post; // how many positions we check at hint, before falling back to full lookup
for (size_t i = hint; i <= max_at_hint && i < _list.size(); i++) {
if (_list[i].id == list_id) {
//_stat_find_with_hint_hit++;
return i;
}
}
// fall back to normal search
return findIdx(list_id);
}
std::optional<size_t> findIdx(const ListID& list_id) const {
extra_assert(verify());
const auto actor_idx_opt = findActor(list_id.id);
if (!actor_idx_opt.has_value()) {
return std::nullopt;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), list_id.seq};
return findIdx(tmp_id);
}
std::optional<size_t> findIdx(const ListID& list_id, size_t hint) const {
extra_assert(verify());
const auto actor_idx_opt = findActor(list_id.id);
if (!actor_idx_opt.has_value()) {
return std::nullopt;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), list_id.seq};
return findIdx(tmp_id, hint);
}
// 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) {
extra_assert(verify());
size_t actor_idx {0};
{ // new actor?
// add, even if op fails
const auto actor_opt = findActor(list_id.id);
if (!actor_opt.has_value()) {
actor_idx = _actors.size();
_last_inserted_idx[_actors.size()] = 0; // hack
_actors.push_back(list_id.id);
} else {
actor_idx = actor_opt.value();
}
}
// check actor op order
if (!_last_seen_seq.count(actor_idx)) {
// we dont know this actor yet, first seq needs to be 0
if (list_id.seq != 0) {
return false;
}
} else {
// making sure we dont skip operations by that actor
if (list_id.seq != _last_seen_seq.at(actor_idx) + 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_opt = std::nullopt;
if (parent_left.has_value()) {
left_idx_opt = findIdx(parent_left.value(), _last_inserted_idx[actor_idx]);
if (!left_idx_opt.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_opt.value() + 1;
} // else insert_idx = 0
const size_t left_idx_hint = insert_idx;
// find right
size_t right_idx = _list.size();
if (parent_right.has_value()) {
auto tmp_right = findIdx(parent_right.value(), left_idx_hint);
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(), left_idx_hint);
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_opt) {
break;
} else if (i_left_idx == left_idx_opt) {
// 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(), insert_idx);
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) {
// actor id tie breaker
if (_actors[actor_idx] < _actors[at_i.id.actor_idx]) {
break;
} else {
scanning = false;
}
} else { // i_right_idx > right_idx
scanning = false;
}
} else { // il > l
// do nothing
}
}
}
{ // actual insert
Entry new_entry;
new_entry.id.actor_idx = actor_idx;
new_entry.id.seq = list_id.seq;
if (parent_left.has_value()) {
new_entry.parent_left = ListIDInternal{findActor(parent_left.value().id).value(), parent_left.value().seq};
}
if (parent_right.has_value()) {
new_entry.parent_right = ListIDInternal{findActor(parent_right.value().id).value(), parent_right.value().seq};
}
new_entry.value = value;
_list.emplace(_list.begin() + insert_idx, new_entry);
_last_inserted_idx[actor_idx] = insert_idx;
}
_doc_size++;
_last_seen_seq[actor_idx] = list_id.seq;
extra_assert(verify());
return true;
}
// returns false if not found
bool del(const ListID& id) {
extra_assert(verify());
auto actor_idx_opt = findActor(id.id);
if (!actor_idx_opt.has_value()) {
// we dont have anything with that actor
return false;
}
const ListIDInternal tmp_id {actor_idx_opt.value(), id.seq};
for (auto& it : _list) {
if (it.id == tmp_id) {
if (it.value.has_value()) {
it.value.reset();
_doc_size--;
extra_assert(verify());
return true;
} else {
extra_assert(verify());
return false; // TODO: allow double deletes?,,,, need ids
}
}
}
extra_assert(verify());
return false;
}
size_t getDocSize(void) const {
return _doc_size;
}
std::vector<ValueType> getArray(void) const {
std::vector<ValueType> array;
for (const auto& e : _list) {
if (e.value.has_value()) {
array.push_back(e.value.value());
}
}
return array;
}
// TODO: only in debug?
bool 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);
return _doc_size == actual_size;
}
};
} // GreenCRDT::V1

214
version4/test1.cpp Normal file
View File

@ -0,0 +1,214 @@
#define EXTRA_ASSERTS 1
#include <green_crdt/v4/list.hpp>
#include <numeric>
#include <random>
#include <iostream>
#include <cassert>
#include <string_view>
#include <vector>
// single letter actor, for testing only
using Actor = char;
using ListType = GreenCRDT::V4::List<char, Actor>;
namespace std {
bool operator==(const std::vector<char>& lhs, const std::string_view& rhs) {
if (lhs.size() != rhs.size()) {
return false;
}
for (size_t i = 0; i < rhs.size(); i++) {
if (lhs[i] != rhs[i]) {
return false;
}
}
return true;
}
} // namespace std
void testSingle1(void) {
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'A', 1}, 'b', ListType::ListID{'A', 0u}, std::nullopt));
assert(list.getArray() == "ab");
}
void testConcurrent1(void) {
// agent_a < agent_b
// concurrent insert of first element
{ // variant 1, a then b
ListType list;
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
{ // variant 2, b then a
ListType list;
assert(list.add({'B', 0}, 'b', std::nullopt, std::nullopt));
assert(list.add({'A', 0}, 'a', std::nullopt, std::nullopt));
assert(list.getArray() == "ab");
}
}
struct AddOp {
ListType::ListID id;
char value;
std::optional<ListType::ListID> parent_left;
std::optional<ListType::ListID> parent_right;
};
void randomAddPermutations(const std::vector<AddOp>& ops, const std::string& expected) {
// TODO: more then 1k?
for (size_t i = 0; i < 1000; i++) {
std::minstd_rand rng(1337 + i);
std::vector<size_t> ops_todo(ops.size());
std::iota(ops_todo.begin(), ops_todo.end(), 0u);
size_t attempts {0};
ListType list;
do {
size_t idx = rng() % ops_todo.size();
if (list.add(ops[ops_todo[idx]].id, ops[ops_todo[idx]].value, ops[ops_todo[idx]].parent_left, ops[ops_todo[idx]].parent_right)) {
// only remove if it was possible -> returned true;
ops_todo.erase(ops_todo.begin()+idx);
}
attempts++;
assert(attempts < 10'000); // in case we run into an endless loop
} while (!ops_todo.empty());
assert(list.getArray() == expected);
}
}
void testInterleave1(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', ListType::ListID{'A', 0u}, std::nullopt},
{{'A', 2u}, 'a', ListType::ListID{'A', 1u}, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', ListType::ListID{'B', 0u}, std::nullopt},
{{'B', 2u}, 'b', ListType::ListID{'B', 1u}, std::nullopt},
};
randomAddPermutations(ops, "aaabbb");
}
void testInterleave2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'A', 1u}, 'a', std::nullopt, ListType::ListID{'A', 0u}},
{{'A', 2u}, 'a', std::nullopt, ListType::ListID{'A', 1u}},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'B', 1u}, 'b', std::nullopt, ListType::ListID{'B', 0u}},
{{'B', 2u}, 'b', std::nullopt, ListType::ListID{'B', 1u}},
};
randomAddPermutations(ops, "aaabbb");
}
void testConcurrent2(void) {
const std::vector<AddOp> ops {
{{'A', 0u}, 'a', std::nullopt, std::nullopt},
{{'C', 0u}, 'c', std::nullopt, std::nullopt},
{{'B', 0u}, 'b', std::nullopt, std::nullopt},
{{'D', 0u}, 'd', ListType::ListID{'A', 0u}, ListType::ListID{'C', 0u}},
};
randomAddPermutations(ops, "adbc");
}
void testMain1(void) {
ListType list;
static_assert('0' < '1');
const std::vector<AddOp> a0_ops {
{{'0', 0u}, 'a', std::nullopt, std::nullopt},
{{'0', 1u}, 'b', ListType::ListID{'0', 0u}, std::nullopt},
{{'0', 2u}, 'c', ListType::ListID{'0', 1u}, std::nullopt},
{{'0', 3u}, 'd', ListType::ListID{'0', 1u}, ListType::ListID{'0', 2u}},
};
const std::vector<AddOp> a1_ops {
// knows of a0 up to {a0, 1}
{{'1', 0u}, 'z', ListType::ListID{'0', 0u}, ListType::ListID{'0', 1u}},
{{'1', 1u}, 'y', ListType::ListID{'0', 1u}, std::nullopt},
};
{ // the ez, in order stuff
// a0 insert first char, 'a', since its the first, we dont have any parents
assert(list.add(a0_ops[0].id, a0_ops[0].value, a0_ops[0].parent_left, a0_ops[0].parent_right));
assert(list.getArray() == "a");
// a0 insert secound char, 'b' after 'a', no parents to right
assert(list.add(a0_ops[1].id, a0_ops[1].value, a0_ops[1].parent_left, a0_ops[1].parent_right));
assert(list.getArray() == "ab");
// a0 insert 'c' after 'b', no parents to right
assert(list.add(a0_ops[2].id, a0_ops[2].value, a0_ops[2].parent_left, a0_ops[2].parent_right));
assert(list.getArray() == "abc");
// a0 insert 'd' after 'b', 'c' parent right
assert(list.add(a0_ops[3].id, a0_ops[3].value, a0_ops[3].parent_left, a0_ops[3].parent_right));
assert(list.getArray() == "abdc");
// a1 insert 'z' after 'a', 'b' parent right
assert(list.add(a1_ops[0].id, a1_ops[0].value, a1_ops[0].parent_left, a1_ops[0].parent_right));
assert(list.getArray() == "azbdc");
}
std::cout << "done with ez\n";
{ // a1 was not uptodate only had 0,1 of a0
// a1 insert 'y' after 'b', no parent right
assert(list.add(a1_ops[1].id, a1_ops[1].value, a1_ops[1].parent_left, a1_ops[1].parent_right));
assert(list.getArray() == "azbdcy");
}
std::cout << "\ndoc size (with tombstones): " << list._list.size() << "\n";
std::cout << "\ndoc size: " << list.getDocSize() << "\n";
std::cout << "doc text:\n";
const auto tmp_array = list.getArray();
std::cout << std::string_view(tmp_array.data(), tmp_array.size()) << "\n";
}
int main(void) {
std::cout << "testSingle1:\n";
testSingle1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent1:\n";
testConcurrent1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave1:\n";
testInterleave1();
std::cout << std::string(40, '-') << "\n";
std::cout << "testInterleave2:\n";
testInterleave2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testConcurrent2:\n";
testConcurrent2();
std::cout << std::string(40, '-') << "\n";
std::cout << "testMain1:\n";
testMain1();
std::cout << std::string(40, '-') << "\n";
return 0;
}

View File

@ -14,3 +14,16 @@ target_link_libraries(vim_research_test1 PUBLIC
nlohmann_json::nlohmann_json nlohmann_json::nlohmann_json
) )
########################################
add_executable(vim_research_test2
./test2.cpp
)
target_link_libraries(vim_research_test2 PUBLIC
crdt_version0
zed_net
nlohmann_json::nlohmann_json
toxcore
)

View File

@ -1,4 +1,4 @@
#include <crdt/text_document.hpp> #include <green_crdt/v0/text_document.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
extern "C" { extern "C" {
@ -15,7 +15,7 @@ extern "C" {
// single letter agent, for testing only // single letter agent, for testing only
//using Agent = char; //using Agent = char;
using Agent = uint16_t; // tmp local port using Agent = uint16_t; // tmp local port
using Doc = GreenCRDT::TextDocument<Agent>; using Doc = GreenCRDT::V0::TextDocument<Agent>;
using ListType = Doc::ListType; using ListType = Doc::ListType;
std::ostream& operator<<(std::ostream& out, const std::optional<ListType::ListID>& id) { std::ostream& operator<<(std::ostream& out, const std::optional<ListType::ListID>& id) {
@ -78,6 +78,19 @@ std::ostream& operator<<(std::ostream& out, const ListType::Entry& e) {
return out; return out;
} }
static bool send_response(zed_net_socket_t* remote_socket, const int64_t id, const nlohmann::json& j) {
auto j_msg = nlohmann::json::array();
j_msg.push_back(id);
j_msg.push_back(j);
std::string str = j_msg.dump();
str += '\n';
auto ret = zed_net_tcp_socket_send(remote_socket, str.data(), str.size());
return ret == 0;
}
static bool send_command(zed_net_socket_t* remote_socket, const std::string_view mode, const std::string_view command) { static bool send_command(zed_net_socket_t* remote_socket, const std::string_view mode, const std::string_view command) {
auto j = nlohmann::json::array(); auto j = nlohmann::json::array();
@ -97,31 +110,65 @@ static bool send_setup(zed_net_socket_t* remote_socket) {
// vars // vars
R"( R"(
let b:green_crdt_timer_can_send = v:true let b:green_crdt_timer_can_send = v:true
let b:green_crdt_timer_can_fetch = v:true
let b:green_crdt_dirty = v:true let b:green_crdt_dirty = v:true
)" )"
// send
R"( R"(
function! GreenCRDTTimerCallback(timer) abort function! GreenCRDTSendTimerCallback(timer) abort
let b:green_crdt_timer_can_send = v:true let b:green_crdt_timer_can_send = v:true
call GreenCRDTCheckTimeAndSendState() call GreenCRDTCheckTimeAndSend()
endfunction endfunction
)" )"
// TODO: make send sync? (ch_evalexpr())
R"( R"(
function! GreenCRDTCheckTimeAndSendState() abort function! GreenCRDTCheckTimeAndSend() abort
if b:green_crdt_timer_can_send && b:green_crdt_dirty if b:green_crdt_timer_can_send && b:green_crdt_dirty
let b:green_crdt_timer_can_send = v:false let b:green_crdt_timer_can_send = v:false
call ch_sendexpr(b:channel, [{'cmd': 'full_buffer', 'lines': getbufline(bufnr(), 1, '$')}]) call ch_sendexpr(b:channel, [{'cmd': 'full_buffer', 'lines': getbufline(bufnr(), 1, '$')}])
let b:green_crdt_dirty = v:false let b:green_crdt_dirty = v:false
call timer_start(100, 'GreenCRDTTimerCallback') call timer_start(100, 'GreenCRDTSendTimerCallback')
endif endif
endfunction endfunction
)" )"
// fetch
R"(
function! GreenCRDTFetchTimerCallback(timer) abort
let b:green_crdt_timer_can_fetch = v:true
call GreenCRDTCheckTimeAndFetch()
endfunction
)"
R"(
function! GreenCRDTCheckTimeAndFetch()
if reg_executing() isnot# '' | return | endif
if b:green_crdt_timer_can_fetch
let b:green_crdt_timer_can_fetch = v:false
" dont update when inserting or visual (or atleast not in visual)
if mode() is# 'n'
let l:response = ch_evalexpr(b:channel, [{'cmd': 'fetch_changes'}])
for [line_number, line] in l:response
call setline(line_number, line)
endfor
endif
let b:green_crdt_fetch_timer = timer_start(503, 'GreenCRDTFetchTimerCallback')
endif
endfunction
)"
// change event
R"( R"(
function! GreenCRDTChangeEvent() function! GreenCRDTChangeEvent()
let b:green_crdt_dirty = v:true let b:green_crdt_dirty = v:true
call GreenCRDTCheckTimeAndSendState() call GreenCRDTCheckTimeAndSend()
call GreenCRDTCheckTimeAndFetch()
endfunction endfunction
)" )"
@ -134,9 +181,15 @@ function! GreenCRDTStop()
augroup green_crdt augroup green_crdt
au! au!
augroup END augroup END
call timer_stop(b:green_crdt_fetch_timer)
call ch_close(b:channel) call ch_close(b:channel)
delfunction GreenCRDTCheckTimeAndSendState
delfunction GreenCRDTTimerCallback delfunction GreenCRDTCheckTimeAndSend
delfunction GreenCRDTCheckTimeAndFetch
delfunction GreenCRDTSendTimerCallback
delfunction GreenCRDTFetchTimerCallback
delfunction GreenCRDTChangeEvent delfunction GreenCRDTChangeEvent
"delfunction GreenCRDTStop "delfunction GreenCRDTStop
let b:green_crdt_timer_can_send = v:true let b:green_crdt_timer_can_send = v:true
@ -157,6 +210,8 @@ delfunction GreenCRDTSetupEvents
)" )"
R"( R"(
let b:green_crdt_fetch_timer = timer_start(900, 'GreenCRDTFetchTimerCallback')
echo 'setup done' echo 'setup done'
)"); )");
} }
@ -279,6 +334,32 @@ int main(void) {
} else if (command == "setup") { // setup callbacks etc, basically the plugin } else if (command == "setup") { // setup callbacks etc, basically the plugin
std::cout << "sending setup\n"; std::cout << "sending setup\n";
send_setup(&remote_socket); send_setup(&remote_socket);
} else if (command == "fetch_changes") { // setup callbacks etc, basically the plugin
// apply changes (some) and gen vim inserts
std::cout << "got fetch changes\n";
auto j_res_line_list = nlohmann::json::array();
if (true) { // external changes
const auto crdt_text = doc.getText();
std::string_view text_view {crdt_text};
for (int64_t i = 1; ; i++) {
const auto nl_pos = text_view.find_first_of("\n");
if (nl_pos == std::string_view::npos) {
// no more lines
j_res_line_list.push_back(nlohmann::json::array({i, text_view}));
break;
} else {
const auto line = text_view.substr(0, nl_pos);
j_res_line_list.push_back(nlohmann::json::array({i, line}));
assert(text_view.size() >= nl_pos+1);
text_view = text_view.substr(nl_pos+1);
}
}
}
send_response(&remote_socket, command_seq, j_res_line_list);
} else if (command == "full_buffer") { // vim is sending the full buffer } else if (command == "full_buffer") { // vim is sending the full buffer
// array of lines // array of lines

1222
vim_research/test2.cpp Normal file

File diff suppressed because it is too large Load Diff