Skip to content

Commit 15f2f46

Browse files
authored
Merge pull request #21 from tidesdb/tdb1-cpp-updates-2
- GetColumnFamilyStat addition.. more
2 parents 5e63a4e + c20ad78 commit 15f2f46

File tree

4 files changed

+317
-61
lines changed

4 files changed

+317
-61
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
cmake_minimum_required(VERSION 3.25)
66
project(tidesdb_cpp CXX)
77

8-
set(CMAKE_CXX_STANDARD 20)
8+
set(CMAKE_CXX_STANDARD 11)
99
set(PROJECT_VERSION 0.1.0)
1010
add_compile_options(-Wextra -Wall -Werror)
1111

README.md

Lines changed: 116 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,171 @@
22
Official C++ binding for TidesDB.
33

44
## 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.
66

77
### Build and install
8-
```
8+
```bash
99
cmake -S . -B build
1010
cmake --build build
1111
cmake --install build
1212
```
1313

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
1528
```cpp
1629
#include <tidesdb.hpp>
1730

1831
int main() {
1932
TidesDB::DB db;
20-
db.Open("your_db_dir");
33+
db.Open("your_db_directory");
2134

22-
// Perform db operations...
35+
/* Database operations... */
2336

2437
db.Close();
2538
return 0;
2639
}
2740
```
2841

29-
### Creating and dropping column families
42+
### Column Family Management
3043
```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+
}
3461

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;
4267

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");
4870
```
4971

50-
#### Get
72+
### Basic Key-Value Operations
5173
```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 */
5285
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);
5490
```
5591
56-
#### Deleting data
92+
### Range Queries
5793
```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);
59106
```
60107

61-
## Transactions
62-
You can add operations to a transaction and commit them atomically.
108+
### Transactions
63109
```cpp
64110
TidesDB::Txn txn(&db);
65111
txn.Begin();
66-
txn.Put(&key, &value, std::chrono::seconds(3600));
67-
txn.Commit();
68-
```
69112

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));
75117

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(); */
80134
```
81135
136+
### Cursors
82137
```cpp
83-
TidesDB::Cursor cursor(&db, "my_column_family");
138+
TidesDB::Cursor cursor(&db, "users");
84139
cursor.Init();
85140
86141
std::vector<uint8_t> key, value;
87142
while (cursor.Get(key, value) == 0) {
88-
// Process key and value
143+
/* Process key and value */
144+
145+
/* Move to next entry */
89146
cursor.Next();
147+
148+
/* Or move to previous entry
149+
* cursor.Prev(); */
90150
}
91-
```
92151
152+
```
93153

94-
## Compactions
95-
You can manually trigger a compaction.
154+
### Compaction Management
96155
```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);
98161
```
99162

100-
Or you can start incremental background merge compactions.
163+
### Exception Handling Example
101164
```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+
}
103172
```

0 commit comments

Comments
 (0)