Skip to content

Commit

Permalink
Merge pull request #1842 from tursodatabase/wal-api-fixes
Browse files Browse the repository at this point in the history
WAL API fixes
  • Loading branch information
penberg authored Nov 26, 2024
2 parents 3d52c07 + e2e3a6e commit 876a4a7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 40 deletions.
15 changes: 10 additions & 5 deletions libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -183219,6 +183219,7 @@ int libsql_wal_frame_count(
sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalFrameCount(pPager, pnFrame);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
#endif
Expand All @@ -183244,12 +183245,13 @@ int libsql_wal_get_frame(
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
#endif

sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf);
sqlite3_mutex_leave(db->mutex);
sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);

return rc;
return rc;
#endif
}

Expand All @@ -183276,6 +183278,7 @@ int libsql_wal_insert_begin(sqlite3 *db) {
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
}
Expand All @@ -183291,6 +183294,7 @@ int libsql_wal_insert_end(sqlite3 *db) {
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
}
Expand Down Expand Up @@ -183322,6 +183326,7 @@ int libsql_wal_insert_frame(
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);

return rc;
Expand Down
15 changes: 10 additions & 5 deletions libsql-ffi/bundled/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -183219,6 +183219,7 @@ int libsql_wal_frame_count(
sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalFrameCount(pPager, pnFrame);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
#endif
Expand All @@ -183244,12 +183245,13 @@ int libsql_wal_get_frame(
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
#endif

sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf);
sqlite3_mutex_leave(db->mutex);
sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);

return rc;
return rc;
#endif
}

Expand All @@ -183276,6 +183278,7 @@ int libsql_wal_insert_begin(sqlite3 *db) {
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
}
Expand All @@ -183291,6 +183294,7 @@ int libsql_wal_insert_end(sqlite3 *db) {
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
}
Expand Down Expand Up @@ -183322,6 +183326,7 @@ int libsql_wal_insert_frame(
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);

return rc;
Expand Down
15 changes: 10 additions & 5 deletions libsql-sqlite3/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,7 @@ int libsql_wal_frame_count(
sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalFrameCount(pPager, pnFrame);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
#endif
Expand All @@ -2495,12 +2496,13 @@ int libsql_wal_get_frame(
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
#endif

sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf);
sqlite3_mutex_leave(db->mutex);
sqlite3_mutex_enter(db->mutex);
pPager = sqlite3BtreePager(db->aDb[0].pBt);
rc = sqlite3PagerWalReadFrameRaw(pPager, iFrame, pBuf, nBuf);
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);

return rc;
return rc;
#endif
}

Expand All @@ -2527,6 +2529,7 @@ int libsql_wal_insert_begin(sqlite3 *db) {
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
}
Expand All @@ -2542,6 +2545,7 @@ int libsql_wal_insert_end(sqlite3 *db) {
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);
return rc;
}
Expand Down Expand Up @@ -2573,6 +2577,7 @@ int libsql_wal_insert_frame(
goto out_unlock;
}
out_unlock:
sqlite3Error(db, rc);
sqlite3_mutex_leave(db->mutex);

return rc;
Expand Down
49 changes: 24 additions & 25 deletions libsql-sqlite3/src/test_walapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,42 +103,41 @@ static void gen_data(sqlite3 *db){
sqlite3_exec(db, "INSERT INTO users (id, name) VALUES (3, 'Jim Beam')", 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;
int rc;

rc = sqlite3_open("primary.db", &db_primary);
rc = open_db("primary.db", &db_primary);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db_primary));
return 1;
}
rc = sqlite3_wal_autocheckpoint(db_primary, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't disable checkpointing: %s\n", sqlite3_errmsg(db_primary));
return 1;
}
sqlite3_exec(db_primary, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);

gen_data(db_primary);

rc = sqlite3_open("backup.db", &db_backup);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
rc = sqlite3_wal_autocheckpoint(db_backup, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't disable checkpointing: %s\n", sqlite3_errmsg(db_backup));
return 1;
}
rc = sqlite3_exec(db_backup, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "Can't set journal mode: %s\n", sqlite3_errmsg(db_backup));
return 1;
}

rc = open_db("backup.db", &db_backup);
sync_db(db_primary, db_backup);

if (cmp_data(db_primary, db_backup)) {
return 1;
}
Expand Down

0 comments on commit 876a4a7

Please sign in to comment.