Shows how to create column-oriented data and access individual columns.
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiredtiger.h>
static const char *home;
typedef struct {
char country[5];
uint16_t year;
uint64_t population;
} POP_RECORD;
static POP_RECORD pop_data[] = {
{ "AU", 1900, 4000000 },
{ "AU", 2000, 19053186 },
{ "CAN", 1900, 5500000 },
{ "CAN", 2000, 31099561 },
{ "UK", 1900, 369000000 },
{ "UK", 2000, 59522468 },
{ "USA", 1900, 76212168 },
{ "USA", 2000, 301279593 },
{ "", 0, 0 }
};
int
main(void)
{
POP_RECORD *p;
const char *country;
uint64_t recno, population;
uint16_t year;
int ret;
if (getenv("WIREDTIGER_HOME") == NULL) {
home = "WT_HOME";
ret = system("rm -rf WT_HOME && mkdir WT_HOME");
} else
home = NULL;
fprintf(stderr, "Error connecting to %s: %s\n",
return (ret);
}
ret = session->
create(session,
"table:poptable",
"key_format=r,"
"value_format=5sHQ,"
"columns=(id,country,year,population),"
"colgroups=(main,population)");
ret = session->
create(session,
"colgroup:poptable:main", "columns=(country,year,population)");
ret = session->
create(session,
"colgroup:poptable:population", "columns=(population)");
ret = session->
create(session,
"index:poptable:country", "columns=(country)");
ret = session->
create(session,
"index:poptable:country_plus_year", "columns=(country,year)");
ret = session->
create(session,
"index:poptable:immutable_year", "columns=(year),immutable");
session, "table:poptable", NULL, "append", &cursor);
for (p = pop_data; p->year != 0; p++) {
cursor->
set_value(cursor, p->country, p->year, p->population);
}
ret = cursor->
close(cursor);
"table:poptable", NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_key(cursor, &recno);
ret = cursor->
get_value(cursor, &country, &year, &population);
cursor->
set_value(cursor, country, year, population + 1);
}
ret = cursor->
close(cursor);
"table:poptable", NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_key(cursor, &recno);
ret = cursor->
get_value(cursor, &country, &year, &population);
printf("ID %" PRIu64, recno);
printf(": country %s, year %u, population %" PRIu64 "\n",
country, year, population);
}
ret = cursor->
close(cursor);
"table:poptable", NULL, "raw", &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_key(cursor, &key);
printf("ID %" PRIu64, recno);
"5sHQ", &country, &year, &population);
printf(": country %s, year %u, population %" PRIu64 "\n",
country, year, population);
}
ret = cursor->
close(cursor);
session, "colgroup:poptable:main", NULL, NULL, &cursor);
if ((ret = cursor->
search(cursor)) == 0) {
ret = cursor->
get_value(cursor, &country, &year, &population);
printf("ID 2: country %s, year %u, population %" PRIu64 "\n",
country, year, population);
}
ret = cursor->
close(cursor);
"colgroup:poptable:population", NULL, NULL, &cursor);
if ((ret = cursor->
search(cursor)) == 0) {
ret = cursor->
get_value(cursor, &population);
printf("ID 2: population %" PRIu64 "\n", population);
}
ret = cursor->
close(cursor);
"index:poptable:country", NULL, NULL, &cursor);
cursor->
set_key(cursor,
"AU\0\0\0");
ret = cursor->
get_value(cursor, &country, &year, &population);
printf("AU: country %s, year %u, population %" PRIu64 "\n",
country, (unsigned int)year, population);
ret = cursor->
close(cursor);
"index:poptable:country_plus_year", NULL, NULL, &cursor);
cursor->
set_key(cursor,
"USA\0\0", (uint16_t)1900);
ret = cursor->
get_value(cursor, &country, &year, &population);
printf("US 1900: country %s, year %u, population %" PRIu64 "\n",
country, (unsigned int)year, population);
ret = cursor->
close(cursor);
"table:poptable(country,year)", NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_value(cursor, &country, &year);
printf("country %s, year %u\n", country, year);
}
ret = cursor->
close(cursor);
"table:poptable(country,year)", NULL, "raw", &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
session, value.
data, value.
size,
"5sH", &country, &year);
printf("country %s, year %u\n", country, year);
}
ret = cursor->
close(cursor);
"index:poptable:country_plus_year(id)", NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_key(cursor, &country, &year);
printf("row ID %" PRIu64 ": country %s, year %u\n",
recno, country, year);
}
ret = cursor->
close(cursor);
"index:poptable:country_plus_year(population)",
NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_key(cursor, &country, &year);
ret = cursor->
get_value(cursor, &population);
printf("population %" PRIu64 ": country %s, year %u\n",
population, country, year);
}
ret = cursor->
close(cursor);
"index:poptable:country_plus_year()", NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_key(cursor, &country, &year);
printf("country %s, year %u\n", country, year);
}
ret = cursor->
close(cursor);
ret = conn->
close(conn, NULL);
return (ret);
}