From e7d6aafd3c9353d9e6169ca775cf1dae618238cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 17 Jun 2023 21:58:32 +0200 Subject: Storage: iterable type for record item list --- include/erebos/storage.h | 187 ++++++++++++++++++++++++++++++++++------------- src/attach.cpp | 7 +- src/channel.cpp | 7 +- src/contact.cpp | 14 +--- src/identity.cpp | 7 +- src/message.cpp | 7 +- src/pubkey.h | 7 +- src/set.cpp | 14 +--- src/state.cpp | 14 +--- src/storage.cpp | 165 +++++++++++++++++++++++++++++++++-------- 10 files changed, 284 insertions(+), 145 deletions(-) diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 3a3073d..32049db 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -40,8 +40,12 @@ template class Head; using std::bind; using std::call_once; using std::make_unique; +using std::monostate; using std::move; +using std::optional; +using std::shared_ptr; using std::string; +using std::variant; using std::vector; class PartialStorage @@ -222,54 +226,8 @@ template class RecordT { public: - class Item { - public: - struct UnknownType - { - std::string type; - std::string value; - }; - - struct Empty {}; - - typedef std::variant< - std::monostate, - Empty, - int, - std::string, - std::vector, - ZonedTime, - UUID, - typename S::Ref, - UnknownType> Variant; - - Item(const std::string & name): - Item(name, std::monostate()) {} - Item(const std::string & name, Variant value): - name(name), value(value) {} - template - Item(const std::string & name, const Stored & value): - Item(name, value.ref()) {} - - Item(const Item &) = default; - Item & operator=(const Item &) = delete; - - operator bool() const; - - std::optional asEmpty() const; - std::optional asInteger() const; - std::optional asText() const; - std::optional> asBinary() const; - std::optional asDate() const; - std::optional asUUID() const; - std::optional asRef() const; - std::optional asUnknown() const; - - template std::optional> as() const; - - const std::string name; - const Variant value; - }; + class Item; + class Items; private: RecordT(const std::shared_ptr> & ptr): @@ -280,10 +238,10 @@ public: RecordT(std::vector &&); std::vector encode() const; - const std::vector & items() const; + Items items() const; Item item(const std::string & name) const; Item operator[](const std::string & name) const; - std::vector items(const std::string & name) const; + Items items(const std::string & name) const; private: friend ObjectT; @@ -295,6 +253,123 @@ private: const std::shared_ptr> ptr; }; +template +class RecordT::Item +{ +public: + struct UnknownType + { + string type; + string value; + }; + + struct Empty {}; + + using Integer = int; + using Text = string; + using Binary = vector; + using Date = ZonedTime; + using UUID = erebos::UUID; + using Ref = typename S::Ref; + + using Variant = variant< + monostate, + Empty, + int, + string, + vector, + ZonedTime, + UUID, + typename S::Ref, + UnknownType>; + + Item(const string & name): + Item(name, monostate()) {} + Item(const string & name, Variant value): + name(name), value(value) {} + template + Item(const string & name, const Stored & value): + Item(name, value.ref()) {} + + Item(const Item &) = default; + Item & operator=(const Item &) = delete; + + operator bool() const; + + optional asEmpty() const; + optional asInteger() const; + optional asText() const; + optional asBinary() const; + optional asDate() const; + optional asUUID() const; + optional asRef() const; + optional asUnknown() const; + + template optional> as() const; + + const string name; + const Variant value; +}; + +template +class RecordT::Items +{ +public: + using Empty = typename Item::Empty; + using Integer = typename Item::Integer; + using Text = typename Item::Text; + using Binary = typename Item::Binary; + using Date = typename Item::Date; + using UUID = typename Item::UUID; + using Ref = typename Item::Ref; + using UnknownType = typename Item::UnknownType; + + Items(shared_ptr> items); + Items(shared_ptr> items, string filter); + + class Iterator + { + Iterator(const Items & source, size_t idx); + friend Items; + public: + using iterator_category = std::forward_iterator_tag; + using value_type = Item; + using difference_type = ssize_t; + using pointer = const Item *; + using reference = const Item &; + + Iterator(const Iterator &) = default; + ~Iterator() = default; + Iterator & operator=(const Iterator &) = default; + Iterator & operator++(); + value_type operator*() const { return (*source.items)[idx]; } + bool operator==(const Iterator & other) const { return idx == other.idx; } + bool operator!=(const Iterator & other) const { return idx != other.idx; } + + private: + const Items & source; + size_t idx; + }; + + Iterator begin() const; + Iterator end() const; + + vector asEmpty() const; + vector asInteger() const; + vector asText() const; + vector asBinary() const; + vector asDate() const; + vector asUUID() const; + vector asRef() const; + vector asUnknown() const; + + template vector> as() const; + +private: + const shared_ptr> items; + const optional filter; +}; + extern template class RecordT; extern template class RecordT; @@ -367,6 +442,18 @@ std::optional> RecordT::Item::as() const return std::nullopt; } +template +template +vector> RecordT::Items::as() const +{ + auto refs = asRef(); + vector> res; + res.reserve(refs.size()); + for (const auto & ref : refs) + res.push_back(Stored::load(ref)); + return res; +} + class Generation { public: diff --git a/src/attach.cpp b/src/attach.cpp index 4bf06b1..74bc875 100644 --- a/src/attach.cpp +++ b/src/attach.cpp @@ -105,14 +105,9 @@ AttachIdentity AttachIdentity::load(const Ref & ref) .keys = {}, }; - vector> keys; - for (auto s : rec->items("skey")) - if (const auto & b = s.asBinary()) - keys.push_back(*b); - return AttachIdentity { .identity = *rec->item("identity").as>(), - .keys = keys, + .keys = rec->items("skey").asBinary(), }; } diff --git a/src/channel.cpp b/src/channel.cpp index 08db911..b317f3d 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -26,15 +26,10 @@ Ref ChannelRequestData::store(const Storage & st) const ChannelRequestData ChannelRequestData::load(const Ref & ref) { if (auto rec = ref->asRecord()) { - remove_const::type peers; - for (const auto & i : rec->items("peer")) - if (auto p = i.as>()) - peers.push_back(*p); - if (rec->item("enc").asText() == "aes-128-gcm") if (auto key = rec->item("key").as()) return ChannelRequestData { - .peers = std::move(peers), + .peers = rec->items("peer").as>(), .key = *key, }; } diff --git a/src/contact.cpp b/src/contact.cpp index 0050f95..01aa710 100644 --- a/src/contact.cpp +++ b/src/contact.cpp @@ -111,19 +111,9 @@ ContactData ContactData::load(const Ref & ref) if (!rec) return ContactData(); - vector> prev; - for (const auto & x : rec->items("PREV")) - if (const auto & p = x.as()) - prev.push_back(*p); - - vector>> identity; - for (const auto & x : rec->items("identity")) - if (const auto & i = x.asRef()) - identity.push_back(*i); - return ContactData { - .prev = std::move(prev), - .identity = std::move(identity), + .prev = rec->items("PREV").as(), + .identity = rec->items("identity").as>(), .name = rec->item("name").asText(), }; } diff --git a/src/identity.cpp b/src/identity.cpp index 9077db2..3b8d3bf 100644 --- a/src/identity.cpp +++ b/src/identity.cpp @@ -239,14 +239,9 @@ void Identity::Builder::owner(const Identity & val) IdentityData IdentityData::load(const Ref & ref) { if (auto rec = ref->asRecord()) { - vector>> prev; - for (auto p : rec->items("SPREV")) - if (const auto & x = p.as>()) - prev.push_back(x.value()); - if (auto keyIdentity = rec->item("key-id").as()) return IdentityData { - .prev = std::move(prev), + .prev = rec->items("SPREV").as>(), .name = rec->item("name").asText(), .owner = rec->item("owner").as>(), .keyIdentity = keyIdentity.value(), diff --git a/src/message.cpp b/src/message.cpp index ff8e05b..06ee8ad 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -55,15 +55,10 @@ DirectMessageData DirectMessageData::load(const Ref & ref) if (!rec) return DirectMessageData(); - vector> prev; - for (auto p : rec->items("PREV")) - if (const auto & x = p.as()) - prev.push_back(*x); - auto fref = rec->item("from").asRef(); return DirectMessageData { - .prev = std::move(prev), + .prev = rec->items("PREV").as(), .from = fref ? Identity::load(*fref) : nullopt, .time = *rec->item("time").asDate(), .text = rec->item("text").asText().value(), diff --git a/src/pubkey.h b/src/pubkey.h index 5cb693b..ca662ba 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -110,10 +110,9 @@ Signed Signed::load(const Ref & ref) if (auto rec = ref->asRecord()) if (auto data = rec->item("SDATA").as()) { vector> sigs; - for (auto item : rec->items("sig")) - if (auto sig = item.as()) - if (sig.value()->verify(data.value().ref())) - sigs.push_back(sig.value()); + for (const auto & sig : rec->items("sig").as()) + if (sig->verify(data.value().ref())) + sigs.push_back(sig); return Signed(*data, sigs); } diff --git a/src/set.cpp b/src/set.cpp index d224af3..ce343d8 100644 --- a/src/set.cpp +++ b/src/set.cpp @@ -153,19 +153,9 @@ vector SetBase::store() const SetItem SetItem::load(const Ref & ref) { if (auto rec = ref->asRecord()) { - vector> prev; - for (auto p : rec->items("PREV")) - if (const auto & x = p.as()) - prev.push_back(*x); - - vector item; - for (auto i : rec->items("item")) - if (const auto & x = i.asRef()) - item.push_back(*x); - return SetItem { - .prev = std::move(prev), - .item = std::move(item), + .prev = rec->items("PREV").as(), + .item = rec->items("item").asRef(), }; } diff --git a/src/state.cpp b/src/state.cpp index 6ad9f89..8e5dcad 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -22,9 +22,7 @@ LocalState::LocalState(const Ref & ref): if (auto x = rec->item("id").asRef()) p->identity = Identity::load(*x); - for (auto i : rec->items("shared")) - if (const auto & x = i.as()) - p->shared.tip.push_back(*x); + p->shared.tip = rec->items("shared").as(); if (p->identity) { vector>> updates; @@ -158,16 +156,10 @@ SharedData::SharedData(const Ref & ref) if (!rec) return; - for (auto i : rec->items("PREV")) - if (const auto & x = i.as()) - prev.push_back(*x); - + prev = rec->items("PREV").as(); 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); + value = rec->items("value").asRef(); } Ref SharedData::store(const Storage & st) const diff --git a/src/storage.cpp b/src/storage.cpp index fb3698c..4e56b2e 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -789,10 +789,9 @@ optional Storage::Priv::copy(const ObjectT & pobj, vector * m { bool fail = false; if (auto rec = pobj.asRecord()) - for (const auto & item : rec->items()) - if (auto r = item.asRef()) - if (!copy(*r, missing)) - fail = true; + for (const auto & r : rec->items().asRef()) + if (!copy(r, missing)) + fail = true; if (fail) return nullopt; @@ -1032,24 +1031,13 @@ vector Ref::previous() const if (!rec) return {}; - vector res; - - auto sdata = rec->item("SDATA").asRef(); - if (sdata) { - auto drec = sdata.value()->asRecord(); - if (!drec) - return {}; - - for (const Record::Item & i : drec->items("SPREV")) - if (auto x = i.asRef()) - res.push_back(*x); - return res; + if (auto sdata = rec->item("SDATA").asRef()) { + if (auto drec = sdata.value()->asRecord()) + return drec->items("SPREV").asRef(); + return {}; } - for (const Record::Item & i : rec->items("PREV")) - if (auto x = i.asRef()) - res.push_back(*x); - return res; + return rec->items("PREV").asRef(); } Generation Ref::generation() const @@ -1177,6 +1165,125 @@ optional::Item::UnknownType> RecordT::Item::asUnknown() c return nullopt; } +template +RecordT::Items::Items(shared_ptr> items): + items(move(items)), filter(nullopt) +{} + +template +RecordT::Items::Items(shared_ptr> items, string filter): + items(move(items)), filter(move(filter)) +{} + +template +RecordT::Items::Iterator::Iterator(const Items & source, size_t idx): + source(source), idx(idx) +{} + +template +typename RecordT::Items::Iterator & RecordT::Items::Iterator::operator++() +{ + const auto & items = *source.items; + do { + idx++; + } while (idx < items.size() && + source.filter && + items[idx].name != *source.filter); + return *this; +} + +template +typename RecordT::Items::Iterator RecordT::Items::begin() const +{ + return ++Iterator(*this, -1); +} + +template +typename RecordT::Items::Iterator RecordT::Items::end() const +{ + return Iterator(*this, items->size()); +} + +template +vector::Item::Empty> RecordT::Items::asEmpty() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + +template +vector::Item::Integer> RecordT::Items::asInteger() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + +template +vector::Item::Text> RecordT::Items::asText() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + +template +vector::Item::Binary> RecordT::Items::asBinary() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + +template +vector::Item::Date> RecordT::Items::asDate() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + +template +vector::Item::UUID> RecordT::Items::asUUID() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + +template +vector::Item::Ref> RecordT::Items::asRef() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + +template +vector::Item::UnknownType> RecordT::Items::asUnknown() const +{ + vector res; + for (const auto & item : *this) + if (holds_alternative(item.value)) + res.push_back(std::get(item.value)); + return res; +} + template RecordT::RecordT(const vector & from): @@ -1274,9 +1381,9 @@ vector RecordT::encode() const } template -const vector::Item> & RecordT::items() const +typename RecordT::Items RecordT::items() const { - return *ptr; + return Items(ptr); } template @@ -1296,14 +1403,9 @@ typename RecordT::Item RecordT::operator[](const string & name) const } template -vector::Item> RecordT::items(const string & name) const +typename RecordT::Items RecordT::items(const string & name) const { - vector res; - for (auto item : *ptr) { - if (item.name == name) - res.push_back(item); - } - return res; + return Items(ptr, name); } template @@ -1544,9 +1646,8 @@ vector> erebos::collectStoredObjects(const Stored & from) res.push_back(cur); if (auto rec = cur->asRecord()) - for (const auto & item : rec->items()) - if (auto ref = item.asRef()) - queue.push_back(Stored::load(*ref)); + for (const auto & ref : rec->items().asRef()) + queue.push_back(Stored::load(ref)); } return res; -- cgit v1.2.3