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", 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(void)
{
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;
if (getenv("WIREDTIGER_HOME") == NULL) {
home = "WT_HOME";
ret = system("rm -rf WT_HOME && mkdir WT_HOME");
} else
home = NULL;
home, NULL, "create,statistics=(fast)", &conn)) != 0) {
fprintf(stderr, "Error connecting to %s: %s\n",
return (EXIT_FAILURE);
}
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 %" PRIu16 ", 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 %" PRIu16 ", 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 %" PRIu16 ", 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 %" PRIu16 ", population %" PRIu64 "\n",
country, 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 %" PRIu16 ", population %" PRIu64 "\n",
country, 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 %" PRIu16 "\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 %" PRIu16 "\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 %" PRIu16 "\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 %" PRIu16 "\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 %" PRIu16 "\n", country, year);
}
ret = cursor->
close(cursor);
"join:table:poptable", NULL, NULL, &join_cursor);
"index:poptable:country", NULL, NULL, &country_cursor);
"index:poptable:immutable_year", NULL, NULL, &year_cursor);
country_cursor->
set_key(country_cursor,
"AU\0\0\0");
ret = country_cursor->
search(country_cursor);
ret = session->
join(session, join_cursor, country_cursor,
"compare=eq,count=10");
year_cursor->
set_key(year_cursor, (uint16_t)1900);
ret = year_cursor->
search(year_cursor);
ret = session->
join(session, join_cursor, year_cursor,
"compare=gt,count=10,strategy=bloom");
while ((ret = join_cursor->
next(join_cursor)) == 0) {
ret = join_cursor->
get_key(join_cursor, &recno);
ret = join_cursor->
get_value(join_cursor, &country, &year,
&population);
printf("ID %" PRIu64, recno);
printf(
": country %s, year %" PRIu16 ", population %" PRIu64 "\n",
country, year, population);
}
"statistics:join",
join_cursor, NULL, &stat_cursor);
ret = stat_cursor->
close(stat_cursor);
ret = join_cursor->
close(join_cursor);
ret = year_cursor->
close(year_cursor);
ret = country_cursor->
close(country_cursor);
"join:table:poptable", NULL, NULL, &join_cursor);
"join:table:poptable", NULL, NULL, &subjoin_cursor);
"index:poptable:country", NULL, NULL, &country_cursor);
"index:poptable:country", NULL, NULL, &country_cursor2);
"index:poptable:immutable_year", NULL, NULL, &year_cursor);
country_cursor->
set_key(country_cursor,
"AU\0\0\0");
ret = country_cursor->
search(country_cursor);
ret = session->
join(session, subjoin_cursor, country_cursor,
"operation=or,compare=eq,count=10");
country_cursor2->
set_key(country_cursor2,
"UK\0\0\0");
ret = country_cursor2->
search(country_cursor2);
ret = session->
join(session, subjoin_cursor, country_cursor2,
"operation=or,compare=eq,count=10");
ret = session->
join(session, join_cursor, subjoin_cursor, NULL);
year_cursor->
set_key(year_cursor, (uint16_t)1900);
ret = year_cursor->
search(year_cursor);
ret = session->
join(session, join_cursor, year_cursor,
"compare=gt,count=10,strategy=bloom");
while ((ret = join_cursor->
next(join_cursor)) == 0) {
ret = join_cursor->
get_key(join_cursor, &recno);
ret = join_cursor->
get_value(join_cursor, &country, &year,
&population);
printf("ID %" PRIu64, recno);
printf(
": country %s, year %" PRIu16 ", population %" PRIu64 "\n",
country, year, population);
}
ret = join_cursor->
close(join_cursor);
ret = subjoin_cursor->
close(subjoin_cursor);
ret = country_cursor->
close(country_cursor);
ret = country_cursor2->
close(country_cursor2);
ret = year_cursor->
close(year_cursor);
ret = conn->
close(conn, NULL);
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}