sl@0: # 2007 May 1 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # $Id: incrblob.test,v 1.21 2008/09/11 11:28:00 danielk1977 Exp $ sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable {!autovacuum || !pragma || !incrblob} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: do_test incrblob-1.1 { sl@0: execsql { sl@0: CREATE TABLE blobs(k PRIMARY KEY, v BLOB); sl@0: INSERT INTO blobs VALUES('one', X'0102030405060708090A'); sl@0: INSERT INTO blobs VALUES('two', X'0A090807060504030201'); sl@0: } sl@0: } {} sl@0: sl@0: do_test incrblob-1.2.1 { sl@0: set ::blob [db incrblob blobs v 1] sl@0: string match incrblob_* $::blob sl@0: } {1} sl@0: unset -nocomplain data sl@0: do_test incrblob-1.2.2 { sl@0: binary scan [read $::blob] c* data sl@0: set data sl@0: } {1 2 3 4 5 6 7 8 9 10} sl@0: do_test incrblob-1.2.3 { sl@0: seek $::blob 0 sl@0: puts -nonewline $::blob "1234567890" sl@0: flush $::blob sl@0: } {} sl@0: do_test incrblob-1.2.4 { sl@0: seek $::blob 0 sl@0: binary scan [read $::blob] c* data sl@0: set data sl@0: } {49 50 51 52 53 54 55 56 57 48} sl@0: do_test incrblob-1.2.5 { sl@0: close $::blob sl@0: } {} sl@0: do_test incrblob-1.2.6 { sl@0: execsql { sl@0: SELECT v FROM blobs WHERE rowid = 1; sl@0: } sl@0: } {1234567890} sl@0: sl@0: #-------------------------------------------------------------------- sl@0: # Test cases incrblob-1.3.X check that it is possible to read and write sl@0: # regions of a blob that lie on overflow pages. sl@0: # sl@0: do_test incrblob-1.3.1 { sl@0: set ::str "[string repeat . 10000]" sl@0: execsql { sl@0: INSERT INTO blobs(rowid, k, v) VALUES(3, 'three', $::str); sl@0: } sl@0: } {} sl@0: sl@0: do_test incrblob-1.3.2 { sl@0: set ::blob [db incrblob blobs v 3] sl@0: seek $::blob 8500 sl@0: read $::blob 10 sl@0: } {..........} sl@0: do_test incrblob-1.3.3 { sl@0: seek $::blob 8500 sl@0: puts -nonewline $::blob 1234567890 sl@0: } {} sl@0: do_test incrblob-1.3.4 { sl@0: seek $::blob 8496 sl@0: read $::blob 10 sl@0: } {....123456} sl@0: do_test incrblob-1.3.10 { sl@0: close $::blob sl@0: } {} sl@0: sl@0: #------------------------------------------------------------------------ sl@0: # incrblob-2.*: sl@0: # sl@0: # Test that the following operations use ptrmap pages to reduce sl@0: # unnecessary reads: sl@0: # sl@0: # * Reading near the end of a blob, sl@0: # * Writing near the end of a blob, and sl@0: # * SELECT a column value that is located on an overflow page. sl@0: # sl@0: proc nRead {db} { sl@0: set bt [btree_from_db $db] sl@0: db_enter $db sl@0: array set stats [btree_pager_stats $bt] sl@0: db_leave $db sl@0: return $stats(read) sl@0: } sl@0: proc nWrite {db} { sl@0: set bt [btree_from_db $db] sl@0: db_enter $db sl@0: array set stats [btree_pager_stats $bt] sl@0: db_leave $db sl@0: return $stats(write) sl@0: } sl@0: sl@0: sqlite3_soft_heap_limit 0 sl@0: sl@0: foreach AutoVacuumMode [list 0 1] { sl@0: sl@0: if {$AutoVacuumMode>0} { sl@0: ifcapable !autovacuum { sl@0: break sl@0: } sl@0: } sl@0: sl@0: db close sl@0: file delete -force test.db test.db-journal sl@0: sl@0: sqlite3 db test.db sl@0: execsql "PRAGMA auto_vacuum = $AutoVacuumMode" sl@0: sl@0: do_test incrblob-2.$AutoVacuumMode.1 { sl@0: set ::str [string repeat abcdefghij 2900] sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER); sl@0: DELETE FROM blobs; sl@0: INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45); sl@0: COMMIT; sl@0: } sl@0: expr [file size test.db]/1024 sl@0: } [expr 31 + $AutoVacuumMode] sl@0: sl@0: ifcapable autovacuum { sl@0: do_test incrblob-2.$AutoVacuumMode.2 { sl@0: execsql { sl@0: PRAGMA auto_vacuum; sl@0: } sl@0: } $AutoVacuumMode sl@0: } sl@0: sl@0: do_test incrblob-2.$AutoVacuumMode.3 { sl@0: # Open and close the db to make sure the page cache is empty. sl@0: db close sl@0: sqlite3 db test.db sl@0: sl@0: # Read the last 20 bytes of the blob via a blob handle. sl@0: set ::blob [db incrblob blobs v 1] sl@0: seek $::blob -20 end sl@0: set ::fragment [read $::blob] sl@0: close $::blob sl@0: sl@0: # If the database is not in auto-vacuum mode, the whole of sl@0: # the overflow-chain must be scanned. In auto-vacuum mode, sl@0: # sqlite uses the ptrmap pages to avoid reading the other pages. sl@0: # sl@0: nRead db sl@0: } [expr $AutoVacuumMode ? 4 : 30] sl@0: sl@0: do_test incrblob-2.$AutoVacuumMode.4 { sl@0: string range [db one {SELECT v FROM blobs}] end-19 end sl@0: } $::fragment sl@0: sl@0: do_test incrblob-2.$AutoVacuumMode.5 { sl@0: # Open and close the db to make sure the page cache is empty. sl@0: db close sl@0: sqlite3 db test.db sl@0: sl@0: # Write the second-to-last 20 bytes of the blob via a blob handle. sl@0: # sl@0: set ::blob [db incrblob blobs v 1] sl@0: seek $::blob -40 end sl@0: puts -nonewline $::blob "1234567890abcdefghij" sl@0: flush $::blob sl@0: sl@0: # If the database is not in auto-vacuum mode, the whole of sl@0: # the overflow-chain must be scanned. In auto-vacuum mode, sl@0: # sqlite uses the ptrmap pages to avoid reading the other pages. sl@0: # sl@0: nRead db sl@0: } [expr $AutoVacuumMode ? 4 : 30] sl@0: sl@0: # Pages 1 (the write-counter) and 32 (the blob data) were written. sl@0: do_test incrblob-2.$AutoVacuumMode.6 { sl@0: close $::blob sl@0: nWrite db sl@0: } 2 sl@0: sl@0: do_test incrblob-2.$AutoVacuumMode.7 { sl@0: string range [db one {SELECT v FROM blobs}] end-39 end-20 sl@0: } "1234567890abcdefghij" sl@0: sl@0: do_test incrblob-2.$AutoVacuumMode.8 { sl@0: # Open and close the db to make sure the page cache is empty. sl@0: db close sl@0: sqlite3 db test.db sl@0: sl@0: execsql { SELECT i FROM blobs } sl@0: } {45} sl@0: sl@0: do_test incrblob-2.$AutoVacuumMode.9 { sl@0: nRead db sl@0: } [expr $AutoVacuumMode ? 4 : 30] sl@0: } sl@0: sqlite3_soft_heap_limit $soft_limit sl@0: sl@0: #------------------------------------------------------------------------ sl@0: # incrblob-3.*: sl@0: # sl@0: # Test the outcome of trying to write to a read-only blob handle. sl@0: # sl@0: do_test incrblob-3.1 { sl@0: set ::blob [db incrblob -readonly blobs v 1] sl@0: seek $::blob -40 end sl@0: read $::blob 20 sl@0: } "1234567890abcdefghij" sl@0: do_test incrblob-3.2 { sl@0: seek $::blob 0 sl@0: set rc [catch { sl@0: puts -nonewline $::blob "helloworld" sl@0: } msg] sl@0: close $::blob sl@0: list $rc $msg sl@0: } "1 {channel \"$::blob\" wasn't opened for writing}" sl@0: sl@0: do_test incrblob-3.3 { sl@0: set ::blob [db incrblob -readonly blobs v 1] sl@0: seek $::blob -40 end sl@0: read $::blob 20 sl@0: } "1234567890abcdefghij" sl@0: do_test incrblob-3.4 { sl@0: set rc [catch { sl@0: sqlite3_blob_write $::blob 20 "qwertyuioplkjhgfds" sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 SQLITE_READONLY} sl@0: catch {close $::blob} sl@0: sl@0: #------------------------------------------------------------------------ sl@0: # incrblob-4.*: sl@0: # sl@0: # Try a couple of error conditions: sl@0: # sl@0: # 4.1 - Attempt to open a row that does not exist. sl@0: # 4.2 - Attempt to open a column that does not exist. sl@0: # 4.3 - Attempt to open a table that does not exist. sl@0: # 4.4 - Attempt to open a database that does not exist. sl@0: # sl@0: # 4.5 - Attempt to open an integer sl@0: # 4.6 - Attempt to open a real value sl@0: # 4.7 - Attempt to open an SQL null sl@0: # sl@0: # 4.8 - Attempt to open an indexed column for writing sl@0: # 4.9 - Attempt to open an indexed column for reading (this works) sl@0: # sl@0: # 4.11 - Attempt to open a column of a view. sl@0: # 4.12 - Attempt to open a column of a virtual table. sl@0: # sl@0: do_test incrblob-4.1 { sl@0: set rc [catch { sl@0: set ::blob [db incrblob blobs v 2] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {no such rowid: 2}} sl@0: do_test incrblob-4.2 { sl@0: set rc [catch { sl@0: set ::blob [db incrblob blobs blue 1] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {no such column: "blue"}} sl@0: do_test incrblob-4.3 { sl@0: set rc [catch { sl@0: set ::blob [db incrblob nosuchtable blue 1] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {no such table: main.nosuchtable}} sl@0: do_test incrblob-4.4 { sl@0: set rc [catch { sl@0: set ::blob [db incrblob nosuchdb blobs v 1] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {no such table: nosuchdb.blobs}} sl@0: sl@0: do_test incrblob-4.5 { sl@0: set rc [catch { sl@0: set ::blob [db incrblob blobs i 1] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {cannot open value of type integer}} sl@0: do_test incrblob-4.6 { sl@0: execsql { sl@0: INSERT INTO blobs(k, v, i) VALUES(123, 567.765, NULL); sl@0: } sl@0: set rc [catch { sl@0: set ::blob [db incrblob blobs v 2] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {cannot open value of type real}} sl@0: do_test incrblob-4.7 { sl@0: set rc [catch { sl@0: set ::blob [db incrblob blobs i 2] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {cannot open value of type null}} sl@0: sl@0: do_test incrblob-4.8 { sl@0: execsql { sl@0: INSERT INTO blobs(k, v, i) VALUES(X'010203040506070809', 'hello', 'world'); sl@0: } sl@0: set rc [catch { sl@0: set ::blob [db incrblob blobs k 3] sl@0: } msg ] sl@0: list $rc $msg sl@0: } {1 {cannot open indexed column for writing}} sl@0: sl@0: do_test incrblob-4.9.1 { sl@0: set rc [catch { sl@0: set ::blob [db incrblob -readonly blobs k 3] sl@0: } msg] sl@0: } {0} sl@0: do_test incrblob-4.9.2 { sl@0: binary scan [read $::blob] c* c sl@0: close $::blob sl@0: set c sl@0: } {1 2 3 4 5 6 7 8 9} sl@0: sl@0: do_test incrblob-4.10 { sl@0: set ::blob [db incrblob -readonly blobs k 3] sl@0: set rc [catch { sqlite3_blob_read $::blob 10 100 } msg] sl@0: list $rc $msg sl@0: } {1 SQLITE_ERROR} sl@0: do_test incrblob-4.10.2 { sl@0: close $::blob sl@0: } {} sl@0: sl@0: ifcapable view { sl@0: do_test incrblob-4.11 { sl@0: execsql { CREATE VIEW blobs_view AS SELECT k, v, i FROM blobs } sl@0: set rc [catch { db incrblob blobs_view v 3 } msg] sl@0: list $rc $msg sl@0: } {1 {cannot open view: blobs_view}} sl@0: } sl@0: ifcapable vtab { sl@0: register_echo_module [sqlite3_connection_pointer db] sl@0: do_test incrblob-4.12 { sl@0: execsql { CREATE VIRTUAL TABLE blobs_echo USING echo(blobs) } sl@0: set rc [catch { db incrblob blobs_echo v 3 } msg] sl@0: list $rc $msg sl@0: } {1 {cannot open virtual table: blobs_echo}} sl@0: } sl@0: sl@0: sl@0: #------------------------------------------------------------------------ sl@0: # incrblob-5.*: sl@0: # sl@0: # Test that opening a blob in an attached database works. sl@0: # sl@0: ifcapable attach { sl@0: do_test incrblob-5.1 { sl@0: file delete -force test2.db test2.db-journal sl@0: set ::size [expr [file size [info script]]] sl@0: execsql { sl@0: ATTACH 'test2.db' AS aux; sl@0: CREATE TABLE aux.files(name, text); sl@0: INSERT INTO aux.files VALUES('this one', zeroblob($::size)); sl@0: } sl@0: set fd [db incrblob aux files text 1] sl@0: fconfigure $fd -translation binary sl@0: set fd2 [open [info script]] sl@0: fconfigure $fd2 -translation binary sl@0: puts -nonewline $fd [read $fd2] sl@0: close $fd sl@0: close $fd2 sl@0: set ::text [db one {select text from aux.files}] sl@0: string length $::text sl@0: } [file size [info script]] sl@0: do_test incrblob-5.2 { sl@0: set fd2 [open [info script]] sl@0: fconfigure $fd2 -translation binary sl@0: set ::data [read $fd2] sl@0: close $fd2 sl@0: set ::data sl@0: } $::text sl@0: } sl@0: sl@0: # free memory sl@0: unset -nocomplain ::data sl@0: unset -nocomplain ::text sl@0: sl@0: #------------------------------------------------------------------------ sl@0: # incrblob-6.*: sl@0: # sl@0: # Test that opening a blob for write-access is impossible if sl@0: # another connection has the database RESERVED lock. sl@0: # sl@0: # Then test that blob writes that take place inside of a sl@0: # transaction are not visible to external connections until sl@0: # after the transaction is commited and the blob channel sl@0: # closed. sl@0: # sl@0: sqlite3_soft_heap_limit 0 sl@0: do_test incrblob-6.1 { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: BEGIN; sl@0: INSERT INTO blobs(k, v, i) VALUES('a', 'different', 'connection'); sl@0: } db2 sl@0: } {} sl@0: do_test incrblob-6.2 { sl@0: execsql { sl@0: SELECT rowid FROM blobs sl@0: } sl@0: } {1 2 3} sl@0: do_test incrblob-6.3 { sl@0: set rc [catch { sl@0: db incrblob blobs v 1 sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {database is locked}} sl@0: do_test incrblob-6.4 { sl@0: set rc [catch { sl@0: db incrblob blobs v 3 sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {database is locked}} sl@0: do_test incrblob-6.5 { sl@0: set ::blob [db incrblob -readonly blobs v 3] sl@0: read $::blob sl@0: } {hello} sl@0: do_test incrblob-6.6 { sl@0: close $::blob sl@0: } {} sl@0: sl@0: do_test incrblob-6.7 { sl@0: set ::blob [db2 incrblob blobs i 4] sl@0: gets $::blob sl@0: } {connection} sl@0: do_test incrblob-6.8 { sl@0: tell $::blob sl@0: } {10} sl@0: do_test incrblob-6.9 { sl@0: seek $::blob 0 sl@0: puts -nonewline $::blob "invocation" sl@0: flush $::blob sl@0: } {} sl@0: sl@0: # At this point rollback or commit should be illegal (because sl@0: # there is an open blob channel). sl@0: do_test incrblob-6.10 { sl@0: catchsql { sl@0: ROLLBACK; sl@0: } db2 sl@0: } {1 {cannot rollback transaction - SQL statements in progress}} sl@0: do_test incrblob-6.11 { sl@0: catchsql { sl@0: COMMIT; sl@0: } db2 sl@0: } {1 {cannot commit transaction - SQL statements in progress}} sl@0: sl@0: do_test incrblob-6.12 { sl@0: execsql { sl@0: SELECT * FROM blobs WHERE rowid = 4; sl@0: } sl@0: } {} sl@0: do_test incrblob-6.13 { sl@0: close $::blob sl@0: execsql { sl@0: COMMIT; sl@0: } db2 sl@0: } {} sl@0: do_test incrblob-6.14 { sl@0: execsql { sl@0: SELECT * FROM blobs WHERE rowid = 4; sl@0: } sl@0: } {a different invocation} sl@0: db2 close sl@0: sqlite3_soft_heap_limit $soft_limit sl@0: sl@0: #----------------------------------------------------------------------- sl@0: # The following tests verify the behaviour of the incremental IO sl@0: # APIs in the following cases: sl@0: # sl@0: # 7.1 A row that containing an open blob is modified. sl@0: # sl@0: # 7.2 A CREATE TABLE requires that an overflow page that is part sl@0: # of an open blob is moved. sl@0: # sl@0: # 7.3 An INCREMENTAL VACUUM moves an overflow page that is part sl@0: # of an open blob. sl@0: # sl@0: # In the first case above, correct behaviour is for all subsequent sl@0: # read/write operations on the blob-handle to return SQLITE_ABORT. sl@0: # More accurately, blob-handles are invalidated whenever the table sl@0: # they belong to is written to. sl@0: # sl@0: # The second two cases have no external effect. They are testing sl@0: # that the internal cache of overflow page numbers is correctly sl@0: # invalidated. sl@0: # sl@0: do_test incrblob-7.1.0 { sl@0: execsql { sl@0: BEGIN; sl@0: DROP TABLE blobs; sl@0: CREATE TABLE t1 (a, b, c, d BLOB); sl@0: INSERT INTO t1(a, b, c, d) VALUES(1, 2, 3, 4); sl@0: COMMIT; sl@0: } sl@0: } {} sl@0: sl@0: foreach {tn arg} {1 "" 2 -readonly} { sl@0: sl@0: execsql { sl@0: UPDATE t1 SET d = zeroblob(10000); sl@0: } sl@0: sl@0: do_test incrblob-7.1.$tn.1 { sl@0: set ::b [eval db incrblob $arg t1 d 1] sl@0: binary scan [sqlite3_blob_read $::b 5000 5] c* c sl@0: set c sl@0: } {0 0 0 0 0} sl@0: do_test incrblob-7.1.$tn.2 { sl@0: execsql { sl@0: UPDATE t1 SET d = 15; sl@0: } sl@0: } {} sl@0: do_test incrblob-7.1.$tn.3 { sl@0: set rc [catch { sqlite3_blob_read $::b 5000 5 } msg] sl@0: list $rc $msg sl@0: } {1 SQLITE_ABORT} sl@0: do_test incrblob-7.1.$tn.4 { sl@0: execsql { sl@0: SELECT d FROM t1; sl@0: } sl@0: } {15} sl@0: do_test incrblob-7.1.$tn.5 { sl@0: set rc [catch { close $::b } msg] sl@0: list $rc $msg sl@0: } {0 {}} sl@0: do_test incrblob-7.1.$tn.6 { sl@0: execsql { sl@0: SELECT d FROM t1; sl@0: } sl@0: } {15} sl@0: sl@0: } sl@0: sl@0: set fd [open [info script]] sl@0: fconfigure $fd -translation binary sl@0: set ::data [read $fd 14000] sl@0: close $fd sl@0: sl@0: db close sl@0: file delete -force test.db test.db-journal sl@0: sqlite3 db test.db sl@0: sl@0: do_test incrblob-7.2.1 { sl@0: execsql { sl@0: PRAGMA auto_vacuum = "incremental"; sl@0: CREATE TABLE t1(a INTEGER PRIMARY KEY, b); -- root@page3 sl@0: INSERT INTO t1 VALUES(123, $::data); sl@0: } sl@0: set ::b [db incrblob -readonly t1 b 123] sl@0: fconfigure $::b -translation binary sl@0: read $::b sl@0: } $::data sl@0: do_test incrblob-7.2.2 { sl@0: execsql { sl@0: CREATE TABLE t2(a INTEGER PRIMARY KEY, b); -- root@page4 sl@0: } sl@0: seek $::b 0 sl@0: read $::b sl@0: } $::data sl@0: do_test incrblob-7.2.3 { sl@0: close $::b sl@0: execsql { sl@0: SELECT rootpage FROM sqlite_master; sl@0: } sl@0: } {3 4} sl@0: sl@0: set ::otherdata "[string range $::data 0 1000][string range $::data 1001 end]" sl@0: do_test incrblob-7.3.1 { sl@0: execsql { sl@0: INSERT INTO t2 VALUES(456, $::otherdata); sl@0: } sl@0: set ::b [db incrblob -readonly t2 b 456] sl@0: fconfigure $::b -translation binary sl@0: read $::b sl@0: } $::otherdata sl@0: do_test incrblob-7.3.2 { sl@0: expr [file size test.db]/1024 sl@0: } 30 sl@0: do_test incrblob-7.3.3 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE a = 123; sl@0: PRAGMA INCREMENTAL_VACUUM(0); sl@0: } sl@0: seek $::b 0 sl@0: read $::b sl@0: } $::otherdata sl@0: sl@0: # Attempt to write on a read-only blob. Make sure the error code sl@0: # gets set. Ticket #2464. sl@0: # sl@0: do_test incrblob-7.4 { sl@0: set rc [catch {sqlite3_blob_write $::b 10 HELLO} msg] sl@0: lappend rc $msg sl@0: } {1 SQLITE_READONLY} sl@0: do_test incrblob-7.5 { sl@0: sqlite3_errcode db sl@0: } {SQLITE_READONLY} sl@0: do_test incrblob-7.6 { sl@0: sqlite3_errmsg db sl@0: } {attempt to write a readonly database} sl@0: sl@0: finish_test