-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathtest_walapi.c
165 lines (147 loc) · 4.31 KB
/
test_walapi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#if 0
static void dump_frame(unsigned char *frame, size_t size){
for(int addr=0; addr<size; addr+=16){
int sum = 0;
for(int i=0; i<16 && addr+1<size; i++){
sum += frame[addr+i] != 0;
}
if( sum ){
printf("%08x: ", addr);
for(int i=0; i<16 && addr+i<size; i++){
printf("%02x ", frame[addr+i]);
}
printf(" |");
for(int i=0; i<16 && addr+i<size; i++){
printf("%c", frame[addr+i] ? frame[addr+i] : '.');
}
printf("|");
printf("\n");
}
}
}
#endif
static int cmp_data(sqlite3 *db1, sqlite3 *db2){
sqlite3_stmt *stmt1, *stmt2;
int rc;
rc = sqlite3_prepare_v2(db1, "SELECT HEX(x) FROM t", -1, &stmt1, 0);
if( rc!=SQLITE_OK ){
fprintf(stderr, "Can't prepare statement: %s\n", sqlite3_errmsg(db1));
return 1;
}
rc = sqlite3_prepare_v2(db2, "SELECT HEX(x) FROM t", -1, &stmt2, 0);
if( rc!=SQLITE_OK ){
fprintf(stderr, "Can't prepare statement: %s\n", sqlite3_errmsg(db2));
return 1;
}
for(;;){
int step1 = sqlite3_step(stmt1);
int step2 = sqlite3_step(stmt2);
if( step1!=step2 ){
fprintf(stderr, "Step mismatch: %d != %d\n", step1, step2);
return 1;
}
if( step1!=SQLITE_ROW ){
break;
}
const unsigned char *text1 = sqlite3_column_text(stmt1, 0);
const unsigned char *text2 = sqlite3_column_text(stmt2, 0);
if( strncmp((const char *)text1, (const char *)text2, 4096)!=0 ){
fprintf(stderr, "Data mismatch\n");
return 1;
}
}
return 0;
}
static int sync_db(sqlite3 *db_primary, sqlite3 *db_backup){
unsigned int max_frame;
int rc;
rc = libsql_wal_frame_count(db_primary, &max_frame);
if( rc!=SQLITE_OK ){
fprintf(stderr, "Can't get frame count: %s\n", sqlite3_errmsg(db_primary));
return 1;
}
rc = libsql_wal_insert_begin(db_backup);
if( rc!=SQLITE_OK ){
fprintf(stderr, "Can't begin commit: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
for(int i=1; i<=max_frame; i++){
char frame[4096+24];
rc = libsql_wal_get_frame(db_primary, i, frame, sizeof(frame));
if( rc!=SQLITE_OK ){
fprintf(stderr, "Can't get frame: %s\n", sqlite3_errmsg(db_primary));
return 1;
}
rc = libsql_wal_insert_frame(db_backup, i, frame, sizeof(frame));
if( rc!=SQLITE_OK ){
fprintf(stderr, "Can't inject frame %d: %s\n", rc, sqlite3_errmsg(db_backup));
return 1;
}
}
rc = libsql_wal_insert_end(db_backup);
if( rc!=SQLITE_OK ){
fprintf(stderr, "Can't end commit: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
return 0;
}
static void gen_data(sqlite3 *db){
sqlite3_exec(db, "CREATE TABLE t (x)", 0, 0, 0);
sqlite3_exec(db, "INSERT INTO t VALUES (randomblob(4 * 1024))", 0, 0, 0);
sqlite3_exec(db, "INSERT INTO t VALUES (randomblob(1 * 1024))", 0, 0, 0);
sqlite3_exec(db, "INSERT INTO t VALUES (randomblob(1 * 1024))", 0, 0, 0);
}
int open_db(const char *path, sqlite3 **db) {
int rc;
rc = sqlite3_open(path, db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't open database %s: %s\n", path, sqlite3_errmsg(*db));
return rc;
}
rc = sqlite3_exec(*db, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't set journal mode for %s: %s\n", path, sqlite3_errmsg(*db));
return rc;
}
rc = sqlite3_wal_autocheckpoint(*db, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't disable checkpointing for %s: %s\n", path, sqlite3_errmsg(*db));
return rc;
}
return rc;
}
int main(int argc, char *argv[])
{
sqlite3 *db_primary, *db_backup;
unsigned int max_frame;
int rc;
rc = open_db("primary.db", &db_primary);
if (rc != SQLITE_OK) {
return 1;
}
gen_data(db_primary);
rc = open_db("backup.db", &db_backup);
if (rc != SQLITE_OK) {
return 1;
}
rc = libsql_wal_frame_count(db_backup, &max_frame);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't get frame count for backup: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
assert(max_frame == 0);
sync_db(db_primary, db_backup);
if (cmp_data(db_primary, db_backup)) {
return 1;
}
sync_db(db_primary, db_backup);
if (cmp_data(db_primary, db_backup)) {
return 1;
}
printf("OK\n");
return 0;
}