Skip to content

Instantly share code, notes, and snippets.

@gm42
Last active May 6, 2024 17:28
Show Gist options
  • Save gm42/a61d2fff82b9dddc47a449579c9307b8 to your computer and use it in GitHub Desktop.
Save gm42/a61d2fff82b9dddc47a449579c9307b8 to your computer and use it in GitHub Desktop.
FoundationDB C Example - Hello World!

FoundationDB Hello World example in C

This is an example C program which sets a key, commits a transaction and then reads it back using a new transaction.

Compile it with:

$ clang -ofdbhello fdbhello.c -Wall -lfdb_c -I/usr/local/include

That's all!

Based on https://gist.github.com/josephg/7fa89ec0f7cfa2d5dd66c352dd0bebfa

/*
Example C API Hello World program for FoundationDB v7.1+
Author: gm42
Compile it with:
```
$ clang -ofdbhello fdbhello.c -Wall -lfdb_c -I/usr/local/include
```
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// See also: https://apple.github.io/foundationdb/api-c.html
#define FDB_API_VERSION 710
#include <foundationdb/fdb_c.h>
FDBDatabase *database;
void abort_on_error(fdb_error_t err) {
if (err) {
fprintf(stderr, "Error: %s\n", fdb_get_error(err));
abort();
}
}
void *run_net(void *_unused) {
abort_on_error(fdb_run_network());
return NULL;
}
FDBDatabase *get_database(const char *cluster_file) {
FDBDatabase *out;
abort_on_error(fdb_create_database(cluster_file, &out));
return out;
}
void setValueAndCommit(const char *key, const char *value) {
FDBTransaction *txn;
abort_on_error(fdb_database_create_transaction(database, &txn));
fdb_transaction_set(txn, (uint8_t *)key, strlen(key), (uint8_t *)value,
strlen(value));
FDBFuture *cf = fdb_transaction_commit(txn);
abort_on_error(fdb_future_block_until_ready(cf));
fdb_future_destroy(cf);
printf("fdbhello: set key '%s' to value '%s'\n", key, value);
}
void getValue(const char *key) {
FDBTransaction *txn;
abort_on_error(fdb_database_create_transaction(database, &txn));
FDBFuture *rf = fdb_transaction_get(txn, (uint8_t *)key, strlen(key), 0);
abort_on_error(fdb_future_block_until_ready(rf));
fdb_bool_t present;
const uint8_t *out_value = NULL;
int out_value_length;
abort_on_error(
fdb_future_get_value(rf, &present, &out_value, &out_value_length));
fdb_future_destroy(rf);
if (present) {
printf("fdbhello: value for key '%s' is '%s'\n", key, out_value);
} else {
printf("fdbhello: key '%s' not found\n", key);
}
// read-only transactions don't need to be committed
fdb_transaction_destroy(txn);
}
int main(int argc, char *argv[]) {
char *clusterFilePath;
if (argc == 2) {
clusterFilePath = argv[1];
} else {
fprintf(stderr, "Usage: fdbhello /etc/foundationdb/fdb.cluster\n");
exit(1);
}
fdb_select_api_version(FDB_API_VERSION);
printf("fdbhello: selected API version %d\n", FDB_API_VERSION);
abort_on_error(fdb_setup_network());
pthread_t net_thread;
int rv = pthread_create(&net_thread, NULL, run_net, NULL);
if (rv) {
fprintf(stderr, "fdbhello: failed to create network thread\n");
exit(rv);
}
database = get_database(clusterFilePath);
printf("fdbhello: connected to cluster described by %s\n", clusterFilePath);
const char *key = "/fdbhello/test/value";
// set the value of a key
setValueAndCommit(key, "hello world!");
// then read it back
getValue(key);
fdb_database_destroy(database);
abort_on_error(fdb_stop_network());
pthread_join(net_thread, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment