Skip to content

Commit

Permalink
pager, wal: virtualize WAL methods
Browse files Browse the repository at this point in the history
This preparatory commit moves all WAL routines to a virtual table.
Also, a helper libsql_open() function is provided. It allows passing
a new parameter - name of the custom WAL methods implementation.
  • Loading branch information
psarna committed Oct 24, 2022
1 parent 63868b1 commit 7d9b46e
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 217 deletions.
8 changes: 5 additions & 3 deletions src/attach.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static void attachFunc(
Db *pNew; /* Db object for the newly attached database */
char *zErrDyn = 0;
sqlite3_vfs *pVfs;
libsql_wal_methods *pWal;

UNUSED_PARAMETER(NotUsed);
zFile = (const char *)sqlite3_value_text(argv[0]);
Expand All @@ -106,12 +107,13 @@ static void attachFunc(
** from sqlite3_deserialize() to close database db->init.iDb and
** reopen it as a MemDB */
pVfs = sqlite3_vfs_find("memdb");
pWal = libsql_wal_methods_find(NULL);
if( pVfs==0 ) return;
pNew = &db->aDb[db->init.iDb];
if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
pNew->pBt = 0;
pNew->pSchema = 0;
rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
rc = sqlite3BtreeOpen(pVfs, pWal, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
}else{
/* This is a real ATTACH
**
Expand Down Expand Up @@ -155,7 +157,7 @@ static void attachFunc(
** or may not be initialized.
*/
flags = db->openFlags;
rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
rc = sqlite3ParseUri(db->pVfs->zName, db->pWalMethods->zName, zFile, &flags, &pVfs, &pWal, &zPath, &zErr);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
sqlite3_result_error(context, zErr, -1);
Expand All @@ -164,7 +166,7 @@ static void attachFunc(
}
assert( pVfs );
flags |= SQLITE_OPEN_MAIN_DB;
rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
rc = sqlite3BtreeOpen(pVfs, pWal, zPath, db, &pNew->pBt, 0, flags);
db->nDb++;
pNew->zDbSName = sqlite3DbStrDup(db, zName);
}
Expand Down
15 changes: 8 additions & 7 deletions src/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2429,12 +2429,13 @@ static int btreeInvokeBusyHandler(void *pArg){
** to problems with locking.
*/
int sqlite3BtreeOpen(
sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
const char *zFilename, /* Name of the file containing the BTree database */
sqlite3 *db, /* Associated database handle */
Btree **ppBtree, /* Pointer to new Btree object written here */
int flags, /* Options */
int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
libsql_wal_methods *pWal,/* WAL methods to use for this b-tree */
const char *zFilename, /* Name of the file containing the BTree database */
sqlite3 *db, /* Associated database handle */
Btree **ppBtree, /* Pointer to new Btree object written here */
int flags, /* Options */
int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
){
BtShared *pBt = 0; /* Shared part of btree structure */
Btree *p; /* Handle to return */
Expand Down Expand Up @@ -2575,7 +2576,7 @@ int sqlite3BtreeOpen(
rc = SQLITE_NOMEM_BKPT;
goto btree_open_out;
}
rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
rc = sqlite3PagerOpen(pVfs, pWal, &pBt->pPager, zFilename,
sizeof(MemPage), flags, vfsFlags, pageReinit);
if( rc==SQLITE_OK ){
sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
Expand Down
2 changes: 2 additions & 0 deletions src/btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ typedef struct BtCursor BtCursor;
typedef struct BtShared BtShared;
typedef struct BtreePayload BtreePayload;

typedef struct libsql_wal_methods libsql_wal_methods;

int sqlite3BtreeOpen(
sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
libsql_wal_methods *pWal,/* WAL methods to use with this b-tree */
const char *zFilename, /* Name of database file to open */
sqlite3 *db, /* Associated database connection */
Btree **ppBtree, /* Return open Btree* here */
Expand Down
2 changes: 1 addition & 1 deletion src/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -5195,7 +5195,7 @@ int sqlite3OpenTempDatabase(Parse *pParse){
SQLITE_OPEN_DELETEONCLOSE |
SQLITE_OPEN_TEMP_DB;

rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
rc = sqlite3BtreeOpen(db->pVfs, db->pWalMethods, 0, db, &pBt, 0, flags);
if( rc!=SQLITE_OK ){
sqlite3ErrorMsg(pParse, "unable to open a temporary database "
"file for storing temporary tables");
Expand Down
33 changes: 27 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2906,15 +2906,18 @@ int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
*/
int sqlite3ParseUri(
const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
const char *zDefaultWal, /* WAL module to use if no "wal=xxx" query option */
const char *zUri, /* Nul-terminated URI to parse */
unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
sqlite3_vfs **ppVfs, /* OUT: VFS to use */
libsql_wal_methods **ppWal, /* OUT: WAL module to use */
char **pzFile, /* OUT: Filename component of URI */
char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
){
int rc = SQLITE_OK;
unsigned int flags = *pFlags;
const char *zVfs = zDefaultVfs;
const char *zWal = zDefaultWal;
char *zFile;
char c;
int nUri = sqlite3Strlen30(zUri);
Expand Down Expand Up @@ -3045,6 +3048,8 @@ int sqlite3ParseUri(

if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
zVfs = zVal;
}else if( nOpt==3 && memcmp("wal", zOpt, 3)==0 ){
zWal = zVal;
}else{
struct OpenMode {
const char *z;
Expand Down Expand Up @@ -3127,6 +3132,11 @@ int sqlite3ParseUri(
*pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
rc = SQLITE_ERROR;
}
*ppWal = libsql_wal_methods_find(zWal);
if (*ppWal == NULL) {
*pzErrMsg = sqlite3_mprintf("no such WAL module: %s", zWal);
rc = SQLITE_ERROR;
}
parse_uri_out:
if( rc!=SQLITE_OK ){
sqlite3_free_filename(zFile);
Expand Down Expand Up @@ -3163,7 +3173,8 @@ static int openDatabase(
const char *zFilename, /* Database filename UTF-8 encoded */
sqlite3 **ppDb, /* OUT: Returned database handle */
unsigned int flags, /* Operational flags */
const char *zVfs /* Name of the VFS to use */
const char *zVfs, /* Name of the VFS to use */
const char *zWal /* Name of WAL module to use */
){
sqlite3 *db; /* Store allocated handle here */
int rc; /* Return code */
Expand Down Expand Up @@ -3387,7 +3398,7 @@ static int openDatabase(
if( ((1<<(flags&7)) & 0x46)==0 ){
rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
}else{
rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
rc = sqlite3ParseUri(zVfs, zWal, zFilename, &flags, &db->pVfs, &db->pWalMethods, &zOpen, &zErrMsg);
}
if( rc!=SQLITE_OK ){
if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
Expand All @@ -3397,7 +3408,7 @@ static int openDatabase(
}

/* Open the backend database driver */
rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
rc = sqlite3BtreeOpen(db->pVfs, db->pWalMethods, zOpen, db, &db->aDb[0].pBt, 0,
flags | SQLITE_OPEN_MAIN_DB);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_IOERR_NOMEM ){
Expand Down Expand Up @@ -3512,15 +3523,25 @@ int sqlite3_open(
sqlite3 **ppDb
){
return openDatabase(zFilename, ppDb,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL, NULL);
}
int sqlite3_open_v2(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb, /* OUT: SQLite db handle */
int flags, /* Flags */
const char *zVfs /* Name of VFS module to use */
){
return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
return openDatabase(filename, ppDb, (unsigned int)flags, zVfs, NULL);
}

int libsql_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb, /* OUT: SQLite db handle */
int flags, /* Flags */
const char *zVfs, /* Name of VFS module to use, NULL for default */
const char *zWal /* Name of WAL module to use */
) {
return openDatabase(filename, ppDb, (unsigned int)flags, zVfs, zWal);
}

#ifndef SQLITE_OMIT_UTF16
Expand Down Expand Up @@ -3549,7 +3570,7 @@ int sqlite3_open16(
zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
if( zFilename8 ){
rc = openDatabase(zFilename8, ppDb,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL, NULL);
assert( *ppDb || rc==SQLITE_NOMEM );
if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
Expand Down
Loading

0 comments on commit 7d9b46e

Please sign in to comment.