-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathsplinterdb_wide_values_example.c
More file actions
118 lines (95 loc) · 3.89 KB
/
Copy pathsplinterdb_wide_values_example.c
File metadata and controls
118 lines (95 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2022-2026 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
/*
* SplinterDB Wide values APIs example program.
*/
#include <stdio.h>
#include <string.h>
#include "splinterdb/default_data_config.h"
#include "splinterdb/splinterdb.h"
#define DB_FILE_NAME "splinterdb_wide_values_example_db"
#define DB_FILE_SIZE_MB 1024 // Size of SplinterDB device; Fixed when created
#define CACHE_SIZE_MB 64 // Size of cache; can be changed across boots
/* Largest key buffer used by this example. */
#define USER_KEY_BUF_SIZE ((int)100)
/* Avg value size and max value sizes we expect to deal with in this program */
#define USER_AVG_VALUE_SIZE ((int)128)
#define USER_MAX_VALUE_SIZE ((int)1024)
/*
* -------------------------------------------------------------------------------
* main() for SplinterDB program handling inserts & lookups of wide values.
* -------------------------------------------------------------------------------
*/
int
main()
{
printf(" **** SplinterDB Wide Values APIs example program ****\n\n");
// Initialize data configuration, using default key-comparison handling.
data_config splinter_data_cfg;
default_data_config_init(&splinter_data_cfg);
// Basic configuration of a SplinterDB instance
splinterdb_config splinterdb_cfg;
memset(&splinterdb_cfg, 0, sizeof(splinterdb_cfg));
splinterdb_cfg.filename = DB_FILE_NAME;
splinterdb_cfg.disk_size = (DB_FILE_SIZE_MB * 1024 * 1024);
splinterdb_cfg.cache_size = (CACHE_SIZE_MB * 1024 * 1024);
splinterdb_cfg.data_cfg = &splinter_data_cfg;
splinterdb *spl_handle = NULL; // To a running SplinterDB instance
int rc = splinterdb_create(&splinterdb_cfg, &spl_handle);
printf("Created SplinterDB instance, dbname '%s'.\n\n", DB_FILE_NAME);
// -- ACTION IS HERE --
char key_buf[USER_KEY_BUF_SIZE];
char val_buf[USER_MAX_VALUE_SIZE];
int nrows = 0;
// Insert few values doubling value-size for each new key inserted
for (int val_len = 16; val_len <= USER_MAX_VALUE_SIZE;
val_len <<= 1, nrows++)
{
snprintf(key_buf, sizeof(key_buf), "Key with val_len=%d", val_len);
memset(val_buf, 'z', val_len);
slice key = slice_create(strlen(key_buf), key_buf);
slice value = slice_create(val_len, val_buf);
rc = splinterdb_insert(spl_handle, key, value, NULL);
if (rc) {
break;
}
}
printf("Inserted %d rows with wide-values\n", nrows);
nrows = 0;
// Initialize a result struct, providing an output buffer for use
char outbuf[USER_AVG_VALUE_SIZE];
splinterdb_lookup_result result;
splinterdb_lookup_result_init(
spl_handle, &result, SPLINTERDB_LOOKUP_VALUE, sizeof(outbuf), outbuf);
printf("Retrieve values of different lengths using output buffer of"
" fixed size=%lu bytes:\n",
sizeof(outbuf));
// Lookup keys which have increasingly wider-values, using a small fixed size
// output buffer. When necessary, memory will be allocated for wider values.
for (int val_len = 16; val_len <= USER_MAX_VALUE_SIZE;
val_len <<= 1, nrows++)
{
char key_buf[USER_KEY_BUF_SIZE];
snprintf(key_buf, sizeof(key_buf), "Key with val_len=%d", val_len);
size_t key_len = strlen(key_buf);
slice key = slice_create(key_len, key_buf);
rc = splinterdb_lookup(spl_handle, key, &result);
slice value;
rc = splinterdb_lookup_result_value(&result, &value);
if (!rc) {
printf(
" [%d] Found key (key_len=%d): '%.*s', value length found = %lu\n",
nrows,
(int)key_len,
(int)key_len,
key_buf,
slice_length(value));
} else {
printf("Did not find key '%.*s'.\n", (int)key_len, key_buf);
}
}
splinterdb_lookup_result_deinit(&result);
splinterdb_close(&spl_handle);
printf("Shutdown SplinterDB instance, dbname '%s'.\n\n", DB_FILE_NAME);
return rc;
}