Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ add_executable(annadb_driver_example main.cpp
../src/query.hpp
../src/query_comparision.hpp
includes/insert_example.hpp
src/insert_example.cpp )
src/insert_example.cpp src/find_example.cpp includes/find_example.hpp)
target_link_libraries(annadb_driver_example cppzmq)

target_compile_options(annadb_driver_example PRIVATE
Expand Down
32 changes: 32 additions & 0 deletions examples/includes/find_example.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by felix on 13.03.23.
//

#ifndef ANNADB_DRIVER_EXAMPLE_FIND_EXAMPLE_HPP
#define ANNADB_DRIVER_EXAMPLE_FIND_EXAMPLE_HPP

#include "../../src/connection.hpp"

using TYSON = tyson::TySonObject;
using tyson::TySonCollectionObject;
using tyson::TySonType;
using annadb::Query::Query;

void find_number(annadb::AnnaDB &connection, const std::string &collection_name, int search_number);

void find_number_limit(
annadb::AnnaDB &connection,
const std::string &collection_name,
int search_number,
int limit
);

void find_number_limit_offset(
annadb::AnnaDB &connection,
const std::string &collection_name,
int search_number,
int limit,
int offset
);

#endif //ANNADB_DRIVER_EXAMPLE_FIND_EXAMPLE_HPP
38 changes: 34 additions & 4 deletions examples/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
#include "../src/connection.hpp"
#include "includes/insert_example.hpp"
#include "includes/find_example.hpp"

int main()
{
annadb::AnnaDB con {"jondoe", "passwd1234", "0.0.0.0", 10001};
con.connect();
insert_some_values(con, "my_collection");
con.close();
{
annadb::AnnaDB con{"jondoe", "passwd1234", "0.0.0.0", 10001};
con.connect();
insert_some_values(con, "my_collection");
con.close();
}

std::cout << "###################################################\n";

{
annadb::AnnaDB con{"jondoe", "passwd1234", "0.0.0.0", 10001};
con.connect();
find_number(con, "my_collection", 99);
con.close();
}

std::cout << "###################################################\n";

{
annadb::AnnaDB con{"jondoe", "passwd1234", "0.0.0.0", 10001};
con.connect();
find_number_limit(con, "my_collection", 99, 1);
con.close();
}

std::cout << "###################################################\n";

{
annadb::AnnaDB con{"jondoe", "passwd1234", "0.0.0.0", 10001};
con.connect();
find_number_limit_offset(con, "my_collection", 99, 1, 13);
con.close();
}

return 0;
}
102 changes: 102 additions & 0 deletions examples/src/find_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "../includes/find_example.hpp"

void find_number(annadb::AnnaDB &connection, const std::string &collection_name, int search_number)
{
auto query = Query(collection_name);

query.find(annadb::Query::Find::GT(TYSON::Number(search_number)));

std::optional<annadb::Journal> result = connection.send(query);
if (result)
{
std::cout << "Find query was successful send\n";

auto journal = result.value();
// I expect only one object so that I would use `short` over `int`
auto new_rows = journal.meta().rows<short>();

std::cout << "Found: " << new_rows.value() << " rows.\n";

TySonCollectionObject new_ids = journal.data().get<TySonType::Objects>().value();
for (auto &val: new_ids.get<TySonType::Objects>(collection_name))
{
std::cout << val.first << ": " << val.second << "\n";
};
}
else
{
std::cout << "Something went wrong during the find\n";
}
}

void find_number_limit(
annadb::AnnaDB &connection,
const std::string &collection_name,
int search_number,
int limit
)
{
auto query = Query(collection_name);

query.find(annadb::Query::Find::GT(TYSON::Number(search_number))).limit<int>(std::move(limit));

std::optional<annadb::Journal> result = connection.send(query);
if (result)
{
std::cout << "Find query was successful send\n";

auto journal = result.value();
// I expect only one object so that I would use `short` over `int`
auto new_rows = journal.meta().rows<short>();

std::cout << "Found: " << new_rows.value() << " rows.\n";

TySonCollectionObject new_ids = journal.data().get<TySonType::Objects>().value();
for (auto &val: new_ids.get<TySonType::Objects>(collection_name))
{
std::cout << val.first << ": " << val.second << "\n";
};
}
else
{
std::cout << "Something went wrong during the find\n";
}
}

void find_number_limit_offset(
annadb::AnnaDB &connection,
const std::string &collection_name,
int search_number,
int limit,
int offset
)
{
auto query = Query(collection_name);

query
.find(annadb::Query::Find::GT(TYSON::Number(search_number)))
.offset(std::move(offset))
.limit<int>(std::move(limit));

std::optional<annadb::Journal> result = connection.send(query);
if (result)
{
std::cout << "Find query was successful send\n";

auto journal = result.value();
// I expect only one object so that I would use `short` over `int`
auto new_rows = journal.meta().rows<short>();

std::cout << "Found: " << new_rows.value() << " rows.\n";

TySonCollectionObject new_ids = journal.data().get<TySonType::Objects>().value();
for (auto &val: new_ids.get<TySonType::Objects>(collection_name))
{
std::cout << val.first << ": " << val.second << "\n";
};
}
else
{
std::cout << "Something went wrong during the find\n";
}
}
22 changes: 15 additions & 7 deletions src/TySON.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,18 @@ namespace tyson
type_ = TySonType::Timestamp;
value_ = object.substr(end_type_sep + 1, end_value_sep);
}
else if (object.starts_with('v'))
else if (object.substr(0, 2) == "v[")
{
type_ = TySonType::Vector;

parse_vector_elements(object);
}
else if (object.starts_with('m'))
else if (object.substr(0, 2) == "m{")
{
type_ = TySonType::Map;
parse_map_elements(object);
}
else if (type.size() > 1)
else if (end_type_sep > 1)
{
type_ = TySonType::Link;
auto val = object.substr(end_type_sep + 1, end_value_sep);
Expand Down Expand Up @@ -617,9 +617,16 @@ namespace tyson

public:
TySonCollectionObject() = default;
explicit TySonCollectionObject(size_t size)
explicit TySonCollectionObject(size_t size, bool objects = false)
{
collection_ids_.reserve(size);
if (objects)
{
collection_objects_.reserve(size);
}
else
{
collection_ids_.reserve(size);
}
}
~TySonCollectionObject() = default;

Expand All @@ -641,9 +648,10 @@ namespace tyson
*
* @param object
*/
void add(const std::pair<std::string_view, std::string_view> &object)
void add(const std::string &link, const std::string &value)
{
collection_objects_.emplace_back(TySonObject(object.first), TySonObject(object.second));
const auto new_val = std::make_pair(TySonObject(link), TySonObject(value));
collection_objects_.emplace_back(new_val);
};

/**
Expand Down
5 changes: 3 additions & 2 deletions src/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ namespace annadb
{
if (data_.starts_with(std::string("s|data|:objects")) && T == tyson::TySonType::Objects)
{
tyson::TySonCollectionObject object {};
auto start_val = data_.find_first_of('{') + 1;
auto end_val = data_.find_last_of('}');

auto tyson_str_data = split_data(data_.substr(start_val, end_val - start_val));
tyson::TySonCollectionObject object {tyson_str_data.size(), true};

for (auto &key_val: tyson_str_data)
{
object.add(std::make_pair(key_val.link, key_val.value));
object.add(key_val.link, key_val.value);
}

tyson_str_data.clear();
Expand Down