Skip to content

Commit 2b4c8d6

Browse files
committed
fix: rt_api history
1 parent 3e4a298 commit 2b4c8d6

File tree

5 files changed

+210
-28
lines changed

5 files changed

+210
-28
lines changed

api/dsr_api.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ std::vector<DSR::Node> DSRGraph::get_nodes_by_types(const std::vector<std::strin
495495
//////////////////////////////////////////////////////////////////////////////////
496496
std::optional<CRDTEdge> DSRGraph::get_edge_(uint64_t from, uint64_t to, const std::string &key)
497497
{
498-
std::shared_lock<std::shared_mutex> lock(_mutex);
498+
//std::shared_lock<std::shared_mutex> lock(_mutex);
499499
if (nodes.contains(from) && nodes.contains(to))
500500
{
501501
auto n = get_(from);
@@ -524,6 +524,7 @@ std::optional<DSR::Edge> DSRGraph::get_edge(const std::string &from, const std::
524524

525525
std::optional<DSR::Edge> DSRGraph::get_edge(uint64_t from, uint64_t to, const std::string &key)
526526
{
527+
std::shared_lock<std::shared_mutex> lock(_mutex);
527528
auto edge_opt = get_edge_(from, to, key);
528529
if (edge_opt.has_value()) return Edge(std::move(edge_opt.value()));
529530
return {};
@@ -679,8 +680,8 @@ bool DSRGraph::delete_edge(uint64_t from, uint64_t to, const std::string &key)
679680
std::optional<IDL::MvregEdge> delta;
680681
std::optional<Edge> deleted_edge;
681682
{
682-
deleted_edge = get_edge_(from, to, key);
683683
std::unique_lock<std::shared_mutex> lock(_mutex);
684+
deleted_edge = get_edge_(from, to, key);
684685
delta = delete_edge_(from, to, key);
685686
}
686687
if (delta.has_value())
@@ -707,8 +708,8 @@ bool DSRGraph::delete_edge(const std::string &from, const std::string &to, const
707708
{
708709
id_from = get_id_from_name(from);
709710
id_to = get_id_from_name(to);
710-
deleted_edge = get_edge_(id_from.value(), id_to.value(), key);
711711
std::unique_lock<std::shared_mutex> lock(_mutex);
712+
deleted_edge = get_edge_(id_from.value(), id_to.value(), key);
712713
if (id_from.has_value() && id_to.has_value())
713714
{
714715
delta = delete_edge_(id_from.value(), id_to.value(), key);

api/dsr_rt_api.cpp

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,29 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, const std::vector<fl
199199
auto rot_pack = rot_pack_o.value_or(std::vector<float> (BLOCK_SIZE * HISTORY_SIZE, 0.f));
200200

201201
auto timestamp_index = 0;
202-
uint32_t index = 0;
202+
int index = 0;
203203
if (timestamp.has_value()) {
204-
std::vector<size_t> diffs;
204+
std::vector<int64_t> diffs;
205205
std::transform(time_stamps.begin(), time_stamps.end(), std::back_inserter(diffs),
206206
[t = *timestamp](auto &val) {
207-
return (val - t > 0) ? (val - t) : std::numeric_limits<int>::max();
207+
return ((int64_t)t - (int64_t)val > 0) ? ((int64_t)t - (int64_t)val) : std::numeric_limits<int64_t>::min();
208208
});
209-
210-
211-
auto pos = (((std::min_element(diffs.begin(), diffs.end())) - diffs.begin()) + 1) % HISTORY_SIZE;
212209

213-
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE+1) % HISTORY_SIZE;
214-
uint32_t index = timestamp_index * BLOCK_SIZE;
210+
auto pos = (((std::min_element(diffs.begin(), diffs.end())) - diffs.begin())) % HISTORY_SIZE;
211+
212+
if (!head_o.has_value()) {
213+
timestamp_index = 0;
214+
} else {
215+
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE) % HISTORY_SIZE;
216+
}
217+
218+
index = timestamp_index * BLOCK_SIZE;
215219

216220
// too old to insert it
217221
if (pos == timestamp_index && *timestamp < time_stamps[pos]) {return;}
222+
if (pos > timestamp_index) {
223+
pos = timestamp_index;
224+
}
218225

219226
time_stamps.erase(time_stamps.begin() + timestamp_index);
220227
tr_pack.erase(tr_pack.begin() + index, tr_pack.begin() + index + 3);
@@ -230,8 +237,12 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, const std::vector<fl
230237

231238

232239
} else {
233-
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE+1) % HISTORY_SIZE;
234-
uint32_t index = timestamp_index * BLOCK_SIZE;
240+
if (!head_o.has_value()) {
241+
timestamp_index = 0;
242+
} else {
243+
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE) % HISTORY_SIZE;
244+
}
245+
index = timestamp_index * BLOCK_SIZE;
235246

236247
tr_pack[index] = trans[0];
237248
tr_pack[index + 1] = trans[1];
@@ -246,7 +257,7 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, const std::vector<fl
246257

247258
CRDTAttribute tr(std::move(tr_pack), get_unix_timestamp(), 0);
248259
CRDTAttribute rot(std::move(rot_pack), get_unix_timestamp(), 0);
249-
CRDTAttribute head_index(index, get_unix_timestamp(), 0);
260+
CRDTAttribute head_index(index+3, get_unix_timestamp(), 0);
250261
CRDTAttribute timestamps(std::move(time_stamps), get_unix_timestamp(), 0);
251262

252263
auto [it, new_el] = e.attrs().insert_or_assign("rt_rotation_euler_xyz", mvreg<CRDTAttribute> ());
@@ -375,23 +386,31 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, std::vector<float> &
375386
auto rot_pack = rot_pack_o.value_or(std::vector<float> (BLOCK_SIZE * HISTORY_SIZE, 0.f));
376387

377388
auto timestamp_index = 0;
378-
uint32_t index = 0;
389+
int index = 0;
390+
bool update_index = true;
379391
if (timestamp.has_value()) {
380-
std::vector<size_t> diffs;
392+
std::vector<int64_t> diffs;
381393
std::transform(time_stamps.begin(), time_stamps.end(), std::back_inserter(diffs),
382394
[t = *timestamp](auto &val) {
383-
return (val - t > 0) ? (val - t) : std::numeric_limits<int>::max();
395+
return ((int64_t)t - (int64_t)val > 0) ? ((int64_t)t - (int64_t)val) : std::numeric_limits<int64_t>::min();
384396
});
385397

386-
387-
auto pos = (((std::min_element(diffs.begin(), diffs.end())) - diffs.begin()) + 1) % HISTORY_SIZE;
398+
auto pos = (((std::min_element(diffs.begin(), diffs.end())) - diffs.begin())) % HISTORY_SIZE;
388399

389-
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE+1) % HISTORY_SIZE;
390-
uint32_t index = timestamp_index * BLOCK_SIZE;
400+
401+
if (!head_o.has_value()) {
402+
timestamp_index = 0;
403+
} else {
404+
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE) % HISTORY_SIZE;
405+
}
406+
index = timestamp_index * BLOCK_SIZE;
391407

392408
// too old to insert it
393409
if (pos == timestamp_index && *timestamp < time_stamps[pos]) {return;}
394-
410+
if (pos > timestamp_index) {
411+
pos = timestamp_index;
412+
update_index=false;
413+
}
395414
time_stamps.erase(time_stamps.begin() + timestamp_index);
396415
tr_pack.erase(tr_pack.begin() + index, tr_pack.begin() + index + 3);
397416
rot_pack.erase(rot_pack.begin() + index, rot_pack.begin() + index + 3);
@@ -406,8 +425,12 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, std::vector<float> &
406425

407426

408427
} else {
409-
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE+1) % HISTORY_SIZE;
410-
uint32_t index = timestamp_index * BLOCK_SIZE;
428+
if (!head_o.has_value()) {
429+
timestamp_index = 0;
430+
} else {
431+
timestamp_index = (int)(head_o.value_or(0)/BLOCK_SIZE) % HISTORY_SIZE;
432+
}
433+
index = timestamp_index * BLOCK_SIZE;
411434

412435
tr_pack[index] = trans[0];
413436
tr_pack[index + 1] = trans[1];
@@ -423,15 +446,17 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, std::vector<float> &
423446

424447
CRDTAttribute tr(std::move(tr_pack), get_unix_timestamp(), 0);
425448
CRDTAttribute rot(std::move(rot_pack), get_unix_timestamp(), 0);
426-
CRDTAttribute head_index(index, get_unix_timestamp(), 0);
449+
CRDTAttribute head_index(index+3, get_unix_timestamp(), 0);
427450
CRDTAttribute timestamps(std::move(time_stamps), get_unix_timestamp(), 0);
428451

429452
auto [it, new_el] = e.attrs().insert_or_assign("rt_rotation_euler_xyz", mvreg<CRDTAttribute> ());
430453
it->second.write(std::move(rot));
431454
std::tie(it, new_el) = e.attrs().insert_or_assign("rt_translation", mvreg<CRDTAttribute> ());
432455
it->second.write(std::move(tr));
433-
std::tie(it, new_el) = e.attrs().insert_or_assign("rt_head_index", mvreg<CRDTAttribute> ());
434-
it->second.write(std::move(head_index));
456+
if (update_index) {
457+
std::tie(it, new_el) = e.attrs().insert_or_assign("rt_head_index", mvreg<CRDTAttribute> ());
458+
it->second.write(std::move(head_index));
459+
}
435460
std::tie(it, new_el) = e.attrs().insert_or_assign("rt_timestamps", mvreg<CRDTAttribute> ());
436461
it->second.write(std::move(timestamps));
437462
}

api/include/dsr/api/dsr_rt_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace DSR
1919
explicit RT_API(DSRGraph *G_);
2020

2121
const int32_t BLOCK_SIZE = 3; // size of 3-vector for translation and euler xyz angles
22-
uint32_t HISTORY_SIZE = 0; // Number of blocks in the history.
22+
uint32_t HISTORY_SIZE = 5; // Number of blocks in the history.
2323

2424
void insert_or_assign_edge_RT(Node &n, uint64_t to, const std::vector<float> &trans, const std::vector<float> &rot_euler, std::optional<uint64_t> timestamp = std::nullopt);
2525
void insert_or_assign_edge_RT(Node &n, uint64_t to, std::vector<float> &&trans, std::vector<float> &&rot_euler, std::optional<uint64_t> timestamp = std::nullopt);

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_executable(tests test.cpp
3030
graph/graph_operations.cpp
3131
graph/attribute_operations.cpp
3232
graph/convenience_operations.cpp
33+
graph/rt_timestamp.cpp
3334
crdt/crdt_operations.cpp
3435
synchronization/graph_synchronization.cpp
3536
synchronization/type_translation.cpp

tests/graph/rt_timestamp.cpp

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
#include "dsr/api/dsr_api.h"
3+
#include "../utils.h"
4+
5+
#include "catch2/catch_test_macros.hpp"
6+
#include "dsr/core/utils.h"
7+
8+
using namespace DSR;
9+
10+
//TODO: add REQUIRES, I'm checking manually
11+
TEST_CASE("RT api timestamp", "[GRAPH][RT]") {
12+
auto ctx = make_edge_config_file();
13+
auto id1 = rand() % 1000;
14+
int argc = 0;
15+
DSRGraph G(random_string(10), id1, ctx);
16+
auto node_name = random_string();
17+
auto n = Node::create<testtype_node_type>(node_name);
18+
G.add_attrib_local<level_att>(n, 0);
19+
const std::optional<uint64_t> r = G.insert_node(n);
20+
REQUIRE(r.has_value());
21+
22+
node_name = random_string();
23+
n = Node::create<testtype_node_type>(node_name);
24+
const std::optional<uint64_t> r2 = G.insert_node(n);
25+
REQUIRE(r2.has_value());
26+
27+
auto timestamp_older = 50000;
28+
29+
auto rt = G.get_rt_api();
30+
REQUIRE (rt);
31+
auto node = G.get_node(*r);
32+
REQUIRE(node.has_value());
33+
n = node.value();
34+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{0., 0.2, 0.5}, std::vector<float>{0., 0., 0.});
35+
36+
node = G.get_node(*r);
37+
n = node.value();
38+
auto edge_rt = rt->get_edge_RT(n, *r2);
39+
REQUIRE(edge_rt.has_value());
40+
41+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
42+
<< " Attribs:[";
43+
for (const auto &v : edge_rt->attrs())
44+
std::cout << v.first << ":" << v.second << " - \n";
45+
std::cout << "]]\n";
46+
47+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{7., 7.2, 7.5}, std::vector<float>{5., 5., 5.}, timestamp_older);
48+
49+
50+
node = G.get_node(*r);
51+
n = node.value();
52+
edge_rt = rt->get_edge_RT(n, *r2);
53+
REQUIRE(edge_rt.has_value());
54+
55+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
56+
<< " Attribs:[";
57+
for (const auto &v : edge_rt->attrs())
58+
std::cout << v.first << ":" << v.second << " - \n";
59+
std::cout << "]]\n";
60+
61+
62+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{13., 12.2, 7.5}, std::vector<float>{5., 3., 2.}, (get_unix_timestamp()/1000000) + 1000);
63+
64+
node = G.get_node(*r);
65+
n = node.value();
66+
edge_rt = rt->get_edge_RT(n, *r2);
67+
REQUIRE(edge_rt.has_value());
68+
69+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
70+
<< " Attribs:[";
71+
for (const auto &v : edge_rt->attrs())
72+
std::cout << v.first << ":" << v.second << " - \n";
73+
std::cout << "]]\n";
74+
75+
76+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{13., 12.2, 7.5}, std::vector<float>{5., 3., 2.});
77+
78+
node = G.get_node(*r);
79+
n = node.value();
80+
edge_rt = rt->get_edge_RT(n, *r2);
81+
REQUIRE(edge_rt.has_value());
82+
83+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
84+
<< " Attribs:[";
85+
for (const auto &v : edge_rt->attrs())
86+
std::cout << v.first << ":" << v.second << " - \n";
87+
std::cout << "]]\n";
88+
89+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{13., 12.2, 7.5}, std::vector<float>{5., 3., 2.});
90+
91+
node = G.get_node(*r);
92+
n = node.value();
93+
edge_rt = rt->get_edge_RT(n, *r2);
94+
REQUIRE(edge_rt.has_value());
95+
96+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
97+
<< " Attribs:[";
98+
for (const auto &v : edge_rt->attrs())
99+
std::cout << v.first << ":" << v.second << " - \n";
100+
std::cout << "]]\n";
101+
102+
103+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{0., 0.2, 0.5}, std::vector<float>{0., 1., 0.}, 3000);
104+
105+
node = G.get_node(*r);
106+
n = node.value();
107+
edge_rt = rt->get_edge_RT(n, *r2);
108+
REQUIRE(edge_rt.has_value());
109+
110+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
111+
<< " Attribs:[";
112+
for (const auto &v : edge_rt->attrs())
113+
std::cout << v.first << ":" << v.second << " - \n";
114+
std::cout << "]]\n";
115+
116+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{0., 0.2, 0.5}, std::vector<float>{0., 1., 0.}, 60000);
117+
118+
node = G.get_node(*r);
119+
n = node.value();
120+
edge_rt = rt->get_edge_RT(n, *r2);
121+
REQUIRE(edge_rt.has_value());
122+
123+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
124+
<< " Attribs:[";
125+
for (const auto &v : edge_rt->attrs())
126+
std::cout << v.first << ":" << v.second << " - \n";
127+
std::cout << "]]\n";
128+
129+
130+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{0., 0.2, 0.5}, std::vector<float>{0., 1., 0.});
131+
132+
node = G.get_node(*r);
133+
n = node.value();
134+
edge_rt = rt->get_edge_RT(n, *r2);
135+
REQUIRE(edge_rt.has_value());
136+
137+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
138+
<< " Attribs:[";
139+
for (const auto &v : edge_rt->attrs())
140+
std::cout << v.first << ":" << v.second << " - \n";
141+
std::cout << "]]\n";
142+
143+
rt->insert_or_assign_edge_RT(n, *r2, std::vector<float>{0., 0.2, 0.5}, std::vector<float>{0., 1., 0.}, 1947239916500);
144+
145+
node = G.get_node(*r);
146+
n = node.value();
147+
edge_rt = rt->get_edge_RT(n, *r2);
148+
REQUIRE(edge_rt.has_value());
149+
150+
std::cout << "IDL::EdgeAttribs[" << edge_rt->type() << ", from:" << std::to_string(edge_rt->from()) << "-> to:" << std::to_string(edge_rt->to())
151+
<< " Attribs:[";
152+
for (const auto &v : edge_rt->attrs())
153+
std::cout << v.first << ":" << v.second << " - \n";
154+
std::cout << "]]\n";
155+
}

0 commit comments

Comments
 (0)