Skip to content

Commit

Permalink
vfs/fsm: Add disk methods
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Borderé <[email protected]>
  • Loading branch information
Mathieu Borderé committed Nov 21, 2022
1 parent d0a03d3 commit 14e14c9
Show file tree
Hide file tree
Showing 20 changed files with 1,737 additions and 127 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ libdqlite_la_SOURCES = \
src/leader.c \
src/lib/addr.c \
src/lib/buffer.c \
src/lib/fs.c \
src/lib/transport.c \
src/logger.c \
src/message.c \
Expand Down
25 changes: 25 additions & 0 deletions include/dqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ int dqlite_node_set_failure_domain(dqlite_node *n, unsigned long long code);
int dqlite_node_set_snapshot_params(dqlite_node *n, unsigned snapshot_threshold,
unsigned snapshot_trailing);

/**
* WARNING: This is an experimental API.
*
* By default dqlite holds the SQLite database file and WAL in memory. By enabling
* disk-mode, dqlite will hold the SQLite database file on-disk while keeping the WAL
* in memory. Has to be called after `dqlite_node_create` and before
* `dqlite_node_start`.
*/
int dqlite_node_enable_disk_mode(dqlite_node *n);

/**
* Start a dqlite node.
*
Expand Down Expand Up @@ -305,6 +315,8 @@ dqlite_node_id dqlite_generate_node_id(const char *address);
*/
int dqlite_vfs_init(sqlite3_vfs *vfs, const char *name);

int dqlite_vfs_enable_disk(sqlite3_vfs *vfs);

/**
* Release all memory used internally by a SQLite VFS object that was
* initialized using @qlite_vfs_init.
Expand Down Expand Up @@ -386,6 +398,11 @@ int dqlite_vfs_shallow_snapshot(sqlite3_vfs *vfs,
struct dqlite_buffer bufs[],
unsigned n);

int dqlite_vfs_snapshot_disk(sqlite3_vfs *vfs,
const char *filename,
struct dqlite_buffer bufs[],
unsigned n);

/**
* Return the number of database pages (excluding WAL).
*/
Expand All @@ -401,4 +418,12 @@ int dqlite_vfs_restore(sqlite3_vfs *vfs,
const void *data,
size_t n);

/**
* Restore a snapshot of the main database file and of the WAL file.
*/
int dqlite_vfs_restore_disk(sqlite3_vfs *vfs,
const char *filename,
const void *data,
size_t main_size,
size_t wal_size);
#endif /* DQLITE_H */
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct config
char name[256]; /* VFS/replication registriatio name */
unsigned long long failure_domain; /* User-provided failure domain */
unsigned long long int weight; /* User-provided node weight */
char dir[1024]; /* Data dir */
char dir[1024]; /* Data dir for on-disk database */
bool disk; /* Disk-mode or not */
};

Expand Down
33 changes: 33 additions & 0 deletions src/dqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ int dqlite_vfs_init(sqlite3_vfs *vfs, const char *name)
return VfsInit(vfs, name);
}

int dqlite_vfs_enable_disk(sqlite3_vfs *vfs)
{
return VfsEnableDisk(vfs);
}

void dqlite_vfs_close(sqlite3_vfs *vfs)
{
VfsClose(vfs);
Expand Down Expand Up @@ -47,6 +52,25 @@ int dqlite_vfs_snapshot(sqlite3_vfs *vfs,
return VfsSnapshot(vfs, filename, data, n);
}

int dqlite_vfs_snapshot_disk(sqlite3_vfs *vfs,
const char *filename,
struct dqlite_buffer bufs[],
unsigned n)
{
int rv;
if (n != 2) {
return -1;
}

rv = VfsDiskSnapshotDb(vfs, filename, &bufs[0]);
if (rv != 0) {
return rv;
}

rv = VfsDiskSnapshotWal(vfs, filename, &bufs[1]);
return rv;
}

int dqlite_vfs_num_pages(sqlite3_vfs *vfs,
const char *filename,
unsigned *n)
Expand All @@ -69,3 +93,12 @@ int dqlite_vfs_restore(sqlite3_vfs *vfs,
{
return VfsRestore(vfs, filename, data, n);
}

int dqlite_vfs_restore_disk(sqlite3_vfs *vfs,
const char *filename,
const void *data,
size_t main_size,
size_t wal_size)
{
return VfsDiskRestore(vfs, filename, data, main_size, wal_size);
}
Loading

0 comments on commit 14e14c9

Please sign in to comment.