danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 1 | # 2007 May 1 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
| 9 | # |
| 10 | #*********************************************************************** |
| 11 | # |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 12 | # $Id: incrblob.test,v 1.5 2007/05/03 16:31:26 danielk1977 Exp $ |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 13 | # |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | |
| 18 | do_test incrblob-1.1 { |
| 19 | execsql { |
| 20 | CREATE TABLE blobs(k PRIMARY KEY, v BLOB); |
| 21 | INSERT INTO blobs VALUES('one', X'0102030405060708090A'); |
| 22 | INSERT INTO blobs VALUES('two', X'0A090807060504030201'); |
| 23 | } |
| 24 | } {} |
| 25 | |
| 26 | do_test incrblob-1.2.1 { |
| 27 | set ::blob [db incrblob blobs v 1] |
| 28 | } {incrblob_1} |
| 29 | do_test incrblob-1.2.2 { |
| 30 | binary scan [read $::blob] c* data |
| 31 | set data |
| 32 | } {1 2 3 4 5 6 7 8 9 10} |
| 33 | do_test incrblob-1.2.3 { |
| 34 | seek $::blob 0 |
| 35 | puts -nonewline $::blob "1234567890" |
| 36 | flush $::blob |
| 37 | } {} |
| 38 | do_test incrblob-1.2.4 { |
| 39 | seek $::blob 0 |
| 40 | binary scan [read $::blob] c* data |
| 41 | set data |
| 42 | } {49 50 51 52 53 54 55 56 57 48} |
| 43 | do_test incrblob-1.2.5 { |
| 44 | close $::blob |
| 45 | } {} |
| 46 | do_test incrblob-1.2.6 { |
| 47 | execsql { |
| 48 | SELECT v FROM blobs WHERE rowid = 1; |
| 49 | } |
| 50 | } {1234567890} |
| 51 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 52 | #-------------------------------------------------------------------- |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 53 | # Test cases incrblob-1.3.X check that it is possible to read and write |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 54 | # regions of a blob that lie on overflow pages. |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 55 | # |
| 56 | do_test incrblob-1.3.1 { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 57 | set ::str "[string repeat . 10000]" |
| 58 | execsql { |
| 59 | INSERT INTO blobs(rowid, k, v) VALUES(3, 'three', $::str); |
| 60 | } |
| 61 | } {} |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 62 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 63 | do_test incrblob-1.3.2 { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 64 | set ::blob [db incrblob blobs v 3] |
| 65 | seek $::blob 8500 |
| 66 | read $::blob 10 |
| 67 | } {..........} |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 68 | do_test incrblob-1.3.3 { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 69 | seek $::blob 8500 |
| 70 | puts -nonewline $::blob 1234567890 |
| 71 | } {} |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 72 | do_test incrblob-1.3.4 { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 73 | seek $::blob 8496 |
| 74 | read $::blob 10 |
| 75 | } {....123456} |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 76 | do_test incrblob-1.3.10 { |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 77 | close $::blob |
| 78 | } {} |
| 79 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 80 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 81 | #------------------------------------------------------------------------ |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 82 | # incrblob-2.*: |
| 83 | # |
| 84 | # Test that the following operations use ptrmap pages to reduce |
| 85 | # unnecessary reads: |
danielk1977 | 44e6c8d | 2007-05-03 13:11:32 +0000 | [diff] [blame] | 86 | # |
| 87 | # * Reading near the end of a blob, |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 88 | # * Writing near the end of a blob, and |
| 89 | # * SELECT a column value that is located on an overflow page. |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 90 | # |
| 91 | proc nRead {db} { |
| 92 | set bt [btree_from_db $db] |
| 93 | array set stats [btree_pager_stats $bt] |
| 94 | return $stats(read) |
| 95 | } |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 96 | proc nWrite {db} { |
| 97 | set bt [btree_from_db $db] |
| 98 | array set stats [btree_pager_stats $bt] |
| 99 | return $stats(write) |
| 100 | } |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 101 | |
| 102 | foreach AutoVacuumMode [list 0 1] { |
| 103 | |
| 104 | db close |
| 105 | file delete -force test.db test.db-journal |
| 106 | |
| 107 | sqlite3 db test.db |
| 108 | execsql "PRAGMA auto_vacuum = $AutoVacuumMode" |
| 109 | |
| 110 | do_test incrblob-2.$AutoVacuumMode.1 { |
| 111 | set ::str [string repeat abcdefghij 2900] |
| 112 | execsql { |
| 113 | BEGIN; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 114 | CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 115 | DELETE FROM blobs; |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 116 | INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 117 | COMMIT; |
| 118 | } |
| 119 | expr [file size test.db]/1024 |
| 120 | } [expr 31 + $AutoVacuumMode] |
| 121 | |
| 122 | do_test incrblob-2.$AutoVacuumMode.2 { |
| 123 | execsql { |
| 124 | PRAGMA auto_vacuum; |
| 125 | } |
| 126 | } $AutoVacuumMode |
| 127 | |
| 128 | do_test incrblob-2.$AutoVacuumMode.3 { |
| 129 | # Open and close the db to make sure the page cache is empty. |
| 130 | db close |
| 131 | sqlite3 db test.db |
| 132 | |
| 133 | # Read the last 20 bytes of the blob via a blob handle. |
| 134 | set ::blob [db incrblob blobs v 1] |
| 135 | seek $::blob -20 end |
| 136 | set ::fragment [read $::blob] |
| 137 | close $::blob |
| 138 | |
| 139 | # If the database is not in auto-vacuum mode, the whole of |
| 140 | # the overflow-chain must be scanned. In auto-vacuum mode, |
| 141 | # sqlite uses the ptrmap pages to avoid reading the other pages. |
| 142 | # |
| 143 | nRead db |
| 144 | } [expr $AutoVacuumMode ? 4 : 30] |
| 145 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 146 | do_test incrblob-2.$AutoVacuumMode.4 { |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 147 | string range [db one {SELECT v FROM blobs}] end-19 end |
| 148 | } $::fragment |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 149 | |
| 150 | do_test incrblob-2.$AutoVacuumMode.5 { |
| 151 | # Open and close the db to make sure the page cache is empty. |
| 152 | db close |
| 153 | sqlite3 db test.db |
| 154 | |
| 155 | # Write the second-to-last 20 bytes of the blob via a blob handle. |
| 156 | # |
| 157 | set ::blob [db incrblob blobs v 1] |
| 158 | seek $::blob -40 end |
| 159 | puts -nonewline $::blob "1234567890abcdefghij" |
| 160 | flush $::blob |
| 161 | |
| 162 | # If the database is not in auto-vacuum mode, the whole of |
| 163 | # the overflow-chain must be scanned. In auto-vacuum mode, |
| 164 | # sqlite uses the ptrmap pages to avoid reading the other pages. |
| 165 | # |
| 166 | nRead db |
| 167 | } [expr $AutoVacuumMode ? 4 : 30] |
| 168 | |
| 169 | # Pages 1 (the write-counter) and 32 (the blob data) were written. |
| 170 | do_test incrblob-2.$AutoVacuumMode.6 { |
| 171 | close $::blob |
| 172 | nWrite db |
| 173 | } 2 |
| 174 | |
| 175 | do_test incrblob-2.$AutoVacuumMode.7 { |
| 176 | string range [db one {SELECT v FROM blobs}] end-39 end-20 |
| 177 | } "1234567890abcdefghij" |
| 178 | |
| 179 | do_test incrblob-2.$AutoVacuumMode.8 { |
| 180 | # Open and close the db to make sure the page cache is empty. |
| 181 | db close |
| 182 | sqlite3 db test.db |
| 183 | |
| 184 | execsql { SELECT i FROM blobs } |
| 185 | } {45} |
| 186 | |
| 187 | do_test incrblob-2.$AutoVacuumMode.9 { |
| 188 | nRead db |
| 189 | } [expr $AutoVacuumMode ? 4 : 30] |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 190 | } |
| 191 | |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 192 | #------------------------------------------------------------------------ |
| 193 | # incrblob-3.*: |
| 194 | # |
| 195 | # Test the outcome of trying to write to a read-only blob handle. |
| 196 | # |
| 197 | # TODO: The following test only tests the tcl interface, not the |
| 198 | # underlying sqlite3 interface. Need to find some other method |
| 199 | # to call sqlite3_blob_write() on a readonly handle... |
| 200 | # |
| 201 | do_test incrblob-3.1 { |
| 202 | set ::blob [db incrblob -readonly blobs v 1] |
| 203 | seek $::blob -40 end |
| 204 | read $::blob 20 |
| 205 | } "1234567890abcdefghij" |
| 206 | do_test incrblob-3.2 { |
| 207 | seek $::blob 0 |
| 208 | set rc [catch { |
| 209 | puts -nonewline $::blob "helloworld" |
| 210 | } msg] |
| 211 | list $rc $msg |
| 212 | } "1 {channel \"$::blob\" wasn't opened for writing}" |
| 213 | |
| 214 | #------------------------------------------------------------------------ |
| 215 | # incrblob-4.*: |
| 216 | # |
| 217 | # Try a couple of error conditions: |
| 218 | # |
| 219 | # 4.1 - Attempt to open a row that does not exist. |
| 220 | # 4.2 - Attempt to open a column that does not exist. |
| 221 | # 4.3 - Attempt to open a table that does not exist. |
| 222 | # 4.4 - Attempt to open a database that does not exist. |
| 223 | # |
| 224 | do_test incrblob-4.1 { |
| 225 | set rc [catch { |
| 226 | set ::blob [db incrblob blobs v 2] |
| 227 | } msg ] |
| 228 | list $rc $msg |
| 229 | } {1 {no such rowid: 2}} |
| 230 | |
| 231 | do_test incrblob-4.2 { |
| 232 | set rc [catch { |
| 233 | set ::blob [db incrblob blobs blue 1] |
| 234 | } msg ] |
| 235 | list $rc $msg |
| 236 | } {1 {no such column: "blue"}} |
| 237 | |
| 238 | do_test incrblob-4.3 { |
| 239 | set rc [catch { |
| 240 | set ::blob [db incrblob nosuchtable blue 1] |
| 241 | } msg ] |
| 242 | list $rc $msg |
| 243 | } {1 {no such table: main.nosuchtable}} |
| 244 | |
| 245 | do_test incrblob-4.4 { |
| 246 | set rc [catch { |
| 247 | set ::blob [db incrblob nosuchdb blobs v 1] |
| 248 | } msg ] |
| 249 | list $rc $msg |
| 250 | } {1 {no such table: nosuchdb.blobs}} |
| 251 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 252 | finish_test |
danielk1977 | 8cbadb0 | 2007-05-03 16:31:26 +0000 | [diff] [blame^] | 253 | |