From 7956f31c018610ace687ed1e3b48cfb50b0fb15c Mon Sep 17 00:00:00 2001 From: felix Date: Thu, 2 Mar 2023 21:49:55 +0100 Subject: [PATCH 01/11] [chore] update doxygen docs --- .github/workflows/testing_debug_clang.yaml | 7 +- docs/html/TySON_8hpp_source.html | 16 +- docs/html/annotated.html | 6 +- docs/html/classannadb_1_1AnnaDB-members.html | 6 +- docs/html/classannadb_1_1AnnaDB.html | 6 +- docs/html/classannadb_1_1Data-members.html | 6 +- docs/html/classannadb_1_1Data.html | 6 +- docs/html/classannadb_1_1Journal-members.html | 6 +- docs/html/classannadb_1_1Journal.html | 6 +- docs/html/classannadb_1_1Meta-members.html | 6 +- docs/html/classannadb_1_1Meta.html | 6 +- .../classannadb_1_1Query_1_1And-members.html | 6 +- docs/html/classannadb_1_1Query_1_1And.html | 6 +- .../classannadb_1_1Query_1_1Asc-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Asc.html | 6 +- ...annadb_1_1Query_1_1Comparison-members.html | 6 +- .../classannadb_1_1Query_1_1Comparison.html | 6 +- ...lassannadb_1_1Query_1_1Delete-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Delete.html | 6 +- .../classannadb_1_1Query_1_1Desc-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Desc.html | 6 +- .../classannadb_1_1Query_1_1Eq-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Eq.html | 6 +- .../classannadb_1_1Query_1_1Find-members.html | 32 +- docs/html/classannadb_1_1Query_1_1Find.html | 24 +- .../classannadb_1_1Query_1_1Get-members.html | 25 +- docs/html/classannadb_1_1Query_1_1Get.html | 97 +- .../classannadb_1_1Query_1_1Gt-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Gt.html | 6 +- .../classannadb_1_1Query_1_1Gte-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Gte.html | 6 +- ...lassannadb_1_1Query_1_1Insert-members.html | 25 +- docs/html/classannadb_1_1Query_1_1Insert.html | 85 +- ...classannadb_1_1Query_1_1Limit-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Limit.html | 6 +- .../classannadb_1_1Query_1_1Lt-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Lt.html | 6 +- .../classannadb_1_1Query_1_1Lte-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Lte.html | 6 +- .../classannadb_1_1Query_1_1Neq-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Neq.html | 6 +- .../classannadb_1_1Query_1_1Not-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Not.html | 6 +- ...lassannadb_1_1Query_1_1Offset-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Offset.html | 6 +- .../classannadb_1_1Query_1_1Or-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Or.html | 6 +- ...classannadb_1_1Query_1_1Query-members.html | 24 +- docs/html/classannadb_1_1Query_1_1Query.html | 186 ++- ...ssannadb_1_1Query_1_1QueryCmd-members.html | 6 +- .../classannadb_1_1Query_1_1QueryCmd.html | 6 +- .../classannadb_1_1Query_1_1Sort-members.html | 10 +- docs/html/classannadb_1_1Query_1_1Sort.html | 18 +- ...lassannadb_1_1Query_1_1Update-members.html | 6 +- docs/html/classannadb_1_1Query_1_1Update.html | 6 +- docs/html/classes.html | 6 +- ...yson_1_1TySonCollectionObject-members.html | 6 +- .../classtyson_1_1TySonCollectionObject.html | 6 +- .../classtyson_1_1TySonObject-members.html | 8 +- docs/html/classtyson_1_1TySonObject.html | 12 +- docs/html/connection_8hpp_source.html | 8 +- .../dir_208d2690607c4e3d3fabfa1d18673d36.html | 6 +- .../dir_68267d1309a1af8e8297ef4c3efbcdba.html | 6 +- docs/html/doxygen.css | 2 +- docs/html/files.html | 6 +- docs/html/functions.html | 14 +- docs/html/functions_func.html | 16 +- docs/html/hierarchy.html | 6 +- docs/html/index.html | 6 +- docs/html/md_pages_create_connection.html | 6 +- docs/html/md_pages_main.html | 14 +- docs/html/namespaceannadb_1_1Query.html | 6 +- docs/html/namespacemembers.html | 6 +- docs/html/namespacemembers_enum.html | 8 +- docs/html/namespaces.html | 6 +- docs/html/pages.html | 9 +- docs/html/query_8hpp_source.html | 1252 +++++++++-------- docs/html/query__comparision_8hpp_source.html | 6 +- docs/html/search/all_11.js | 2 +- docs/html/search/all_2.js | 3 +- docs/html/search/all_3.js | 9 +- docs/html/search/all_6.js | 12 +- docs/html/search/all_7.js | 3 +- docs/html/search/all_a.js | 2 +- docs/html/search/all_d.js | 2 +- docs/html/search/all_f.js | 5 +- docs/html/search/functions_10.js | 2 +- docs/html/search/functions_3.js | 4 +- docs/html/search/functions_6.js | 7 +- docs/html/search/functions_7.js | 3 +- docs/html/search/functions_9.js | 2 +- docs/html/search/functions_c.js | 2 +- docs/html/search/functions_e.js | 3 +- docs/html/search/pages_1.js | 3 +- docs/html/search/search.js | 6 +- docs/html/structannadb_1_1KeyVal-members.html | 6 +- docs/html/structannadb_1_1KeyVal.html | 6 +- ...uctannadb_1_1Query_1_1SortCmd-members.html | 6 +- .../structannadb_1_1Query_1_1SortCmd.html | 6 +- docs/html/utils_8hpp_source.html | 6 +- 100 files changed, 1407 insertions(+), 921 deletions(-) diff --git a/.github/workflows/testing_debug_clang.yaml b/.github/workflows/testing_debug_clang.yaml index a650a44..361968e 100644 --- a/.github/workflows/testing_debug_clang.yaml +++ b/.github/workflows/testing_debug_clang.yaml @@ -27,6 +27,7 @@ jobs: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug .. make -j2 - - name: run test - working-directory: ./src/build - run: ./annadb_driver +# currently broken +# - name: run test +# working-directory: ./src/build +# run: ./annadb_driver diff --git a/docs/html/TySON_8hpp_source.html b/docs/html/TySON_8hpp_source.html index 325b35e..26abb76 100644 --- a/docs/html/TySON_8hpp_source.html +++ b/docs/html/TySON_8hpp_source.html @@ -3,7 +3,7 @@ - + AnnaDB: src/TySON.hpp Source File @@ -29,7 +29,7 @@ - + + + + + + + + +
+
+ + + + + + +
+
AnnaDB 1.0 +
+
+
+ + + + + + + + +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+
+
Create a AnnaDB query
+
+
+

+The insert query

+
    +
  • Can only be used as a single statement
    {c++}
    +
    #include "query.hpp"
    +
    +
    // open a connection
    +
    annadb::AnnaDB con {"jondoe", "passwd1234", "localhost", 10001};
    +
    con.connect();
    +
    +
    // create a query object with the name of the collection you want to modify
    +
    auto query = annadb::Query::Query(<collection_name>);
    +
    +
    // create a TySON Object(s) you want to insert
    +
    auto new_num = tyson::TySonObject::Number(10);
    +
    +
    // pass the TySON Object(s) to the `insert` command
    +
    query.insert(new_num);
    +
    +
    // send the query
    +
    auto answer = con.send(query);
    +
  • +
  • with multiple values
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    auto query = annadb::Query::Query(<collection_name>);
    +
    +
    // create the TySON Objects
    +
    auto val_1 = tyson::TySonObject::Number(10);
    +
    auto val_2 = tyson::TySonObject::String("fizzbuzz");
    +
    auto val_3 = tyson::TySonObject::Bool(false);
    +
    +
    // pass the TySON Objects to the insert statement
    +
    query.insert(val_1, val_2, val_3);
    +
    +
    ...
    +
  • +
