Shows how to access the database log files.
#include <test_util.h>
static const char *home1 = "WT_HOME_LOG_1";
static const char *home2 = "WT_HOME_LOG_2";
static const char * const uri = "table:logtest";
#define CONN_CONFIG "create,cache_size=100MB,log=(archive=false,enabled=true)"
#define MAX_KEYS 10
static void
{
error_check((*wt_connp)->open_session(*wt_connp, NULL, NULL, sessionp));
error_check((*sessionp)->create(
*sessionp, uri, "key_format=S,value_format=S"));
}
static void
{
int ret;
const char *key, *key_copy, *value, *value_copy;
error_check(session->
open_cursor(session, uri, NULL, NULL, &cursor));
error_check(
sess_copy->
open_cursor(sess_copy, uri, NULL, NULL, &curs_copy));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(curs_copy->
next(curs_copy));
error_check(cursor->
get_key(cursor, &key));
error_check(cursor->
get_value(cursor, &value));
error_check(curs_copy->
get_key(curs_copy, &key_copy));
error_check(curs_copy->
get_value(curs_copy, &value_copy));
if (strcmp(key, key_copy) != 0 ||
strcmp(value, value_copy) != 0) {
fprintf(stderr,
"Mismatched: key %s, key_copy %s "
"value %s value_copy %s\n",
key, key_copy, value, value_copy);
exit (1);
}
}
error_check(cursor->
close(cursor));
ret = curs_copy->
next(curs_copy);
error_check(curs_copy->
close(curs_copy));
}
static void
print_record(uint32_t log_file, uint32_t log_offset, uint32_t opcount,
uint32_t rectype, uint32_t optype, uint64_t txnid, uint32_t fileid,
{
printf(
"LSN [%" PRIu32 "][%" PRIu32 "].%" PRIu32
": record type %" PRIu32 " optype %" PRIu32
" txnid %" PRIu64 " fileid %" PRIu32,
log_file, log_offset, opcount,
rectype, optype, txnid, fileid);
printf(
" key size %zu value size %zu\n", key->
size, value->
size);
printf(
"Application Record: %s\n", (
char *)value->
data);
}
static void
simple_walk_log(
WT_SESSION *session,
int count_min)
{
uint64_t txnid;
uint32_t fileid, log_file, log_offset, opcount, optype, rectype;
int count, ret;
error_check(session->
open_cursor(session,
"log:", NULL, NULL, &cursor));
count = 0;
while ((ret = cursor->
next(cursor)) == 0) {
count++;
cursor, &log_file, &log_offset, &opcount));
error_check(cursor->
get_value(cursor, &txnid,
&rectype, &optype, &fileid, &logrec_key, &logrec_value));
print_record(log_file, log_offset, opcount,
rectype, optype, txnid, fileid, &logrec_key, &logrec_value);
}
error_check(cursor->
close(cursor));
if (count < count_min) {
fprintf(stderr,
"Expected minimum %d records, found %d\n",
count_min, count);
exit (1);
}
}
static void
{
uint64_t txnid;
uint32_t fileid, opcount, optype, rectype;
uint32_t log_file, log_offset, save_file, save_offset;
int first, i, in_txn, ret;
setup_copy(&wt_conn2, &session2);
error_check(session->
open_cursor(session,
"log:", NULL, NULL, &cursor));
session2, uri, NULL, "raw=true", &cursor2));
i = 0;
in_txn = 0;
txnid = 0;
save_file = save_offset = 0;
while ((ret = cursor->
next(cursor)) == 0) {
cursor, &log_file, &log_offset, &opcount));
if (++i == MAX_KEYS) {
save_file = log_file;
save_offset = log_offset;
}
error_check(cursor->
get_value(cursor, &txnid, &rectype,
&optype, &fileid, &logrec_key, &logrec_value));
print_record(log_file, log_offset, opcount,
rectype, optype, txnid, fileid, &logrec_key, &logrec_value);
if (in_txn && opcount == 0) {
error_check(
in_txn = 0;
}
if (fileid != 0 &&
if (!in_txn) {
session2, NULL));
in_txn = 1;
}
cursor2->
set_key(cursor2, &logrec_key);
error_check(cursor2->
insert(cursor2));
}
}
if (in_txn)
error_check(cursor2->
close(cursor2));
compare_tables(session, session2);
error_check(session2->
close(session2, NULL));
error_check(wt_conn2->
close(wt_conn2, NULL));
error_check(cursor->
reset(cursor));
cursor->
set_key(cursor, save_file, save_offset, 0);
error_check(cursor->
search(cursor));
printf("Reset to saved...\n");
for (first = 1;;) {
cursor, &log_file, &log_offset, &opcount));
if (first) {
first = 0;
if (save_file != log_file ||
save_offset != log_offset) {
fprintf(stderr,
"search returned the wrong LSN\n");
exit (1);
}
}
error_check(cursor->
get_value(cursor, &txnid, &rectype,
&optype, &fileid, &logrec_key, &logrec_value));
print_record(log_file, log_offset, opcount,
rectype, optype, txnid, fileid, &logrec_key, &logrec_value);
ret = cursor->
next(cursor);
if (ret != 0)
break;
}
error_check(cursor->
close(cursor));
}
int
main(int argc, char *argv[])
{
int count_min, i, record_count;
char cmd_buf[256], k[32], v[32];
(void)argc;
(void)testutil_set_progname(argv);
count_min = 0;
(void)snprintf(cmd_buf, sizeof(cmd_buf),
"rm -rf %s %s && mkdir %s %s", home1, home2, home1, home2);
error_check(system(cmd_buf));
error_check(wt_conn->
open_session(wt_conn, NULL, NULL, &session));
error_check(
session->
create(session, uri,
"key_format=S,value_format=S"));
count_min++;
error_check(session->
open_cursor(session, uri, NULL, NULL, &cursor));
for (record_count = 0, i = 0; i < MAX_KEYS; i++, record_count++) {
(void)snprintf(k, sizeof(k), "key%d", i);
(void)snprintf(v, sizeof(v), "value%d", i);
error_check(cursor->
insert(cursor));
count_min++;
}
for (i = MAX_KEYS; i < MAX_KEYS+5; i++, record_count++) {
(void)snprintf(k, sizeof(k), "key%d", i);
(void)snprintf(v, sizeof(v), "value%d", i);
error_check(cursor->
insert(cursor));
}
count_min++;
error_check(cursor->
close(cursor));
error_check(
session->
log_printf(session,
"Wrote %d records", record_count));
count_min++;
error_check(wt_conn->
close(wt_conn, NULL));
error_check(wt_conn->
open_session(wt_conn, NULL, NULL, &session));
simple_walk_log(session, count_min);
walk_log(session);
error_check(wt_conn->
close(wt_conn, NULL));
return (EXIT_SUCCESS);
}