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 | 44e6c8d | 2007-05-03 13:11:32 +0000 | [diff] [blame^] | 12 | # $Id: incrblob.test,v 1.4 2007/05/03 13:11:32 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 | 44e6c8d | 2007-05-03 13:11:32 +0000 | [diff] [blame^] | 82 | # incrblob-2.*: Test that the following operations use ptrmap pages: |
| 83 | # |
| 84 | # * Reading near the end of a blob, |
| 85 | # * Writing near the end of a blob (TODO), |
| 86 | # * SELECT a column value that is located on an overflow page (TODO). |
| 87 | # |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 88 | # |
| 89 | proc nRead {db} { |
| 90 | set bt [btree_from_db $db] |
| 91 | array set stats [btree_pager_stats $bt] |
| 92 | return $stats(read) |
| 93 | } |
| 94 | |
| 95 | foreach AutoVacuumMode [list 0 1] { |
| 96 | |
| 97 | db close |
| 98 | file delete -force test.db test.db-journal |
| 99 | |
| 100 | sqlite3 db test.db |
| 101 | execsql "PRAGMA auto_vacuum = $AutoVacuumMode" |
| 102 | |
| 103 | do_test incrblob-2.$AutoVacuumMode.1 { |
| 104 | set ::str [string repeat abcdefghij 2900] |
| 105 | execsql { |
| 106 | BEGIN; |
| 107 | CREATE TABLE blobs(k PRIMARY KEY, v BLOB); |
| 108 | DELETE FROM blobs; |
| 109 | INSERT INTO blobs VALUES('one', $::str || randstr(500,500)); |
| 110 | COMMIT; |
| 111 | } |
| 112 | expr [file size test.db]/1024 |
| 113 | } [expr 31 + $AutoVacuumMode] |
| 114 | |
| 115 | do_test incrblob-2.$AutoVacuumMode.2 { |
| 116 | execsql { |
| 117 | PRAGMA auto_vacuum; |
| 118 | } |
| 119 | } $AutoVacuumMode |
| 120 | |
| 121 | do_test incrblob-2.$AutoVacuumMode.3 { |
| 122 | # Open and close the db to make sure the page cache is empty. |
| 123 | db close |
| 124 | sqlite3 db test.db |
| 125 | |
| 126 | # Read the last 20 bytes of the blob via a blob handle. |
| 127 | set ::blob [db incrblob blobs v 1] |
| 128 | seek $::blob -20 end |
| 129 | set ::fragment [read $::blob] |
| 130 | close $::blob |
| 131 | |
| 132 | # If the database is not in auto-vacuum mode, the whole of |
| 133 | # the overflow-chain must be scanned. In auto-vacuum mode, |
| 134 | # sqlite uses the ptrmap pages to avoid reading the other pages. |
| 135 | # |
| 136 | nRead db |
| 137 | } [expr $AutoVacuumMode ? 4 : 30] |
| 138 | |
danielk1977 | 44e6c8d | 2007-05-03 13:11:32 +0000 | [diff] [blame^] | 139 | do_test incrblob-2.$AutoVacuumMode.3 { |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 140 | string range [db one {SELECT v FROM blobs}] end-19 end |
| 141 | } $::fragment |
| 142 | } |
| 143 | |
| 144 | finish_test |