+

+The get query

+
    +
  • can be combined with find, get, sort, limit, offset, update, delete
  • +
  • can only be used with TySON Link Objects
  • +
  • can also be used with a std::vector or as ...T to send multiple links in one query
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    // create a TySON Link object
    +
    auto val_1 = tyson::TySonObject::Link("test", "b2279b93-00b3-4b44-9670-82a76922c0da");
    +
    +
    // use get command combined with limit
    +
    query.get(val_1).limit<short>(5);
    +
    +
    // send the query
    +
    auto answer = con.send(query);
    +
  • +
+

+The find query

+
    +
  • can be combined with find, get, sort, limit, offset
  • +
  • must be used with the Find class
  • +
  • needs to be called with any of
  • +
+ + + + + + + + + + + + + + + +
Static Version Alternative Meaning
EQ eq equals
NEQ neq not equals
GT gt greater
GTE gte greater than
LT le less
LTE lte less than
+
    +
  • multiple find statements can be combined with AND, OR, NOT
  • +
  • if you need to compare with a specific field inside of the collection pass the name of it to the constructor
      +
    • annadb::Query::Gt("num", min_num); will compare against the num field inside of the collection you could also write num.inside.a.nested.other.collection without you compare against the root node annadb::Query::Gt(min_num);
    • +
    +
  • +
+

Example Static Version from root

{c++}
+
#include "query.hpp"
+
...
+
+
// create a TySonObject object with which we want to compare
+
auto min_num = tyson::TySonObject::Number(5);
+
+
query.find(
+
// this is the static version
+
annadb::Query::Find::GT(min_num)
+
// the other comparison statements can be used in the same way
+
// annadb::Query::Find::EQ(min_num)
+
// annadb::Query::Find::NEQ(min_num)
+
// annadb::Query::Find::GTE(min_num)
+
// annadb::Query::Find::LT(min_num)
+
// annadb::Query::Find::LTE(min_num)
+
);
+
+
...
+

Example Static Version with field

{c++}
+
#include "query.hpp"
+
...
+
+
// create a TySonObject object with which we want to compare
+
auto min_num = tyson::TySonObject::Number(5);
+
+
query.find(
+
annadb::Query::Find::GT("my.field.num", min_num)
+
);
+
+
...
+

Example with instance of Find class

    +
  • this makes it easier to concat multiple finds which will not result in an And statement
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    // create a TySonObject object with which we want to compare
    +
    auto min_num = tyson::TySonObject::Number(5);
    +
    +
    // searching from the root
    +
    auto findQuery = annadb::Query::Find();
    +
    +
    // requires moving
    +
    query.find(std::move(
    +
    // can be concatenated
    +
    findQuery.gt(min_num).eq(min_num))
    +
    // if you want to compare with a specific field you can add this information
    +
    // findQuery.lt("my.num", min_num)
    +
    );
    +
    +
    ...
    +
  • +
+

Example with AND

{c++}
+
#include "query.hpp"
+
...
+
+
auto min_num = tyson::TySonObject::Number(5);
+
auto max_num = tyson::TySonObject::Number(50);
+
+
auto lower_bound = annadb::Query::Gt("num", min_num);
+
auto upper_bound = annadb::Query::Lte("num", max_num);
+
+
query.find(
+
annadb::Query::Find::AND(lower_bound, upper_bound)
+
);
+
+
...
+

Example with OR

{c++}
+
#include "query.hpp"
+
...
+
+
auto min_num = tyson::TySonObject::Number(5);
+
auto max_num = tyson::TySonObject::Number(50);
+
+
auto lower_bound = annadb::Query::Gt("num", min_num);
+
auto upper_bound = annadb::Query::Lte("num", max_num);
+
+
query.find(
+
annadb::Query::Find::OR(lower_bound, upper_bound)
+
);
+
+
...
+

Example with NOT

{c++}
+
#include "query.hpp"
+
...
+
+
query.find(
+
annadb::Query::Find::NOT("exclude.me")
+
);
+
+
...
+

+The sort query

+
    +
  • can not be used alone
  • +
  • must be used with the Sort class
  • +
  • possible choices are ASC, DESC both can be used static
  • +
  • one or more field names are required
  • +
+

single field

{c++}
+
#include "query.hpp"
+
...
+
+
auto min_num = tyson::TySonObject::Number(5);
+
+
query.find(annadb::Query::Find::GT(min_num))
+
.sort(annadb::Query::Sort::DESC("some.field"));
+
+
...
+

multiple fields

{c++}
+
#include "query.hpp"
+
...
+
+
auto min_num = tyson::TySonObject::Number(5);
+
+
query.find(annadb::Query::Find::GT(min_num))
+
.sort(annadb::Query::Sort::ASC("some", "fields", "to", "sort"));
+
+
...
+

+The limit query

+
    +
  • can not be used alone
  • +
  • needs an integral
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    auto min_num = tyson::TySonObject::Number(5);
    +
    +
    query.find(
    +
    annadb::Query::Find::GT("my.field.num", min_num).limit<short>(6)
    +
    );
    +
    +
    ...
    +
  • +
+

+The offset query

+
    +
  • can not be used alone
  • +
  • needs an integral
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    auto min_num = tyson::TySonObject::Number(5);
    +
    +
    query.find(
    +
    annadb::Query::Find::GT("my.field.num", min_num).offset<int>(260)
    +
    );
    +
    +
    ...
    +
  • +
+

+The update query

+
    +
  • can not be used alone and must be the last if you combine multiple statements
  • +
  • you can either use annadb::Query::UpdateType::Set to set a field to a specific value or annadb::Query::UpdateType::Inc to increase a field by a specific value
  • +
  • can only be used with an instance of tyson::TySonObject::Value
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    // create a TySON Link object
    +
    auto link = tyson::TySonObject::Link("test", "b2279b93-00b3-4b44-9670-82a76922c0da");
    +
    +
    // create a TySON value object
    +
    auto new_val = tyson::TySonObject::Value("num", // the name of the field
    +
    tyson::TySonObject::Number(100) // the new value
    +
    );
    +
    +
    query.get(link).update(annadb::Query::UpdateType::Set, // how you want to update
    +
    new_val // the value with which you want to update
    +
    );
    +
    +
    // use get command combined with limit
    +
    query.get(val_1).limit<short>(5);
    +
    +
    ...
    +
  • +
+

+The delete query

+
    +
  • can not be used alone and must be the last if you combine multiple statements
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    auto min_num = tyson::TySonObject::Number(5);
    +
    +
    auto query = annadb::Query::Query("test");
    +
    +
    // this query will delete all findings
    +
    query.find(annadb::Query::Find::GT(min_num)).delete_q();
    +
    +
    ...
    +
  • +
