diff --git a/CMakeLists.txt b/CMakeLists.txt index 4334ef4..0b6f43a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,8 +71,8 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) FetchContent_MakeAvailable(catch) include_directories(${catch_SOURCE_DIR}/single_include) - add_executable(test_exe test/test.cc test/test_vfstrace.c src/SQLiteNestedVFS.h) - target_link_libraries(test_exe PRIVATE SQLiteCpp sqlite3) + add_executable(test_exe test/test.cc test/test_vfstrace.c src/SQLiteNestedVFS.h src/zstd_vfs.h) + target_link_libraries(test_exe PRIVATE SQLiteCpp sqlite3 zstd) include(CTest) enable_testing() diff --git a/src/SQLiteNestedVFS.h b/src/SQLiteNestedVFS.h index c107630..e31cef2 100644 --- a/src/SQLiteNestedVFS.h +++ b/src/SQLiteNestedVFS.h @@ -222,14 +222,15 @@ class InnerDatabaseFile : public SQLiteVFS::File { PutState(State::NEW); } - // reset the outer database cursor; should be done when it otherwise might go stale. + // Reset the outer database cursor whenever its position is stale or + // unknown. cursor_pageno == 0 must mean safe to bind, but missing-page + // seeks can leave it stepped at 0: reset unconditionally via + // tryReset(), which discards prior execution errors without throwing. virtual void ResetCursor() { src = nullptr; src_size = 0; - if (cursor_pageno > 0) { - cursor.reset(); - cursor_pageno = 0; - } + cursor.tryReset(); + cursor_pageno = 0; last_op = 0; } @@ -336,7 +337,7 @@ class InnerDatabaseFile : public SQLiteVFS::File { DecodePage(); } catch (std::exception &exn) { errmsg = exn.what(); - cursor_pageno = 0; + ResetCursor(); } PutState(State::DONE); } diff --git a/test/test.cc b/test/test.cc index 4240a9b..b34df8e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7,6 +7,7 @@ #include #include "../src/SQLiteNestedVFS.h" +#include "../src/zstd_vfs.h" using namespace std; @@ -22,6 +23,14 @@ void setup() { } } +void setup_zstd() { + static bool first = true; + if (first) { + (new ZstdVFS)->Register("zstd"); + first = false; + } +} + TEST_CASE("hello") { string testdir("/tmp/sqlite3_nested_vfs_test_XXXXXX"); REQUIRE(mkdtemp((char *)testdir.data()) != nullptr); @@ -84,6 +93,65 @@ TEST_CASE("bigpages") { } } +// noprefetch=1 keeps fetches in the foreground; this fixture's probes reuse the failed job. +TEST_CASE("fetch cursor recovers after page errors") { + string testdir("/tmp/sqlite3_nested_vfs_test_XXXXXX"); + REQUIRE(mkdtemp((char *)testdir.data()) != nullptr); + + setup_zstd(); + + const string dbfile = testdir + "/t.db"; + const string uri = "file:" + dbfile + "?noprefetch=1"; + const int rw_flags = SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE | SQLite::OPEN_URI; + + int64_t mroot = 0; + { + SQLite::Database guest(uri, rw_flags, 0, "zstd"); + guest.exec("CREATE TABLE u(x)"); + guest.exec("INSERT INTO u VALUES('u-row')"); + guest.exec("CREATE TABLE t(x)"); + guest.exec("WITH RECURSIVE c(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM c WHERE n<300)" + " INSERT INTO t SELECT zeroblob(1900) FROM c"); + guest.exec("CREATE TABLE m(x)"); + guest.exec("INSERT INTO m VALUES('m-row')"); + guest.exec("CREATE TABLE v(x)"); + guest.exec("INSERT INTO v VALUES('v-row')"); + mroot = guest.execAndGet("SELECT rootpage FROM sqlite_master WHERE name='m'").getInt64(); + REQUIRE(mroot > 0); + } + + // Preserve the blob length while making its contents undecodable. + { + SQLite::Database outer(dbfile, SQLite::OPEN_READWRITE); + outer.exec("PRAGMA journal_mode=WAL"); + outer.exec("UPDATE nested_vfs_zstd_pages SET data=zeroblob(length(data))" + " WHERE pageno=20"); + } + + { + SQLite::Database guest(uri, SQLite::OPEN_READWRITE | SQLite::OPEN_URI, 0, "zstd"); + guest.exec("PRAGMA cache_size=10"); + REQUIRE_THROWS_AS(guest.execAndGet("SELECT count(*) FROM t"), SQLite::Exception); + REQUIRE(guest.execAndGet("SELECT x FROM u").getString() == "u-row"); + } + +#ifdef NDEBUG + // Debug builds assert on gaps at open; trailing table v preserves the apparent file size. + // Seeking m's missing root steps past it with cursor_pageno == 0. + { + SQLite::Database outer(dbfile, SQLite::OPEN_READWRITE); + outer.exec("DELETE FROM nested_vfs_zstd_pages WHERE pageno=" + std::to_string(mroot)); + } + + { + SQLite::Database guest(uri, SQLite::OPEN_READWRITE | SQLite::OPEN_URI, 0, "zstd"); + guest.exec("PRAGMA cache_size=10"); + REQUIRE_THROWS_AS(guest.execAndGet("SELECT x FROM m"), SQLite::Exception); + REQUIRE(guest.execAndGet("SELECT x FROM u").getString() == "u-row"); + } +#endif +} + void ElementaryCellularAutomaton(uint8_t rule, const vector &state, vector &ans) { size_t size2 = state.size() + 2; ans.clear();