Shows how to create column-oriented data and access individual columns.
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <wiredtiger.h>
const char *home = NULL;
typedef struct {
char country[5];
uint16_t year;
uint64_t population;
} POP_RECORD;
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 (ret != 0)
fprintf(stderr, "Error connecting to %s: %s\n",
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)");
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);
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);
}