+
+
+ + + + diff --git a/docs/html/md_pages_result_meta_part.html b/docs/html/md_pages_result_meta_part.html new file mode 100644 index 0000000..8e1dce5 --- /dev/null +++ b/docs/html/md_pages_result_meta_part.html @@ -0,0 +1,151 @@ + + + + + + + +AnnaDB: Journal Meta + + + + + + + + + +
+
+ + + + + + +
+
AnnaDB 1.0 +
+
+
+ + + + + + + + +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+
+
Journal Meta
+
+
+
    +
  • Each result of a transaction will be an instance of the Journal class this class has two properties Meta and Data.
  • +
  • We will now look at the Meta property
    {c++}
    +
    #include "query.hpp"
    +
    +
    annadb::AnnaDB con {"jondoe", "passwd1234", "localhost", 10001};
    +
    con.connect();
    +
    +
    auto val_1 = tyson::TySonObject::Number(10);
    +
    auto val_2 = tyson::TySonObject::String("fizzbuzz");
    +
    auto val_3 = tyson::TySonObject::Bool(false);
    +
    +
    // pass the TySON Objects to the insert statement
    +
    query.insert(val_1, val_2, val_3);
    +
    +
    // create a transaction
    +
    std::optional<Journal> answer = con.send(query);
    +
    +
    if (answer)
    +
    {
    +
    // extract the Meta information
    +
    Meta meta_information = answer.value().meta();
    +
    }
    +
    +
    con.close();
    +
  • +
+

+Extract the type of the transaction from Meta

+
    +
  • the MetaType can be any of these
      +
    • MetaType::insert_meta
    • +
    • MetaType::get_meta
    • +
    • MetaType::find_meta
    • +
    • MetaType::update_meta
    • +
    +
  • +
  • this can be used to validate that your transaction/query did what you expected
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    Meta meta_information = answer.value().meta();
    +
    +
    if (meta_information.type() == MetaType::insert_meta)
    +
    {
    +
    ...
    +
    }
    +
    ...
    +
  • +
+

+Extract the number of rows which where affected

+
    +
  • you can either use the underlying TySON Map or directly the rows count
    {c++}
    +
    #include "query.hpp"
    +
    ...
    +
    +
    Meta meta_information = answer.value().meta();
    +
    +
    // you can extract the rows count wall
    +
    // ewith an integral
    +
    short affected_rows = meta_information.rows<short>();
    +
    +
    // or you can access the underlying TySON Map
    +
    tyson::TySonObject affected_rows = meta.data();
    +
    +
    ...
    +
  • +
