aboutsummaryrefslogtreecommitdiff
path: root/examples/print.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/print.c')
-rw-r--r--examples/print.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/examples/print.c b/examples/print.c
new file mode 100644
index 0000000..066d33c
--- /dev/null
+++ b/examples/print.c
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+
+#include <hkvs.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+int main(int argc, char **argv) {
+ argc--;
+ argv++;
+ if(argc % 2 != 0) {
+ return 1;
+ }
+
+ (void)argc;
+ (void)argv;
+
+ int dfd = open("dbdir", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if(dfd < 0) {
+ fprintf(stderr, "Failed to open dbdir: %s\n", strerror(errno));
+ return 1;
+ }
+
+ hkvs_io *io = hkvs_io_new_unsafe_dirfd(dfd);
+ if(!io) {
+ fprintf(stderr, "Failed to create hkvs_io\n");
+ return 1;
+ }
+
+ hkvs *c;
+ int r = hkvs_new(&c, io);
+ if(r < 0) {
+ fprintf(stderr, "Failed to open database: %s\n", strerror(-r));
+ return 1;
+ }
+
+ for(size_t i = 0;; i++) {
+ hkvs_table *t = hkvs_table_at(c, i);
+ if(!t) break;
+ size_t key_size = hkvs_table_key_size(c, t);
+
+ printf("Table %zu: %u [key size = %zu]\n", i, hkvs_table_id(c, t), key_size);
+
+ hkvs_iter it;
+ hkvs_iter_open(c, t, &it);
+ const char *key;
+ for(hkvs_rid rid; hkvs_rid_ok((rid = hkvs_iter_next(c, t, &it, &key)));) {
+ char *val;
+ size_t val_size;
+ r = hkvs_record_value(c, t, rid, &val, &val_size);
+ if(r < 0) {
+ fprintf(stderr, "Error: %s\n", strerror(-r));
+ return 1;
+ }
+
+ printf("[%llu] %.*s: [%zu] %.*s\n", (unsigned long long)rid.id, (int)key_size, key, val_size, (int)val_size, val);
+ }
+ }
+
+ hkvs_free(c);
+}