summaryrefslogtreecommitdiff
path: root/src/state.cpp
blob: 6ad9f89ac616fa24c3da9b33118b00edc7ae15ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "state.h"

#include "identity.h"

using namespace erebos;

using std::make_shared;

const UUID LocalState::headTypeId { "1d7491a9-7bcb-4eaa-8f13-c8c4c4087e4e" };

LocalState::LocalState():
	p(make_shared<Priv>())
{}

LocalState::LocalState(const Ref & ref):
	LocalState()
{
	auto rec = ref->asRecord();
	if (!rec)
		return;

	if (auto x = rec->item("id").asRef())
		p->identity = Identity::load(*x);

	for (auto i : rec->items("shared"))
		if (const auto & x = i.as<SharedData>())
			p->shared.tip.push_back(*x);

	if (p->identity) {
		vector<Stored<Signed<IdentityData>>> updates;
		for (const auto & r : lookupShared(SharedType<optional<Identity>>::id))
			updates.push_back(Stored<Signed<IdentityData>>::load(r));
		if (!updates.empty())
			p->identity = p->identity->update(updates);
	}
}

Ref LocalState::store(const Storage & st) const
{
	vector<Record::Item> items;

	if (p->identity)
		items.emplace_back("id", *p->identity->ref());
	for (const auto & x : p->shared.tip)
		items.emplace_back("shared", x);

	return st.storeObject(Record(std::move(items)));
}

const optional<Identity> & LocalState::identity() const
{
	return p->identity;
}

LocalState LocalState::identity(const Identity & id) const
{
	LocalState ret;
	ret.p->identity = id;
	ret.p->shared = p->shared;
	return ret;
}

vector<Ref> LocalState::lookupShared(UUID type) const
{
	return p->shared.lookup(type);
}

vector<Ref> SharedState::lookup(UUID type) const
{
	return p->lookup(type);
}

vector<Ref> SharedState::Priv::lookup(UUID type) const
{
	vector<Stored<SharedData>> found;
	vector<Stored<SharedData>> process = tip;

	while (!process.empty()) {
		auto cur = std::move(process.back());
		process.pop_back();

		if (cur->type == type) {
			found.push_back(std::move(cur));
			continue;
		}

		for (const auto & x : cur->prev)
			process.push_back(x);
	}

	filterAncestors(found);
	vector<Ref> res;
	for (const auto & s : found)
		for (const auto & v : s->value)
			res.push_back(v);
	return res;
}

vector<Ref> LocalState::sharedRefs() const
{
	vector<Ref> refs;
	for (const auto & x : p->shared.tip)
		refs.push_back(x.ref());
	return refs;
}

LocalState LocalState::sharedRefAdd(const Ref & ref) const
{
	const Storage * st;
	if (p->shared.tip.size() > 0)
		st = &p->shared.tip[0].ref().storage();
	else if (p->identity)
		st = &p->identity->ref()->storage();
	else
		st = &ref.storage();

	LocalState ret;
	ret.p->identity = p->identity;
	ret.p->shared = p->shared;
	ret.p->shared.tip.push_back(SharedData(ref).store(*st));
	filterAncestors(ret.p->shared.tip);
	return ret;
}

LocalState LocalState::updateShared(UUID type, const vector<Ref> & xs) const
{
	const Storage * st;
	if (p->shared.tip.size() > 0)
		st = &p->shared.tip[0].ref().storage();
	else if (p->identity)
		st = &p->identity->ref()->storage();
	else if (xs.size() > 0)
		st = &xs[0].storage();
	else
		return *this;

	LocalState ret;
	ret.p->identity = p->identity;
	ret.p->shared.tip.push_back(SharedData(p->shared.tip, type, xs).store(*st));
	return ret;
}


bool SharedState::operator==(const SharedState & other) const
{
	return p->tip == other.p->tip;
}

bool SharedState::operator!=(const SharedState & other) const
{
	return p->tip != other.p->tip;
}


SharedData::SharedData(const Ref & ref)
{
	auto rec = ref->asRecord();
	if (!rec)
		return;

	for (auto i : rec->items("PREV"))
		if (const auto & x = i.as<SharedData>())
			prev.push_back(*x);

	if (auto x = rec->item("type").asUUID())
		type = *x;

	for (auto i : rec->items("value"))
		if (const auto & x = i.asRef())
			value.push_back(*x);
}

Ref SharedData::store(const Storage & st) const
{
	vector<Record::Item> items;

	for (const auto & x : prev)
		items.emplace_back("PREV", x);
	items.emplace_back("type", type);
	for (const auto & x : value)
		items.emplace_back("value", x);

	return st.storeObject(Record(std::move(items)));
}

template<>
optional<Identity> LocalState::lens<optional<Identity>>(const LocalState & x)
{
	return x.identity();
}

template<>
vector<Ref> LocalState::lens<vector<Ref>>(const LocalState & x)
{
	return x.sharedRefs();
}

template<>
SharedState LocalState::lens<SharedState>(const LocalState & x)
{
	return SharedState(shared_ptr<SharedState::Priv>(x.p, &x.p->shared));
}