Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ jobs:
run: |
brew install python sqlite zstd samtools aria2
$(brew --prefix)/bin/pip3 install --break-system-packages pytest pytest-xdist
# test web_vfs using the older system libcurl
brew uninstall --ignore-dependencies curl
curl --version
which curl
otool -L $(which curl)
- name: build
run: |
CXXFLAGS="-I$(brew --prefix)/include -I$(brew --prefix)/opt/sqlite/include" \
LDFLAGS="-L$(brew --prefix)/lib -L$(brew --prefix)/opt/sqlite/lib" \
cmake -DCMAKE_BUILD_TYPE=${{ matrix.BUILD_TYPE }} \
-DCMAKE_PREFIX_PATH=$(brew --prefix)/opt/sqlite \
-B build .
# test web_vfs using the older system libcurl. Uninstall brew's curl only
# after configure: FetchContent clones dependencies with git during
# configure, and the runner's git itself loads brew's libcurl, so removing
# the keg earlier leaves git unable to run at all.
brew uninstall --ignore-dependencies curl
curl --version
which curl
otool -L $(which curl)
cmake --build build -j 4
- name: ctest
run: |
Expand Down
8 changes: 7 additions & 1 deletion src/SQLiteNestedVFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,12 @@ class InnerDatabaseFile : public SQLiteVFS::File {
// 2 above, inner db should always recover to a consistent state.
std::unique_ptr<SQLite::Transaction> txn_;
std::unique_ptr<SQLite::Statement> insert_page_, update_page_, delete_pages_;
// Retain write-path history across Sync(): connections that never called begin()
// freed no outer pages and need not vacuum at close.
bool wrote_ = false;
void begin() {
assert(!read_only_);
wrote_ = true;
if (!txn_) {
txn_.reset(new SQLite::Transaction(*outer_db_));
}
Expand Down Expand Up @@ -1008,7 +1012,9 @@ class InnerDatabaseFile : public SQLiteVFS::File {
SQLiteVFS::File::Close(); // deletes this
return rc;
}
outer_db_->exec("PRAGMA incremental_vacuum");
if (wrote_) {
outer_db_->exec("PRAGMA incremental_vacuum");
}
}
} catch (SQLite::Exception &exn) {
SQLITE_NVFS_LOG(1, exn.what())
Expand Down
98 changes: 98 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#define SQLITE_CORE
#include "SQLiteCpp/SQLiteCpp.h"
#include "catch2/catch.hpp"
#include <atomic>
#include <iostream>
#include <sqlite3ext.h>
#include <thread>
#include <vector>

#include "../src/SQLiteNestedVFS.h"
Expand Down Expand Up @@ -363,6 +365,102 @@ TEST_CASE("cellular_automata") {
system(("ls -lh " + testdir).c_str());
}

TEST_CASE("close vacuums the outer database only if the connection wrote") {
string testdir("/tmp/sqlite3_nested_vfs_test_XXXXXX");
REQUIRE(mkdtemp((char *)testdir.data()) != nullptr);

setup();

// the outer database is the file itself; the inner database lives in its pages table
const string dbfile = testdir + "/vacuum";
{
SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE, 0, "nested");
guest.exec("CREATE TABLE t (x TEXT)");
SQLite::Transaction txn(guest);
for (int i = 0; i < 200; i++) {
guest.exec("INSERT INTO t VALUES (zeroblob(1024))");
}
txn.commit();
}

auto outer_freelist_count = [&]() {
SQLite::Database outer(dbfile, SQLite::OPEN_READONLY);
return outer.execAndGet("PRAGMA freelist_count").getInt();
};

// seed the outer freelist without triggering nested close-time vacuuming
{
SQLite::Database outer(dbfile, SQLite::OPEN_READWRITE);
outer.exec("CREATE TABLE scratch (x BLOB)");
SQLite::Transaction txn(outer);
for (int i = 0; i < 200; i++) {
outer.exec("INSERT INTO scratch VALUES (zeroblob(4000))");
}
txn.commit();
outer.exec("DROP TABLE scratch");
}
const int freelist = outer_freelist_count();
REQUIRE(freelist > 0);

// a read-write connection that only reads must leave the existing outer freelist untouched
{
SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE, 0, "nested");
REQUIRE(guest.execAndGet("SELECT count(*) FROM t").getInt() == 200);
}
REQUIRE(outer_freelist_count() == freelist);

// a writer's close must still reclaim the outer freelist
{
SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE, 0, "nested");
guest.exec("INSERT INTO t VALUES ('x')");
}
REQUIRE(outer_freelist_count() == 0);
}

TEST_CASE("concurrent opens survive read-write close churn") {
string testdir("/tmp/sqlite3_nested_vfs_test_XXXXXX");
REQUIRE(mkdtemp((char *)testdir.data()) != nullptr);

setup();

const string dbfile = testdir + "/churn";
{
SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE, 0, "nested");
guest.exec("CREATE TABLE t (x INTEGER)");
guest.exec("INSERT INTO t VALUES (1)");
}

// a never-wrote close must not take the outer write lock and make concurrent opens fail
const int NTHREADS = 4, NITERS = 1000;
std::atomic<int> n_opened{0}, n_cantopen{0}, n_other_errors{0};
auto churn = [&]() {
for (int i = 0; i < NITERS; i++) {
try {
SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE, 0, "nested");
guest.execAndGet("SELECT count(*) FROM t");
n_opened++;
} catch (SQLite::Exception &exn) {
if ((exn.getErrorCode() & 0xFF) == SQLITE_CANTOPEN) {
n_cantopen++;
} else {
n_other_errors++;
}
}
}
};
std::vector<std::thread> threads;
for (int i = 0; i < NTHREADS; i++) {
threads.emplace_back(churn);
}
for (auto &thread : threads) {
thread.join();
}

REQUIRE(n_other_errors == 0);
REQUIRE(n_cantopen == 0);
REQUIRE(n_opened == NTHREADS * NITERS);
}

/*
The following BS is needed for this test executable to use SQLiteCpp even though it's built for use
within a loadable extension (SQLITECPP_IN_EXTENSION). The sqlite3Apis definition is copied from
Expand Down
Loading