blob: 96038c6c6e08bcfd825aa510a0e24f0af643aff3 [file] [log] [blame]
danielk1977b4e9af92007-05-01 17:49:49 +00001# 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#
danielk19778cbadb02007-05-03 16:31:26 +000012# $Id: incrblob.test,v 1.5 2007/05/03 16:31:26 danielk1977 Exp $
danielk197720713f32007-05-03 11:43:33 +000013#
danielk1977b4e9af92007-05-01 17:49:49 +000014
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18do_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
26do_test incrblob-1.2.1 {
27 set ::blob [db incrblob blobs v 1]
28} {incrblob_1}
29do_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}
33do_test incrblob-1.2.3 {
34 seek $::blob 0
35 puts -nonewline $::blob "1234567890"
36 flush $::blob
37} {}
38do_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}
43do_test incrblob-1.2.5 {
44 close $::blob
45} {}
46do_test incrblob-1.2.6 {
47 execsql {
48 SELECT v FROM blobs WHERE rowid = 1;
49 }
50} {1234567890}
51
danielk1977d04417962007-05-02 13:16:30 +000052#--------------------------------------------------------------------
danielk197720713f32007-05-03 11:43:33 +000053# Test cases incrblob-1.3.X check that it is possible to read and write
danielk1977d04417962007-05-02 13:16:30 +000054# regions of a blob that lie on overflow pages.
danielk197720713f32007-05-03 11:43:33 +000055#
56do_test incrblob-1.3.1 {
danielk1977d04417962007-05-02 13:16:30 +000057 set ::str "[string repeat . 10000]"
58 execsql {
59 INSERT INTO blobs(rowid, k, v) VALUES(3, 'three', $::str);
60 }
61} {}
danielk1977b4e9af92007-05-01 17:49:49 +000062
danielk197720713f32007-05-03 11:43:33 +000063do_test incrblob-1.3.2 {
danielk1977d04417962007-05-02 13:16:30 +000064 set ::blob [db incrblob blobs v 3]
65 seek $::blob 8500
66 read $::blob 10
67} {..........}
danielk197720713f32007-05-03 11:43:33 +000068do_test incrblob-1.3.3 {
danielk1977d04417962007-05-02 13:16:30 +000069 seek $::blob 8500
70 puts -nonewline $::blob 1234567890
71} {}
danielk197720713f32007-05-03 11:43:33 +000072do_test incrblob-1.3.4 {
danielk1977d04417962007-05-02 13:16:30 +000073 seek $::blob 8496
74 read $::blob 10
75} {....123456}
danielk197720713f32007-05-03 11:43:33 +000076do_test incrblob-1.3.10 {
danielk1977d04417962007-05-02 13:16:30 +000077 close $::blob
78} {}
79
danielk1977b4e9af92007-05-01 17:49:49 +000080
danielk197720713f32007-05-03 11:43:33 +000081#------------------------------------------------------------------------
danielk19778cbadb02007-05-03 16:31:26 +000082# incrblob-2.*:
83#
84# Test that the following operations use ptrmap pages to reduce
85# unnecessary reads:
danielk197744e6c8d2007-05-03 13:11:32 +000086#
87# * Reading near the end of a blob,
danielk19778cbadb02007-05-03 16:31:26 +000088# * Writing near the end of a blob, and
89# * SELECT a column value that is located on an overflow page.
danielk197720713f32007-05-03 11:43:33 +000090#
91proc nRead {db} {
92 set bt [btree_from_db $db]
93 array set stats [btree_pager_stats $bt]
94 return $stats(read)
95}
danielk19778cbadb02007-05-03 16:31:26 +000096proc nWrite {db} {
97 set bt [btree_from_db $db]
98 array set stats [btree_pager_stats $bt]
99 return $stats(write)
100}
danielk197720713f32007-05-03 11:43:33 +0000101
102foreach 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;
danielk19778cbadb02007-05-03 16:31:26 +0000114 CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);
danielk197720713f32007-05-03 11:43:33 +0000115 DELETE FROM blobs;
danielk19778cbadb02007-05-03 16:31:26 +0000116 INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45);
danielk197720713f32007-05-03 11:43:33 +0000117 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
danielk19778cbadb02007-05-03 16:31:26 +0000146 do_test incrblob-2.$AutoVacuumMode.4 {
danielk197720713f32007-05-03 11:43:33 +0000147 string range [db one {SELECT v FROM blobs}] end-19 end
148 } $::fragment
danielk19778cbadb02007-05-03 16:31:26 +0000149
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]
danielk197720713f32007-05-03 11:43:33 +0000190}
191
danielk19778cbadb02007-05-03 16:31:26 +0000192#------------------------------------------------------------------------
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#
201do_test incrblob-3.1 {
202 set ::blob [db incrblob -readonly blobs v 1]
203 seek $::blob -40 end
204 read $::blob 20
205} "1234567890abcdefghij"
206do_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#
224do_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
231do_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
238do_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
245do_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
danielk197720713f32007-05-03 11:43:33 +0000252finish_test
danielk19778cbadb02007-05-03 16:31:26 +0000253