0
# C API Reference
1
2
The FoundationDB C API provides the core native interface to the database engine. All other language bindings are built on top of this C library (`libfdb_c`). This API offers direct control over network management, database connections, transactions, and asynchronous operations.
3
4
## Core Headers
5
6
- `fdb_c.h` - Main C API header
7
- `fdb_c_types.h` - Type definitions
8
- `fdb_c_options.g.h` - Generated configuration options
9
10
## Capabilities
11
12
### Network Management
13
14
Network initialization and lifecycle management for the FoundationDB client.
15
16
```c { .api }
17
/**
18
* Select the API version to use (must be called first)
19
* @param version API version (typically 740 for v7.4.0+)
20
* @return Error code (0 on success)
21
*/
22
fdb_error_t fdb_select_api_version(int version);
23
24
/**
25
* Initialize the network subsystem
26
* @return Error code (0 on success)
27
*/
28
fdb_error_t fdb_setup_network(void);
29
30
/**
31
* Start the network thread (blocking call)
32
* @return Error code (0 on success)
33
*/
34
fdb_error_t fdb_run_network(void);
35
36
/**
37
* Stop the network thread
38
* @return Error code (0 on success)
39
*/
40
fdb_error_t fdb_stop_network(void);
41
42
/**
43
* Set network-level configuration options
44
* @param option Option identifier
45
* @param value Option value
46
* @param value_length Length of value
47
* @return Error code (0 on success)
48
*/
49
fdb_error_t fdb_network_set_option(FDBNetworkOption option, uint8_t const* value, int value_length);
50
```
51
52
**Usage Example:**
53
54
```c
55
// Initialize FoundationDB client
56
fdb_select_api_version(740);
57
fdb_setup_network();
58
59
// Start network in separate thread or use fdb_run_network() blocking call
60
pthread_t network_thread;
61
pthread_create(&network_thread, NULL, (void*)fdb_run_network, NULL);
62
63
// Application code here...
64
65
// Cleanup
66
fdb_stop_network();
67
pthread_join(network_thread, NULL);
68
```
69
70
### Database Operations
71
72
Database connection and management operations.
73
74
```c { .api }
75
/**
76
* Create database handle from cluster file
77
* @param cluster_file_path Path to cluster file (NULL for default)
78
* @param out_database Output database handle
79
* @return Error code (0 on success)
80
*/
81
fdb_error_t fdb_create_database(const char* cluster_file_path, FDBDatabase** out_database);
82
83
/**
84
* Create database handle from connection string
85
* @param connection_string Connection string
86
* @param out_database Output database handle
87
* @return Error code (0 on success)
88
*/
89
fdb_error_t fdb_create_database_from_connection_string(const char* connection_string, FDBDatabase** out_database);
90
91
/**
92
* Release database handle
93
* @param database Database handle to release
94
*/
95
void fdb_database_destroy(FDBDatabase* database);
96
97
/**
98
* Set database-level configuration options
99
* @param database Database handle
100
* @param option Option identifier
101
* @param value Option value
102
* @param value_length Length of value
103
* @return Error code (0 on success)
104
*/
105
fdb_error_t fdb_database_set_option(FDBDatabase* database, FDBDatabaseOption option, uint8_t const* value, int value_length);
106
107
/**
108
* Create new transaction
109
* @param database Database handle
110
* @param out_transaction Output transaction handle
111
* @return Error code (0 on success)
112
*/
113
fdb_error_t fdb_database_create_transaction(FDBDatabase* database, FDBTransaction** out_transaction);
114
115
/**
116
* Open tenant handle for multi-tenancy
117
* @param database Database handle
118
* @param tenant_name Tenant name
119
* @param tenant_name_length Length of tenant name
120
* @param out_tenant Output tenant handle
121
* @return Error code (0 on success)
122
*/
123
fdb_error_t fdb_database_open_tenant(FDBDatabase* database, uint8_t const* tenant_name, int tenant_name_length, FDBTenant** out_tenant);
124
```
125
126
### Transaction Operations
127
128
ACID transaction operations for reading and writing data.
129
130
```c { .api }
131
/**
132
* Release transaction handle
133
* @param transaction Transaction handle to release
134
*/
135
void fdb_transaction_destroy(FDBTransaction* transaction);
136
137
/**
138
* Read single key (asynchronous)
139
* @param transaction Transaction handle
140
* @param key_name Key to read
141
* @param key_name_length Length of key
142
* @param snapshot Whether to use snapshot read
143
* @return Future handle for result
144
*/
145
FDBFuture* fdb_transaction_get(FDBTransaction* transaction, uint8_t const* key_name, int key_name_length, fdb_bool_t snapshot);
146
147
/**
148
* Read key range (asynchronous)
149
* @param transaction Transaction handle
150
* @param begin_key_name Start key
151
* @param begin_key_name_length Length of start key
152
* @param begin_or_equal Whether start key is inclusive
153
* @param begin_offset Start key offset
154
* @param end_key_name End key
155
* @param end_key_name_length Length of end key
156
* @param end_or_equal Whether end key is inclusive
157
* @param end_offset End key offset
158
* @param limit Maximum number of key-value pairs (0 for no limit)
159
* @param target_bytes Target bytes to read (0 for default)
160
* @param mode Streaming mode
161
* @param iteration Iteration number
162
* @param snapshot Whether to use snapshot read
163
* @param reverse Whether to read in reverse order
164
* @return Future handle for result
165
*/
166
FDBFuture* fdb_transaction_get_range(FDBTransaction* transaction, uint8_t const* begin_key_name, int begin_key_name_length, fdb_bool_t begin_or_equal, int begin_offset, uint8_t const* end_key_name, int end_key_name_length, fdb_bool_t end_or_equal, int end_offset, int limit, int target_bytes, FDBStreamingMode mode, int iteration, fdb_bool_t snapshot, fdb_bool_t reverse);
167
168
/**
169
* Write key-value pair
170
* @param transaction Transaction handle
171
* @param key_name Key to write
172
* @param key_name_length Length of key
173
* @param value Value to write
174
* @param value_length Length of value
175
*/
176
void fdb_transaction_set(FDBTransaction* transaction, uint8_t const* key_name, int key_name_length, uint8_t const* value, int value_length);
177
178
/**
179
* Delete single key
180
* @param transaction Transaction handle
181
* @param key_name Key to delete
182
* @param key_name_length Length of key
183
*/
184
void fdb_transaction_clear(FDBTransaction* transaction, uint8_t const* key_name, int key_name_length);
185
186
/**
187
* Delete key range
188
* @param transaction Transaction handle
189
* @param begin_key_name Start key
190
* @param begin_key_name_length Length of start key
191
* @param end_key_name End key
192
* @param end_key_name_length Length of end key
193
*/
194
void fdb_transaction_clear_range(FDBTransaction* transaction, uint8_t const* begin_key_name, int begin_key_name_length, uint8_t const* end_key_name, int end_key_name_length);
195
196
/**
197
* Commit transaction (asynchronous)
198
* @param transaction Transaction handle
199
* @return Future handle for commit result
200
*/
201
FDBFuture* fdb_transaction_commit(FDBTransaction* transaction);
202
203
/**
204
* Handle transaction errors and determine retry logic
205
* @param transaction Transaction handle
206
* @param error Error code from previous operation
207
* @return Future handle for retry decision
208
*/
209
FDBFuture* fdb_transaction_on_error(FDBTransaction* transaction, fdb_error_t error);
210
211
/**
212
* Set transaction-level configuration options
213
* @param transaction Transaction handle
214
* @param option Option identifier
215
* @param value Option value
216
* @param value_length Length of value
217
* @return Error code (0 on success)
218
*/
219
fdb_error_t fdb_transaction_set_option(FDBTransaction* transaction, FDBTransactionOption option, uint8_t const* value, int value_length);
220
```
221
222
### Future Operations
223
224
Asynchronous result handling and synchronization.
225
226
```c { .api }
227
/**
228
* Release future handle
229
* @param future Future handle to release
230
*/
231
void fdb_future_destroy(FDBFuture* future);
232
233
/**
234
* Block until future is ready
235
* @param future Future handle
236
* @return Error code (0 on success)
237
*/
238
fdb_error_t fdb_future_block_until_ready(FDBFuture* future);
239
240
/**
241
* Check if future is ready (non-blocking)
242
* @param future Future handle
243
* @return 1 if ready, 0 if not ready
244
*/
245
fdb_bool_t fdb_future_is_ready(FDBFuture* future);
246
247
/**
248
* Set completion callback
249
* @param future Future handle
250
* @param callback Callback function
251
* @param callback_parameter Parameter passed to callback
252
* @return Error code (0 on success)
253
*/
254
fdb_error_t fdb_future_set_callback(FDBFuture* future, FDBCallback callback, void* callback_parameter);
255
256
/**
257
* Get error code from future
258
* @param future Future handle
259
* @return Error code (0 if no error)
260
*/
261
fdb_error_t fdb_future_get_error(FDBFuture* future);
262
263
/**
264
* Get value result from read operation
265
* @param future Future handle from fdb_transaction_get
266
* @param out_present Whether value was found
267
* @param out_value Pointer to value data
268
* @param out_value_length Length of value data
269
* @return Error code (0 on success)
270
*/
271
fdb_error_t fdb_future_get_value(FDBFuture* future, fdb_bool_t* out_present, uint8_t const** out_value, int* out_value_length);
272
273
/**
274
* Get key-value array from range operation
275
* @param future Future handle from fdb_transaction_get_range
276
* @param out_kv Pointer to key-value array
277
* @param out_count Number of key-value pairs
278
* @param out_more Whether more data is available
279
* @return Error code (0 on success)
280
*/
281
fdb_error_t fdb_future_get_keyvalue_array(FDBFuture* future, FDBKeyValue const** out_kv, int* out_count, fdb_bool_t* out_more);
282
```
283
284
### Error Handling
285
286
Error code handling and error description functions.
287
288
```c { .api }
289
/**
290
* Get human-readable error description
291
* @param code Error code
292
* @return Error description string
293
*/
294
const char* fdb_get_error(fdb_error_t code);
295
296
/**
297
* Check error conditions using predicate functions
298
* @param predicate_type Predicate type
299
* @param code Error code
300
* @return 1 if condition is true, 0 otherwise
301
*/
302
fdb_bool_t fdb_error_predicate(int predicate_type, fdb_error_t code);
303
```
304
305
### Administrative Database Operations
306
307
Advanced database management and monitoring functions.
308
309
```c { .api }
310
/**
311
* Reboot worker on specified address
312
* @param database Database handle
313
* @param address Worker address to reboot
314
* @param address_length Length of address
315
* @param check_exists Verify worker exists before rebooting
316
* @param duration Reboot duration
317
* @return Future handle for result
318
*/
319
FDBFuture* fdb_database_reboot_worker(FDBDatabase* database, uint8_t const* address, int address_length,
320
fdb_bool_t check_exists, int duration);
321
322
/**
323
* Force recovery to specified configuration
324
* @param database Database handle
325
* @param description_xml Recovery configuration as XML
326
* @param description_xml_length Length of XML description
327
* @return Future handle for result
328
*/
329
FDBFuture* fdb_database_force_recovery_with_data_loss(FDBDatabase* database, uint8_t const* description_xml,
330
int description_xml_length);
331
332
/**
333
* Create snapshot for backup
334
* @param database Database handle
335
* @param uid Snapshot identifier
336
* @param uid_length Length of identifier
337
* @param snap_command Snapshot command
338
* @param snap_command_length Length of command
339
* @return Future handle for result
340
*/
341
FDBFuture* fdb_database_create_snapshot(FDBDatabase* database, uint8_t const* uid, int uid_length,
342
uint8_t const* snap_command, int snap_command_length);
343
344
/**
345
* Get client status information
346
* @param database Database handle
347
* @return Future handle containing status JSON
348
*/
349
FDBFuture* fdb_database_get_client_status(FDBDatabase* database);
350
351
/**
352
* Get server protocol version information
353
* @param database Database handle
354
* @param expected_version Expected protocol version
355
* @return Future handle for result
356
*/
357
FDBFuture* fdb_database_get_server_protocol(FDBDatabase* database, uint64_t expected_version);
358
359
/**
360
* Get main thread business metric
361
* @param database Database handle
362
* @return Main thread business as double
363
*/
364
double fdb_database_get_main_thread_busyness(FDBDatabase* database);
365
```
366
367
### Locality and Topology Operations
368
369
Functions for querying cluster topology and key distribution.
370
371
```c { .api }
372
/**
373
* Get addresses of servers storing specified key
374
* @param transaction Transaction handle
375
* @param key_name Key to query
376
* @param key_name_length Length of key
377
* @return Future handle containing string array of addresses
378
*/
379
FDBFuture* fdb_transaction_get_addresses_for_key(FDBTransaction* transaction, uint8_t const* key_name,
380
int key_name_length);
381
382
/**
383
* Get estimated range size in bytes
384
* @param transaction Transaction handle
385
* @param begin_key_name Start of range
386
* @param begin_key_name_length Length of start key
387
* @param end_key_name End of range
388
* @param end_key_name_length Length of end key
389
* @return Future handle containing size estimate
390
*/
391
FDBFuture* fdb_transaction_get_estimated_range_size_bytes(FDBTransaction* transaction,
392
uint8_t const* begin_key_name, int begin_key_name_length,
393
uint8_t const* end_key_name, int end_key_name_length);
394
395
/**
396
* Get range split points for load balancing
397
* @param transaction Transaction handle
398
* @param begin_key_name Start of range
399
* @param begin_key_name_length Length of start key
400
* @param end_key_name End of range
401
* @param end_key_name_length Length of end key
402
* @param chunk_size Target chunk size
403
* @return Future handle containing key array of split points
404
*/
405
FDBFuture* fdb_transaction_get_range_split_points(FDBTransaction* transaction,
406
uint8_t const* begin_key_name, int begin_key_name_length,
407
uint8_t const* end_key_name, int end_key_name_length,
408
int64_t chunk_size);
409
```
410
411
### Future Result Extraction
412
413
Functions for extracting results from completed futures.
414
415
```c { .api }
416
/**
417
* Extract string array from future
418
* @param future Future handle
419
* @param out_strings Output string array
420
* @param out_count Output count
421
* @return Error code (0 on success)
422
*/
423
fdb_error_t fdb_future_get_string_array(FDBFuture* future, const char*** out_strings, int* out_count);
424
425
/**
426
* Extract key array from future
427
* @param future Future handle
428
* @param out_keys Output key array
429
* @param out_count Output count
430
* @return Error code (0 on success)
431
*/
432
fdb_error_t fdb_future_get_key_array(FDBFuture* future, FDBKey const** out_keys, int* out_count);
433
434
/**
435
* Extract uint64 value from future
436
* @param future Future handle
437
* @param out_value Output value
438
* @return Error code (0 on success)
439
*/
440
fdb_error_t fdb_future_get_uint64(FDBFuture* future, uint64_t* out_value);
441
442
/**
443
* Extract double value from future
444
* @param future Future handle
445
* @param out_value Output value
446
* @return Error code (0 on success)
447
*/
448
fdb_error_t fdb_future_get_double(FDBFuture* future, double* out_value);
449
450
/**
451
* Extract database from future
452
* @param future Future handle
453
* @param out_database Output database handle
454
* @return Error code (0 on success)
455
*/
456
fdb_error_t fdb_future_get_database(FDBFuture* future, FDBDatabase** out_database);
457
```
458
459
## Core Data Types
460
461
```c { .api }
462
// Handle types
463
typedef struct FDB_future FDBFuture;
464
typedef struct FDB_result FDBResult;
465
typedef struct FDB_database FDBDatabase;
466
typedef struct FDB_tenant FDBTenant;
467
typedef struct FDB_transaction FDBTransaction;
468
469
// Basic types
470
typedef int fdb_error_t;
471
typedef int fdb_bool_t;
472
473
// Data structures
474
typedef struct {
475
const void* key;
476
int key_length;
477
const void* value;
478
int value_length;
479
} FDBKeyValue;
480
481
typedef struct {
482
const void* key;
483
int key_length;
484
} FDBKey;
485
486
typedef struct {
487
const void* key;
488
int key_length;
489
fdb_bool_t or_equal;
490
int offset;
491
} FDBKeySelector;
492
493
typedef struct {
494
const void* begin_key;
495
int begin_key_length;
496
const void* end_key;
497
int end_key_length;
498
} FDBKeyRange;
499
500
// Callback type
501
typedef void (*FDBCallback)(FDBFuture* future, void* callback_parameter);
502
503
// Enumerations
504
typedef enum {
505
FDB_STREAMING_MODE_WANT_ALL = -2,
506
FDB_STREAMING_MODE_ITERATOR = -1,
507
FDB_STREAMING_MODE_EXACT = 0,
508
FDB_STREAMING_MODE_SMALL = 1,
509
FDB_STREAMING_MODE_MEDIUM = 2,
510
FDB_STREAMING_MODE_LARGE = 3,
511
FDB_STREAMING_MODE_SERIAL = 4
512
} FDBStreamingMode;
513
```
514
515
## Configuration Options
516
517
### Network Options
518
519
```c { .api }
520
typedef enum {
521
FDB_NET_OPTION_LOCAL_ADDRESS,
522
FDB_NET_OPTION_CLUSTER_FILE,
523
FDB_NET_OPTION_TRACE_ENABLE,
524
FDB_NET_OPTION_TRACE_ROLL_SIZE,
525
FDB_NET_OPTION_TRACE_MAX_LOGS_SIZE,
526
FDB_NET_OPTION_TRACE_LOG_GROUP,
527
FDB_NET_OPTION_KNOB,
528
FDB_NET_OPTION_TLS_PLUGIN,
529
FDB_NET_OPTION_TLS_CERT_PATH,
530
FDB_NET_OPTION_TLS_KEY_PATH,
531
FDB_NET_OPTION_TLS_VERIFY_PEERS,
532
FDB_NET_OPTION_BUGGIFY_ENABLE,
533
FDB_NET_OPTION_BUGGIFY_DISABLE,
534
FDB_NET_OPTION_BUGGIFY_SECTION_ACTIVATED_PROBABILITY,
535
FDB_NET_OPTION_BUGGIFY_SECTION_FIRED_PROBABILITY,
536
FDB_NET_OPTION_DISABLE_MULTI_VERSION_CLIENT_API,
537
FDB_NET_OPTION_CALLBACKS_ON_EXTERNAL_THREADS,
538
FDB_NET_OPTION_EXTERNAL_CLIENT_LIBRARY,
539
FDB_NET_OPTION_EXTERNAL_CLIENT_DIRECTORY,
540
FDB_NET_OPTION_DISABLE_LOCAL_CLIENT,
541
FDB_NET_OPTION_CLIENT_THREADS_PER_VERSION,
542
FDB_NET_OPTION_FUTURE_VERSION_CLIENT_LIBRARY,
543
FDB_NET_OPTION_DISABLE_CLIENT_STATISTICS_LOGGING,
544
FDB_NET_OPTION_ENABLE_SLOW_TASK_PROFILING,
545
FDB_NET_OPTION_ENABLE_RUN_LOOP_PROFILING,
546
FDB_NET_OPTION_CLIENT_BUGGIFY_ENABLE,
547
FDB_NET_OPTION_CLIENT_BUGGIFY_DISABLE,
548
FDB_NET_OPTION_CLIENT_BUGGIFY_SECTION_ACTIVATED_PROBABILITY,
549
FDB_NET_OPTION_CLIENT_BUGGIFY_SECTION_FIRED_PROBABILITY,
550
FDB_NET_OPTION_DISTRIBUTED_CLIENT_TRACER,
551
FDB_NET_OPTION_CLIENT_TMP_DIR,
552
FDB_NET_OPTION_SUPPORTED_CLIENT_VERSIONS,
553
FDB_NET_OPTION_EXTERNAL_CLIENT,
554
FDB_NET_OPTION_EXTERNAL_CLIENT_TRANSPORT_ID,
555
FDB_NET_OPTION_RETAIN_CLIENT_LIBRARY_COPIES,
556
FDB_NET_OPTION_IGNORE_EXTERNAL_CLIENT_FAILURES,
557
FDB_NET_OPTION_DISABLE_CLIENT_BYPASS,
558
FDB_NET_OPTION_FAIL_INCOMPATIBLE_CLIENT,
559
FDB_NET_OPTION_USE_CONFIG_DATABASE
560
} FDBNetworkOption;
561
```
562
563
### Database Options
564
565
```c { .api }
566
typedef enum {
567
FDB_DB_OPTION_LOCATION_CACHE_SIZE,
568
FDB_DB_OPTION_MAX_WATCHES,
569
FDB_DB_OPTION_MACHINE_ID,
570
FDB_DB_OPTION_DATACENTER_ID,
571
FDB_DB_OPTION_SNAPSHOT_RYW_ENABLE,
572
FDB_DB_OPTION_SNAPSHOT_RYW_DISABLE,
573
FDB_DB_OPTION_TRANSACTION_LOGGING_MAX_FIELD_LENGTH,
574
FDB_DB_OPTION_TRANSACTION_TIMEOUT,
575
FDB_DB_OPTION_TRANSACTION_RETRY_LIMIT,
576
FDB_DB_OPTION_TRANSACTION_MAX_RETRY_DELAY,
577
FDB_DB_OPTION_TRANSACTION_SIZE_LIMIT,
578
FDB_DB_OPTION_TRANSACTION_CAUSAL_READ_RISKY,
579
FDB_DB_OPTION_TRANSACTION_INCLUDE_PORT_IN_ADDRESS,
580
FDB_DB_OPTION_TRANSACTION_BYPASS_STORAGE_QUOTA,
581
FDB_DB_OPTION_TRANSACTION_USE_GRV_CACHE
582
} FDBDatabaseOption;
583
```
584
585
### Transaction Options
586
587
```c { .api }
588
typedef enum {
589
FDB_TR_OPTION_CAUSAL_READ_RISKY,
590
FDB_TR_OPTION_CAUSAL_WRITE_RISKY,
591
FDB_TR_OPTION_READ_YOUR_WRITES_DISABLE,
592
FDB_TR_OPTION_READ_AHEAD_DISABLE,
593
FDB_TR_OPTION_DURABILITY_DATACENTER,
594
FDB_TR_OPTION_DURABILITY_RISKY,
595
FDB_TR_OPTION_DURABILITY_DEV_NULL_IS_WEB_SCALE,
596
FDB_TR_OPTION_PRIORITY_SYSTEM_IMMEDIATE,
597
FDB_TR_OPTION_PRIORITY_BATCH,
598
FDB_TR_OPTION_INITIALIZE_NEW_DATABASE,
599
FDB_TR_OPTION_ACCESS_SYSTEM_KEYS,
600
FDB_TR_OPTION_READ_SYSTEM_KEYS,
601
FDB_TR_OPTION_RAW_ACCESS,
602
FDB_TR_OPTION_DEBUG_DUMP,
603
FDB_TR_OPTION_DEBUG_RETRY_LOGGING,
604
FDB_TR_OPTION_TRANSACTION_LOGGING_ENABLE,
605
FDB_TR_OPTION_TIMEOUT,
606
FDB_TR_OPTION_RETRY_LIMIT,
607
FDB_TR_OPTION_MAX_RETRY_DELAY,
608
FDB_TR_OPTION_SIZE_LIMIT,
609
FDB_TR_OPTION_SNAPSHOT_RYW_ENABLE,
610
FDB_TR_OPTION_SNAPSHOT_RYW_DISABLE,
611
FDB_TR_OPTION_LOCK_AWARE,
612
FDB_TR_OPTION_USED_DURING_COMMIT_PROTECTION_DISABLE,
613
FDB_TR_OPTION_READ_LOCK_AWARE,
614
FDB_TR_OPTION_USE_PROVISIONAL_PROXIES,
615
FDB_TR_OPTION_INCLUDE_PORT_IN_ADDRESS,
616
FDB_TR_OPTION_TAG,
617
FDB_TR_OPTION_AUTO_THROTTLE_TAG,
618
FDB_TR_OPTION_SPAN_PARENT,
619
FDB_TR_OPTION_EXPENSIVE_CLEAR_COST_ESTIMATION_ENABLE,
620
FDB_TR_OPTION_USE_GRV_CACHE,
621
FDB_TR_OPTION_SKIP_GRV_CACHE,
622
FDB_TR_OPTION_BYPASS_STORAGE_QUOTA,
623
FDB_TR_OPTION_REPORT_CONFLICTING_KEYS
624
} FDBTransactionOption;
625
```
626
627
## Usage Example
628
629
```c
630
#include <fdb_c.h>
631
#include <stdio.h>
632
#include <string.h>
633
634
int main() {
635
FDBDatabase* db;
636
FDBTransaction* tr;
637
FDBFuture* f;
638
fdb_error_t err;
639
640
// Initialize FoundationDB
641
fdb_select_api_version(740);
642
fdb_setup_network();
643
644
// Start network in background thread (simplified)
645
pthread_t network_thread;
646
pthread_create(&network_thread, NULL, (void*)fdb_run_network, NULL);
647
648
// Open database
649
err = fdb_create_database(NULL, &db);
650
if (err) {
651
printf("Error creating database: %s\n", fdb_get_error(err));
652
return 1;
653
}
654
655
// Create and use transaction
656
err = fdb_database_create_transaction(db, &tr);
657
if (err) {
658
printf("Error creating transaction: %s\n", fdb_get_error(err));
659
return 1;
660
}
661
662
// Write a key-value pair
663
const char* key = "hello";
664
const char* value = "world";
665
fdb_transaction_set(tr, (uint8_t*)key, strlen(key), (uint8_t*)value, strlen(value));
666
667
// Commit transaction
668
f = fdb_transaction_commit(tr);
669
err = fdb_future_block_until_ready(f);
670
if (err) {
671
printf("Error waiting for commit: %s\n", fdb_get_error(err));
672
} else {
673
err = fdb_future_get_error(f);
674
if (err) {
675
printf("Error committing: %s\n", fdb_get_error(err));
676
} else {
677
printf("Successfully wrote key-value pair\n");
678
}
679
}
680
681
// Cleanup
682
fdb_future_destroy(f);
683
fdb_transaction_destroy(tr);
684
fdb_database_destroy(db);
685
fdb_stop_network();
686
pthread_join(network_thread, NULL);
687
688
return 0;
689
}
690
```