SQLite Containers
Loading...
Searching...
No Matches
example-multimap-struct.cpp
Go to the documentation of this file.
2#include <iostream>
3#include <map>
4#include <list>
5
6// Structure to be used as a value in the database
7struct MyStruct {
8 int64_t a;
9 double b;
10
11 // Operators for serialization and deserialization of 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 friend std::istream& operator>>(std::istream& is, MyStruct& ms) {
18 is >> ms.a >> ms.b;
19 return is;
20 }
21};
22
23// Function to print the contents of a multimap
24template <typename MultimapType>
25void print_multimap(const MultimapType& multimap, const std::string& header) {
26 std::cout << header << std::endl;
27 for (const auto& pair : multimap) {
28 std::cout << "Key: " << pair.first << " -> Struct: {" << pair.second.a << ", " << pair.second.b << "}" << std::endl;
29 }
30}
31
32// Function to print the contents of a map where values are lists
33template <typename MapType>
34void print_map_with_list(const MapType& map, const std::string& header) {
35 std::cout << header << std::endl;
36 for (const auto& pair : map) {
37 std::cout << "Key: " << pair.first << " -> ";
38 for (const auto& item : pair.second) {
39 std::cout << "Struct: {" << item.a << ", " << item.b << "} ";
40 }
41 std::cout << std::endl;
42 }
43}
44
45int main() {
46 try {
47 // Creating a configuration for the database
49 config.db_path = "example-multimap-struct.db";
50
51 // Creating an instance of KeyMultiValueDB
53 key_value_db.connect();
54
55 // Clearing the table for a fresh start
56 key_value_db.clear();
57
58 // Creating a std::multimap with key-value pairs (structures)
59 std::multimap<int, MyStruct> multimap_pairs = {
60 {1, {10, 1.1}},
61 {2, {20, 2.2}},
62 {2, {20, 2.2}}, // Duplicate values
63 {2, {20, 2.2}}, // Another duplicate
64 {1, {14, 4.0}},
65 {3, {15, 4.0}},
66 {2, {30, 1.3}}
67 };
68
69 // Creating a std::map with std::list as values
70 std::map<int, std::list<MyStruct>> map_with_list_pairs = {
71 {3, {{15, 4.0}}},
72 {1, {{10, 1.1}}}
73 };
74
75 // Adding the contents of std::multimap to the database
76 key_value_db.append(multimap_pairs);
77
78 // Adding the contents of std::map with std::list values to the database
79 key_value_db.append(map_with_list_pairs);
80
81 // Retrieving all key-value pairs from the database and printing them
82 auto retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
83 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after append:");
84
85 // Inserting a new key-value pair
86 key_value_db.insert(4, {50, 7.7});
87 key_value_db.insert(4, {50, 7.7});
88
89 // Retrieving all key-value pairs into a std::map with std::list values and printing them
90 using map_with_list_t = std::map<int, std::list<MyStruct>>;
91 map_with_list_t retrieved_map_with_list_pairs = key_value_db.retrieve_all<std::map, std::list>();
92 print_map_with_list(retrieved_map_with_list_pairs, "Key-value pairs in database after insert:");
93
94 // Checking the existence of a key in the database
95 std::list<MyStruct> values;
96 if (key_value_db.find(4, values)) {
97 std::cout << "Key 4 found in the database with values:" << std::endl;
98 for (const auto& value : values) {
99 std::cout << "Struct: {" << value.a << ", " << value.b << "}" << std::endl;
100 }
101 } else {
102 std::cout << "Key 4 not found in the database." << std::endl;
103 }
104
105 // Checking a non-existent key
106 values.clear();
107 if (key_value_db.find(10, values)) {
108 std::cout << "Key 10 found in the database with values:" << std::endl;
109 for (const auto& value : values) {
110 std::cout << "Struct: {" << value.a << ", " << value.b << "}" << std::endl;
111 }
112 } else {
113 std::cout << "Key 10 not found in the database." << std::endl;
114 }
115
116 // Removing a specific key-value pair
117 key_value_db.remove(2, {20, 2.2});
118
119 // Removing all values associated with a key
120 key_value_db.remove(1);
121
122 // Retrieving all key-value pairs after removal and printing them
123 retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
124 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after removals:");
125
126 // Synchronizing the database with the contents of std::multimap
127 key_value_db.reconcile(multimap_pairs);
128
129 // Retrieving all key-value pairs after synchronization and printing them
130 retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
131 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after reconcile:");
132
133 } catch (const sqlite_containers::sqlite_exception &e) {
134 std::cerr << "SQLite error: " << e.what() << std::endl;
135 } catch (const std::exception &e) {
136 std::cerr << "Error: " << e.what() << std::endl;
137 }
138
139 return 0;
140}
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 ...
void append(const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Appends the content of the container to the database with a transaction.
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.
bool find(const KeyT &key, ContainerT< ValueT > &values)
Finds values by key in the database.
void remove(const KeyT &key, const ValueT &value)
Removes a specific key-value pair from the database.
Exception class for SQLite errors.
Definition Utils.hpp:27
void print_multimap(const MultimapType &multimap, const std::string &header)
void print_map_with_list(const MapType &map, const std::string &header)
friend std::istream & operator>>(std::istream &is, MyStruct &ms)
friend std::ostream & operator<<(std::ostream &os, const MyStruct &ms)