+
+
+ + + + diff --git a/docs/html/search/pages_2.js b/docs/html/search/pages_2.js new file mode 100644 index 0000000..e85d6ff --- /dev/null +++ b/docs/html/search/pages_2.js @@ -0,0 +1,4 @@ +var searchData= +[ + ['journal_20meta_0',['Journal Meta',['../md_pages_result_meta_part.html',1,'']]] +]; From 42c41acef4b16cbf20c70d74cec93f1d48ab935d Mon Sep 17 00:00:00 2001 From: felix Date: Sun, 5 Mar 2023 22:02:18 +0100 Subject: [PATCH 06/11] [chore] improving TySON Map usage --- docs/html/TySON_8hpp_source.html | 1034 +++++++++-------- docs/html/classannadb_1_1Query_1_1And.html | 2 +- .../classannadb_1_1Query_1_1Comparison.html | 2 +- docs/html/classannadb_1_1Query_1_1Eq.html | 2 +- docs/html/classannadb_1_1Query_1_1Gt.html | 2 +- docs/html/classannadb_1_1Query_1_1Gte.html | 2 +- docs/html/classannadb_1_1Query_1_1Lt.html | 2 +- docs/html/classannadb_1_1Query_1_1Lte.html | 2 +- docs/html/classannadb_1_1Query_1_1Neq.html | 2 +- docs/html/classannadb_1_1Query_1_1Not.html | 2 +- docs/html/classannadb_1_1Query_1_1Or.html | 2 +- .../classtyson_1_1TySonObject-members.html | 26 +- docs/html/classtyson_1_1TySonObject.html | 209 +++- docs/html/connection_8hpp_source.html | 6 +- docs/html/functions.html | 4 + docs/html/functions_func.html | 4 + docs/html/md_pages_create_query.html | 2 +- docs/html/menudata.js | 3 +- docs/html/namespaceannadb_1_1Query.html | 25 +- docs/html/namespacemembers.html | 1 + docs/html/namespacemembers_enum.html | 1 + docs/html/query_8hpp_source.html | 10 +- docs/html/query__comparision_8hpp_source.html | 439 +++---- docs/html/search/all_12.js | 3 +- docs/html/search/all_2.js | 7 +- docs/html/search/all_3.js | 9 +- docs/html/search/all_4.js | 5 +- docs/html/search/all_7.js | 5 +- docs/html/search/all_b.js | 2 +- docs/html/search/all_d.js | 8 +- docs/html/search/enums_0.js | 2 +- docs/html/search/functions_11.js | 3 +- docs/html/search/functions_3.js | 4 +- docs/html/search/functions_4.js | 3 +- docs/html/search/functions_7.js | 4 +- docs/html/search/functions_a.js | 2 +- docs/html/search/functions_c.js | 6 +- docs/html/search/searchdata.js | 2 +- src/TySON.hpp | 137 ++- src/query_comparision.hpp | 4 + src/tests/test_tyson_parsing.cpp | 172 +-- 41 files changed, 1295 insertions(+), 867 deletions(-) diff --git a/docs/html/TySON_8hpp_source.html b/docs/html/TySON_8hpp_source.html index 00d2a38..286aacf 100644 --- a/docs/html/TySON_8hpp_source.html +++ b/docs/html/TySON_8hpp_source.html @@ -106,499 +106,559 @@
31 IDs = 'z',
32 Value = 'e'
33 };
-
34
- -
36 {
-
37 std::vector<TySonObject> vector_{};
-
38 std::map<TySonObject, TySonObject> map_{};
-
39 std::pair<std::string, std::string> link_{};
-
40 TySonType type_;
-
41 std::string value_;
-
42
-
51 void parse_vector_elements(std::string_view object)
-
52 {
-
53 const auto to_tyson_object = [](std::string &val) -> TySonObject
-
54 {
-
55 return TySonObject{val};
-
56 };
-
57
-
58 auto end_type_sep = object.find_first_of('[') + 1;
-
59 auto end_value_sep = (object.size() - 1) - end_type_sep;
-
60
-
61 auto vector_data = object.substr(end_type_sep, end_value_sep);
-
62
-
63 auto vec_data = utils::split(vector_data, ',');
-
64 std::transform(vec_data.begin(), vec_data.end(), std::back_inserter(vector_), to_tyson_object);
-
65 }
-
66
-
76 std::vector<TySonObject> parse_map_element(std::string_view object)
-
77 {
-
78 const auto to_tyson_object = [](std::string &val) -> TySonObject
-
79 {
-
80 return TySonObject{val};
-
81 };
-
82
-
83 auto vec_data = utils::split(object, ':');
-
84
-
85 std::vector<TySonObject> result;
-
86 result.reserve(vec_data.size());
-
87
-
88 std::transform(vec_data.begin(), vec_data.end(), std::back_inserter(result), to_tyson_object);
-
89 return result;
-
90 }
-
91
-
100 void parse_map_elements(std::string_view object)
-
101 {
-
102 auto end_type_sep = object.find_first_of('{') + 1;
+
34
+
40 template<std::convertible_to<TySonType> Obj>
+
41 std::string_view TySonType_repr(Obj &&obj)
+
42 {
+
43 switch (obj)
+
44 {
+
45 case TySonType::Number:
+
46 return "TySON-Number";
+
47 case TySonType::String:
+
48 return "TySON-String";
+
49 case TySonType::Bool:
+
50 return "TySON-Bool";
+
51 case TySonType::Null:
+
52 return "TySON-Null";
+
53 case TySonType::Timestamp:
+
54 return "TySON-Timestamp";
+
55 case TySonType::Link:
+
56 return "TySON-Link";
+
57 case TySonType::Vector:
+
58 return "TySON-Vector";
+
59 case TySonType::Map:
+
60 return "TySON-Map";
+
61 case TySonType::Object:
+
62 return "TySON-Object";
+
63 case TySonType::ID:
+
64 return "TySON-ID";
+
65 case TySonType::Objects:
+
66 return "TySON-Objects";
+
67 case TySonType::IDs:
+
68 return "TySON-IDs";
+
69 case TySonType::Value:
+
70 return "TySON-Value";
+
71 }
+
72 return "";
+
73 }
+
74
+ +
80 {
+
81 std::vector<TySonObject> vector_{};
+
82 std::map<TySonObject, TySonObject> map_{};
+
83 std::pair<std::string, std::string> link_{};
+
84 TySonType type_;
+
85 std::string value_;
+
86
+
95 void parse_vector_elements(std::string_view object)
+
96 {
+
97 const auto to_tyson_object = [](std::string &val) -> TySonObject
+
98 {
+
99 return TySonObject{val};
+
100 };
+
101
+
102 auto end_type_sep = object.find_first_of('[') + 1;
103 auto end_value_sep = (object.size() - 1) - end_type_sep;
-
104 auto map_data = object.substr(end_type_sep, end_value_sep);
-
105
-
106 auto vec_data = utils::split(map_data, ',');
-
107
-
108 auto to_map_element = [this](auto &val) -> std::vector<TySonObject>
-
109 {
-
110 return parse_map_element(val);
-
111 };
-
112
-
113 std::vector<std::vector<TySonObject>> result;
-
114 std::transform(vec_data.begin(), vec_data.end(), std::back_inserter(result), to_map_element);
-
115 std::for_each(result.begin(), result.end(), [this](auto key_val)
-
116 {
-
117 map_.try_emplace(key_val[0], key_val[1]);
-
118 });
-
119 }
-
120
-
121 friend std::ostream& operator<<(std::ostream &out, TySonObject const &obj)
-
122 {
-
123 switch (obj.type())
-
124 {
-
125 case TySonType::Number:
-
126 return out << "n|" << obj.value_ << "|";
-
127 case TySonType::String:
-
128 return out << "s|" << obj.value_ << "|";
-
129 case TySonType::Bool:
-
130 return out << "b|" << obj.value_ << "|";
-
131 case TySonType::Null:
-
132 return out << "null";
-
133 case TySonType::Timestamp:
-
134 return out << "utc|" << obj.value_ << "|";
-
135 case TySonType::Link:
-
136 return out << std::get<0>(obj.link_) << "|" << std::get<1>(obj.link_) << "|";
-
137 case TySonType::Value:
-
138 return out << "value|" << obj.map_.begin()->first.value_ << "|:" << obj.map_.begin()->second;
-
139 case TySonType::Vector:
-
140 {
-
141 std::stringstream sstream;
-
142 std::for_each(obj.vector_.begin(),
-
143 obj.vector_.end(),
-
144 [&sstream](const auto &val){ sstream << val << ","; });
-
145
-
146 return out << "v[" << sstream.str() << "]";
-
147 }
-
148 case TySonType::Map:
-
149 {
-
150 std::stringstream sstream;
-
151 std::for_each(obj.map_.begin(),
-
152 obj.map_.end(),
-
153 [&sstream](const std::pair<TySonObject, TySonObject> &val)
-
154 {
-
155 sstream << val.first << ":" << val.second << ",";
-
156 });
-
157
-
158 return out << "m{" << sstream.str() << "}";
-
159 }
-
160 default:
-
161 return out << "";
-
162 }
+
104
+
105 auto vector_data = object.substr(end_type_sep, end_value_sep);
+
106
+
107 auto vec_data = utils::split(vector_data, ',');
+
108 std::transform(vec_data.begin(), vec_data.end(), std::back_inserter(vector_), to_tyson_object);
+
109 }
+
110
+
120 [[ nodiscard ]] std::vector<TySonObject> parse_map_element(std::string_view object)
+
121 {
+
122 const auto to_tyson_object = [](std::string &val) -> TySonObject
+
123 {
+
124 return TySonObject{val};
+
125 };
+
126
+
127 auto vec_data = utils::split(object, ':');
+
128
+
129 std::vector<TySonObject> result;
+
130 result.reserve(vec_data.size());
+
131
+
132 std::transform(vec_data.begin(), vec_data.end(), std::back_inserter(result), to_tyson_object);
+
133 return result;
+
134 }
+
135
+
144 void parse_map_elements(std::string_view object)
+
145 {
+
146 auto end_type_sep = object.find_first_of('{') + 1;
+
147 auto end_value_sep = (object.size() - 1) - end_type_sep;
+
148 auto map_data = object.substr(end_type_sep, end_value_sep);
+
149
+
150 auto vec_data = utils::split(map_data, ',');
+
151
+
152 auto to_map_element = [this](auto &val) -> std::vector<TySonObject>
+
153 {
+
154 return parse_map_element(val);
+
155 };
+
156
+
157 std::vector<std::vector<TySonObject>> result;
+
158 std::transform(vec_data.begin(), vec_data.end(), std::back_inserter(result), to_map_element);
+
159 std::for_each(result.begin(), result.end(), [this](auto key_val)
+
160 {
+
161 map_.try_emplace(key_val[0], key_val[1]);
+
162 });
163 }
164
-
165 public:
-
166
-
167 TySonObject() : type_(tyson::TySonType::Null) {};
-
168 ~TySonObject() = default;
-
169
-
176 explicit TySonObject(std::string_view object)
-
177 {
-
178 auto end_type_sep = object.find_first_of('|');
-
179 auto end_value_sep = (object.size() - 1) - (end_type_sep + 1);
-
180 auto type = object.substr(0, end_type_sep);
-
181
-
182 if (object == "null")
-
183 {
-
184 type_ = TySonType::Null;
-
185 value_ = {};
-
186 return;
-
187 }
-
188
-
189 if (object.starts_with("uts"))
-
190 {
-
191 type_ = TySonType::Timestamp;
-
192 value_ = object.substr(end_type_sep + 1, end_value_sep);
-
193 }
-
194 else if (object.starts_with('v'))
-
195 {
-
196 type_ = TySonType::Vector;
-
197
-
198 parse_vector_elements(object);
-
199 }
-
200 else if (object.starts_with('m'))
-
201 {
-
202 type_ = TySonType::Map;
-
203 parse_map_elements(object);
-
204 }
-
205 else if (type.size() > 1)
-
206 {
-
207 type_ = TySonType::Link;
-
208 auto val = object.substr(end_type_sep + 1, end_value_sep);
-
209 link_ = std::make_pair(type, val);
+
169 friend std::ostream& operator<<(std::ostream &out, TySonObject const &obj)
+
170 {
+
171 switch (obj.type())
+
172 {
+
173 case TySonType::Number:
+
174 return out << "n|" << obj.value_ << "|";
+
175 case TySonType::String:
+
176 return out << "s|" << obj.value_ << "|";
+
177 case TySonType::Bool:
+
178 return out << "b|" << obj.value_ << "|";
+
179 case TySonType::Null:
+
180 return out << "null";
+
181 case TySonType::Timestamp:
+
182 return out << "utc|" << obj.value_ << "|";
+
183 case TySonType::Link:
+
184 return out << std::get<0>(obj.link_) << "|" << std::get<1>(obj.link_) << "|";
+
185 case TySonType::Value:
+
186 return out << "value|" << obj.map_.begin()->first.value_ << "|:" << obj.map_.begin()->second;
+
187 case TySonType::Vector:
+
188 {
+
189 std::stringstream sstream;
+
190 std::for_each(obj.vector_.begin(),
+
191 obj.vector_.end(),
+
192 [&sstream](const auto &val){ sstream << val << ","; });
+
193
+
194 return out << "v[" << sstream.str() << "]";
+
195 }
+
196 case TySonType::Map:
+
197 {
+
198 std::stringstream sstream;
+
199 std::for_each(obj.map_.begin(),
+
200 obj.map_.end(),
+
201 [&sstream](const std::pair<TySonObject, TySonObject> &val)
+
202 {
+
203 sstream << val.first << ":" << val.second << ",";
+
204 });
+
205
+
206 return out << "m{" << sstream.str() << "}";
+
207 }
+
208 default:
+
209 return out << "";
210 }
-
211 else
-
212 {
-
213 type_ = static_cast<TySonType>(type.at(0));
-
214 value_ = object.substr(end_type_sep + 1, end_value_sep);
-
215 }
-
216 }
+
211 }
+
212
+
213 public:
+
214
+
215 TySonObject() : type_(tyson::TySonType::Null) {};
+
216 ~TySonObject() = default;
217
-
218 bool operator==(const TySonObject &rhs) const
-
219 {
-
220 return std::tie(this->type_, this->value_, this->vector_, this->map_, this->link_) ==
-
221 std::tie(rhs.type_, rhs.value_, rhs.vector_, rhs.map_, rhs.link_);
-
222 }
-
223
-
224 bool operator<(const TySonObject &rhs) const
-
225 {
-
226 return std::tie(this->type_, this->value_, this->vector_) < std::tie(rhs.type_, rhs.value_, rhs.vector_);
-
227 }
-
228
-
236 template<typename T>
-
237 requires std::is_integral_v<T>
-
238 static TySonObject Number(T number)
-
239 {
-
240 TySonObject tySonObject {};
-
241 tySonObject.value_ = std::to_string(number);
-
242 tySonObject.type_ = TySonType::Number;
-
243 return tySonObject;
-
244 }
+
224 explicit TySonObject(std::string_view object)
+
225 {
+
226 auto end_type_sep = object.find_first_of('|');
+
227 auto end_value_sep = (object.size() - 1) - (end_type_sep + 1);
+
228 auto type = object.substr(0, end_type_sep);
+
229
+
230 if (object == "null")
+
231 {
+
232 type_ = TySonType::Null;
+
233 value_ = {};
+
234 return;
+
235 }
+
236
+
237 if (object.starts_with("uts"))
+
238 {
+
239 type_ = TySonType::Timestamp;
+
240 value_ = object.substr(end_type_sep + 1, end_value_sep);
+
241 }
+
242 else if (object.starts_with('v'))
+
243 {
+
244 type_ = TySonType::Vector;
245
-
246
-
253 static TySonObject String(const std::string &str)
-
254 {
-
255 TySonObject tySonObject {};
-
256 tySonObject.value_ = str;
-
257 tySonObject.type_ = TySonType::String;
-
258 return tySonObject;
-
259 }
-
260
-
267 static TySonObject Bool(bool bl)
-
268 {
-
269 TySonObject tySonObject {};
-
270 tySonObject.value_ = bl ? "true" : "false";
-
271 tySonObject.type_ = TySonType::Bool;
-
272 return tySonObject;
-
273 }
-
274
- -
281 {
-
282 TySonObject tySonObject {};
-
283 tySonObject.value_ = "null";
-
284 tySonObject.type_ = TySonType::Null;
-
285 return tySonObject;
-
286 }
-
287
-
294 static TySonObject Timestamp(unsigned long long seconds)
-
295 {
-
296 TySonObject tySonObject {};
-
297 tySonObject.value_ = std::to_string(seconds);
-
298 tySonObject.type_ = TySonType::Timestamp;
-
299 return tySonObject;
-
300 }
-
301
-
309 static TySonObject Link(const std::string &collection, const std::string &uuid)
-
310 {
-
311 TySonObject tySonObject {};
-
312 tySonObject.link_ = {collection, uuid};
-
313 tySonObject.type_ = TySonType::Link;
-
314 return tySonObject;
-
315 }
-
316
-
323 template<std::convertible_to<tyson::TySonObject> ...Values>
-
324 static TySonObject Vector(Values &&...objs)
-
325 {
-
326 TySonObject tySonObject {};
-
327 tySonObject.vector_.reserve(sizeof ...(objs));
-
328 (tySonObject.vector_.emplace_back(objs), ...);
-
329 tySonObject.type_ = TySonType::Vector;
-
330 return tySonObject;
-
331 }
-
332
-
333 static TySonObject Value(const std::string &field, TySonObject &&val)
-
334 {
-
335 TySonObject tySonObject {};
-
336 tySonObject.map_.try_emplace(TySonObject::String(field), val);
-
337 tySonObject.type_ = TySonType::Value;
-
338 return tySonObject;
-
339 }
-
340
-
347 static TySonObject Map(std::map<std::string, TySonObject> &objs)
-
348 {
-
349 TySonObject tySonObject {};
-
350 std::for_each(objs.begin(), objs.end(),
-
351 [&tySonObject](std::pair<const std::string, TySonObject> &val)
-
352 {
-
353 tySonObject.map_.try_emplace(TySonObject::String(val.first), std::move(val.second));
-
354 });
-
355 tySonObject.type_ = TySonType::Map;
-
356 return tySonObject;
-
357 }
-
358
-
365 std::optional<TySonObject> operator[](const std::string_view key) const
-
366 {
-
367 if (type_ == TySonType::Map)
-
368 {
-
369 TySonObject result;
-
370 std::for_each(map_.begin(),
-
371 map_.end(),
-
372 [&result, &key](const std::pair<TySonObject, TySonObject> &obj)
-
373 {
-
374 if (obj.first.value_ == key)
-
375 {
-
376 result = obj.second;
-
377 }
-
378 });
-
379 return result;
-
380 }
-
381 else
-
382 {
-
383 return {};
-
384 }
-
385 }
-
386
-
391 [[nodiscard]] TySonType type() const
-
392 {
-
393 return type_;
-
394 }
-
395
-
402 template<TySonType T>
-
403 [[nodiscard]] std::string value() const
-
404 {
-
405 return value_;
-
406 }
-
407
-
414 template<TySonType T>
-
415 requires (T == TySonType::Bool)
-
416 [[nodiscard]] bool value() const
-
417 {
-
418 return value_ == "true";
-
419 }
-
420
-
427 template<TySonType T>
-
428 requires (T == TySonType::Null)
-
429 [[nodiscard]] std::string value() const
-
430 {
-
431 return "";
-
432 }
-
433
-
440 template<TySonType T>
-
441 requires (T == TySonType::Link)
-
442 [[nodiscard]] std::pair<std::string, std::string> value() const
-
443 {
-
444 return link_;
-
445 }
-
446
-
453 template<TySonType T>
-
454 requires (T == TySonType::Vector)
-
455 [[nodiscard]] std::vector<TySonObject> value() const
-
456 {
-
457 return vector_;
-
458 }
-
459
-
466 template<TySonType T>
-
467 requires (T == TySonType::Map)
-
468 [[nodiscard]] std::map<TySonObject, TySonObject> value() const
-
469 {
-
470 return map_;
-
471 }
-
472
-
480 template<typename T>
-
481 requires std::is_arithmetic_v<T>
-
482 [[nodiscard]] T value() const
-
483 {
-
484 if (type_ == TySonType::Number || type_ == TySonType::Bool || type_ == TySonType::Timestamp)
-
485 {
-
486 std::size_t pos{};
-
487
-
488 switch (*typeid(T).name())
-
489 {
-
490 case 's':
-
491 case 'j':
-
492 case 'i':
-
493 return std::stoi(value_, &pos);
-
494 case 'l':
-
495 return std::stol(value_, &pos);
-
496 case 'f':
-
497 return static_cast<T>(std::stof(value_, &pos));
-
498 case 'd':
-
499 return static_cast<T>(std::stod(value_, &pos));
-
500 case 'x':
-
501 return std::stoll(value_, &pos);
-
502 case 'e':
-
503 return static_cast<T>(std::stold(value_, &pos));
-
504 case 'b':
-
505 return value_ == "true";
-
506 case 'c':
-
507 return value_.c_str()[0];
-
508 }
-
509 }
-
510
-
511 throw std::invalid_argument("Invalid Type");
-
512 }
-
513 };
-
514
-
515
- -
517 {
-
518 std::vector<TySonObject> collection_ids_{};
-
519 std::vector<std::pair<TySonObject, TySonObject>> collection_objects_{};
-
520
-
521 public:
-
522 TySonCollectionObject() = default;
-
523
-
524 ~TySonCollectionObject() = default;
-
525
-
532 void add(const std::string_view &object)
-
533 {
-
534 collection_ids_.emplace_back(object);
-
535 };
-
536
-
543 void add(const std::pair<std::string_view, std::string_view> &object)
-
544 {
-
545 collection_objects_.emplace_back(TySonObject(object.first), TySonObject(object.second));
-
546 };
-
547
-
558 template<TySonType T>
-
559 requires (T == TySonType::Object)
-
560 std::optional<std::pair<TySonObject, TySonObject>> get(std::string_view obj_id)
-
561 {
-
562 for (const auto &val: collection_objects_)
-
563 {
-
564 if (val.first.value<TySonType::Link>().second == obj_id)
-
565 {
-
566 return val;
-
567 }
-
568 }
-
569 return {};
-
570 }
-
571
-
579 template<TySonType T>
-
580 requires (T == TySonType::Objects)
-
581 std::vector<std::pair<TySonObject, TySonObject>> get(std::string_view collection)
-
582 {
-
583 std::vector<std::pair<TySonObject, TySonObject>> result{};
-
584 std::for_each(collection_objects_.begin(), collection_objects_.end(),
-
585 [&collection, &result](const std::pair<TySonObject, TySonObject> &val)
-
586 {
-
587 if (val.first.value<TySonType::Link>().first == collection)
-
588 {
-
589 result.emplace_back(val);
-
590 }
-
591 });
-
592 return result;
-
593 }
-
594
-
603 template<TySonType T>
-
604 requires (T == TySonType::Object)
-
605 std::optional<std::pair<TySonObject, TySonObject>> get(std::string_view collection, std::string_view obj_id)
-
606 {
-
607 for (const std::pair<TySonObject, TySonObject> &val: collection_objects_)
-
608 {
-
609 auto tysonLink = val.first.value<TySonType::Link>();
-
610 if (std::tie(tysonLink.first, tysonLink.second) == std::tie(collection, obj_id))
-
611 {
-
612 return val;
-
613 }
-
614 }
-
615
-
616 return {};
-
617 }
-
618
-
626 template<TySonType T>
-
627 requires (T == TySonType::ID)
-
628 std::optional<TySonObject> get(std::string_view obj_id)
-
629 {
-
630 for (const TySonObject &val: collection_ids_)
-
631 {
-
632 if (val.value<TySonType::Link>().second == obj_id)
-
633 {
-
634 return val;
-
635 }
-
636 }
-
637 return {};
-
638 }
-
639
-
647 template<TySonType T>
-
648 requires (T == TySonType::ID)
-
649 std::vector<TySonObject> get(std::string_view collection)
-
650 {
-
651 std::vector<TySonObject> result{};
-
652 std::copy_if(collection_ids_.begin(), collection_ids_.end(),
-
653 std::back_inserter(result),
-
654 [&collection](const TySonObject &val)
-
655 {
-
656 if (val.value<TySonType::Link>().first == collection)
-
657 {
-
658 return val;
-
659 }
-
660 });
-
661 return result;
-
662 }
-
663
-
672 template<TySonType T>
-
673 requires (T == TySonType::ID)
-
674 std::optional<TySonObject> get(std::string_view collection, std::string_view obj_id)
-
675 {
-
676 for (const TySonObject &val: collection_ids_)
-
677 {
-
678 auto tysonLink = val.value<TySonType::Link>();
-
679 if (std::tie(tysonLink.first, tysonLink.second) == std::tie(collection, obj_id))
-
680 {
-
681 return val;
-
682 }
-
683 }
-
684
-
685 return {};
-
686 }
-
687 };
-
688}
-
689
-
690#endif //ANNADB_DRIVER_TYSON_HPP
-
Definition: TySON.hpp:517
-
std::optional< TySonObject > get(std::string_view collection, std::string_view obj_id)
Definition: TySON.hpp:674
-
std::optional< TySonObject > get(std::string_view obj_id)
Definition: TySON.hpp:628
-
std::vector< std::pair< TySonObject, TySonObject > > get(std::string_view collection)
Definition: TySON.hpp:581
-
std::optional< std::pair< TySonObject, TySonObject > > get(std::string_view obj_id)
Definition: TySON.hpp:560
-
std::optional< std::pair< TySonObject, TySonObject > > get(std::string_view collection, std::string_view obj_id)
Definition: TySON.hpp:605
-
void add(const std::string_view &object)
Definition: TySON.hpp:532
-
std::vector< TySonObject > get(std::string_view collection)
Definition: TySON.hpp:649
-
void add(const std::pair< std::string_view, std::string_view > &object)
Definition: TySON.hpp:543
-
Definition: TySON.hpp:36
-
T value() const
Definition: TySON.hpp:482
-
static TySonObject Number(T number)
Definition: TySON.hpp:238
-
std::pair< std::string, std::string > value() const
Definition: TySON.hpp:442
-
std::map< TySonObject, TySonObject > value() const
Definition: TySON.hpp:468
-
std::string value() const
Definition: TySON.hpp:403
-
TySonObject(std::string_view object)
Definition: TySON.hpp:176
-
static TySonObject Timestamp(unsigned long long seconds)
Definition: TySON.hpp:294
-
static TySonObject Vector(Values &&...objs)
Definition: TySON.hpp:324
-
TySonType type() const
Definition: TySON.hpp:391
-
static TySonObject Bool(bool bl)
Definition: TySON.hpp:267
-
std::vector< TySonObject > value() const
Definition: TySON.hpp:455
-
bool value() const
Definition: TySON.hpp:416
-
std::optional< TySonObject > operator[](const std::string_view key) const
Definition: TySON.hpp:365
-
static TySonObject String(const std::string &str)
Definition: TySON.hpp:253
-
static TySonObject Map(std::map< std::string, TySonObject > &objs)
Definition: TySON.hpp:347
-
static TySonObject Null()
Definition: TySON.hpp:280
-
static TySonObject Link(const std::string &collection, const std::string &uuid)
Definition: TySON.hpp:309
-
std::string value() const
Definition: TySON.hpp:429
+
246 parse_vector_elements(object);
+
247 }
+
248 else if (object.starts_with('m'))
+
249 {
+
250 type_ = TySonType::Map;
+
251 parse_map_elements(object);
+
252 }
+
253 else if (type.size() > 1)
+
254 {
+
255 type_ = TySonType::Link;
+
256 auto val = object.substr(end_type_sep + 1, end_value_sep);
+
257 link_ = std::make_pair(type, val);
+
258 }
+
259 else
+
260 {
+
261 type_ = static_cast<TySonType>(type.at(0));
+
262 value_ = object.substr(end_type_sep + 1, end_value_sep);
+
263 }
+
264 }
+
265
+
271 [[ nodiscard ]] bool operator==(const TySonObject &rhs) const
+
272 {
+
273 return std::tie(this->type_, this->value_, this->vector_, this->map_, this->link_) ==
+
274 std::tie(rhs.type_, rhs.value_, rhs.vector_, rhs.map_, rhs.link_);
+
275 }
+
276
+
282 [[ nodiscard ]] bool operator<(const TySonObject &rhs) const
+
283 {
+
284 return std::tie(this->type_, this->value_, this->vector_) < std::tie(rhs.type_, rhs.value_, rhs.vector_);
+
285 }
+
286
+
293 bool emplace(const std::string &key, tyson::TySonObject &&value)
+
294 {
+
295 if (type_ != TySonType::Map)
+
296 {
+
297 std::stringstream sstream;
+
298 sstream << "Can not be used with";
+
299 sstream << TySonType_repr(type_);
+
300 throw std::invalid_argument(sstream.str());
+
301 }
+
302 const auto [it, success] = map_.try_emplace(TySonObject::String(key), std::move(value));
+
303 return success;
+
304 }
+
305
+
313 template<typename T>
+
314 requires std::is_integral_v<T>
+
315 [[ nodiscard ]] static TySonObject Number(T number)
+
316 {
+
317 TySonObject tySonObject {};
+
318 tySonObject.value_ = std::to_string(number);
+
319 tySonObject.type_ = TySonType::Number;
+
320 return tySonObject;
+
321 }
+
322
+
323
+
330 [[ nodiscard ]] static TySonObject String(const std::string &str)
+
331 {
+
332 TySonObject tySonObject {};
+
333 tySonObject.value_ = str;
+
334 tySonObject.type_ = TySonType::String;
+
335 return tySonObject;
+
336 }
+
337
+
344 [[ nodiscard ]] static TySonObject Bool(bool bl)
+
345 {
+
346 TySonObject tySonObject {};
+
347 tySonObject.value_ = bl ? "true" : "false";
+
348 tySonObject.type_ = TySonType::Bool;
+
349 return tySonObject;
+
350 }
+
351
+
357 [[ nodiscard ]] static TySonObject Null()
+
358 {
+
359 TySonObject tySonObject {};
+
360 tySonObject.value_ = "null";
+
361 tySonObject.type_ = TySonType::Null;
+
362 return tySonObject;
+
363 }
+
364
+
371 [[ nodiscard ]] static TySonObject Timestamp(unsigned long long seconds)
+
372 {
+
373 TySonObject tySonObject {};
+
374 tySonObject.value_ = std::to_string(seconds);
+
375 tySonObject.type_ = TySonType::Timestamp;
+
376 return tySonObject;
+
377 }
+
378
+
386 [[ nodiscard ]] static TySonObject Link(const std::string &collection, const std::string &uuid)
+
387 {
+
388 TySonObject tySonObject {};
+
389 tySonObject.link_ = {collection, uuid};
+
390 tySonObject.type_ = TySonType::Link;
+
391 return tySonObject;
+
392 }
+
393
+
400 template<std::convertible_to<tyson::TySonObject> ...Values>
+
401 [[ nodiscard ]] static TySonObject Vector(Values &&...objs)
+
402 {
+
403 TySonObject tySonObject {};
+
404 tySonObject.vector_.reserve(sizeof ...(objs));
+
405 (tySonObject.vector_.emplace_back(objs), ...);
+
406 tySonObject.type_ = TySonType::Vector;
+
407 return tySonObject;
+
408 }
+
409
+
417 [[ nodiscard ]] static TySonObject Value(const std::string &field, TySonObject &&val)
+
418 {
+
419 TySonObject tySonObject {};
+
420 tySonObject.map_.try_emplace(TySonObject::String(field), val);
+
421 tySonObject.type_ = TySonType::Value;
+
422 return tySonObject;
+
423 }
+
424
+
431 [[ nodiscard ]] static TySonObject Map(std::map<std::string, TySonObject> &objs)
+
432 {
+
433 TySonObject tySonObject {};
+
434 std::for_each(objs.begin(), objs.end(),
+
435 [&tySonObject](std::pair<const std::string, TySonObject> &val)
+
436 {
+
437 tySonObject.map_.try_emplace(TySonObject::String(val.first), std::move(val.second));
+
438 });
+
439 tySonObject.type_ = TySonType::Map;
+
440 return tySonObject;
+
441 }
+
442
+
449 [[ nodiscard ]] static TySonObject Map()
+
450 {
+
451 TySonObject tySonObject {};
+
452 tySonObject.type_ = TySonType::Map;
+
453 return tySonObject;
+
454 }
+
455
+
462 [[ nodiscard ]] std::optional<TySonObject> operator[](const std::string_view key) const
+
463 {
+
464 if (type_ == TySonType::Map)
+
465 {
+
466 TySonObject result;
+
467 std::for_each(map_.begin(),
+
468 map_.end(),
+
469 [&result, &key](const std::pair<TySonObject, TySonObject> &obj)
+
470 {
+
471 if (obj.first.value_ == key)
+
472 {
+
473 result = obj.second;
+
474 }
+
475 });
+
476 return result;
+
477 }
+
478 else
+
479 {
+
480 return {};
+
481 }
+
482 }
+
483
+
488 [[nodiscard]] TySonType type() const
+
489 {
+
490 return type_;
+
491 }
+
492
+
499 template<TySonType T>
+
500 [[nodiscard]] std::string value() const
+
501 {
+
502 return value_;
+
503 }
+
504
+
511 template<TySonType T>
+
512 requires (T == TySonType::Bool)
+
513 [[nodiscard]] bool value() const
+
514 {
+
515 return value_ == "true";
+
516 }
+
517
+
524 template<TySonType T>
+
525 requires (T == TySonType::Null)
+
526 [[nodiscard]] std::string value() const
+
527 {
+
528 return "";
+
529 }
+
530
+
537 template<TySonType T>
+
538 requires (T == TySonType::Link)
+
539 [[nodiscard]] std::pair<std::string, std::string> value() const
+
540 {
+
541 return link_;
+
542 }
+
543
+
550 template<TySonType T>
+
551 requires (T == TySonType::Vector)
+
552 [[nodiscard]] std::vector<TySonObject> value() const
+
553 {
+
554 return vector_;
+
555 }
+
556
+
563 template<TySonType T>
+
564 requires (T == TySonType::Map)
+
565 [[nodiscard]] std::map<TySonObject, TySonObject> value() const
+
566 {
+
567 return map_;
+
568 }
+
569
+
577 template<typename T>
+
578 requires std::is_arithmetic_v<T>
+
579 [[nodiscard]] T value() const
+
580 {
+
581 if (type_ == TySonType::Number || type_ == TySonType::Bool || type_ == TySonType::Timestamp)
+
582 {
+
583 std::size_t pos{};
+
584
+
585 switch (*typeid(T).name())
+
586 {
+
587 case 's':
+
588 case 'j':
+
589 case 'i':
+
590 return std::stoi(value_, &pos);
+
591 case 'l':
+
592 return std::stol(value_, &pos);
+
593 case 'f':
+
594 return static_cast<T>(std::stof(value_, &pos));
+
595 case 'd':
+
596 return static_cast<T>(std::stod(value_, &pos));
+
597 case 'x':
+
598 return std::stoll(value_, &pos);
+
599 case 'e':
+
600 return static_cast<T>(std::stold(value_, &pos));
+
601 case 'b':
+
602 return value_ == "true";
+
603 case 'c':
+
604 return value_.c_str()[0];
+
605 }
+
606 }
+
607
+
608 throw std::invalid_argument("Invalid Type");
+
609 }
+
610 };
+
611
+
612
+ +
614 {
+
615 std::vector<TySonObject> collection_ids_{};
+
616 std::vector<std::pair<TySonObject, TySonObject>> collection_objects_{};
+
617
+
618 public:
+
619 TySonCollectionObject() = default;
+
620
+
621 ~TySonCollectionObject() = default;
+
622
+
629 void add(const std::string_view &object)
+
630 {
+
631 collection_ids_.emplace_back(object);
+
632 };
+
633
+
640 void add(const std::pair<std::string_view, std::string_view> &object)
+
641 {
+
642 collection_objects_.emplace_back(TySonObject(object.first), TySonObject(object.second));
+
643 };
+
644
+
655 template<TySonType T>
+
656 requires (T == TySonType::Object)
+
657 [[ nodiscard ]] std::optional<std::pair<TySonObject, TySonObject>> get(std::string_view obj_id)
+
658 {
+
659 for (const auto &val: collection_objects_)
+
660 {
+
661 if (val.first.value<TySonType::Link>().second == obj_id)
+
662 {
+
663 return val;
+
664 }
+
665 }
+
666 return {};
+
667 }
+
668
+
676 template<TySonType T>
+
677 requires (T == TySonType::Objects)
+
678 [[ nodiscard ]] std::vector<std::pair<TySonObject, TySonObject>> get(std::string_view collection)
+
679 {
+
680 std::vector<std::pair<TySonObject, TySonObject>> result{};
+
681 std::for_each(collection_objects_.begin(), collection_objects_.end(),
+
682 [&collection, &result](const std::pair<TySonObject, TySonObject> &val)
+
683 {
+
684 if (val.first.value<TySonType::Link>().first == collection)
+
685 {
+
686 result.emplace_back(val);
+
687 }
+
688 });
+
689 return result;
+
690 }
+
691
+
700 template<TySonType T>
+
701 requires (T == TySonType::Object)
+
702 [[ nodiscard ]] std::optional<std::pair<TySonObject, TySonObject>> get(std::string_view collection, std::string_view obj_id)
+
703 {
+
704 for (const std::pair<TySonObject, TySonObject> &val: collection_objects_)
+
705 {
+
706 auto tysonLink = val.first.value<TySonType::Link>();
+
707 if (std::tie(tysonLink.first, tysonLink.second) == std::tie(collection, obj_id))
+
708 {
+
709 return val;
+
710 }
+
711 }
+
712
+
713 return {};
+
714 }
+
715
+
723 template<TySonType T>
+
724 requires (T == TySonType::ID)
+
725 [[ nodiscard ]] std::optional<TySonObject> get(std::string_view obj_id)
+
726 {
+
727 for (const TySonObject &val: collection_ids_)
+
728 {
+
729 if (val.value<TySonType::Link>().second == obj_id)
+
730 {
+
731 return val;
+
732 }
+
733 }
+
734 return {};
+
735 }
+
736
+
744 template<TySonType T>
+
745 requires (T == TySonType::ID)
+
746 [[ nodiscard ]] std::vector<TySonObject> get(std::string_view collection)
+
747 {
+
748 std::vector<TySonObject> result{};
+
749 std::copy_if(collection_ids_.begin(), collection_ids_.end(),
+
750 std::back_inserter(result),
+
751 [&collection](const TySonObject &val)
+
752 {
+
753 if (val.value<TySonType::Link>().first == collection)
+
754 {
+
755 return val;
+
756 }
+
757 });
+
758 return result;
+
759 }
+
760
+
769 template<TySonType T>
+
770 requires (T == TySonType::ID)
+
771 [[ nodiscard ]] std::optional<TySonObject> get(std::string_view collection, std::string_view obj_id)
+
772 {
+
773 for (const TySonObject &val: collection_ids_)
+
774 {
+
775 auto tysonLink = val.value<TySonType::Link>();
+
776 if (std::tie(tysonLink.first, tysonLink.second) == std::tie(collection, obj_id))
+
777 {
+
778 return val;
+
779 }
+
780 }
+
781
+
782 return {};
+
783 }
+
784 };
+
785}
+
786
+
787#endif //ANNADB_DRIVER_TYSON_HPP
+
Definition: TySON.hpp:614
+
std::optional< TySonObject > get(std::string_view collection, std::string_view obj_id)
Definition: TySON.hpp:771
+
std::optional< TySonObject > get(std::string_view obj_id)
Definition: TySON.hpp:725
+
std::vector< std::pair< TySonObject, TySonObject > > get(std::string_view collection)
Definition: TySON.hpp:678
+
std::optional< std::pair< TySonObject, TySonObject > > get(std::string_view obj_id)
Definition: TySON.hpp:657
+
std::optional< std::pair< TySonObject, TySonObject > > get(std::string_view collection, std::string_view obj_id)
Definition: TySON.hpp:702
+
void add(const std::string_view &object)
Definition: TySON.hpp:629
+
std::vector< TySonObject > get(std::string_view collection)
Definition: TySON.hpp:746
+
void add(const std::pair< std::string_view, std::string_view > &object)
Definition: TySON.hpp:640
+
Definition: TySON.hpp:80
+
T value() const
Definition: TySON.hpp:579
+
static TySonObject Number(T number)
Definition: TySON.hpp:315
+
bool emplace(const std::string &key, tyson::TySonObject &&value)
Definition: TySON.hpp:293
+
std::pair< std::string, std::string > value() const
Definition: TySON.hpp:539
+
std::map< TySonObject, TySonObject > value() const
Definition: TySON.hpp:565
+
bool operator<(const TySonObject &rhs) const
Definition: TySON.hpp:282
+
std::string value() const
Definition: TySON.hpp:500
+
static TySonObject Map()
Definition: TySON.hpp:449
+
TySonObject(std::string_view object)
Definition: TySON.hpp:224
+
static TySonObject Timestamp(unsigned long long seconds)
Definition: TySON.hpp:371
+
static TySonObject Vector(Values &&...objs)
Definition: TySON.hpp:401
+
TySonType type() const
Definition: TySON.hpp:488
+
static TySonObject Value(const std::string &field, TySonObject &&val)
Definition: TySON.hpp:417
+
static TySonObject Bool(bool bl)
Definition: TySON.hpp:344
+
std::vector< TySonObject > value() const
Definition: TySON.hpp:552
+
bool value() const
Definition: TySON.hpp:513
+
std::optional< TySonObject > operator[](const std::string_view key) const
Definition: TySON.hpp:462
+
static TySonObject String(const std::string &str)
Definition: TySON.hpp:330
+
static TySonObject Map(std::map< std::string, TySonObject > &objs)
Definition: TySON.hpp:431
+
bool operator==(const TySonObject &rhs) const
Definition: TySON.hpp:271
+
static TySonObject Null()
Definition: TySON.hpp:357
+
static TySonObject Link(const std::string &collection, const std::string &uuid)
Definition: TySON.hpp:386
+
std::string value() const
Definition: TySON.hpp:526