SQLite Containers
Loading...
Searching...
No Matches
example-map-set-2.cpp
Go to the documentation of this file.
1#include <iostream>
3#include <map>
4#include <set>
5
6// Structure for storing data in the database
7struct MyStruct {
8 int64_t a;
9 double b;
10
11 // Overloading the output operator for serializing the structure
12 friend std::ostream& operator<<(std::ostream& os, const MyStruct& ms) {
13 os << ms.a << " " << ms.b;
14 return os;
15 }
16
17 // Overloading the input operator for deserializing the structure
18 friend std::istream& operator>>(std::istream& is, MyStruct& ms) {
19 is >> ms.a >> ms.b;
20 return is;
21 }
22
23 // Equality operator for use in std::unordered_map for the reconcile method
24 bool operator==(const MyStruct& other) const {
25 return a == other.a && b == other.b;
26 }
27
28 // Comparison operator for use in std::set
29 bool operator<(const MyStruct& other) const {
30 return a < other.a || (a == other.a && b < other.b);
31 }
32};
33
34// std::hash specialization for MyStruct
35namespace std {
36 template<>
37 struct hash<MyStruct> {
38 std::size_t operator()(const MyStruct& ms) const {
39 return std::hash<int64_t>()(ms.a) ^ std::hash<double>()(ms.b);
40 }
41 };
42}
43
44// Function to print the contents of a std::map where values are represented as std::set
45template <typename MapType>
46void print_map_with_set(const MapType& map, const std::string& header) {
47 std::cout << header << std::endl;
48 for (const auto& pair : map) {
49 if (!pair.second.empty()) {
50 for (const auto& item : pair.second) {
51 std::cout << "Key: " << pair.first << " -> Struct: {" << item.a << ", " << item.b << "}" << std::endl;
52 }
53 } else {
54 std::cout << "Key: " << pair.first << " has an empty set." << std::endl;
55 }
56 }
57}
58
59int main() {
60 try {
61 // Creating database configuration
63 config.db_path = "example-struct-map-set.db"; // Path to the database
64
65 // Creating a KeyMultiValueDB instance to work with int keys and MyStruct values
67 key_value_db.connect(); // Connect to the database
68
69 // Clearing the table to start fresh
70 key_value_db.clear();
71
72 // Creating a std::map with int keys and std::set<MyStruct> values
73 std::map<int, std::set<MyStruct>> map_with_set_pairs = {
74 {3, {{1, 1.1}, {2, 2.2}}},
75 {1, {}} // Empty set for key 1
76 };
77
78 // Synchronizing the contents of std::map with the database
79 std::cout << "Appending data to the database using reconcile..." << std::endl;
80 key_value_db.reconcile(map_with_set_pairs); // Synchronize data with the database
81
82 // Retrieving all key-value pairs from the database and printing them
83 auto retrieved_map_with_set_pairs = key_value_db.retrieve_all<std::map, std::set>();
84 print_map_with_set(retrieved_map_with_set_pairs, "Key-value pairs in database after reconcile:");
85
86 // Inserting a new key-value pair
87 key_value_db.insert(4, {3, 3.3}); // Key 4 -> Set with MyStruct {3, 3.3}
88 key_value_db.insert(4, {5, 5.5}); // Adding another value to the set for key 4
89
90 // Retrieving all key-value pairs again and printing them after insertion
91 retrieved_map_with_set_pairs = key_value_db.retrieve_all<std::map, std::set>();
92 print_map_with_set(retrieved_map_with_set_pairs, "Key-value pairs in database after inserting new values:");
93
94 } catch (const sqlite_containers::sqlite_exception &e) {
95 std::cerr << "SQLite error: " << e.what() << std::endl;
96 } catch (const std::exception &e) {
97 std::cerr << "Error: " << e.what() << std::endl;
98 }
99
100 return 0;
101}
Template class for managing key-value pairs in a SQLite database.
void connect()
Connects to the database using the current configuration. Initializes a connection to the database by...
Definition BaseDB.hpp:53
Configuration class for SQLite database settings.
Definition Config.hpp:11
std::string db_path
Path to the SQLite database file.
Definition Config.hpp:13
Template class for managing key-value pairs in a SQLite database, where each key can map to multiple ...
ContainerT< KeyT, ValueT > retrieve_all(const TransactionMode &mode)
Retrieves all key-value pairs from the database with a transaction.
void insert(const KeyT &key, const ValueT &value, const TransactionMode &mode)
Inserts a key-value pair into the database with a transaction.
void clear(const TransactionMode &mode)
Clears all key-value pairs from the database with a transaction.
void reconcile(const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Reconciles the content of the container with the database with a transaction.
Exception class for SQLite errors.
Definition Utils.hpp:27
void print_map_with_set(const MapType &map, const std::string &header)
int main()
bool operator==(const MyStruct &other) const
friend std::istream & operator>>(std::istream &is, MyStruct &ms)
bool operator<(const MyStruct &other) const
friend std::ostream & operator<<(std::ostream &os, const MyStruct &ms)
std::size_t operator()(const MyStruct &ms) const