Skip to content

Commit

Permalink
Add a pre-open-main-db-file hook
Browse files Browse the repository at this point in the history
WAL is open lazily on first access, while it is useful to be able
to set up ground for it even before the main db file is open.
This optional hook allows it.
  • Loading branch information
psarna committed Dec 13, 2022
1 parent 47e59b7 commit 07f0c58
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/vwal/vwal.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ void libsql_register_vwal() {
.xDb = v_db,
.xPathnameLen = v_pathname_len,
.xGetWalPathname = v_get_wal_pathname,
.xPreMainDbOpen = NULL,
.zName = "vwal"
};
libsql_wal_methods_register(&methods);
Expand Down
8 changes: 8 additions & 0 deletions src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -4893,6 +4893,14 @@ int sqlite3PagerOpen(
sqlite3FileSuffix3(zFilename, pPager->zWal);
pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
#endif

if (pWalMethods->xPreMainDbOpen) {
int rc = pWalMethods->xPreMainDbOpen(zPathname);
if (rc != SQLITE_OK) {
return rc;
}
}

}else{
pPager->zWal = 0;
}
Expand Down
5 changes: 5 additions & 0 deletions src/wal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4092,6 +4092,10 @@ static void libsqlGetWalPathname(char *buf, const char *orig, int orig_len) {
memcpy(buf + orig_len, "-wal", 4);
}

static int libsqlPreMainDbOpen(Wal *, const char *) {
return SQLITE_OK;
}

libsql_wal_methods *libsql_wal_methods_find(const char *zName) {
static libsql_wal_methods methods;
static libsql_wal_methods *methods_head = NULL;
Expand Down Expand Up @@ -4146,6 +4150,7 @@ libsql_wal_methods *libsql_wal_methods_find(const char *zName) {
methods.xDb = sqlite3WalDb;
methods.xPathnameLen = libsqlWalPathnameLen;
methods.xGetWalPathname = libsqlGetWalPathname;
methods.xPreMainDbOpen = libsqlPreMainDbOpen;

methods.bUsesShm = 1;
methods.zName = "default";
Expand Down
7 changes: 7 additions & 0 deletions src/wal.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ typedef struct libsql_wal_methods {
** */
void (*xGetWalPathname)(char *buf, const char *orig, int orig_len);

/*
** This optional callback gets called before the main database file which owns
** the WAL file is open. It is a good place for initialization routines, as WAL
** is otherwise open lazily.
*/
int (*xPreMainDbOpen)(Wal *pWal, const char *main_db_path);

/* True if the implementation relies on shared memory routines (e.g. locks) */
int bUsesShm;

Expand Down

0 comments on commit 07f0c58

Please sign in to comment.