SQLite Containers
Loading...
Searching...
No Matches
example-map-struct.cpp
Go to the documentation of this file.
2#include <iostream>
3#include <map>
4#include <vector>
5
6// Data structure for storing 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
24// Helper function to print the contents of a std::map
25template <typename MapType>
26void print_map(const MapType& map, const std::string& header) {
27 std::cout << header << std::endl;
28 for (const auto& pair : map) {
29 std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
30 }
31}
32
33int main() {
34 // Creating a database configuration
36 config.db_path = "example_struct.db"; // Specify the database path
37
38 // Creating a database instance for working with int keys and MyStruct values
40 map_db.connect(); // Connect to the database
41
42 // Inserting several key-value pairs
43 map_db.insert(1, {10, 1.1});
44 map_db.insert(2, {20, 2.2});
45 map_db.insert(3, {30, 3.3});
46
47 // Check the number of keys in the database
48 std::cout << "Number of keys in the database: " << map_db.count() << std::endl;
49
50 // Check if the database is empty
51 std::cout << "Is the database empty? " << (map_db.empty() ? "Yes" : "No") << std::endl;
52
53 // Find and output the value for key 2
54 MyStruct value;
55 if (map_db.find(2, value)) {
56 std::cout << "Found value for key 2: " << value << std::endl;
57 } else {
58 std::cout << "Key 2 not found." << std::endl;
59 }
60
61 // Load database contents into a std::map using overloaded operator()
62 std::map<int, MyStruct> my_map;
63 my_map = map_db(); // Use overloaded operator() to load data
64 print_map(my_map, "Contents of my_map after using operator():");
65
66 // Load database contents into another std::map using the load method
67 std::map<int, MyStruct> my_map2;
68 map_db.load(my_map2); // Use the load method for explicit data loading
69 print_map(my_map2, "Contents of my_map2 after using load:");
70
71 // Retrieve and output all key-value pairs directly from the database
72 std::map<int, MyStruct> all_entries = map_db.retrieve_all<std::map>();
73 print_map(all_entries, "Contents of database using retrieve_all:");
74
75 // Check the number of keys in the database
76 std::cout << "Number of keys in the database: " << map_db.count() << std::endl;
77
78 // Remove key-value pair with key 3 and output the result
79 map_db.remove(3);
80 all_entries = map_db.retrieve_all<std::map>();
81 print_map(all_entries, "Contents of database after removing key 3:");
82
83 // Insert a new key-value pair and output the result
84 map_db.insert(4, {40, 4.4});
85 all_entries = map_db.retrieve_all<std::map>();
86 print_map(all_entries, "Contents of database after inserting key 4:");
87
88 // Add the contents of a std::map to the database using the append method
89 my_map[5] = {50, 5.5};
90 map_db.append(my_map); // Append data from my_map to the database
91 all_entries = map_db.retrieve_all<std::map>();
92 print_map(all_entries, "Contents of database after append:");
93
94 // Example of synchronizing the contents of a std::map with the database using the reconcile method
95 my_map.erase(5);
96 my_map[6] = {60, 6.6};
97 map_db.reconcile(my_map); // Synchronize data
98 all_entries = map_db.retrieve_all<std::map>();
99 print_map(all_entries, "Contents of database after reconcile:");
100
101 // Using the assignment operator for synchronization (similar to calling the reconcile method)
102 my_map.erase(6);
103 my_map[7] = {70, 7.7};
104 map_db = my_map; // Use the assignment operator for synchronization
105 all_entries = map_db.retrieve_all<std::map>();
106 print_map(all_entries, "Contents of database after using operator= to reconcile:");
107
108 // Output the number of elements and whether the database is empty
109 std::cout << "Number of keys in the database: " << map_db.count() << std::endl;
110 std::cout << "Is the database empty? " << (map_db.empty() ? "Yes" : "No") << std::endl;
111
112 // Clear the database and output the result
113 map_db.clear();
114 all_entries = map_db.retrieve_all<std::map>();
115 if (all_entries.empty()) {
116 std::cout << "Database is empty after clear." << std::endl;
117 } else {
118 print_map(all_entries, "Contents of database after clear:");
119 }
120
121 // Final check for emptiness
122 std::cout << "Is the database empty? " << (map_db.empty() ? "Yes" : "No") << std::endl;
123
124 return 0;
125}
Declaration of the KeyValueDB 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.
std::size_t count() const
Returns the number of elements in the database.
void clear()
Clears all key-value pairs from the database.
void remove(const KeyT &key)
Removes a key-value pair from the database.
void reconcile(const ContainerT< KeyT, ValueT > &container)
Reconciles the database with the container.
void insert(const KeyT &key, const ValueT &value)
Inserts a key-value pair into the database.
ContainerT< KeyT, ValueT > retrieve_all()
Retrieves all key-value pairs.
bool empty() const
Checks if the database is empty.
bool find(const KeyT &key, ValueT &value)
Finds a value by key.
void append(const ContainerT< KeyT, ValueT > &container)
Appends data to the database.
void load(ContainerT< KeyT, ValueT > &container)
Loads data from the database into the container.
void print_map(const MapType &map, const std::string &header)
int main()
friend std::istream & operator>>(std::istream &is, MyStruct &ms)
friend std::ostream & operator<<(std::ostream &os, const MyStruct &ms)