Shows how to extend WiredTiger with a more complex custom extractor.
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wiredtiger.h>
#define RET_OK(ret) ((ret) == 0 || (ret) == WT_NOTFOUND)
static const char *home;
struct president_data {
int id;
const char *last_name;
const char *first_name;
uint16_t term_start;
uint16_t term_end;
};
static const struct president_data example_data[] = {
{ 0, "Obama", "Barack", 2009, 2014 },
{ 1, "Bush", "George W", 2001, 2009 },
{ 2, "Clinton", "Bill", 1993, 2001 },
{ 3, "Bush", "George H", 1989, 1993 },
{ 4, "Reagan", "Ronald", 1981, 1989 },
{ 0, NULL, NULL, 0, 0 }
};
#define YEAR_BASE 1981
#define YEAR_SPAN (2014-1981)
static int
{
char *last_name, *first_name;
uint16_t term_end, term_start, year;
int ret;
(void)extractor;
(void)key;
session, value->
data, value->
size,
"SSHH",
&last_name, &first_name, &term_start, &term_end)) != 0)
return (ret);
for (year = term_start; year <= term_end; ++year) {
fprintf(stderr,
"EXTRACTOR: index op for year %" PRIu16 ": %s %s\n",
year, first_name, last_name);
result_cursor->
set_key(result_cursor, year);
if ((ret = result_cursor->
insert(result_cursor)) != 0) {
fprintf(stderr,
"EXTRACTOR: op year %" PRIu16 ": error %d\n",
year, ret);
return (ret);
}
}
return (0);
}
static int
{
(void)extractor;
(void)session;
return (0);
}
int
{
int ret;
my_extract, NULL, my_extract_terminate
};
ret = conn->
add_extractor(conn,
"my_extractor", &my_extractor, NULL);
return (ret);
}
static int
{
int i, ret;
char *first_name, *last_name;
uint16_t rec_year, term_end, term_start, year;
year = 0;
srand((unsigned int)getpid());
session, "index:presidents:term", NULL, NULL, &cursor);
for (i = 0; i < 10 && RET_OK(ret); i++) {
year = (uint16_t)((rand() % YEAR_SPAN) + YEAR_BASE);
printf("Year %" PRIu16 ":\n", year);
if ((ret = cursor->
search(cursor)) != 0)
break;
if ((ret = cursor->
get_key(cursor, &rec_year)) != 0)
break;
&last_name, &first_name, &term_start, &term_end)) != 0)
break;
while (term_start <= year &&
year <= term_end && year == rec_year) {
printf("\t%s %s\n", first_name, last_name);
if ((ret = cursor->
next(cursor)) != 0)
break;
if ((ret = cursor->
get_key(cursor, &rec_year)) != 0)
break;
if ((ret = cursor->
get_value(cursor, &last_name,
&first_name, &term_start, &term_end)) != 0)
break;
}
}
if (!RET_OK(ret))
fprintf(stderr, "Error %d for year %" PRIu16 "\n", ret, year);
ret = cursor->
close(cursor);
return (ret);
}
static int
{
struct president_data p;
int i, ret;
session, "table:presidents", NULL, NULL, &cursor);
for (i = 0; example_data[i].last_name != NULL && i < 2; i++) {
p = example_data[i];
}
return (ret);
}
static int
{
struct president_data p;
int i, ret;
ret = session->
create(session,
"table:presidents",
"key_format=I,value_format=SSHH,"
"columns=(ID,last_name,first_name,term_begin,term_end)");
ret = session->
create(session,
"index:presidents:term",
"key_format=H,columns=(term),extractor=my_extractor");
session, "table:presidents", NULL, NULL, &cursor);
for (i = 0; example_data[i].last_name != NULL; i++) {
p = example_data[i];
p.last_name, p.first_name, p.term_start, p.term_end);
fprintf(stderr,
"SETUP: table insert %" PRIu16 "-%" PRIu16 ": %s %s\n",
p.term_start, p.term_end,
p.first_name, p.last_name);
}
return (ret);
}
int
main(void)
{
int ret;
if (getenv("WIREDTIGER_HOME") == NULL) {
home = "WT_HOME";
ret = system("rm -rf WT_HOME && mkdir WT_HOME");
} else
home = NULL;
ret = add_extractor(conn);
ret = setup_table(session);
ret = read_index(session);
ret = remove_items(session);
ret = conn->
close(conn, NULL);
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}