|
2 | 2 | Official C++ binding for TidesDB.
|
3 | 3 |
|
4 | 4 | ## Getting Started
|
5 |
| -You must make sure you have the TidesDB shared C library installed on your system. Be sure to also compile with TIDESDB_WITH_SANITIZER and TIDESDB_BUILD_TESTS OFF. |
| 5 | +You must make sure you have the TidesDB shared C library installed on your system. Be sure to also compile with TIDESDB_WITH_SANITIZER and TIDESDB_BUILD_TESTS OFF. You will also require a C++11 compatible compiler. |
6 | 6 |
|
7 | 7 | ### Build and install
|
8 |
| -``` |
| 8 | +```bash |
9 | 9 | cmake -S . -B build
|
10 | 10 | cmake --build build
|
11 | 11 | cmake --install build
|
12 | 12 | ```
|
13 | 13 |
|
14 |
| -### Create a TidesDB instance |
| 14 | +### Linking |
| 15 | +```cmake |
| 16 | +# Find the TidesDB C library |
| 17 | +find_library(LIBRARY_TIDEDB NAMES tidesdb REQUIRED) |
| 18 | +
|
| 19 | +# Find the TidesDB C++ binding |
| 20 | +find_library(LIBRARY_TIDEDB_CPP NAMES tidesdb_cpp REQUIRED) |
| 21 | +
|
| 22 | +# Link with your target |
| 23 | +target_link_libraries(your_target PRIVATE ${LIBRARY_TIDEDB_CPP} ${LIBRARY_TIDEDB}) |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +### Open and Close |
15 | 28 | ```cpp
|
16 | 29 | #include <tidesdb.hpp>
|
17 | 30 |
|
18 | 31 | int main() {
|
19 | 32 | TidesDB::DB db;
|
20 |
| - db.Open("your_db_dir"); |
| 33 | + db.Open("your_db_directory"); |
21 | 34 |
|
22 |
| - // Perform db operations... |
| 35 | + /* Database operations... */ |
23 | 36 |
|
24 | 37 | db.Close();
|
25 | 38 | return 0;
|
26 | 39 | }
|
27 | 40 | ```
|
28 | 41 |
|
29 |
| -### Creating and dropping column families |
| 42 | +### Column Family Management |
30 | 43 | ```cpp
|
31 |
| -db.CreateColumnFamily("my_column_family", (1024*1024)*64, TDB_DEFAULT_SKIP_LIST_MAX_LEVEL, TDB_DEFAULT_SKIP_LIST_PROBABILITY, true, TIDESDB_COMPRESSION_LZ4, true); |
32 |
| -db.DropColumnFamily("my_column_family"); |
33 |
| -``` |
| 44 | +/* Create a column family with custom parameters */ |
| 45 | +db.CreateColumnFamily( |
| 46 | + "users", /* Column family name */ |
| 47 | + 64 * 1024 * 1024, /* Flush threshold (64MB) */ |
| 48 | + TDB_DEFAULT_SKIP_LIST_MAX_LEVEL, /* Max level for skip list */ |
| 49 | + TDB_DEFAULT_SKIP_LIST_PROBABILITY, /* Skip list probability */ |
| 50 | + true, /* Enable compression */ |
| 51 | + TIDESDB_COMPRESSION_LZ4, /* Use LZ4 compression */ |
| 52 | + true /* Enable bloom filter */ |
| 53 | +); |
| 54 | + |
| 55 | +/* List all column families */ |
| 56 | +std::vector<std::string> families; |
| 57 | +db.ListColumnFamilies(&families); |
| 58 | +for (const auto& family : families) { |
| 59 | + std::cout << "Found column family: " << family << std::endl; |
| 60 | +} |
34 | 61 |
|
35 |
| -### CRUD operations |
36 |
| -#### No TTL |
37 |
| -```cpp |
38 |
| -std::vector<uint8_t> key = {1, 2, 3}; |
39 |
| -std::vector<uint8_t> value = {4, 5, 6}; |
40 |
| -db.Put("my_column_family", &key, &value, -1); |
41 |
| -``` |
| 62 | +/* Get column family statistics */ |
| 63 | +TidesDB::ColumnFamilyStat stat; |
| 64 | +db.GetColumnFamilyStat("users", &stat); |
| 65 | +std::cout << "Memtable size: " << stat.memtable_size << " bytes" << std::endl; |
| 66 | +std::cout << "Number of SSTables: " << stat.num_sstables << std::endl; |
42 | 67 |
|
43 |
| -#### With TTL |
44 |
| -```cpp |
45 |
| -std::vector<uint8_t> key = {1, 2, 3}; |
46 |
| -std::vector<uint8_t> value = {4, 5, 6}; |
47 |
| -db.Put("my_column_family", &key, &value, std::chrono::seconds(3600)); |
| 68 | +/* Drop a column family */ |
| 69 | +db.DropColumnFamily("users"); |
48 | 70 | ```
|
49 | 71 |
|
50 |
| -#### Get |
| 72 | +### Basic Key-Value Operations |
51 | 73 | ```cpp
|
| 74 | +/* Create binary key and value */ |
| 75 | +std::vector<uint8_t> key = {1, 2, 3, 4}; |
| 76 | +std::vector<uint8_t> value = {10, 20, 30, 40}; |
| 77 | + |
| 78 | +/* Insert with no TTL */ |
| 79 | +db.Put("users", &key, &value, std::chrono::seconds(0)); |
| 80 | + |
| 81 | +/* Insert with 1 hour TTL */ |
| 82 | +db.Put("users", &key, &value, std::chrono::seconds(3600)); |
| 83 | + |
| 84 | +/* Retrieve a value */ |
52 | 85 | std::vector<uint8_t> retrieved_value;
|
53 |
| -db.Get("my_column_family", &key, &retrieved_value); |
| 86 | +db.Get("users", &key, &retrieved_value); |
| 87 | + |
| 88 | +/* Delete a key */ |
| 89 | +db.Delete("users", &key); |
54 | 90 | ```
|
55 | 91 |
|
56 |
| -#### Deleting data |
| 92 | +### Range Queries |
57 | 93 | ```cpp
|
58 |
| -db.Delete("my_column_family", &key); |
| 94 | +std::vector<uint8_t> start_key = {1, 0, 0}; |
| 95 | +std::vector<uint8_t> end_key = {1, 255, 255}; |
| 96 | +std::vector<std::pair<std::vector<uint8_t>, std::vector<uint8_t>>> results; |
| 97 | +
|
| 98 | +db.Range("users", &start_key, &end_key, &results); |
| 99 | +
|
| 100 | +for (const auto& [k, v] : results) { |
| 101 | + /* Process key-value pairs.... */ |
| 102 | +} |
| 103 | +
|
| 104 | +/* Delete a range of keys */ |
| 105 | +db.DeleteByRange("users", &start_key, &end_key); |
59 | 106 | ```
|
60 | 107 |
|
61 |
| -## Transactions |
62 |
| -You can add operations to a transaction and commit them atomically. |
| 108 | +### Transactions |
63 | 109 | ```cpp
|
64 | 110 | TidesDB::Txn txn(&db);
|
65 | 111 | txn.Begin();
|
66 |
| -txn.Put(&key, &value, std::chrono::seconds(3600)); |
67 |
| -txn.Commit(); |
68 |
| -``` |
69 | 112 |
|
70 |
| -## Cursor |
71 |
| -You can iterate over the keys in a column family. |
72 |
| -```cpp |
73 |
| -TidesDB::Cursor cursor(&db, "my_column_family"); |
74 |
| -cursor.Init(); |
| 113 | +/* Perform multiple operations atomically */ |
| 114 | +std::vector<uint8_t> key1 = {1, 1}; |
| 115 | +std::vector<uint8_t> value1 = {10, 10}; |
| 116 | +txn.Put(&key1, &value1, std::chrono::seconds(0)); |
75 | 117 |
|
76 |
| -std::vector<uint8_t> key, value; |
77 |
| -cursor.Get(key, value); |
78 |
| -cursor.Next(); // Go forwards |
79 |
| -cursor.Prev(); // Go backwards |
| 118 | +std::vector<uint8_t> key2 = {2, 2}; |
| 119 | +std::vector<uint8_t> value2 = {20, 20}; |
| 120 | +txn.Put(&key2, &value2, std::chrono::seconds(0)); |
| 121 | + |
| 122 | +/* Read within the transaction */ |
| 123 | +std::vector<uint8_t> read_value; |
| 124 | +txn.Get(&key1, &read_value); |
| 125 | + |
| 126 | +/* Delete within the transaction */ |
| 127 | +txn.Delete(&key1); |
| 128 | + |
| 129 | +/* Commit the transaction */ |
| 130 | +txn.Commit(); |
| 131 | + |
| 132 | +/* Or roll back if needed |
| 133 | + * txn.Rollback(); */ |
80 | 134 | ```
|
81 | 135 |
|
| 136 | +### Cursors |
82 | 137 | ```cpp
|
83 |
| -TidesDB::Cursor cursor(&db, "my_column_family"); |
| 138 | +TidesDB::Cursor cursor(&db, "users"); |
84 | 139 | cursor.Init();
|
85 | 140 |
|
86 | 141 | std::vector<uint8_t> key, value;
|
87 | 142 | while (cursor.Get(key, value) == 0) {
|
88 |
| - // Process key and value |
| 143 | + /* Process key and value */ |
| 144 | +
|
| 145 | + /* Move to next entry */ |
89 | 146 | cursor.Next();
|
| 147 | +
|
| 148 | + /* Or move to previous entry |
| 149 | + * cursor.Prev(); */ |
90 | 150 | }
|
91 |
| -``` |
92 | 151 |
|
| 152 | +``` |
93 | 153 |
|
94 |
| -## Compactions |
95 |
| -You can manually trigger a compaction. |
| 154 | +### Compaction Management |
96 | 155 | ```cpp
|
97 |
| -db.CompactSSTables("my_column_family", 4); // Use 4 threads for compaction |
| 156 | +/* Manual compaction with 4 threads */ |
| 157 | +db.CompactSSTables("users", 4); |
| 158 | + |
| 159 | +/* Automated incremental merges (run every 60 seconds if at least 5 SSTables exist) */ |
| 160 | +db.StartIncrementalMerges("users", std::chrono::seconds(60), 5); |
98 | 161 | ```
|
99 | 162 |
|
100 |
| -Or you can start incremental background merge compactions. |
| 163 | +### Exception Handling Example |
101 | 164 | ```cpp
|
102 |
| -db.StartIncrementalMerges("my_column_family", std::chrono::seconds(60), 5); // Merge every 60 seconds if there are at least 5 SSTables |
| 165 | +try { |
| 166 | + db.Open("non_existent_directory"); |
| 167 | +} catch (const std::runtime_error& e) { |
| 168 | + std::cerr << "Database error: " << e.what() << std::endl; |
| 169 | + /* The error message will contain both the error code and description |
| 170 | + * Format: "Error {code}: {message}" */ |
| 171 | +} |
103 | 172 | ```
|
0 commit comments