Created
April 26, 2017 18:59
-
-
Save emmiegit/fea60d8db1bf3dddaadb8441e194f9d3 to your computer and use it in GitHub Desktop.
Fill a Helium datastore with items to be read
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <he.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <time.h> | |
#define URL "he://./path_to_dev_here" | |
#define ITEMS 100 | |
static void rand_chars(char *buf, size_t *len) | |
{ | |
size_t i; | |
int ch; | |
*len = (size_t)((rand() % 10) + 3); | |
for (i = 0; i < *len; i++) { | |
ch = rand() % 52; | |
if (ch < 26) { | |
buf[i] = 'a' + ch; | |
} | |
else { | |
buf[i] = 'A' + ch - 26; | |
} | |
} | |
} | |
int main(void) | |
{ | |
struct he_item item; | |
char key[256], val[512]; | |
he_t he1, he2; | |
size_t i; | |
srand(time(NULL)); | |
item.key = key; | |
item.val = val; | |
he1 = he_open(URL, | |
"sequential", | |
HE_O_VOLUME_CREATE | HE_O_VOLUME_TRUNCATE | HE_O_CREATE | HE_O_TRUNCATE, | |
NULL); | |
if (!he1) { | |
he_perror("he_open"); | |
return 1; | |
} | |
he2 = he_open(URL, | |
"empty", | |
HE_O_CREATE, | |
NULL); | |
if (!he2) { | |
he_perror("he_open"); | |
he_close(he1); | |
return 1; | |
} | |
he_close(he2); | |
he2 = he_open(URL, | |
"big", | |
HE_O_CREATE, | |
NULL); | |
if (!he2) { | |
he_perror("he_open"); | |
he_close(he1); | |
return 1; | |
} | |
item.key_len = sprintf(key, "AAA"); | |
item.val_len = sizeof(val); | |
memset(val, 'a', item.val_len); | |
if (he_insert(he2, &item)) { | |
he_perror("he_insert"); | |
he_close(he1); | |
he_close(he2); | |
return 1; | |
} | |
he_close(he2); | |
he2 = he_open(URL, | |
"random", | |
HE_O_CREATE, | |
NULL); | |
if (!he2) { | |
he_perror("he_open"); | |
he_close(he1); | |
return 1; | |
} | |
for (i = 0; i < ITEMS; i++) { | |
item.key_len = sprintf(key, "%lu", i); | |
rand_chars(item.val, &item.val_len); | |
if (he_insert(he1, &item)) { | |
he_perror("he_insert"); | |
he_close(he1); | |
he_close(he2); | |
return 1; | |
} | |
item.key_len = sprintf(key, "%d", rand()); | |
rand_chars(item.val, &item.val_len); | |
if (he_update(he2, &item)) { | |
he_perror("he_update"); | |
he_close(he1); | |
he_close(he2); | |
return 1; | |
} | |
} | |
he_close(he1); | |
he_close(he2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment