Shows how to create column-oriented data and access individual columns.
#include <test_util.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", 1950, 8267337},
{"AU", 2000, 19053186}, {"CAN", 1900, 5500000}, {"CAN", 1950, 14011422}, {"CAN", 2000, 31099561},
{"UK", 1900, 369000000}, {"UK", 1950, 50127000}, {"UK", 2000, 59522468}, {"USA", 1900, 76212168},
{"USA", 1950, 150697361}, {"USA", 2000, 301279593}, {"", 0, 0}};
int
main(int argc, char *argv[])
{
POP_RECORD *p;
WT_CURSOR *country_cursor, *country_cursor2, *cursor, *join_cursor, *stat_cursor,
*subjoin_cursor, *year_cursor;
const char *country;
uint64_t recno, population;
uint16_t year;
int ret;
home = example_setup(argc, argv);
error_check(
wiredtiger_open(home, NULL,
"create,statistics=(fast)", &conn));
error_check(conn->
open_session(conn, NULL, NULL, &session));
error_check(session->
create(session,
"table:poptable",
"key_format=r,value_format=5sHQ,columns=(id,country,year,population),colgroups=(main,"
"population)"));
error_check(
session->
create(session,
"colgroup:poptable:main",
"columns=(country,year,population)"));
error_check(session->
create(session,
"colgroup:poptable:population",
"columns=(population)"));
error_check(session->
create(session,
"index:poptable:country",
"columns=(country)"));
error_check(
session->
create(session,
"index:poptable:country_plus_year",
"columns=(country,year)"));
error_check(
session->
create(session,
"index:poptable:immutable_year",
"columns=(year),immutable"));
error_check(session->
open_cursor(session,
"table:poptable", NULL,
"append", &cursor));
for (p = pop_data; p->year != 0; p++) {
cursor->
set_value(cursor, p->country, p->year, p->population);
error_check(cursor->
insert(cursor));
}
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"table:poptable", NULL, NULL, &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &recno));
error_check(cursor->
get_value(cursor, &country, &year, &population));
cursor->
set_value(cursor, country, year, population + 1);
error_check(cursor->
update(cursor));
}
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"table:poptable", NULL, NULL, &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &recno));
error_check(cursor->
get_value(cursor, &country, &year, &population));
printf("ID %" PRIu64, recno);
printf(
": country %s, year %" PRIu16 ", population %" PRIu64 "\n", country, year, population);
}
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"table:poptable", NULL,
"raw", &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &key));
printf("ID %" PRIu64, recno);
error_check(cursor->
get_value(cursor, &value));
session, value.
data, value.
size,
"5sHQ", &country, &year, &population));
printf(
": country %s, year %" PRIu16 ", population %" PRIu64 "\n", country, year, population);
}
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"colgroup:poptable:main", NULL, NULL, &cursor));
error_check(cursor->
search(cursor));
error_check(cursor->
get_value(cursor, &country, &year, &population));
printf(
"ID 2: country %s, year %" PRIu16 ", population %" PRIu64 "\n", country, year, population);
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"colgroup:poptable:population", NULL, NULL, &cursor));
error_check(cursor->
search(cursor));
error_check(cursor->
get_value(cursor, &population));
printf("ID 2: population %" PRIu64 "\n", population);
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"index:poptable:country", NULL, NULL, &cursor));
cursor->
set_key(cursor,
"AU\0\0\0");
error_check(cursor->
search(cursor));
error_check(cursor->
get_value(cursor, &country, &year, &population));
printf("AU: country %s, year %" PRIu16 ", population %" PRIu64 "\n", country, year, population);
error_check(cursor->
close(cursor));
error_check(
session->
open_cursor(session,
"index:poptable:country_plus_year", NULL, NULL, &cursor));
cursor->
set_key(cursor,
"USA\0\0", (uint16_t)1900);
error_check(cursor->
search(cursor));
error_check(cursor->
get_value(cursor, &country, &year, &population));
printf(
"US 1900: country %s, year %" PRIu16 ", population %" PRIu64 "\n", country, year, population);
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"table:poptable(country,year)", NULL, NULL, &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_value(cursor, &country, &year));
printf("country %s, year %" PRIu16 "\n", country, year);
}
error_check(cursor->
close(cursor));
error_check(
session->
open_cursor(session,
"table:poptable(country,year)", NULL,
"raw", &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_value(cursor, &value));
error_check(
printf("country %s, year %" PRIu16 "\n", country, year);
}
error_check(cursor->
close(cursor));
error_check(
session->
open_cursor(session,
"index:poptable:country_plus_year(id)", NULL, NULL, &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &country, &year));
error_check(cursor->
get_value(cursor, &recno));
printf("row ID %" PRIu64 ": country %s, year %" PRIu16 "\n", recno, country, year);
}
error_check(cursor->
close(cursor));
session, "index:poptable:country_plus_year(population)", NULL, NULL, &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &country, &year));
error_check(cursor->
get_value(cursor, &population));
printf("population %" PRIu64 ": country %s, year %" PRIu16 "\n", population, country, year);
}
error_check(cursor->
close(cursor));
error_check(
session->
open_cursor(session,
"index:poptable:country_plus_year()", NULL, NULL, &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &country, &year));
printf("country %s, year %" PRIu16 "\n", country, year);
}
error_check(cursor->
close(cursor));
error_check(session->
open_cursor(session,
"join:table:poptable", NULL, NULL, &join_cursor));
error_check(
session->
open_cursor(session,
"index:poptable:country", NULL, NULL, &country_cursor));
error_check(
session->
open_cursor(session,
"index:poptable:immutable_year", NULL, NULL, &year_cursor));
country_cursor->
set_key(country_cursor,
"AU\0\0\0");
error_check(country_cursor->
search(country_cursor));
error_check(session->
join(session, join_cursor, country_cursor,
"compare=eq,count=10"));
year_cursor->
set_key(year_cursor, (uint16_t)1900);
error_check(year_cursor->
search(year_cursor));
error_check(
session->
join(session, join_cursor, year_cursor,
"compare=gt,count=10,strategy=bloom"));
while ((ret = join_cursor->
next(join_cursor)) == 0) {
error_check(join_cursor->
get_key(join_cursor, &recno));
error_check(join_cursor->
get_value(join_cursor, &country, &year, &population));
printf("ID %" PRIu64, recno);
printf(
": country %s, year %" PRIu16 ", population %" PRIu64 "\n", country, year, population);
}
error_check(session->
open_cursor(session,
"statistics:join", join_cursor, NULL, &stat_cursor));
error_check(stat_cursor->
close(stat_cursor));
error_check(join_cursor->
close(join_cursor));
error_check(year_cursor->
close(year_cursor));
error_check(country_cursor->
close(country_cursor));
error_check(session->
open_cursor(session,
"join:table:poptable", NULL, NULL, &join_cursor));
error_check(session->
open_cursor(session,
"join:table:poptable", NULL, NULL, &subjoin_cursor));
error_check(
session->
open_cursor(session,
"index:poptable:country", NULL, NULL, &country_cursor));
error_check(
session->
open_cursor(session,
"index:poptable:country", NULL, NULL, &country_cursor2));
error_check(
session->
open_cursor(session,
"index:poptable:immutable_year", NULL, NULL, &year_cursor));
country_cursor->
set_key(country_cursor,
"AU\0\0\0");
error_check(country_cursor->
search(country_cursor));
error_check(
session->
join(session, subjoin_cursor, country_cursor,
"operation=or,compare=eq,count=10"));
country_cursor2->
set_key(country_cursor2,
"UK\0\0\0");
error_check(country_cursor2->
search(country_cursor2));
error_check(
session->
join(session, subjoin_cursor, country_cursor2,
"operation=or,compare=eq,count=10"));
error_check(session->
join(session, join_cursor, subjoin_cursor, NULL));
year_cursor->
set_key(year_cursor, (uint16_t)1900);
error_check(year_cursor->
search(year_cursor));
error_check(
session->
join(session, join_cursor, year_cursor,
"compare=gt,count=10,strategy=bloom"));
while ((ret = join_cursor->
next(join_cursor)) == 0) {
error_check(join_cursor->
get_key(join_cursor, &recno));
error_check(join_cursor->
get_value(join_cursor, &country, &year, &population));
printf("ID %" PRIu64, recno);
printf(
": country %s, year %" PRIu16 ", population %" PRIu64 "\n", country, year, population);
}
error_check(join_cursor->
close(join_cursor));
error_check(subjoin_cursor->
close(subjoin_cursor));
error_check(country_cursor->
close(country_cursor));
error_check(country_cursor2->
close(country_cursor2));
error_check(year_cursor->
close(year_cursor));
error_check(conn->
close(conn, NULL));
return (EXIT_SUCCESS);
}