Kitxuuu commited on
Commit
3550529
·
verified ·
1 Parent(s): 8dceeb0

Add files using upload-large-folder tool

Browse files
Files changed (20) hide show
  1. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts3/unicode/parseunicode.tcl +205 -0
  2. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5bigid.test +62 -0
  3. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5corrupt4.test +63 -0
  4. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5faultA.test +63 -0
  5. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5faultH.test +150 -0
  6. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5merge2.test +57 -0
  7. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5near.test +70 -0
  8. local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5vocab2.test +310 -0
  9. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/README +40 -0
  10. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest1.c +656 -0
  11. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest4.c +127 -0
  12. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest8.c +324 -0
  13. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest9.c +140 -0
  14. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_func.c +177 -0
  15. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_mem.c +409 -0
  16. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb.c +846 -0
  17. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb.h +174 -0
  18. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb2.cc +369 -0
  19. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb3.c +1429 -0
  20. local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb4.c +980 -0
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts3/unicode/parseunicode.tcl ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #--------------------------------------------------------------------------
3
+ # Parameter $zName must be a path to the file UnicodeData.txt. This command
4
+ # reads the file and returns a list of mappings required to remove all
5
+ # diacritical marks from a unicode string. Each mapping is itself a list
6
+ # consisting of two elements - the unicode codepoint and the single ASCII
7
+ # character that it should be replaced with, or an empty string if the
8
+ # codepoint should simply be removed from the input. Examples:
9
+ #
10
+ # { 224 a 0 } (replace codepoint 224 to "a")
11
+ # { 769 "" 0 } (remove codepoint 769 from input)
12
+ #
13
+ # Mappings are only returned for non-upper case codepoints. It is assumed
14
+ # that the input has already been folded to lower case.
15
+ #
16
+ # The third value in the list is always either 0 or 1. 0 if the
17
+ # UnicodeData.txt file maps the codepoint to a single ASCII character and
18
+ # a diacritic, or 1 if the mapping is indirect. For example, consider the
19
+ # two entries:
20
+ #
21
+ # 1ECD;LATIN SMALL LETTER O WITH DOT BELOW;Ll;0;L;006F 0323;;;;N;;;1ECC;;1ECC
22
+ # 1ED9;LATIN SMALL LETTER O WITH CIRCUMFLEX AND DOT BELOW;Ll;0;L;1ECD 0302;;;;N;;;1ED8;;1ED8
23
+ #
24
+ # The first codepoint is a direct mapping (as 006F is ASCII and 0323 is a
25
+ # diacritic). The second is an indirect mapping, as it maps to the
26
+ # first codepoint plus 0302 (a diacritic).
27
+ #
28
+ proc rd_load_unicodedata_text {zName} {
29
+ global tl_lookup_table
30
+
31
+ set fd [open $zName]
32
+ set lField {
33
+ code
34
+ character_name
35
+ general_category
36
+ canonical_combining_classes
37
+ bidirectional_category
38
+ character_decomposition_mapping
39
+ decimal_digit_value
40
+ digit_value
41
+ numeric_value
42
+ mirrored
43
+ unicode_1_name
44
+ iso10646_comment_field
45
+ uppercase_mapping
46
+ lowercase_mapping
47
+ titlecase_mapping
48
+ }
49
+ set lRet [list]
50
+
51
+ while { ![eof $fd] } {
52
+ set line [gets $fd]
53
+ if {$line == ""} continue
54
+
55
+ set fields [split $line ";"]
56
+ if {[llength $fields] != [llength $lField]} { error "parse error: $line" }
57
+ foreach $lField $fields {}
58
+ if { [llength $character_decomposition_mapping]!=2
59
+ || [string is xdigit [lindex $character_decomposition_mapping 0]]==0
60
+ } {
61
+ continue
62
+ }
63
+
64
+ set iCode [expr "0x$code"]
65
+ set iAscii [expr "0x[lindex $character_decomposition_mapping 0]"]
66
+ set iDia [expr "0x[lindex $character_decomposition_mapping 1]"]
67
+
68
+ # Filter out upper-case characters, as they will be mapped to their
69
+ # lower-case equivalents before this data is used.
70
+ if {[info exists tl_lookup_table($iCode)]} continue
71
+
72
+ # Check if this is an indirect mapping. If so, set bIndirect to true
73
+ # and change $iAscii to the indirectly mappped ASCII character.
74
+ set bIndirect 0
75
+ if {[info exists dia($iDia)] && [info exists mapping($iAscii)]} {
76
+ set iAscii $mapping($iAscii)
77
+ set bIndirect 1
78
+ }
79
+
80
+ if { ($iAscii >= 97 && $iAscii <= 122)
81
+ || ($iAscii >= 65 && $iAscii <= 90)
82
+ } {
83
+ lappend lRet [list $iCode [string tolower [format %c $iAscii]] $bIndirect]
84
+ set mapping($iCode) $iAscii
85
+ set dia($iDia) 1
86
+ }
87
+ }
88
+
89
+ foreach d [array names dia] {
90
+ lappend lRet [list $d "" 0]
91
+ }
92
+ set lRet [lsort -integer -index 0 $lRet]
93
+
94
+ close $fd
95
+ set lRet
96
+ }
97
+
98
+ #-------------------------------------------------------------------------
99
+ # Parameter $zName must be a path to the file UnicodeData.txt. This command
100
+ # reads the file and returns a list of codepoints (integers). The list
101
+ # contains all codepoints in the UnicodeData.txt assigned to any "General
102
+ # Category" that is not a "Letter" or "Number".
103
+ #
104
+ proc an_load_unicodedata_text {zName} {
105
+ set fd [open $zName]
106
+ set lField {
107
+ code
108
+ character_name
109
+ general_category
110
+ canonical_combining_classes
111
+ bidirectional_category
112
+ character_decomposition_mapping
113
+ decimal_digit_value
114
+ digit_value
115
+ numeric_value
116
+ mirrored
117
+ unicode_1_name
118
+ iso10646_comment_field
119
+ uppercase_mapping
120
+ lowercase_mapping
121
+ titlecase_mapping
122
+ }
123
+ set lRet [list]
124
+
125
+ while { ![eof $fd] } {
126
+ set line [gets $fd]
127
+ if {$line == ""} continue
128
+
129
+ set fields [split $line ";"]
130
+ if {[llength $fields] != [llength $lField]} { error "parse error: $line" }
131
+ foreach $lField $fields {}
132
+
133
+ set iCode [expr "0x$code"]
134
+ set bAlnum [expr {
135
+ [lsearch {L N} [string range $general_category 0 0]] >= 0
136
+ || $general_category=="Co"
137
+ }]
138
+
139
+ if { !$bAlnum } { lappend lRet $iCode }
140
+ }
141
+
142
+ close $fd
143
+ set lRet
144
+ }
145
+
146
+ proc tl_load_casefolding_txt {zName} {
147
+ global tl_lookup_table
148
+
149
+ set fd [open $zName]
150
+ while { ![eof $fd] } {
151
+ set line [gets $fd]
152
+ if {[string range $line 0 0] == "#"} continue
153
+ if {$line == ""} continue
154
+
155
+ foreach x {a b c d} {unset -nocomplain $x}
156
+ foreach {a b c d} [split $line ";"] {}
157
+
158
+ set a2 [list]
159
+ set c2 [list]
160
+ foreach elem $a { lappend a2 [expr "0x[string trim $elem]"] }
161
+ foreach elem $c { lappend c2 [expr "0x[string trim $elem]"] }
162
+ set b [string trim $b]
163
+ set d [string trim $d]
164
+
165
+ if {$b=="C" || $b=="S"} { set tl_lookup_table($a2) $c2 }
166
+ }
167
+ }
168
+
169
+ proc cc_load_unicodedata_text {zName} {
170
+ set fd [open $zName]
171
+ set lField {
172
+ code
173
+ character_name
174
+ general_category
175
+ canonical_combining_classes
176
+ bidirectional_category
177
+ character_decomposition_mapping
178
+ decimal_digit_value
179
+ digit_value
180
+ numeric_value
181
+ mirrored
182
+ unicode_1_name
183
+ iso10646_comment_field
184
+ uppercase_mapping
185
+ lowercase_mapping
186
+ titlecase_mapping
187
+ }
188
+ set lRet [list]
189
+
190
+ while { ![eof $fd] } {
191
+ set line [gets $fd]
192
+ if {$line == ""} continue
193
+
194
+ set fields [split $line ";"]
195
+ if {[llength $fields] != [llength $lField]} { error "parse error: $line" }
196
+ foreach $lField $fields {}
197
+
198
+ lappend lRet [list $code $general_category]
199
+ }
200
+
201
+ close $fd
202
+ set lRet
203
+ }
204
+
205
+
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5bigid.test ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2023 May 28
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
+ #
12
+
13
+ source [file join [file dirname [info script]] fts5_common.tcl]
14
+ set testprefix fts5bigid
15
+
16
+ # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
17
+ ifcapable !fts5 {
18
+ finish_test
19
+ return
20
+ }
21
+
22
+ set nRow 20000
23
+
24
+ proc do_ascdesc_test {tn query} {
25
+ set ::lAsc [db eval { SELECT rowid FROM x1($query) }]
26
+ set ::lDesc [db eval { SELECT rowid FROM x1($query) ORDER BY rowid DESC }]
27
+ do_test $tn.1 { lsort -integer $::lAsc } $::lAsc
28
+ do_test $tn.2 { lsort -integer -decr $::lDesc } $::lDesc
29
+ do_test $tn.3 { lsort -integer $::lDesc } $::lAsc
30
+ }
31
+
32
+ do_execsql_test 1.0 {
33
+ CREATE VIRTUAL TABLE x1 USING fts5(a);
34
+ }
35
+
36
+ do_test 1.1 {
37
+ for {set ii 0} {$ii < $nRow} {incr ii} {
38
+ db eval {
39
+ REPLACE INTO x1(rowid, a) VALUES(random(), 'movement at the station');
40
+ }
41
+ }
42
+ } {}
43
+
44
+ do_ascdesc_test 1.2 "the"
45
+
46
+ do_execsql_test 1.3 {
47
+ DELETE FROM x1
48
+ }
49
+
50
+ do_test 1.4 {
51
+ for {set ii 0} {$ii < $nRow} {incr ii} {
52
+ db eval {
53
+ INSERT INTO x1(rowid, a) VALUES(
54
+ $ii + 0x6FFFFFFFFFFFFFFF, 'movement at the station'
55
+ );
56
+ }
57
+ }
58
+ } {}
59
+
60
+ do_ascdesc_test 1.5 "movement"
61
+
62
+ finish_test
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5corrupt4.test ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2019 May 16
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
+ #
12
+ #
13
+
14
+ source [file join [file dirname [info script]] fts5_common.tcl]
15
+ set testprefix fts5corrupt4
16
+
17
+ # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
18
+ ifcapable !fts5 {
19
+ finish_test
20
+ return
21
+ }
22
+ sqlite3_fts5_may_be_corrupt 1
23
+
24
+ do_execsql_test 1.0 {
25
+ CREATE VIRTUAL TABLE ttt USING fts5(a, b);
26
+ INSERT INTO ttt
27
+ VALUES('e ee eee e ee eee e ee eee', 'eee ee e e e ee eee ee ee');
28
+ INSERT INTO ttt SELECT a||a, b||b FROM ttt;
29
+ INSERT INTO ttt SELECT a||a, b||b FROM ttt;
30
+ }
31
+
32
+ expr srand(1)
33
+
34
+ proc mutate {blob i} {
35
+ set o [expr {$i % [string length $blob]}]
36
+ set a [string range $blob 0 $o-1]
37
+ set b [string range $blob $o+1 end]
38
+ set v [expr int(rand()*255) - 127]
39
+ return "$a[binary format c $v]$b"
40
+ }
41
+ db func mutate mutate
42
+
43
+ for {set j 1000} {$j <= 5000} {incr j 1000} {
44
+ do_test 1.$j {
45
+ for {set i 0} {$i < 1000} {incr i} {
46
+ execsql {
47
+ BEGIN;
48
+ UPDATE ttt_data SET block = mutate(block, $i) WHERE id>10;
49
+ }
50
+ foreach sql {
51
+ {SELECT snippet(ttt, -1, '.', '..', '[', ']'), * FROM ttt('e*')}
52
+ {SELECT snippet(ttt, -1, '.', '..', '[', ']'), * FROM ttt('e* NOT ee*')}
53
+ } {
54
+ catch { execsql $sql }
55
+ }
56
+ execsql ROLLBACK
57
+ }
58
+ } {}
59
+ }
60
+
61
+ sqlite3_fts5_may_be_corrupt 0
62
+ finish_test
63
+
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5faultA.test ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2016 February 2
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
+ #
12
+ # This file is focused on OOM errors.
13
+ #
14
+
15
+ source [file join [file dirname [info script]] fts5_common.tcl]
16
+ source $testdir/malloc_common.tcl
17
+ set testprefix fts5faultA
18
+
19
+ # If SQLITE_ENABLE_FTS3 is defined, omit this file.
20
+ ifcapable !fts5 {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ foreach_detail_mode $testprefix {
26
+ do_execsql_test 1.0 {
27
+ CREATE VIRTUAL TABLE o1 USING fts5(a, detail=%DETAIL%);
28
+ INSERT INTO o1(o1, rank) VALUES('pgsz', 32);
29
+
30
+ WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<300 )
31
+ INSERT INTO o1 SELECT 'A B C' FROM s;
32
+
33
+ INSERT INTO o1 VALUES('A X C');
34
+
35
+ WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<300 )
36
+ INSERT INTO o1 SELECT 'A B C' FROM s;
37
+ }
38
+
39
+ do_faultsim_test 1 -faults oom* -prep {
40
+ sqlite3 db test.db
41
+ } -body {
42
+ execsql { SELECT rowid FROM o1('a NOT b') }
43
+ } -test {
44
+ faultsim_test_result {0 301}
45
+ }
46
+ }
47
+
48
+ do_execsql_test 2.0 {
49
+ CREATE VIRTUAL TABLE o2 USING fts5(a);
50
+
51
+ INSERT INTO o2 VALUES('A B C');
52
+ WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<300 )
53
+ INSERT INTO o2 SELECT group_concat('A B C ') FROM s;
54
+ }
55
+
56
+ do_faultsim_test 2 -faults oom* -prep {
57
+ sqlite3 db test.db
58
+ } -body {
59
+ execsql { SELECT rowid FROM o2('a+b+c NOT xyz') }
60
+ } -test {
61
+ faultsim_test_result {0 {1 2}}
62
+ }
63
+ finish_test
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5faultH.test ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2010 June 15
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
+ #
12
+
13
+ source [file join [file dirname [info script]] fts5_common.tcl]
14
+ source $testdir/malloc_common.tcl
15
+ set testprefix fts5faultG
16
+
17
+ # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
18
+ ifcapable !fts5 {
19
+ finish_test
20
+ return
21
+ }
22
+
23
+ set ::testprefix fts5faultH
24
+
25
+ sqlite3_fts5_register_origintext db
26
+
27
+ do_execsql_test 1.0 {
28
+ CREATE VIRTUAL TABLE t1 USING fts5(
29
+ x, tokenize="origintext unicode61", tokendata=1
30
+ );
31
+
32
+ BEGIN;
33
+ INSERT INTO t1 VALUES('oNe tWo thRee');
34
+ INSERT INTO t1 VALUES('One Two Three');
35
+ INSERT INTO t1 VALUES('onE twO threE');
36
+ COMMIT;
37
+ BEGIN;
38
+ INSERT INTO t1 VALUES('one two three');
39
+ INSERT INTO t1 VALUES('one two three');
40
+ INSERT INTO t1 VALUES('one two three');
41
+ COMMIT;
42
+ }
43
+
44
+ do_faultsim_test 1 -faults oom* -prep {
45
+ } -body {
46
+ execsql {
47
+ SELECT rowid FROM t1('three');
48
+ }
49
+ } -test {
50
+ faultsim_integrity_check
51
+ faultsim_test_result {0 {1 2 3 4 5 6}}
52
+ }
53
+
54
+
55
+ reset_db
56
+ sqlite3_fts5_register_origintext db
57
+ do_execsql_test 2.0 {
58
+ CREATE VIRTUAL TABLE t1 USING fts5(
59
+ x, tokenize="origintext unicode61", tokendata=1
60
+ );
61
+ INSERT INTO t1(t1, rank) VALUES('pgsz', 64);
62
+
63
+ BEGIN;
64
+ INSERT INTO t1(rowid, x) VALUES(10, 'aaa bbb BBB');
65
+ INSERT INTO t1(rowid, x) VALUES(12, 'bbb bbb bbb');
66
+ INSERT INTO t1(rowid, x) VALUES(13, 'bbb bbb bbb');
67
+ INSERT INTO t1(rowid, x) VALUES(14, 'bbb BBB bbb');
68
+ INSERT INTO t1(rowid, x) VALUES(15, 'bbb bbb bbb');
69
+ INSERT INTO t1(rowid, x) VALUES(16, 'bbb bbb bbb');
70
+ INSERT INTO t1(rowid, x) VALUES(17, 'bbb bbb bbb');
71
+ INSERT INTO t1(rowid, x) VALUES(18, 'bbb bbb bbb');
72
+ INSERT INTO t1(rowid, x) VALUES(19, 'bbb bbb bbb');
73
+ INSERT INTO t1(rowid, x) VALUES(20, 'bbb bbb bbb');
74
+ INSERT INTO t1(rowid, x) VALUES(21, 'bbb bbb bbb');
75
+ INSERT INTO t1(rowid, x) VALUES(22, 'bbb bbb bbb');
76
+ INSERT INTO t1(rowid, x) VALUES(23, 'bbb bbb bbb');
77
+ INSERT INTO t1(rowid, x) VALUES(24, 'aaa bbb BBB');
78
+ COMMIT;
79
+ }
80
+
81
+ do_faultsim_test 2 -faults oom* -prep {
82
+ } -body {
83
+ execsql {
84
+ SELECT rowid FROM t1('BBB AND AAA');
85
+ }
86
+ } -test {
87
+ faultsim_integrity_check
88
+ faultsim_test_result {0 {10 24}}
89
+ }
90
+
91
+ reset_db
92
+ sqlite3_fts5_register_origintext db
93
+ do_execsql_test 3.0 {
94
+ CREATE VIRTUAL TABLE t1 USING fts5(
95
+ x, tokenize="origintext unicode61", tokendata=1
96
+ );
97
+ INSERT INTO t1(t1, rank) VALUES('pgsz', 64);
98
+
99
+ INSERT INTO t1(rowid, x) VALUES(9, 'bbb Bbb BBB');
100
+ BEGIN;
101
+ INSERT INTO t1(rowid, x) VALUES(10, 'aaa bbb BBB');
102
+ INSERT INTO t1(rowid, x) VALUES(11, 'bbb Bbb BBB');
103
+ INSERT INTO t1(rowid, x) VALUES(12, 'bbb Bbb BBB');
104
+ INSERT INTO t1(rowid, x) VALUES(13, 'bbb Bbb BBB');
105
+ INSERT INTO t1(rowid, x) VALUES(14, 'bbb Bbb BBB');
106
+ INSERT INTO t1(rowid, x) VALUES(15, 'bbb Bbb BBB');
107
+ INSERT INTO t1(rowid, x) VALUES(16, 'bbb Bbb BBB');
108
+ INSERT INTO t1(rowid, x) VALUES(17, 'bbb Bbb BBB');
109
+ INSERT INTO t1(rowid, x) VALUES(18, 'bbb Bbb BBB');
110
+ INSERT INTO t1(rowid, x) VALUES(19, 'bbb Bbb BBB');
111
+ INSERT INTO t1(rowid, x) VALUES(20, 'bbb Bbb BBB');
112
+ INSERT INTO t1(rowid, x) VALUES(21, 'bbb Bbb BBB');
113
+ INSERT INTO t1(rowid, x) VALUES(22, 'bbb Bbb BBB');
114
+ INSERT INTO t1(rowid, x) VALUES(23, 'bbb Bbb BBB');
115
+ INSERT INTO t1(rowid, x) VALUES(24, 'bbb Bbb BBB');
116
+ INSERT INTO t1(rowid, x) VALUES(25, 'bbb Bbb BBB');
117
+ INSERT INTO t1(rowid, x) VALUES(26, 'bbb Bbb BBB');
118
+ INSERT INTO t1(rowid, x) VALUES(27, 'bbb Bbb BBB');
119
+ INSERT INTO t1(rowid, x) VALUES(28, 'bbb Bbb BBB');
120
+ INSERT INTO t1(rowid, x) VALUES(29, 'bbb Bbb BBB');
121
+ INSERT INTO t1(rowid, x) VALUES(30, 'bbb Bbb BBB');
122
+ INSERT INTO t1(rowid, x) VALUES(31, 'bbb Bbb BBB');
123
+ INSERT INTO t1(rowid, x) VALUES(32, 'bbb Bbb BBB');
124
+ INSERT INTO t1(rowid, x) VALUES(33, 'bbb Bbb BBB');
125
+ INSERT INTO t1(rowid, x) VALUES(34, 'bbb Bbb BBB');
126
+ INSERT INTO t1(rowid, x) VALUES(35, 'aaa bbb BBB');
127
+ COMMIT;
128
+ }
129
+
130
+ do_faultsim_test 3.1 -faults oom* -prep {
131
+ } -body {
132
+ execsql {
133
+ SELECT rowid FROM t1('BBB AND AAA');
134
+ }
135
+ } -test {
136
+ faultsim_integrity_check
137
+ faultsim_test_result {0 {10 35}}
138
+ }
139
+ do_faultsim_test 3.2 -faults oom* -prep {
140
+ } -body {
141
+ execsql {
142
+ SELECT count(*) FROM t1('BBB');
143
+ }
144
+ } -test {
145
+ faultsim_integrity_check
146
+ faultsim_test_result {0 27}
147
+ }
148
+
149
+
150
+ finish_test
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5merge2.test ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2014 Dec 20
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
+ #
12
+ # Test that focus on incremental merges of segments.
13
+ #
14
+
15
+ source [file join [file dirname [info script]] fts5_common.tcl]
16
+ set testprefix fts5merge2
17
+ return_if_no_fts5
18
+
19
+ proc dump_structure {} {
20
+ db eval {SELECT fts5_decode(id, block) AS t FROM t1_data WHERE id=10} {
21
+ foreach lvl [lrange $t 1 end] {
22
+ set seg [string repeat . [expr [llength $lvl]-2]]
23
+ puts "[lrange $lvl 0 1] $seg"
24
+ }
25
+ }
26
+ }
27
+
28
+ foreach_detail_mode $testprefix {
29
+
30
+ do_execsql_test 1.0 {
31
+ CREATE VIRTUAL TABLE t1 USING fts5(x, detail=%DETAIL%);
32
+ INSERT INTO t1(t1, rank) VALUES('pgsz', 32);
33
+ INSERT INTO t1(t1, rank) VALUES('crisismerge', 2);
34
+ INSERT INTO t1 VALUES('1 2 3 4');
35
+ }
36
+
37
+ expr srand(0)
38
+ db func rnddoc fts5_rnddoc
39
+ do_test 1.1 {
40
+ for {set i 0} {$i < 100} {incr i} {
41
+ execsql {
42
+ BEGIN;
43
+ DELETE FROM t1 WHERE rowid = 1;
44
+ INSERT INTO t1(rowid, x) VALUES(1, '1 2 3 4');
45
+ INSERT INTO t1 VALUES(rnddoc(10));
46
+ COMMIT;
47
+ }
48
+ }
49
+ } {}
50
+
51
+ do_execsql_test 1.2 {
52
+ INSERT INTO t1(t1) VALUES('integrity-check');
53
+ }
54
+
55
+ }
56
+
57
+ finish_test
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5near.test ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2014 Jan 08
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
+ #
12
+ # Tests focused on the NEAR operator.
13
+ #
14
+
15
+ source [file join [file dirname [info script]] fts5_common.tcl]
16
+ set testprefix fts5near
17
+
18
+ # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
19
+ ifcapable !fts5 {
20
+ finish_test
21
+ return
22
+ }
23
+
24
+ proc do_near_test {tn doc near res} {
25
+ uplevel [list do_execsql_test $tn "
26
+ DELETE FROM t1;
27
+ INSERT INTO t1 VALUES('$doc');
28
+ SELECT count(*) FROM t1 WHERE t1 MATCH '$near';
29
+ " $res]
30
+ }
31
+
32
+ execsql {
33
+ CREATE VIRTUAL TABLE t1 USING fts5(x, tokenize = "ascii tokenchars '.'")
34
+ }
35
+
36
+ do_near_test 1.1 ". . a . . . b . ." { NEAR(a b, 5) } 1
37
+ do_near_test 1.2 ". . a . . . b . ." { NEAR(a b, 4) } 1
38
+ do_near_test 1.3 ". . a . . . b . ." { NEAR(a b, 3) } 1
39
+ do_near_test 1.4 ". . a . . . b . ." { NEAR(a b, 2) } 0
40
+
41
+ do_near_test 1.5 ". . a . . . b . ." { NEAR(b a, 5) } 1
42
+ do_near_test 1.6 ". . a . . . b . ." { NEAR(b a, 4) } 1
43
+ do_near_test 1.7 ". . a . . . b . ." { NEAR(b a, 3) } 1
44
+ do_near_test 1.8 ". . a . . . b . ." { NEAR(b a, 2) } 0
45
+
46
+ do_near_test 1.9 ". a b . . . c . ." { NEAR("a b" c, 3) } 1
47
+ do_near_test 1.10 ". a b . . . c . ." { NEAR("a b" c, 2) } 0
48
+ do_near_test 1.11 ". a b . . . c . ." { NEAR(c "a b", 3) } 1
49
+ do_near_test 1.12 ". a b . . . c . ." { NEAR(c "a b", 2) } 0
50
+
51
+ do_near_test 1.13 ". a b . . . c d ." { NEAR(a+b c+d, 3) } 1
52
+ do_near_test 1.14 ". a b . . . c d ." { NEAR(a+b c+d, 2) } 0
53
+ do_near_test 1.15 ". a b . . . c d ." { NEAR(c+d a+b, 3) } 1
54
+ do_near_test 1.16 ". a b . . . c d ." { NEAR(c+d a+b, 2) } 0
55
+
56
+ do_near_test 1.17 ". a b . . . c d ." { NEAR(a b c d, 5) } 1
57
+ do_near_test 1.18 ". a b . . . c d ." { NEAR(a b c d, 4) } 0
58
+ do_near_test 1.19 ". a b . . . c d ." { NEAR(a+b c d, 4) } 1
59
+
60
+ do_near_test 1.20 "a b c d e f g h i" { NEAR(b+c a+b+c+d i, 5) } 1
61
+ do_near_test 1.21 "a b c d e f g h i" { NEAR(b+c a+b+c+d i, 4) } 0
62
+
63
+ do_near_test 1.22 "a b c d e f g h i" { NEAR(a+b+c+d i b+c, 5) } 1
64
+ do_near_test 1.23 "a b c d e f g h i" { NEAR(a+b+c+d i b+c, 4) } 0
65
+
66
+ do_near_test 1.24 "a b c d e f g h i" { NEAR(i a+b+c+d b+c, 5) } 1
67
+ do_near_test 1.25 "a b c d e f g h i" { NEAR(i a+b+c+d b+c, 4) } 0
68
+
69
+
70
+ finish_test
local-test-sqlite3-delta-02/afc-sqlite3/ext/fts5/test/fts5vocab2.test ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2017 August 10
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
+ #
12
+ # The tests in this file focus on testing the fts5vocab module.
13
+ #
14
+
15
+ source [file join [file dirname [info script]] fts5_common.tcl]
16
+ set testprefix fts5vocab2
17
+
18
+ # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
19
+ ifcapable !fts5 {
20
+ finish_test
21
+ return
22
+ }
23
+
24
+ do_execsql_test 1.0 {
25
+ CREATE VIRTUAL TABLE t1 USING fts5(a, b);
26
+ CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
27
+
28
+ INSERT INTO t1 VALUES('one two', 'two three');
29
+ INSERT INTO t1 VALUES('three four', 'four five five five');
30
+ }
31
+
32
+ do_execsql_test 1.1 {
33
+ SELECT * FROM v1;
34
+ } {
35
+ five 2 b 1
36
+ five 2 b 2
37
+ five 2 b 3
38
+ four 2 a 1
39
+ four 2 b 0
40
+ one 1 a 0
41
+ three 1 b 1
42
+ three 2 a 0
43
+ two 1 a 1
44
+ two 1 b 0
45
+ }
46
+
47
+ do_execsql_test 1.2 {
48
+ SELECT * FROM v1 WHERE term='three';
49
+ } {
50
+ three 1 b 1
51
+ three 2 a 0
52
+ }
53
+
54
+ do_execsql_test 1.3 {
55
+ BEGIN;
56
+ DELETE FROM t1 WHERE rowid=2;
57
+ SELECT * FROM v1;
58
+ ROLLBACK;
59
+ } {
60
+ one 1 a 0
61
+ three 1 b 1
62
+ two 1 a 1
63
+ two 1 b 0
64
+ }
65
+
66
+ do_execsql_test 1.4 {
67
+ BEGIN;
68
+ DELETE FROM t1 WHERE rowid=1;
69
+ SELECT * FROM v1;
70
+ ROLLBACK;
71
+ } {
72
+ five 2 b 1
73
+ five 2 b 2
74
+ five 2 b 3
75
+ four 2 a 1
76
+ four 2 b 0
77
+ three 2 a 0
78
+ }
79
+
80
+ do_execsql_test 1.5 {
81
+ DELETE FROM t1;
82
+ SELECT * FROM v1;
83
+ } {}
84
+
85
+ #-------------------------------------------------------------------------
86
+ #
87
+ do_execsql_test 2.0 {
88
+ DROP TABLE IF EXISTS t1;
89
+ DROP TABLE IF EXISTS v1;
90
+
91
+ CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=column);
92
+ CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
93
+
94
+ INSERT INTO t1 VALUES('one two', 'two three');
95
+ INSERT INTO t1 VALUES('three four', 'four five five five');
96
+ }
97
+
98
+ do_execsql_test 2.1 {
99
+ SELECT * FROM v1;
100
+ } {
101
+ five 2 b {}
102
+ four 2 a {}
103
+ four 2 b {}
104
+ one 1 a {}
105
+ three 1 b {}
106
+ three 2 a {}
107
+ two 1 a {}
108
+ two 1 b {}
109
+ }
110
+
111
+ do_execsql_test 2.2 {
112
+ SELECT * FROM v1 WHERE term='three';
113
+ } {
114
+ three 1 b {}
115
+ three 2 a {}
116
+ }
117
+
118
+ do_execsql_test 2.3 {
119
+ BEGIN;
120
+ DELETE FROM t1 WHERE rowid=2;
121
+ SELECT * FROM v1;
122
+ ROLLBACK;
123
+ } {
124
+ one 1 a {}
125
+ three 1 b {}
126
+ two 1 a {}
127
+ two 1 b {}
128
+ }
129
+
130
+ do_execsql_test 2.4 {
131
+ BEGIN;
132
+ DELETE FROM t1 WHERE rowid=1;
133
+ SELECT * FROM v1;
134
+ ROLLBACK;
135
+ } {
136
+ five 2 b {}
137
+ four 2 a {}
138
+ four 2 b {}
139
+ three 2 a {}
140
+ }
141
+
142
+ do_execsql_test 2.5 {
143
+ DELETE FROM t1;
144
+ SELECT * FROM v1;
145
+ } {}
146
+
147
+ #-------------------------------------------------------------------------
148
+ #
149
+ do_execsql_test 3.0 {
150
+ DROP TABLE IF EXISTS t1;
151
+ DROP TABLE IF EXISTS v1;
152
+
153
+ CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=none);
154
+ CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
155
+
156
+ INSERT INTO t1 VALUES('one two', 'two three');
157
+ INSERT INTO t1 VALUES('three four', 'four five five five');
158
+ }
159
+
160
+ do_execsql_test 3.1 {
161
+ SELECT * FROM v1;
162
+ } {
163
+ five 2 {} {}
164
+ four 2 {} {}
165
+ one 1 {} {}
166
+ three 1 {} {}
167
+ three 2 {} {}
168
+ two 1 {} {}
169
+ }
170
+
171
+ do_execsql_test 3.2 {
172
+ SELECT * FROM v1 WHERE term='three';
173
+ } {
174
+ three 1 {} {}
175
+ three 2 {} {}
176
+ }
177
+
178
+ do_execsql_test 3.3 {
179
+ BEGIN;
180
+ DELETE FROM t1 WHERE rowid=2;
181
+ SELECT * FROM v1;
182
+ ROLLBACK;
183
+ } {
184
+ one 1 {} {}
185
+ three 1 {} {}
186
+ two 1 {} {}
187
+ }
188
+
189
+ do_execsql_test 3.4 {
190
+ BEGIN;
191
+ DELETE FROM t1 WHERE rowid=1;
192
+ SELECT * FROM v1;
193
+ ROLLBACK;
194
+ } {
195
+ five 2 {} {}
196
+ four 2 {} {}
197
+ three 2 {} {}
198
+ }
199
+
200
+ do_execsql_test 3.5 {
201
+ DELETE FROM t1;
202
+ SELECT * FROM v1;
203
+ } {}
204
+
205
+ #-------------------------------------------------------------------------
206
+ #
207
+ reset_db
208
+ do_execsql_test 4.0 {
209
+ CREATE VIRTUAL TABLE v1 USING fts5vocab(nosuchtable, col);
210
+ }
211
+
212
+ do_catchsql_test 4.1 {
213
+ SELECT * FROM v1 WHERE term=='nosuchterm';
214
+ } {1 {no such fts5 table: main.nosuchtable}}
215
+
216
+ do_execsql_test 4.2.1 {
217
+ CREATE TABLE nosuchtable(nosuchtable, y, z);
218
+ }
219
+ do_catchsql_test 4.2.2 {
220
+ SELECT * FROM v1 WHERE term=='nosuchterm';
221
+ } {1 {no such fts5 table: main.nosuchtable}}
222
+
223
+ ifcapable fts3 {
224
+ do_execsql_test 4.3.1 {
225
+ DROP TABLE nosuchtable;
226
+ CREATE VIRTUAL TABLE nosuchtable USING fts3(a, b);
227
+ } {}
228
+ do_catchsql_test 4.3.2 {
229
+ SELECT * FROM v1 WHERE term=='nosuchterm';
230
+ } {1 {no such fts5 table: main.nosuchtable}}
231
+ do_catchsql_test 4.3.3 {
232
+ INSERT INTO nosuchtable VALUES('id', '*id');
233
+ SELECT * FROM v1 WHERE term=='nosuchterm';
234
+ } {1 {no such fts5 table: main.nosuchtable}}
235
+ }
236
+
237
+ #-------------------------------------------------------------------------
238
+ # Check that the fts5 table cannot be written while there are vocab
239
+ # cursors open.
240
+ reset_db
241
+ do_execsql_test 5.0 {
242
+ CREATE VIRTUAL TABLE t1 USING fts5(a);
243
+ CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
244
+ INSERT INTO t1 VALUES('one'), ('two'), ('three'), ('four');
245
+ }
246
+
247
+ do_test 5.1 {
248
+ list [catch {
249
+ db eval { SELECT * FROM v1 } {
250
+ db eval {INSERT INTO t1 VALUES('five')}
251
+ }
252
+ } msg] $msg
253
+ } {1 {query aborted}}
254
+
255
+ do_execsql_test 5.2 {
256
+ SELECT * FROM t1
257
+ } {one two three four five}
258
+
259
+ #-------------------------------------------------------------------------
260
+ # Check that the fts5 table cannot be written while there are vocab
261
+ # cursors open.
262
+ reset_db
263
+ do_execsql_test 5.0 {
264
+ CREATE VIRTUAL TABLE t1 USING fts5(a);
265
+ CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
266
+ WITH s(i) AS (
267
+ VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10000
268
+ )
269
+ INSERT INTO t1 SELECT
270
+ 'State Emergency Service (SES), Rural Fire Service (RFS) and Volunteers'
271
+ FROM s;
272
+ }
273
+
274
+ do_catchsql_test 5.1 {
275
+ INSERT INTO t1 SELECT rowid FROM v1
276
+ } {1 {query aborted}}
277
+
278
+ do_catchsql_test 5.2 {
279
+ DELETE FROM t1 WHERE rowid>100;
280
+ INSERT INTO t1 SELECT randomblob(3000) FROM v1
281
+ } {1 {query aborted}}
282
+
283
+ #-------------------------------------------------------------------------
284
+ reset_db
285
+ sqlite3_fts5_may_be_corrupt 1
286
+
287
+ do_execsql_test 6.0 {
288
+ BEGIN TRANSACTION;
289
+ CREATE VIRTUAL TABLE t1 USING fts5(a,b unindexed,c,tokenize="porter ascii",tokendata=1);
290
+ REPLACE INTO t1_data VALUES(1,X'03090009');
291
+ REPLACE INTO t1_data VALUES(10,X'000000000103030003010101020101030101');
292
+ REPLACE INTO t1_data VALUES(137438953473,X'0000002e023061010202010162010203010163010204010167010601020201016801060102030101690106010204040606060808');
293
+ REPLACE INTO t1_data VALUES(274877906945,X'0000001f013067020802010202010168020803010203010169020804010204040909');
294
+ REPLACE INTO t1_data VALUES(412316860417,X'0000002e023061030202010162030203010163030204010167030601020201016803060102030101690306010204040606060808');
295
+ COMMIT;
296
+ }
297
+
298
+ do_execsql_test 6.1 {
299
+ CREATE VIRTUAL TABLE t3 USING fts5vocab('t1', 'row');
300
+ }
301
+
302
+ do_catchsql_test 6.2 {
303
+ SELECT * FROM t3;
304
+ } {1 {database disk image is malformed}}
305
+
306
+ sqlite3_fts5_may_be_corrupt 0
307
+
308
+ finish_test
309
+
310
+
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/README ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ Organization of test case files:
4
+
5
+ lsmtest1.c: Data tests. Tests that perform many inserts and deletes on a
6
+ database file, then verify that the contents of the database can
7
+ be queried.
8
+
9
+ lsmtest2.c: Crash tests. Tests that attempt to verify that the database
10
+ recovers correctly following an application or system crash.
11
+
12
+ lsmtest3.c: Rollback tests. Tests that focus on the explicit rollback of
13
+ transactions and sub-transactions.
14
+
15
+ lsmtest4.c: Multi-client tests.
16
+
17
+ lsmtest5.c: Multi-client tests with a different thread for each client.
18
+
19
+ lsmtest6.c: OOM injection tests.
20
+
21
+ lsmtest7.c: API tests.
22
+
23
+ lsmtest8.c: Writer crash tests. Tests in this file attempt to verify that
24
+ the system recovers and other clients proceed unaffected if
25
+ a process fails in the middle of a write transaction.
26
+
27
+ The difference from lsmtest2.c is that this file tests
28
+ live-recovery (recovery from a failure that occurs while other
29
+ clients are still running) whereas lsmtest2.c tests recovery
30
+ from a system or power failure.
31
+
32
+ lsmtest9.c: More data tests. These focus on testing that calling
33
+ lsm_work(nMerge=1) to compact the database does not corrupt it.
34
+ In other words, that databases containing block-redirects
35
+ can be read and written.
36
+
37
+
38
+
39
+
40
+
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest1.c ADDED
@@ -0,0 +1,656 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include "lsmtest.h"
3
+
4
+ #define DATA_SEQUENTIAL TEST_DATASOURCE_SEQUENCE
5
+ #define DATA_RANDOM TEST_DATASOURCE_RANDOM
6
+
7
+ typedef struct Datatest1 Datatest1;
8
+ typedef struct Datatest2 Datatest2;
9
+
10
+ /*
11
+ ** An instance of the following structure contains parameters used to
12
+ ** customize the test function in this file. Test procedure:
13
+ **
14
+ ** 1. Create a data-source based on the "datasource definition" vars.
15
+ **
16
+ ** 2. Insert nRow key value pairs into the database.
17
+ **
18
+ ** 3. Delete all keys from the database. Deletes are done in the same
19
+ ** order as the inserts.
20
+ **
21
+ ** During steps 2 and 3 above, after each Datatest1.nVerify inserts or
22
+ ** deletes, the following:
23
+ **
24
+ ** a. Run Datasource.nTest key lookups and check the results are as expected.
25
+ **
26
+ ** b. If Datasource.bTestScan is true, run a handful (8) of range
27
+ ** queries (scanning forwards and backwards). Check that the results
28
+ ** are as expected.
29
+ **
30
+ ** c. Close and reopen the database. Then run (a) and (b) again.
31
+ */
32
+ struct Datatest1 {
33
+ /* Datasource definition */
34
+ DatasourceDefn defn;
35
+
36
+ /* Test procedure parameters */
37
+ int nRow; /* Number of rows to insert then delete */
38
+ int nVerify; /* How often to verify the db contents */
39
+ int nTest; /* Number of keys to test (0==all) */
40
+ int bTestScan; /* True to do scan tests */
41
+ };
42
+
43
+ /*
44
+ ** An instance of the following data structure is used to describe the
45
+ ** second type of test case in this file. The chief difference between
46
+ ** these tests and those described by Datatest1 is that these tests also
47
+ ** experiment with range-delete operations. Tests proceed as follows:
48
+ **
49
+ ** 1. Open the datasource described by Datatest2.defn.
50
+ **
51
+ ** 2. Open a connection on an empty database.
52
+ **
53
+ ** 3. Do this Datatest2.nIter times:
54
+ **
55
+ ** a) Insert Datatest2.nWrite key-value pairs from the datasource.
56
+ **
57
+ ** b) Select two pseudo-random keys and use them as the start
58
+ ** and end points of a range-delete operation.
59
+ **
60
+ ** c) Verify that the contents of the database are as expected (see
61
+ ** below for details).
62
+ **
63
+ ** d) Close and then reopen the database handle.
64
+ **
65
+ ** e) Verify that the contents of the database are still as expected.
66
+ **
67
+ ** The inserts and range deletes are run twice - once on the database being
68
+ ** tested and once using a control system (sqlite3, kc etc. - something that
69
+ ** works). In order to verify that the contents of the db being tested are
70
+ ** correct, the test runs a bunch of scans and lookups on both the test and
71
+ ** control databases. If the results are the same, the test passes.
72
+ */
73
+ struct Datatest2 {
74
+ DatasourceDefn defn;
75
+ int nRange;
76
+ int nWrite; /* Number of writes per iteration */
77
+ int nIter; /* Total number of iterations to run */
78
+ };
79
+
80
+ /*
81
+ ** Generate a unique name for the test case pTest with database system
82
+ ** zSystem.
83
+ */
84
+ static char *getName(const char *zSystem, int bRecover, Datatest1 *pTest){
85
+ char *zRet;
86
+ char *zData;
87
+ zData = testDatasourceName(&pTest->defn);
88
+ zRet = testMallocPrintf("data.%s.%s.rec=%d.%d.%d",
89
+ zSystem, zData, bRecover, pTest->nRow, pTest->nVerify
90
+ );
91
+ testFree(zData);
92
+ return zRet;
93
+ }
94
+
95
+ int testControlDb(TestDb **ppDb){
96
+ #ifdef HAVE_KYOTOCABINET
97
+ return tdb_open("kyotocabinet", "tmp.db", 1, ppDb);
98
+ #else
99
+ return tdb_open("sqlite3", "", 1, ppDb);
100
+ #endif
101
+ }
102
+
103
+ void testDatasourceFetch(
104
+ TestDb *pDb, /* Database handle */
105
+ Datasource *pData,
106
+ int iKey,
107
+ int *pRc /* IN/OUT: Error code */
108
+ ){
109
+ void *pKey; int nKey; /* Database key to query for */
110
+ void *pVal; int nVal; /* Expected result of query */
111
+
112
+ testDatasourceEntry(pData, iKey, &pKey, &nKey, &pVal, &nVal);
113
+ testFetch(pDb, pKey, nKey, pVal, nVal, pRc);
114
+ }
115
+
116
+ /*
117
+ ** This function is called to test that the contents of database pDb
118
+ ** are as expected. In this case, expected is defined as containing
119
+ ** key-value pairs iFirst through iLast, inclusive, from data source
120
+ ** pData. In other words, a loop like the following could be used to
121
+ ** construct a database with identical contents from scratch.
122
+ **
123
+ ** for(i=iFirst; i<=iLast; i++){
124
+ ** testDatasourceEntry(pData, i, &pKey, &nKey, &pVal, &nVal);
125
+ ** // insert (pKey, nKey) -> (pVal, nVal) into database
126
+ ** }
127
+ **
128
+ ** The key domain consists of keys 0 to (nRow-1), inclusive, from
129
+ ** data source pData. For both scan and lookup tests, keys are selected
130
+ ** pseudo-randomly from within this set.
131
+ **
132
+ ** This function runs nLookupTest lookup tests and nScanTest scan tests.
133
+ **
134
+ ** A lookup test consists of selecting a key from the domain and querying
135
+ ** pDb for it. The test fails if the presence of the key and, if present,
136
+ ** the associated value do not match the expectations defined above.
137
+ **
138
+ ** A scan test involves selecting a key from the domain and running
139
+ ** the following queries:
140
+ **
141
+ ** 1. Scan all keys equal to or greater than the key, in ascending order.
142
+ ** 2. Scan all keys equal to or smaller than the key, in descending order.
143
+ **
144
+ ** Additionally, if nLookupTest is greater than zero, the following are
145
+ ** run once:
146
+ **
147
+ ** 1. Scan all keys in the db, in ascending order.
148
+ ** 2. Scan all keys in the db, in descending order.
149
+ **
150
+ ** As you would assume, the test fails if the returned values do not match
151
+ ** expectations.
152
+ */
153
+ void testDbContents(
154
+ TestDb *pDb, /* Database handle being tested */
155
+ Datasource *pData, /* pDb contains data from here */
156
+ int nRow, /* Size of key domain */
157
+ int iFirst, /* Index of first key from pData in pDb */
158
+ int iLast, /* Index of last key from pData in pDb */
159
+ int nLookupTest, /* Number of lookup tests to run */
160
+ int nScanTest, /* Number of scan tests to run */
161
+ int *pRc /* IN/OUT: Error code */
162
+ ){
163
+ int j;
164
+ int rc = *pRc;
165
+
166
+ if( rc==0 && nScanTest ){
167
+ TestDb *pDb2 = 0;
168
+
169
+ /* Open a control db (i.e. one that we assume works) */
170
+ rc = testControlDb(&pDb2);
171
+
172
+ for(j=iFirst; rc==0 && j<=iLast; j++){
173
+ void *pKey; int nKey; /* Database key to insert */
174
+ void *pVal; int nVal; /* Database value to insert */
175
+ testDatasourceEntry(pData, j, &pKey, &nKey, &pVal, &nVal);
176
+ rc = tdb_write(pDb2, pKey, nKey, pVal, nVal);
177
+ }
178
+
179
+ if( rc==0 ){
180
+ int iKey1;
181
+ int iKey2;
182
+ void *pKey1; int nKey1; /* Start key */
183
+ void *pKey2; int nKey2; /* Final key */
184
+
185
+ iKey1 = testPrngValue((iFirst<<8) + (iLast<<16)) % nRow;
186
+ iKey2 = testPrngValue((iLast<<8) + (iFirst<<16)) % nRow;
187
+ testDatasourceEntry(pData, iKey1, &pKey2, &nKey1, 0, 0);
188
+ pKey1 = testMalloc(nKey1+1);
189
+ memcpy(pKey1, pKey2, nKey1+1);
190
+ testDatasourceEntry(pData, iKey2, &pKey2, &nKey2, 0, 0);
191
+
192
+ testScanCompare(pDb2, pDb, 0, 0, 0, 0, 0, &rc);
193
+ testScanCompare(pDb2, pDb, 0, 0, 0, pKey2, nKey2, &rc);
194
+ testScanCompare(pDb2, pDb, 0, pKey1, nKey1, 0, 0, &rc);
195
+ testScanCompare(pDb2, pDb, 0, pKey1, nKey1, pKey2, nKey2, &rc);
196
+ testScanCompare(pDb2, pDb, 1, 0, 0, 0, 0, &rc);
197
+ testScanCompare(pDb2, pDb, 1, 0, 0, pKey2, nKey2, &rc);
198
+ testScanCompare(pDb2, pDb, 1, pKey1, nKey1, 0, 0, &rc);
199
+ testScanCompare(pDb2, pDb, 1, pKey1, nKey1, pKey2, nKey2, &rc);
200
+ testFree(pKey1);
201
+ }
202
+ tdb_close(pDb2);
203
+ }
204
+
205
+ /* Test some lookups. */
206
+ for(j=0; rc==0 && j<nLookupTest; j++){
207
+ int iKey; /* Datasource key to test */
208
+ void *pKey; int nKey; /* Database key to query for */
209
+ void *pVal; int nVal; /* Expected result of query */
210
+
211
+ if( nLookupTest>=nRow ){
212
+ iKey = j;
213
+ }else{
214
+ iKey = testPrngValue(j + (iFirst<<8) + (iLast<<16)) % nRow;
215
+ }
216
+
217
+ testDatasourceEntry(pData, iKey, &pKey, &nKey, &pVal, &nVal);
218
+ if( iFirst>iKey || iKey>iLast ){
219
+ pVal = 0;
220
+ nVal = -1;
221
+ }
222
+
223
+ testFetch(pDb, pKey, nKey, pVal, nVal, &rc);
224
+ }
225
+
226
+ *pRc = rc;
227
+ }
228
+
229
+ /*
230
+ ** This function should be called during long running test cases to output
231
+ ** the progress dots (...) to stdout.
232
+ */
233
+ void testCaseProgress(int i, int n, int nDot, int *piDot){
234
+ int iDot = *piDot;
235
+ while( iDot < ( ((nDot*2+1) * i) / (n*2) ) ){
236
+ printf(".");
237
+ fflush(stdout);
238
+ iDot++;
239
+ }
240
+ *piDot = iDot;
241
+ }
242
+
243
+ int testCaseNDot(void){ return 20; }
244
+
245
+ #if 0
246
+ static void printScanCb(
247
+ void *pCtx, void *pKey, int nKey, void *pVal, int nVal
248
+ ){
249
+ printf("%s\n", (char *)pKey);
250
+ fflush(stdout);
251
+ }
252
+ #endif
253
+
254
+ void testReopenRecover(TestDb **ppDb, int *pRc){
255
+ if( *pRc==0 ){
256
+ const char *zLib = tdb_library_name(*ppDb);
257
+ const char *zDflt = tdb_default_db(zLib);
258
+ testCopyLsmdb(zDflt, "bak.db");
259
+ testClose(ppDb);
260
+ testCopyLsmdb("bak.db", zDflt);
261
+ *pRc = tdb_open(zLib, 0, 0, ppDb);
262
+ }
263
+ }
264
+
265
+
266
+ static void doDataTest1(
267
+ const char *zSystem, /* Database system to test */
268
+ int bRecover,
269
+ Datatest1 *p, /* Structure containing test parameters */
270
+ int *pRc /* OUT: Error code */
271
+ ){
272
+ int i;
273
+ int iDot;
274
+ int rc = LSM_OK;
275
+ Datasource *pData;
276
+ TestDb *pDb;
277
+ int iToggle = 0;
278
+
279
+ /* Start the test case, open a database and allocate the datasource. */
280
+ pDb = testOpen(zSystem, 1, &rc);
281
+ pData = testDatasourceNew(&p->defn);
282
+
283
+ i = 0;
284
+ iDot = 0;
285
+ while( rc==LSM_OK && i<p->nRow ){
286
+
287
+ /* Insert some data */
288
+ testWriteDatasourceRange(pDb, pData, i, p->nVerify, &rc);
289
+ i += p->nVerify;
290
+
291
+ if( iToggle ) testBegin(pDb, 1, &rc);
292
+ /* Check that the db content is correct. */
293
+ testDbContents(pDb, pData, p->nRow, 0, i-1, p->nTest, p->bTestScan, &rc);
294
+ if( iToggle ) testCommit(pDb, 0, &rc);
295
+ iToggle = (iToggle+1)%2;
296
+
297
+ if( bRecover ){
298
+ testReopenRecover(&pDb, &rc);
299
+ }else{
300
+ testReopen(&pDb, &rc);
301
+ }
302
+
303
+ /* Check that the db content is still correct. */
304
+ testDbContents(pDb, pData, p->nRow, 0, i-1, p->nTest, p->bTestScan, &rc);
305
+
306
+ /* Update the progress dots... */
307
+ testCaseProgress(i, p->nRow, testCaseNDot()/2, &iDot);
308
+ }
309
+
310
+ i = 0;
311
+ iDot = 0;
312
+ while( rc==LSM_OK && i<p->nRow ){
313
+
314
+ /* Delete some entries */
315
+ testDeleteDatasourceRange(pDb, pData, i, p->nVerify, &rc);
316
+ i += p->nVerify;
317
+
318
+ /* Check that the db content is correct. */
319
+ testDbContents(pDb, pData, p->nRow, i, p->nRow-1,p->nTest,p->bTestScan,&rc);
320
+
321
+ /* Close and reopen the database. */
322
+ if( bRecover ){
323
+ testReopenRecover(&pDb, &rc);
324
+ }else{
325
+ testReopen(&pDb, &rc);
326
+ }
327
+
328
+ /* Check that the db content is still correct. */
329
+ testDbContents(pDb, pData, p->nRow, i, p->nRow-1,p->nTest,p->bTestScan,&rc);
330
+
331
+ /* Update the progress dots... */
332
+ testCaseProgress(i, p->nRow, testCaseNDot()/2, &iDot);
333
+ }
334
+
335
+ /* Free the datasource, close the database and finish the test case. */
336
+ testDatasourceFree(pData);
337
+ tdb_close(pDb);
338
+ testCaseFinish(rc);
339
+ *pRc = rc;
340
+ }
341
+
342
+
343
+ void test_data_1(
344
+ const char *zSystem, /* Database system name */
345
+ const char *zPattern, /* Run test cases that match this pattern */
346
+ int *pRc /* IN/OUT: Error code */
347
+ ){
348
+ Datatest1 aTest[] = {
349
+ { {DATA_RANDOM, 500,600, 1000,2000}, 1000, 100, 10, 0},
350
+ { {DATA_RANDOM, 20,25, 100,200}, 1000, 250, 1000, 1},
351
+ { {DATA_RANDOM, 8,10, 100,200}, 1000, 250, 1000, 1},
352
+ { {DATA_RANDOM, 8,10, 10,20}, 1000, 250, 1000, 1},
353
+ { {DATA_RANDOM, 8,10, 1000,2000}, 1000, 250, 1000, 1},
354
+ { {DATA_RANDOM, 8,100, 10000,20000}, 100, 25, 100, 1},
355
+ { {DATA_RANDOM, 80,100, 10,20}, 1000, 250, 1000, 1},
356
+ { {DATA_RANDOM, 5000,6000, 10,20}, 100, 25, 100, 1},
357
+ { {DATA_SEQUENTIAL, 5,10, 10,20}, 1000, 250, 1000, 1},
358
+ { {DATA_SEQUENTIAL, 5,10, 100,200}, 1000, 250, 1000, 1},
359
+ { {DATA_SEQUENTIAL, 5,10, 1000,2000}, 1000, 250, 1000, 1},
360
+ { {DATA_SEQUENTIAL, 5,100, 10000,20000}, 100, 25, 100, 1},
361
+ { {DATA_RANDOM, 10,10, 100,100}, 100000, 1000, 100, 0},
362
+ { {DATA_SEQUENTIAL, 10,10, 100,100}, 100000, 1000, 100, 0},
363
+ };
364
+
365
+ int i;
366
+ int bRecover;
367
+
368
+ for(bRecover=0; bRecover<2; bRecover++){
369
+ if( bRecover==1 && memcmp(zSystem, "lsm", 3) ) break;
370
+ for(i=0; *pRc==LSM_OK && i<ArraySize(aTest); i++){
371
+ char *zName = getName(zSystem, bRecover, &aTest[i]);
372
+ if( testCaseBegin(pRc, zPattern, "%s", zName) ){
373
+ doDataTest1(zSystem, bRecover, &aTest[i], pRc);
374
+ }
375
+ testFree(zName);
376
+ }
377
+ }
378
+ }
379
+
380
+ void testCompareDb(
381
+ Datasource *pData,
382
+ int nData,
383
+ int iSeed,
384
+ TestDb *pControl,
385
+ TestDb *pDb,
386
+ int *pRc
387
+ ){
388
+ int i;
389
+
390
+ static int nCall = 0;
391
+ nCall++;
392
+
393
+ testScanCompare(pControl, pDb, 0, 0, 0, 0, 0, pRc);
394
+ testScanCompare(pControl, pDb, 1, 0, 0, 0, 0, pRc);
395
+
396
+ if( *pRc==0 ){
397
+ int iKey1;
398
+ int iKey2;
399
+ void *pKey1; int nKey1; /* Start key */
400
+ void *pKey2; int nKey2; /* Final key */
401
+
402
+ iKey1 = testPrngValue(iSeed) % nData;
403
+ iKey2 = testPrngValue(iSeed+1) % nData;
404
+ testDatasourceEntry(pData, iKey1, &pKey2, &nKey1, 0, 0);
405
+ pKey1 = testMalloc(nKey1+1);
406
+ memcpy(pKey1, pKey2, nKey1+1);
407
+ testDatasourceEntry(pData, iKey2, &pKey2, &nKey2, 0, 0);
408
+
409
+ testScanCompare(pControl, pDb, 0, 0, 0, pKey2, nKey2, pRc);
410
+ testScanCompare(pControl, pDb, 0, pKey1, nKey1, 0, 0, pRc);
411
+ testScanCompare(pControl, pDb, 0, pKey1, nKey1, pKey2, nKey2, pRc);
412
+ testScanCompare(pControl, pDb, 1, 0, 0, pKey2, nKey2, pRc);
413
+ testScanCompare(pControl, pDb, 1, pKey1, nKey1, 0, 0, pRc);
414
+ testScanCompare(pControl, pDb, 1, pKey1, nKey1, pKey2, nKey2, pRc);
415
+ testFree(pKey1);
416
+ }
417
+
418
+ for(i=0; i<nData && *pRc==0; i++){
419
+ void *pKey; int nKey;
420
+ testDatasourceEntry(pData, i, &pKey, &nKey, 0, 0);
421
+ testFetchCompare(pControl, pDb, pKey, nKey, pRc);
422
+ }
423
+ }
424
+
425
+ static void doDataTest2(
426
+ const char *zSystem, /* Database system to test */
427
+ int bRecover,
428
+ Datatest2 *p, /* Structure containing test parameters */
429
+ int *pRc /* OUT: Error code */
430
+ ){
431
+ TestDb *pDb;
432
+ TestDb *pControl;
433
+ Datasource *pData;
434
+ int i;
435
+ int rc = LSM_OK;
436
+ int iDot = 0;
437
+
438
+ /* Start the test case, open a database and allocate the datasource. */
439
+ pDb = testOpen(zSystem, 1, &rc);
440
+ pData = testDatasourceNew(&p->defn);
441
+ rc = testControlDb(&pControl);
442
+
443
+ if( tdb_lsm(pDb) ){
444
+ int nBuf = 32 * 1024 * 1024;
445
+ lsm_config(tdb_lsm(pDb), LSM_CONFIG_AUTOFLUSH, &nBuf);
446
+ }
447
+
448
+ for(i=0; rc==0 && i<p->nIter; i++){
449
+ void *pKey1; int nKey1;
450
+ void *pKey2; int nKey2;
451
+ int ii;
452
+ int nRange = MIN(p->nIter*p->nWrite, p->nRange);
453
+
454
+ for(ii=0; rc==0 && ii<p->nWrite; ii++){
455
+ int iKey = (i*p->nWrite + ii) % p->nRange;
456
+ testWriteDatasource(pControl, pData, iKey, &rc);
457
+ testWriteDatasource(pDb, pData, iKey, &rc);
458
+ }
459
+
460
+ testDatasourceEntry(pData, i+1000000, &pKey1, &nKey1, 0, 0);
461
+ pKey1 = testMallocCopy(pKey1, nKey1);
462
+ testDatasourceEntry(pData, i+2000000, &pKey2, &nKey2, 0, 0);
463
+
464
+ testDeleteRange(pDb, pKey1, nKey1, pKey2, nKey2, &rc);
465
+ testDeleteRange(pControl, pKey1, nKey1, pKey2, nKey2, &rc);
466
+ testFree(pKey1);
467
+
468
+ testCompareDb(pData, nRange, i, pControl, pDb, &rc);
469
+ if( bRecover ){
470
+ testReopenRecover(&pDb, &rc);
471
+ }else{
472
+ testReopen(&pDb, &rc);
473
+ }
474
+ testCompareDb(pData, nRange, i, pControl, pDb, &rc);
475
+
476
+ /* Update the progress dots... */
477
+ testCaseProgress(i, p->nIter, testCaseNDot(), &iDot);
478
+ }
479
+
480
+ testClose(&pDb);
481
+ testClose(&pControl);
482
+ testDatasourceFree(pData);
483
+ testCaseFinish(rc);
484
+ *pRc = rc;
485
+ }
486
+
487
+ static char *getName2(const char *zSystem, int bRecover, Datatest2 *pTest){
488
+ char *zRet;
489
+ char *zData;
490
+ zData = testDatasourceName(&pTest->defn);
491
+ zRet = testMallocPrintf("data2.%s.%s.rec=%d.%d.%d.%d",
492
+ zSystem, zData, bRecover, pTest->nRange, pTest->nWrite, pTest->nIter
493
+ );
494
+ testFree(zData);
495
+ return zRet;
496
+ }
497
+
498
+ void test_data_2(
499
+ const char *zSystem, /* Database system name */
500
+ const char *zPattern, /* Run test cases that match this pattern */
501
+ int *pRc /* IN/OUT: Error code */
502
+ ){
503
+ Datatest2 aTest[] = {
504
+ /* defn, nRange, nWrite, nIter */
505
+ { {DATA_RANDOM, 20,25, 100,200}, 10000, 10, 50 },
506
+ { {DATA_RANDOM, 20,25, 100,200}, 10000, 200, 50 },
507
+ { {DATA_RANDOM, 20,25, 100,200}, 100, 10, 1000 },
508
+ { {DATA_RANDOM, 20,25, 100,200}, 100, 200, 50 },
509
+ };
510
+
511
+ int i;
512
+ int bRecover;
513
+
514
+ for(bRecover=0; bRecover<2; bRecover++){
515
+ if( bRecover==1 && memcmp(zSystem, "lsm", 3) ) break;
516
+ for(i=0; *pRc==LSM_OK && i<ArraySize(aTest); i++){
517
+ char *zName = getName2(zSystem, bRecover, &aTest[i]);
518
+ if( testCaseBegin(pRc, zPattern, "%s", zName) ){
519
+ doDataTest2(zSystem, bRecover, &aTest[i], pRc);
520
+ }
521
+ testFree(zName);
522
+ }
523
+ }
524
+ }
525
+
526
+ /*************************************************************************
527
+ ** Test case data3.*
528
+ */
529
+
530
+ typedef struct Datatest3 Datatest3;
531
+ struct Datatest3 {
532
+ int nRange; /* Keys are between 1 and this value, incl. */
533
+ int nIter; /* Number of iterations */
534
+ int nWrite; /* Number of writes per iteration */
535
+ int nDelete; /* Number of deletes per iteration */
536
+
537
+ int nValMin; /* Minimum value size for writes */
538
+ int nValMax; /* Maximum value size for writes */
539
+ };
540
+
541
+ void testPutU32(u8 *aBuf, u32 iVal){
542
+ aBuf[0] = (iVal >> 24) & 0xFF;
543
+ aBuf[1] = (iVal >> 16) & 0xFF;
544
+ aBuf[2] = (iVal >> 8) & 0xFF;
545
+ aBuf[3] = (iVal >> 0) & 0xFF;
546
+ }
547
+
548
+ void dt3PutKey(u8 *aBuf, int iKey){
549
+ assert( iKey<100000 && iKey>=0 );
550
+ sprintf((char *)aBuf, "%.5d", iKey);
551
+ }
552
+
553
+ static void doDataTest3(
554
+ const char *zSystem, /* Database system to test */
555
+ Datatest3 *p, /* Structure containing test parameters */
556
+ int *pRc /* OUT: Error code */
557
+ ){
558
+ int iDot = 0;
559
+ int rc = *pRc;
560
+ TestDb *pDb;
561
+ u8 *abPresent; /* Array of boolean */
562
+ char *aVal; /* Buffer to hold values */
563
+ int i;
564
+ u32 iSeq = 10; /* prng counter */
565
+
566
+ abPresent = (u8 *)testMalloc(p->nRange+1);
567
+ aVal = (char *)testMalloc(p->nValMax+1);
568
+ pDb = testOpen(zSystem, 1, &rc);
569
+
570
+ for(i=0; i<p->nIter && rc==0; i++){
571
+ int ii;
572
+
573
+ testCaseProgress(i, p->nIter, testCaseNDot(), &iDot);
574
+
575
+ /* Perform nWrite inserts */
576
+ for(ii=0; ii<p->nWrite; ii++){
577
+ u8 aKey[6];
578
+ u32 iKey;
579
+ int nVal;
580
+
581
+ iKey = (testPrngValue(iSeq++) % p->nRange) + 1;
582
+ nVal = (testPrngValue(iSeq++) % (p->nValMax - p->nValMin)) + p->nValMin;
583
+ testPrngString(testPrngValue(iSeq++), aVal, nVal);
584
+ dt3PutKey(aKey, iKey);
585
+
586
+ testWrite(pDb, aKey, sizeof(aKey)-1, aVal, nVal, &rc);
587
+ abPresent[iKey] = 1;
588
+ }
589
+
590
+ /* Perform nDelete deletes */
591
+ for(ii=0; ii<p->nDelete; ii++){
592
+ u8 aKey1[6];
593
+ u8 aKey2[6];
594
+ u32 iKey;
595
+
596
+ iKey = (testPrngValue(iSeq++) % p->nRange) + 1;
597
+ dt3PutKey(aKey1, iKey-1);
598
+ dt3PutKey(aKey2, iKey+1);
599
+
600
+ testDeleteRange(pDb, aKey1, sizeof(aKey1)-1, aKey2, sizeof(aKey2)-1, &rc);
601
+ abPresent[iKey] = 0;
602
+ }
603
+
604
+ testReopen(&pDb, &rc);
605
+
606
+ for(ii=1; rc==0 && ii<=p->nRange; ii++){
607
+ int nDbVal;
608
+ void *pDbVal;
609
+ u8 aKey[6];
610
+ int dbrc;
611
+
612
+ dt3PutKey(aKey, ii);
613
+ dbrc = tdb_fetch(pDb, aKey, sizeof(aKey)-1, &pDbVal, &nDbVal);
614
+ testCompareInt(0, dbrc, &rc);
615
+
616
+ if( abPresent[ii] ){
617
+ testCompareInt(1, (nDbVal>0), &rc);
618
+ }else{
619
+ testCompareInt(1, (nDbVal<0), &rc);
620
+ }
621
+ }
622
+ }
623
+
624
+ testClose(&pDb);
625
+ testCaseFinish(rc);
626
+ *pRc = rc;
627
+ }
628
+
629
+ static char *getName3(const char *zSystem, Datatest3 *p){
630
+ return testMallocPrintf("data3.%s.%d.%d.%d.%d.(%d..%d)",
631
+ zSystem, p->nRange, p->nIter, p->nWrite, p->nDelete,
632
+ p->nValMin, p->nValMax
633
+ );
634
+ }
635
+
636
+ void test_data_3(
637
+ const char *zSystem, /* Database system name */
638
+ const char *zPattern, /* Run test cases that match this pattern */
639
+ int *pRc /* IN/OUT: Error code */
640
+ ){
641
+ Datatest3 aTest[] = {
642
+ /* nRange, nIter, nWrite, nDelete, nValMin, nValMax */
643
+ { 100, 1000, 5, 5, 50, 100 },
644
+ { 100, 1000, 2, 2, 5, 10 },
645
+ };
646
+
647
+ int i;
648
+
649
+ for(i=0; *pRc==LSM_OK && i<ArraySize(aTest); i++){
650
+ char *zName = getName3(zSystem, &aTest[i]);
651
+ if( testCaseBegin(pRc, zPattern, "%s", zName) ){
652
+ doDataTest3(zSystem, &aTest[i], pRc);
653
+ }
654
+ testFree(zName);
655
+ }
656
+ }
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest4.c ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /*
3
+ ** This file contains test cases involving multiple database clients.
4
+ */
5
+
6
+ #include "lsmtest.h"
7
+
8
+ /*
9
+ ** The following code implements test cases "mc1.*".
10
+ **
11
+ ** This test case uses one writer and $nReader readers. All connections
12
+ ** are driven by a single thread. All connections are opened at the start
13
+ ** of the test and remain open until the test is finished.
14
+ **
15
+ ** The test consists of $nStep steps. Each step the following is performed:
16
+ **
17
+ ** 1. The writer inserts $nWriteStep records into the db.
18
+ **
19
+ ** 2. The writer checks that the contents of the db are as expected.
20
+ **
21
+ ** 3. Each reader that currently has an open read transaction also checks
22
+ ** that the contents of the db are as expected (according to the snapshot
23
+ ** the read transaction is reading - see below).
24
+ **
25
+ ** After step 1, reader 1 opens a read transaction. After step 2, reader
26
+ ** 2 opens a read transaction, and so on. At step ($nReader+1), reader 1
27
+ ** closes the current read transaction and opens a new one. And so on.
28
+ ** The result is that at step N (for N > $nReader), there exists a reader
29
+ ** with an open read transaction reading the snapshot committed following
30
+ ** steps (N-$nReader-1) to N.
31
+ */
32
+ typedef struct Mctest Mctest;
33
+ struct Mctest {
34
+ DatasourceDefn defn; /* Datasource to use */
35
+ int nStep; /* Total number of steps in test */
36
+ int nWriteStep; /* Number of rows to insert each step */
37
+ int nReader; /* Number of read connections */
38
+ };
39
+ static void do_mc_test(
40
+ const char *zSystem, /* Database system to test */
41
+ Mctest *pTest,
42
+ int *pRc /* IN/OUT: return code */
43
+ ){
44
+ const int nDomain = pTest->nStep * pTest->nWriteStep;
45
+ Datasource *pData; /* Source of data */
46
+ TestDb *pDb; /* First database connection (writer) */
47
+ int iReader; /* Used to iterate through aReader */
48
+ int iStep; /* Current step in test */
49
+ int iDot = 0; /* Current step in test */
50
+
51
+ /* Array of reader connections */
52
+ struct Reader {
53
+ TestDb *pDb; /* Connection handle */
54
+ int iLast; /* Current snapshot contains keys 0..iLast */
55
+ } *aReader;
56
+
57
+ /* Create a data source */
58
+ pData = testDatasourceNew(&pTest->defn);
59
+
60
+ /* Open the writer connection */
61
+ pDb = testOpen(zSystem, 1, pRc);
62
+
63
+ /* Allocate aReader */
64
+ aReader = (struct Reader *)testMalloc(sizeof(aReader[0]) * pTest->nReader);
65
+ for(iReader=0; iReader<pTest->nReader; iReader++){
66
+ aReader[iReader].pDb = testOpen(zSystem, 0, pRc);
67
+ }
68
+
69
+ for(iStep=0; iStep<pTest->nStep; iStep++){
70
+ int iLast;
71
+ int iBegin; /* Start read trans using aReader[iBegin] */
72
+
73
+ /* Insert nWriteStep more records into the database */
74
+ int iFirst = iStep*pTest->nWriteStep;
75
+ testWriteDatasourceRange(pDb, pData, iFirst, pTest->nWriteStep, pRc);
76
+
77
+ /* Check that the db is Ok according to the writer */
78
+ iLast = (iStep+1) * pTest->nWriteStep - 1;
79
+ testDbContents(pDb, pData, nDomain, 0, iLast, iLast, 1, pRc);
80
+
81
+ /* Have reader (iStep % nReader) open a read transaction here. */
82
+ iBegin = (iStep % pTest->nReader);
83
+ if( iBegin<iStep ) tdb_commit(aReader[iBegin].pDb, 0);
84
+ tdb_begin(aReader[iBegin].pDb, 1);
85
+ aReader[iBegin].iLast = iLast;
86
+
87
+ /* Check that the db is Ok for each open reader */
88
+ for(iReader=0; iReader<pTest->nReader && aReader[iReader].iLast; iReader++){
89
+ iLast = aReader[iReader].iLast;
90
+ testDbContents(
91
+ aReader[iReader].pDb, pData, nDomain, 0, iLast, iLast, 1, pRc
92
+ );
93
+ }
94
+
95
+ /* Report progress */
96
+ testCaseProgress(iStep, pTest->nStep, testCaseNDot(), &iDot);
97
+ }
98
+
99
+ /* Close all readers */
100
+ for(iReader=0; iReader<pTest->nReader; iReader++){
101
+ testClose(&aReader[iReader].pDb);
102
+ }
103
+ testFree(aReader);
104
+
105
+ /* Close the writer-connection and free the datasource */
106
+ testClose(&pDb);
107
+ testDatasourceFree(pData);
108
+ }
109
+
110
+
111
+ void test_mc(
112
+ const char *zSystem, /* Database system name */
113
+ const char *zPattern, /* Run test cases that match this pattern */
114
+ int *pRc /* IN/OUT: Error code */
115
+ ){
116
+ int i;
117
+ Mctest aTest[] = {
118
+ { { TEST_DATASOURCE_RANDOM, 10,10, 100,100 }, 100, 10, 5 },
119
+ };
120
+
121
+ for(i=0; i<ArraySize(aTest); i++){
122
+ if( testCaseBegin(pRc, zPattern, "mc1.%s.%d", zSystem, i) ){
123
+ do_mc_test(zSystem, &aTest[i], pRc);
124
+ testCaseFinish(*pRc);
125
+ }
126
+ }
127
+ }
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest8.c ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /*
3
+ ** This file contains test cases to verify that "live-recovery" following
4
+ ** a mid-transaction failure of a writer process.
5
+ */
6
+
7
+
8
+ /*
9
+ ** This test file includes lsmInt.h to get access to the definition of the
10
+ ** ShmHeader structure. This is required to cause strategic damage to the
11
+ ** shared memory header as part of recovery testing.
12
+ */
13
+ #include "lsmInt.h"
14
+
15
+ #include "lsmtest.h"
16
+
17
+ typedef struct SetupStep SetupStep;
18
+ struct SetupStep {
19
+ int bFlush; /* Flush to disk and checkpoint */
20
+ int iInsStart; /* First key-value from ds to insert */
21
+ int nIns; /* Number of rows to insert */
22
+ int iDelStart; /* First key from ds to delete */
23
+ int nDel; /* Number of rows to delete */
24
+ };
25
+
26
+ static void doSetupStep(
27
+ TestDb *pDb,
28
+ Datasource *pData,
29
+ const SetupStep *pStep,
30
+ int *pRc
31
+ ){
32
+ testWriteDatasourceRange(pDb, pData, pStep->iInsStart, pStep->nIns, pRc);
33
+ testDeleteDatasourceRange(pDb, pData, pStep->iDelStart, pStep->nDel, pRc);
34
+ if( *pRc==0 ){
35
+ int nSave = -1;
36
+ int nBuf = 64;
37
+ lsm_db *db = tdb_lsm(pDb);
38
+
39
+ lsm_config(db, LSM_CONFIG_AUTOFLUSH, &nSave);
40
+ lsm_config(db, LSM_CONFIG_AUTOFLUSH, &nBuf);
41
+ lsm_begin(db, 1);
42
+ lsm_commit(db, 0);
43
+ lsm_config(db, LSM_CONFIG_AUTOFLUSH, &nSave);
44
+
45
+ *pRc = lsm_work(db, 0, 0, 0);
46
+ if( *pRc==0 ){
47
+ *pRc = lsm_checkpoint(db, 0);
48
+ }
49
+ }
50
+ }
51
+
52
+ static void doSetupStepArray(
53
+ TestDb *pDb,
54
+ Datasource *pData,
55
+ const SetupStep *aStep,
56
+ int nStep
57
+ ){
58
+ int i;
59
+ for(i=0; i<nStep; i++){
60
+ int rc = 0;
61
+ doSetupStep(pDb, pData, &aStep[i], &rc);
62
+ assert( rc==0 );
63
+ }
64
+ }
65
+
66
+ static void setupDatabase1(TestDb *pDb, Datasource **ppData){
67
+ const SetupStep aStep[] = {
68
+ { 0, 1, 2000, 0, 0 },
69
+ { 1, 0, 0, 0, 0 },
70
+ { 0, 10001, 1000, 0, 0 },
71
+ };
72
+ const DatasourceDefn defn = {TEST_DATASOURCE_RANDOM, 12, 16, 100, 500};
73
+ Datasource *pData;
74
+
75
+ pData = testDatasourceNew(&defn);
76
+ doSetupStepArray(pDb, pData, aStep, ArraySize(aStep));
77
+ if( ppData ){
78
+ *ppData = pData;
79
+ }else{
80
+ testDatasourceFree(pData);
81
+ }
82
+ }
83
+
84
+ #include <stdio.h>
85
+ void testReadFile(const char *zFile, int iOff, void *pOut, int nByte, int *pRc){
86
+ if( *pRc==0 ){
87
+ FILE *fd;
88
+ fd = fopen(zFile, "rb");
89
+ if( fd==0 ){
90
+ *pRc = 1;
91
+ }else{
92
+ if( 0!=fseek(fd, iOff, SEEK_SET) ){
93
+ *pRc = 1;
94
+ }else{
95
+ assert( nByte>=0 );
96
+ if( (size_t)nByte!=fread(pOut, 1, nByte, fd) ){
97
+ *pRc = 1;
98
+ }
99
+ }
100
+ fclose(fd);
101
+ }
102
+ }
103
+ }
104
+
105
+ void testWriteFile(
106
+ const char *zFile,
107
+ int iOff,
108
+ void *pOut,
109
+ int nByte,
110
+ int *pRc
111
+ ){
112
+ if( *pRc==0 ){
113
+ FILE *fd;
114
+ fd = fopen(zFile, "r+b");
115
+ if( fd==0 ){
116
+ *pRc = 1;
117
+ }else{
118
+ if( 0!=fseek(fd, iOff, SEEK_SET) ){
119
+ *pRc = 1;
120
+ }else{
121
+ assert( nByte>=0 );
122
+ if( (size_t)nByte!=fwrite(pOut, 1, nByte, fd) ){
123
+ *pRc = 1;
124
+ }
125
+ }
126
+ fclose(fd);
127
+ }
128
+ }
129
+ }
130
+
131
+ static ShmHeader *getShmHeader(const char *zDb){
132
+ int rc = 0;
133
+ char *zShm = testMallocPrintf("%s-shm", zDb);
134
+ ShmHeader *pHdr;
135
+
136
+ pHdr = testMalloc(sizeof(ShmHeader));
137
+ testReadFile(zShm, 0, (void *)pHdr, sizeof(ShmHeader), &rc);
138
+ assert( rc==0 );
139
+
140
+ return pHdr;
141
+ }
142
+
143
+ /*
144
+ ** This function makes a copy of the three files associated with LSM
145
+ ** database zDb (i.e. if zDb is "test.db", it makes copies of "test.db",
146
+ ** "test.db-log" and "test.db-shm").
147
+ **
148
+ ** It then opens a new database connection to the copy with the xLock() call
149
+ ** instrumented so that it appears that some other process already connected
150
+ ** to the db (holding a shared lock on DMS2). This prevents recovery from
151
+ ** running. Then:
152
+ **
153
+ ** 1) Check that the checksum of the database is zCksum.
154
+ ** 2) Write a few keys to the database. Then delete the same keys.
155
+ ** 3) Check that the checksum is zCksum.
156
+ ** 4) Flush the db to disk and run a checkpoint.
157
+ ** 5) Check once more that the checksum is still zCksum.
158
+ */
159
+ static void doLiveRecovery(const char *zDb, const char *zCksum, int *pRc){
160
+ if( *pRc==LSM_OK ){
161
+ const DatasourceDefn defn = {TEST_DATASOURCE_RANDOM, 20, 25, 100, 500};
162
+ Datasource *pData;
163
+ const char *zCopy = "testcopy.lsm";
164
+ char zCksum2[TEST_CKSUM_BYTES];
165
+ TestDb *pDb = 0;
166
+ int rc;
167
+
168
+ pData = testDatasourceNew(&defn);
169
+
170
+ testCopyLsmdb(zDb, zCopy);
171
+ rc = tdb_lsm_open("test_no_recovery=1", zCopy, 0, &pDb);
172
+ if( rc==0 ){
173
+ ShmHeader *pHdr;
174
+ lsm_db *db;
175
+ testCksumDatabase(pDb, zCksum2);
176
+ testCompareStr(zCksum, zCksum2, &rc);
177
+
178
+ testWriteDatasourceRange(pDb, pData, 1, 10, &rc);
179
+ testDeleteDatasourceRange(pDb, pData, 1, 10, &rc);
180
+
181
+ /* Test that the two tree-headers are now consistent. */
182
+ pHdr = getShmHeader(zCopy);
183
+ if( rc==0 && memcmp(&pHdr->hdr1, &pHdr->hdr2, sizeof(pHdr->hdr1)) ){
184
+ rc = 1;
185
+ }
186
+ testFree(pHdr);
187
+
188
+ if( rc==0 ){
189
+ int nBuf = 64;
190
+ db = tdb_lsm(pDb);
191
+ lsm_config(db, LSM_CONFIG_AUTOFLUSH, &nBuf);
192
+ lsm_begin(db, 1);
193
+ lsm_commit(db, 0);
194
+ rc = lsm_work(db, 0, 0, 0);
195
+ }
196
+
197
+ testCksumDatabase(pDb, zCksum2);
198
+ testCompareStr(zCksum, zCksum2, &rc);
199
+ }
200
+
201
+ testDatasourceFree(pData);
202
+ testClose(&pDb);
203
+ testDeleteLsmdb(zCopy);
204
+ *pRc = rc;
205
+ }
206
+ }
207
+
208
+ static void doWriterCrash1(int *pRc){
209
+ const int nWrite = 2000;
210
+ const int nStep = 10;
211
+ const int iWriteStart = 20000;
212
+ int rc = 0;
213
+ TestDb *pDb = 0;
214
+ Datasource *pData = 0;
215
+
216
+ rc = tdb_lsm_open("autowork=0", "testdb.lsm", 1, &pDb);
217
+ if( rc==0 ){
218
+ int iDot = 0;
219
+ char zCksum[TEST_CKSUM_BYTES];
220
+ int i;
221
+ setupDatabase1(pDb, &pData);
222
+ testCksumDatabase(pDb, zCksum);
223
+ testBegin(pDb, 2, &rc);
224
+ for(i=0; rc==0 && i<nWrite; i+=nStep){
225
+ testCaseProgress(i, nWrite, testCaseNDot(), &iDot);
226
+ testWriteDatasourceRange(pDb, pData, iWriteStart+i, nStep, &rc);
227
+ doLiveRecovery("testdb.lsm", zCksum, &rc);
228
+ }
229
+ }
230
+ testCommit(pDb, 0, &rc);
231
+ testClose(&pDb);
232
+ testDatasourceFree(pData);
233
+ *pRc = rc;
234
+ }
235
+
236
+ /*
237
+ ** This test case verifies that inconsistent tree-headers in shared-memory
238
+ ** are resolved correctly.
239
+ */
240
+ static void doWriterCrash2(int *pRc){
241
+ int rc = 0;
242
+ TestDb *pDb = 0;
243
+ Datasource *pData = 0;
244
+
245
+ rc = tdb_lsm_open("autowork=0", "testdb.lsm", 1, &pDb);
246
+ if( rc==0 ){
247
+ ShmHeader *pHdr1;
248
+ ShmHeader *pHdr2;
249
+ char zCksum1[TEST_CKSUM_BYTES];
250
+ char zCksum2[TEST_CKSUM_BYTES];
251
+
252
+ pHdr1 = testMalloc(sizeof(ShmHeader));
253
+ pHdr2 = testMalloc(sizeof(ShmHeader));
254
+ setupDatabase1(pDb, &pData);
255
+
256
+ /* Grab a copy of the shared-memory header. And the db checksum */
257
+ testReadFile("testdb.lsm-shm", 0, (void *)pHdr1, sizeof(ShmHeader), &rc);
258
+ testCksumDatabase(pDb, zCksum1);
259
+
260
+ /* Modify the database */
261
+ testBegin(pDb, 2, &rc);
262
+ testWriteDatasourceRange(pDb, pData, 30000, 200, &rc);
263
+ testCommit(pDb, 0, &rc);
264
+
265
+ /* Grab a second copy of the shared-memory header. And the db checksum */
266
+ testReadFile("testdb.lsm-shm", 0, (void *)pHdr2, sizeof(ShmHeader), &rc);
267
+ testCksumDatabase(pDb, zCksum2);
268
+ doLiveRecovery("testdb.lsm", zCksum2, &rc);
269
+
270
+ /* If both tree-headers are valid, tree-header-1 is used. */
271
+ memcpy(&pHdr2->hdr1, &pHdr1->hdr1, sizeof(pHdr1->hdr1));
272
+ pHdr2->bWriter = 1;
273
+ testWriteFile("testdb.lsm-shm", 0, (void *)pHdr2, sizeof(ShmHeader), &rc);
274
+ doLiveRecovery("testdb.lsm", zCksum1, &rc);
275
+
276
+ /* If both tree-headers are valid, tree-header-1 is used. */
277
+ memcpy(&pHdr2->hdr1, &pHdr2->hdr2, sizeof(pHdr1->hdr1));
278
+ memcpy(&pHdr2->hdr2, &pHdr1->hdr1, sizeof(pHdr1->hdr1));
279
+ pHdr2->bWriter = 1;
280
+ testWriteFile("testdb.lsm-shm", 0, (void *)pHdr2, sizeof(ShmHeader), &rc);
281
+ doLiveRecovery("testdb.lsm", zCksum2, &rc);
282
+
283
+ /* If tree-header 1 is invalid, tree-header-2 is used */
284
+ memcpy(&pHdr2->hdr2, &pHdr2->hdr1, sizeof(pHdr1->hdr1));
285
+ pHdr2->hdr1.aCksum[0] = 5;
286
+ pHdr2->hdr1.aCksum[0] = 6;
287
+ pHdr2->bWriter = 1;
288
+ testWriteFile("testdb.lsm-shm", 0, (void *)pHdr2, sizeof(ShmHeader), &rc);
289
+ doLiveRecovery("testdb.lsm", zCksum2, &rc);
290
+
291
+ /* If tree-header 2 is invalid, tree-header-1 is used */
292
+ memcpy(&pHdr2->hdr1, &pHdr2->hdr2, sizeof(pHdr1->hdr1));
293
+ pHdr2->hdr2.aCksum[0] = 5;
294
+ pHdr2->hdr2.aCksum[0] = 6;
295
+ pHdr2->bWriter = 1;
296
+ testWriteFile("testdb.lsm-shm", 0, (void *)pHdr2, sizeof(ShmHeader), &rc);
297
+ doLiveRecovery("testdb.lsm", zCksum2, &rc);
298
+
299
+ testFree(pHdr1);
300
+ testFree(pHdr2);
301
+ testClose(&pDb);
302
+ }
303
+
304
+ *pRc = rc;
305
+ }
306
+
307
+ void do_writer_crash_test(const char *zPattern, int *pRc){
308
+ struct Test {
309
+ const char *zName;
310
+ void (*xFunc)(int *);
311
+ } aTest[] = {
312
+ { "writercrash1.lsm", doWriterCrash1 },
313
+ { "writercrash2.lsm", doWriterCrash2 },
314
+ };
315
+ int i;
316
+ for(i=0; i<ArraySize(aTest); i++){
317
+ struct Test *p = &aTest[i];
318
+ if( testCaseBegin(pRc, zPattern, p->zName) ){
319
+ p->xFunc(pRc);
320
+ testCaseFinish(*pRc);
321
+ }
322
+ }
323
+
324
+ }
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest9.c ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include "lsmtest.h"
3
+
4
+ #define DATA_SEQUENTIAL TEST_DATASOURCE_SEQUENCE
5
+ #define DATA_RANDOM TEST_DATASOURCE_RANDOM
6
+
7
+ typedef struct Datatest4 Datatest4;
8
+
9
+ /*
10
+ ** Test overview:
11
+ **
12
+ ** 1. Insert (Datatest4.nRec) records into a database.
13
+ **
14
+ ** 2. Repeat (Datatest4.nRepeat) times:
15
+ **
16
+ ** 2a. Delete 2/3 of the records in the database.
17
+ **
18
+ ** 2b. Run lsm_work(nMerge=1).
19
+ **
20
+ ** 2c. Insert as many records as were deleted in 2a.
21
+ **
22
+ ** 2d. Check database content is as expected.
23
+ **
24
+ ** 2e. If (Datatest4.bReopen) is true, close and reopen the database.
25
+ */
26
+ struct Datatest4 {
27
+ /* Datasource definition */
28
+ DatasourceDefn defn;
29
+
30
+ int nRec;
31
+ int nRepeat;
32
+ int bReopen;
33
+ };
34
+
35
+ static void doDataTest4(
36
+ const char *zSystem, /* Database system to test */
37
+ Datatest4 *p, /* Structure containing test parameters */
38
+ int *pRc /* OUT: Error code */
39
+ ){
40
+ lsm_db *db = 0;
41
+ TestDb *pDb;
42
+ TestDb *pControl;
43
+ Datasource *pData;
44
+ int i;
45
+ int rc = 0;
46
+ int iDot = 0;
47
+ int bMultiThreaded = 0; /* True for MT LSM database */
48
+
49
+ int nRecOn3 = (p->nRec / 3);
50
+ int iData = 0;
51
+
52
+ /* Start the test case, open a database and allocate the datasource. */
53
+ rc = testControlDb(&pControl);
54
+ pDb = testOpen(zSystem, 1, &rc);
55
+ pData = testDatasourceNew(&p->defn);
56
+ if( rc==0 ){
57
+ db = tdb_lsm(pDb);
58
+ bMultiThreaded = tdb_lsm_multithread(pDb);
59
+ }
60
+
61
+ testWriteDatasourceRange(pControl, pData, iData, nRecOn3*3, &rc);
62
+ testWriteDatasourceRange(pDb, pData, iData, nRecOn3*3, &rc);
63
+
64
+ for(i=0; rc==0 && i<p->nRepeat; i++){
65
+
66
+ testDeleteDatasourceRange(pControl, pData, iData, nRecOn3*2, &rc);
67
+ testDeleteDatasourceRange(pDb, pData, iData, nRecOn3*2, &rc);
68
+
69
+ if( db ){
70
+ int nDone;
71
+ #if 0
72
+ fprintf(stderr, "lsm_work() start...\n"); fflush(stderr);
73
+ #endif
74
+ do {
75
+ nDone = 0;
76
+ rc = lsm_work(db, 1, (1<<30), &nDone);
77
+ }while( rc==0 && nDone>0 );
78
+ if( bMultiThreaded && rc==LSM_BUSY ) rc = LSM_OK;
79
+ #if 0
80
+ fprintf(stderr, "lsm_work() done...\n"); fflush(stderr);
81
+ #endif
82
+ }
83
+
84
+ if( i+1<p->nRepeat ){
85
+ iData += (nRecOn3*2);
86
+ testWriteDatasourceRange(pControl, pData, iData+nRecOn3, nRecOn3*2, &rc);
87
+ testWriteDatasourceRange(pDb, pData, iData+nRecOn3, nRecOn3*2, &rc);
88
+
89
+ testCompareDb(pData, nRecOn3*3, iData, pControl, pDb, &rc);
90
+
91
+ /* If Datatest4.bReopen is true, close and reopen the database */
92
+ if( p->bReopen ){
93
+ testReopen(&pDb, &rc);
94
+ if( rc==0 ) db = tdb_lsm(pDb);
95
+ }
96
+ }
97
+
98
+ /* Update the progress dots... */
99
+ testCaseProgress(i, p->nRepeat, testCaseNDot(), &iDot);
100
+ }
101
+
102
+ testClose(&pDb);
103
+ testClose(&pControl);
104
+ testDatasourceFree(pData);
105
+ testCaseFinish(rc);
106
+ *pRc = rc;
107
+ }
108
+
109
+ static char *getName4(const char *zSystem, Datatest4 *pTest){
110
+ char *zRet;
111
+ char *zData;
112
+ zData = testDatasourceName(&pTest->defn);
113
+ zRet = testMallocPrintf("data4.%s.%s.%d.%d.%d",
114
+ zSystem, zData, pTest->nRec, pTest->nRepeat, pTest->bReopen
115
+ );
116
+ testFree(zData);
117
+ return zRet;
118
+ }
119
+
120
+ void test_data_4(
121
+ const char *zSystem, /* Database system name */
122
+ const char *zPattern, /* Run test cases that match this pattern */
123
+ int *pRc /* IN/OUT: Error code */
124
+ ){
125
+ Datatest4 aTest[] = {
126
+ /* defn, nRec, nRepeat, bReopen */
127
+ { {DATA_RANDOM, 20,25, 500,600}, 10000, 10, 0 },
128
+ { {DATA_RANDOM, 20,25, 500,600}, 10000, 10, 1 },
129
+ };
130
+
131
+ int i;
132
+
133
+ for(i=0; *pRc==LSM_OK && i<ArraySize(aTest); i++){
134
+ char *zName = getName4(zSystem, &aTest[i]);
135
+ if( testCaseBegin(pRc, zPattern, "%s", zName) ){
136
+ doDataTest4(zSystem, &aTest[i], pRc);
137
+ }
138
+ testFree(zName);
139
+ }
140
+ }
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_func.c ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include "lsmtest.h"
3
+
4
+
5
+ int do_work(int nArg, char **azArg){
6
+ struct Option {
7
+ const char *zName;
8
+ } aOpt [] = {
9
+ { "-nmerge" },
10
+ { "-nkb" },
11
+ { 0 }
12
+ };
13
+
14
+ lsm_db *pDb;
15
+ int rc;
16
+ int i;
17
+ const char *zDb;
18
+ int nMerge = 1;
19
+ int nKB = (1<<30);
20
+
21
+ if( nArg==0 ) goto usage;
22
+ zDb = azArg[nArg-1];
23
+ for(i=0; i<(nArg-1); i++){
24
+ int iSel;
25
+ rc = testArgSelect(aOpt, "option", azArg[i], &iSel);
26
+ if( rc ) return rc;
27
+ switch( iSel ){
28
+ case 0:
29
+ i++;
30
+ if( i==(nArg-1) ) goto usage;
31
+ nMerge = atoi(azArg[i]);
32
+ break;
33
+ case 1:
34
+ i++;
35
+ if( i==(nArg-1) ) goto usage;
36
+ nKB = atoi(azArg[i]);
37
+ break;
38
+ }
39
+ }
40
+
41
+ rc = lsm_new(0, &pDb);
42
+ if( rc!=LSM_OK ){
43
+ testPrintError("lsm_open(): rc=%d\n", rc);
44
+ }else{
45
+ rc = lsm_open(pDb, zDb);
46
+ if( rc!=LSM_OK ){
47
+ testPrintError("lsm_open(): rc=%d\n", rc);
48
+ }else{
49
+ int n = -1;
50
+ lsm_config(pDb, LSM_CONFIG_BLOCK_SIZE, &n);
51
+ n = n*2;
52
+ lsm_config(pDb, LSM_CONFIG_AUTOCHECKPOINT, &n);
53
+
54
+ rc = lsm_work(pDb, nMerge, nKB, 0);
55
+ if( rc!=LSM_OK ){
56
+ testPrintError("lsm_work(): rc=%d\n", rc);
57
+ }
58
+ }
59
+ }
60
+ if( rc==LSM_OK ){
61
+ rc = lsm_checkpoint(pDb, 0);
62
+ }
63
+
64
+ lsm_close(pDb);
65
+ return rc;
66
+
67
+ usage:
68
+ testPrintUsage("?-optimize? ?-n N? DATABASE");
69
+ return -1;
70
+ }
71
+
72
+
73
+ /*
74
+ ** lsmtest show ?-config LSM-CONFIG? DATABASE ?COMMAND ?PGNO??
75
+ */
76
+ int do_show(int nArg, char **azArg){
77
+ lsm_db *pDb;
78
+ int rc;
79
+ const char *zDb;
80
+
81
+ int eOpt = LSM_INFO_DB_STRUCTURE;
82
+ unsigned int iPg = 0;
83
+ int bConfig = 0;
84
+ const char *zConfig = "";
85
+
86
+ struct Option {
87
+ const char *zName;
88
+ int bConfig;
89
+ int eOpt;
90
+ } aOpt [] = {
91
+ { "array", 0, LSM_INFO_ARRAY_STRUCTURE },
92
+ { "array-pages", 0, LSM_INFO_ARRAY_PAGES },
93
+ { "blocksize", 1, LSM_CONFIG_BLOCK_SIZE },
94
+ { "pagesize", 1, LSM_CONFIG_PAGE_SIZE },
95
+ { "freelist", 0, LSM_INFO_FREELIST },
96
+ { "page-ascii", 0, LSM_INFO_PAGE_ASCII_DUMP },
97
+ { "page-hex", 0, LSM_INFO_PAGE_HEX_DUMP },
98
+ { 0, 0 }
99
+ };
100
+
101
+ char *z = 0;
102
+ int iDb = 0; /* Index of DATABASE in azArg[] */
103
+
104
+ /* Check if there is a "-config" option: */
105
+ if( nArg>2 && strlen(azArg[0])>1
106
+ && memcmp(azArg[0], "-config", strlen(azArg[0]))==0
107
+ ){
108
+ zConfig = azArg[1];
109
+ iDb = 2;
110
+ }
111
+ if( nArg<(iDb+1) ) goto usage;
112
+
113
+ if( nArg>(iDb+1) ){
114
+ rc = testArgSelect(aOpt, "option", azArg[iDb+1], &eOpt);
115
+ if( rc!=0 ) return rc;
116
+ bConfig = aOpt[eOpt].bConfig;
117
+ eOpt = aOpt[eOpt].eOpt;
118
+ if( (bConfig==0 && eOpt==LSM_INFO_FREELIST)
119
+ || (bConfig==1 && eOpt==LSM_CONFIG_BLOCK_SIZE)
120
+ || (bConfig==1 && eOpt==LSM_CONFIG_PAGE_SIZE)
121
+ ){
122
+ if( nArg!=(iDb+2) ) goto usage;
123
+ }else{
124
+ if( nArg!=(iDb+3) ) goto usage;
125
+ iPg = atoi(azArg[iDb+2]);
126
+ }
127
+ }
128
+ zDb = azArg[iDb];
129
+
130
+ rc = lsm_new(0, &pDb);
131
+ tdb_lsm_configure(pDb, zConfig);
132
+ if( rc!=LSM_OK ){
133
+ testPrintError("lsm_new(): rc=%d\n", rc);
134
+ }else{
135
+ rc = lsm_open(pDb, zDb);
136
+ if( rc!=LSM_OK ){
137
+ testPrintError("lsm_open(): rc=%d\n", rc);
138
+ }
139
+ }
140
+
141
+ if( rc==LSM_OK ){
142
+ if( bConfig==0 ){
143
+ switch( eOpt ){
144
+ case LSM_INFO_DB_STRUCTURE:
145
+ case LSM_INFO_FREELIST:
146
+ rc = lsm_info(pDb, eOpt, &z);
147
+ break;
148
+ case LSM_INFO_ARRAY_STRUCTURE:
149
+ case LSM_INFO_ARRAY_PAGES:
150
+ case LSM_INFO_PAGE_ASCII_DUMP:
151
+ case LSM_INFO_PAGE_HEX_DUMP:
152
+ rc = lsm_info(pDb, eOpt, iPg, &z);
153
+ break;
154
+ default:
155
+ assert( !"no chance" );
156
+ }
157
+
158
+ if( rc==LSM_OK ){
159
+ printf("%s\n", z ? z : "");
160
+ fflush(stdout);
161
+ }
162
+ lsm_free(lsm_get_env(pDb), z);
163
+ }else{
164
+ int iRes = -1;
165
+ lsm_config(pDb, eOpt, &iRes);
166
+ printf("%d\n", iRes);
167
+ fflush(stdout);
168
+ }
169
+ }
170
+
171
+ lsm_close(pDb);
172
+ return rc;
173
+
174
+ usage:
175
+ testPrintUsage("DATABASE ?array|page-ascii|page-hex PGNO?");
176
+ return -1;
177
+ }
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_mem.c ADDED
@@ -0,0 +1,409 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include <stdio.h>
3
+ #include <assert.h>
4
+ #include <string.h>
5
+
6
+ #define ArraySize(x) ((int)(sizeof(x) / sizeof((x)[0])))
7
+
8
+ #define MIN(x,y) ((x)<(y) ? (x) : (y))
9
+
10
+ typedef unsigned int u32;
11
+ typedef unsigned char u8;
12
+ typedef long long int i64;
13
+ typedef unsigned long long int u64;
14
+
15
+ #if defined(__GLIBC__) && defined(LSM_DEBUG_MEM)
16
+ extern int backtrace(void**,int);
17
+ extern void backtrace_symbols_fd(void*const*,int,int);
18
+ # define TM_BACKTRACE 12
19
+ #else
20
+ # define backtrace(A,B) 1
21
+ # define backtrace_symbols_fd(A,B,C)
22
+ #endif
23
+
24
+
25
+ typedef struct TmBlockHdr TmBlockHdr;
26
+ typedef struct TmAgg TmAgg;
27
+ typedef struct TmGlobal TmGlobal;
28
+
29
+ struct TmGlobal {
30
+ /* Linked list of all currently outstanding allocations. And a table of
31
+ ** all allocations, past and present, indexed by backtrace() info. */
32
+ TmBlockHdr *pFirst;
33
+ #ifdef TM_BACKTRACE
34
+ TmAgg *aHash[10000];
35
+ #endif
36
+
37
+ /* Underlying malloc/realloc/free functions */
38
+ void *(*xMalloc)(int); /* underlying malloc(3) function */
39
+ void *(*xRealloc)(void *, int); /* underlying realloc(3) function */
40
+ void (*xFree)(void *); /* underlying free(3) function */
41
+
42
+ /* Mutex to protect pFirst and aHash */
43
+ void (*xEnterMutex)(TmGlobal*); /* Call this to enter the mutex */
44
+ void (*xLeaveMutex)(TmGlobal*); /* Call this to leave mutex */
45
+ void (*xDelMutex)(TmGlobal*); /* Call this to delete mutex */
46
+ void *pMutex; /* Mutex handle */
47
+
48
+ void *(*xSaveMalloc)(void *, size_t);
49
+ void *(*xSaveRealloc)(void *, void *, size_t);
50
+ void (*xSaveFree)(void *, void *);
51
+
52
+ /* OOM injection scheduling. If nCountdown is greater than zero when a
53
+ ** malloc attempt is made, it is decremented. If this means nCountdown
54
+ ** transitions from 1 to 0, then the allocation fails. If bPersist is true
55
+ ** when this happens, nCountdown is then incremented back to 1 (so that the
56
+ ** next attempt fails too).
57
+ */
58
+ int nCountdown;
59
+ int bPersist;
60
+ int bEnable;
61
+ void (*xHook)(void *);
62
+ void *pHookCtx;
63
+ };
64
+
65
+ struct TmBlockHdr {
66
+ TmBlockHdr *pNext;
67
+ TmBlockHdr *pPrev;
68
+ int nByte;
69
+ #ifdef TM_BACKTRACE
70
+ TmAgg *pAgg;
71
+ #endif
72
+ u32 iForeGuard;
73
+ };
74
+
75
+ #ifdef TM_BACKTRACE
76
+ struct TmAgg {
77
+ int nAlloc; /* Number of allocations at this path */
78
+ int nByte; /* Total number of bytes allocated */
79
+ int nOutAlloc; /* Number of outstanding allocations */
80
+ int nOutByte; /* Number of outstanding bytes */
81
+ void *aFrame[TM_BACKTRACE]; /* backtrace() output */
82
+ TmAgg *pNext; /* Next object in hash-table collision */
83
+ };
84
+ #endif
85
+
86
+ #define FOREGUARD 0x80F5E153
87
+ #define REARGUARD 0xE4676B53
88
+ static const u32 rearguard = REARGUARD;
89
+
90
+ #define ROUND8(x) (((x)+7)&~7)
91
+
92
+ #define BLOCK_HDR_SIZE (ROUND8( sizeof(TmBlockHdr) ))
93
+
94
+ static void lsmtest_oom_error(void){
95
+ static int nErr = 0;
96
+ nErr++;
97
+ }
98
+
99
+ static void tmEnterMutex(TmGlobal *pTm){
100
+ pTm->xEnterMutex(pTm);
101
+ }
102
+ static void tmLeaveMutex(TmGlobal *pTm){
103
+ pTm->xLeaveMutex(pTm);
104
+ }
105
+
106
+ static void *tmMalloc(TmGlobal *pTm, int nByte){
107
+ TmBlockHdr *pNew; /* New allocation header block */
108
+ u8 *pUser; /* Return value */
109
+ int nReq; /* Total number of bytes requested */
110
+
111
+ assert( sizeof(rearguard)==4 );
112
+ nReq = BLOCK_HDR_SIZE + nByte + 4;
113
+ pNew = (TmBlockHdr *)pTm->xMalloc(nReq);
114
+ memset(pNew, 0, sizeof(TmBlockHdr));
115
+
116
+ tmEnterMutex(pTm);
117
+ assert( pTm->nCountdown>=0 );
118
+ assert( pTm->bPersist==0 || pTm->bPersist==1 );
119
+
120
+ if( pTm->bEnable && pTm->nCountdown==1 ){
121
+ /* Simulate an OOM error. */
122
+ lsmtest_oom_error();
123
+ pTm->xFree(pNew);
124
+ pTm->nCountdown = pTm->bPersist;
125
+ if( pTm->xHook ) pTm->xHook(pTm->pHookCtx);
126
+ pUser = 0;
127
+ }else{
128
+ if( pTm->bEnable && pTm->nCountdown ) pTm->nCountdown--;
129
+
130
+ pNew->iForeGuard = FOREGUARD;
131
+ pNew->nByte = nByte;
132
+ pNew->pNext = pTm->pFirst;
133
+
134
+ if( pTm->pFirst ){
135
+ pTm->pFirst->pPrev = pNew;
136
+ }
137
+ pTm->pFirst = pNew;
138
+
139
+ pUser = &((u8 *)pNew)[BLOCK_HDR_SIZE];
140
+ memset(pUser, 0x56, nByte);
141
+ memcpy(&pUser[nByte], &rearguard, 4);
142
+
143
+ #ifdef TM_BACKTRACE
144
+ {
145
+ TmAgg *pAgg;
146
+ int i;
147
+ u32 iHash = 0;
148
+ void *aFrame[TM_BACKTRACE];
149
+ memset(aFrame, 0, sizeof(aFrame));
150
+ backtrace(aFrame, TM_BACKTRACE);
151
+
152
+ for(i=0; i<ArraySize(aFrame); i++){
153
+ iHash += (u64)(aFrame[i]) + (iHash<<3);
154
+ }
155
+ iHash = iHash % ArraySize(pTm->aHash);
156
+
157
+ for(pAgg=pTm->aHash[iHash]; pAgg; pAgg=pAgg->pNext){
158
+ if( memcmp(pAgg->aFrame, aFrame, sizeof(aFrame))==0 ) break;
159
+ }
160
+ if( !pAgg ){
161
+ pAgg = (TmAgg *)pTm->xMalloc(sizeof(TmAgg));
162
+ memset(pAgg, 0, sizeof(TmAgg));
163
+ memcpy(pAgg->aFrame, aFrame, sizeof(aFrame));
164
+ pAgg->pNext = pTm->aHash[iHash];
165
+ pTm->aHash[iHash] = pAgg;
166
+ }
167
+ pAgg->nAlloc++;
168
+ pAgg->nByte += nByte;
169
+ pAgg->nOutAlloc++;
170
+ pAgg->nOutByte += nByte;
171
+ pNew->pAgg = pAgg;
172
+ }
173
+ #endif
174
+ }
175
+
176
+ tmLeaveMutex(pTm);
177
+ return pUser;
178
+ }
179
+
180
+ static void tmFree(TmGlobal *pTm, void *p){
181
+ if( p ){
182
+ TmBlockHdr *pHdr;
183
+ u8 *pUser = (u8 *)p;
184
+
185
+ tmEnterMutex(pTm);
186
+ pHdr = (TmBlockHdr *)(pUser - BLOCK_HDR_SIZE);
187
+ assert( pHdr->iForeGuard==FOREGUARD );
188
+ assert( 0==memcmp(&pUser[pHdr->nByte], &rearguard, 4) );
189
+
190
+ if( pHdr->pPrev ){
191
+ assert( pHdr->pPrev->pNext==pHdr );
192
+ pHdr->pPrev->pNext = pHdr->pNext;
193
+ }else{
194
+ assert( pHdr==pTm->pFirst );
195
+ pTm->pFirst = pHdr->pNext;
196
+ }
197
+ if( pHdr->pNext ){
198
+ assert( pHdr->pNext->pPrev==pHdr );
199
+ pHdr->pNext->pPrev = pHdr->pPrev;
200
+ }
201
+
202
+ #ifdef TM_BACKTRACE
203
+ pHdr->pAgg->nOutAlloc--;
204
+ pHdr->pAgg->nOutByte -= pHdr->nByte;
205
+ #endif
206
+
207
+ tmLeaveMutex(pTm);
208
+ memset(pUser, 0x58, pHdr->nByte);
209
+ memset(pHdr, 0x57, sizeof(TmBlockHdr));
210
+ pTm->xFree(pHdr);
211
+ }
212
+ }
213
+
214
+ static void *tmRealloc(TmGlobal *pTm, void *p, int nByte){
215
+ void *pNew;
216
+
217
+ pNew = tmMalloc(pTm, nByte);
218
+ if( pNew && p ){
219
+ TmBlockHdr *pHdr;
220
+ u8 *pUser = (u8 *)p;
221
+ pHdr = (TmBlockHdr *)(pUser - BLOCK_HDR_SIZE);
222
+ memcpy(pNew, p, MIN(nByte, pHdr->nByte));
223
+ tmFree(pTm, p);
224
+ }
225
+ return pNew;
226
+ }
227
+
228
+ static void tmMallocOom(
229
+ TmGlobal *pTm,
230
+ int nCountdown,
231
+ int bPersist,
232
+ void (*xHook)(void *),
233
+ void *pHookCtx
234
+ ){
235
+ assert( nCountdown>=0 );
236
+ assert( bPersist==0 || bPersist==1 );
237
+ pTm->nCountdown = nCountdown;
238
+ pTm->bPersist = bPersist;
239
+ pTm->xHook = xHook;
240
+ pTm->pHookCtx = pHookCtx;
241
+ pTm->bEnable = 1;
242
+ }
243
+
244
+ static void tmMallocOomEnable(
245
+ TmGlobal *pTm,
246
+ int bEnable
247
+ ){
248
+ pTm->bEnable = bEnable;
249
+ }
250
+
251
+ static void tmMallocCheck(
252
+ TmGlobal *pTm,
253
+ int *pnLeakAlloc,
254
+ int *pnLeakByte,
255
+ FILE *pFile
256
+ ){
257
+ TmBlockHdr *pHdr;
258
+ int nLeak = 0;
259
+ int nByte = 0;
260
+
261
+ if( pTm==0 ) return;
262
+
263
+ for(pHdr=pTm->pFirst; pHdr; pHdr=pHdr->pNext){
264
+ nLeak++;
265
+ nByte += pHdr->nByte;
266
+ }
267
+ if( pnLeakAlloc ) *pnLeakAlloc = nLeak;
268
+ if( pnLeakByte ) *pnLeakByte = nByte;
269
+
270
+ #ifdef TM_BACKTRACE
271
+ if( pFile ){
272
+ int i;
273
+ fprintf(pFile, "LEAKS\n");
274
+ for(i=0; i<ArraySize(pTm->aHash); i++){
275
+ TmAgg *pAgg;
276
+ for(pAgg=pTm->aHash[i]; pAgg; pAgg=pAgg->pNext){
277
+ if( pAgg->nOutAlloc ){
278
+ int j;
279
+ fprintf(pFile, "%d %d ", pAgg->nOutByte, pAgg->nOutAlloc);
280
+ for(j=0; j<TM_BACKTRACE; j++){
281
+ fprintf(pFile, "%p ", pAgg->aFrame[j]);
282
+ }
283
+ fprintf(pFile, "\n");
284
+ }
285
+ }
286
+ }
287
+ fprintf(pFile, "\nALLOCATIONS\n");
288
+ for(i=0; i<ArraySize(pTm->aHash); i++){
289
+ TmAgg *pAgg;
290
+ for(pAgg=pTm->aHash[i]; pAgg; pAgg=pAgg->pNext){
291
+ int j;
292
+ fprintf(pFile, "%d %d ", pAgg->nByte, pAgg->nAlloc);
293
+ for(j=0; j<TM_BACKTRACE; j++) fprintf(pFile, "%p ", pAgg->aFrame[j]);
294
+ fprintf(pFile, "\n");
295
+ }
296
+ }
297
+ }
298
+ #else
299
+ (void)pFile;
300
+ #endif
301
+ }
302
+
303
+
304
+ #include "lsm.h"
305
+ #include "stdlib.h"
306
+
307
+ typedef struct LsmMutex LsmMutex;
308
+ struct LsmMutex {
309
+ lsm_env *pEnv;
310
+ lsm_mutex *pMutex;
311
+ };
312
+
313
+ static void tmLsmMutexEnter(TmGlobal *pTm){
314
+ LsmMutex *p = (LsmMutex *)pTm->pMutex;
315
+ p->pEnv->xMutexEnter(p->pMutex);
316
+ }
317
+ static void tmLsmMutexLeave(TmGlobal *pTm){
318
+ LsmMutex *p = (LsmMutex *)(pTm->pMutex);
319
+ p->pEnv->xMutexLeave(p->pMutex);
320
+ }
321
+ static void tmLsmMutexDel(TmGlobal *pTm){
322
+ LsmMutex *p = (LsmMutex *)pTm->pMutex;
323
+ pTm->xFree(p);
324
+ }
325
+ static void *tmLsmMalloc(int n){ return malloc(n); }
326
+ static void tmLsmFree(void *ptr){ free(ptr); }
327
+ static void *tmLsmRealloc(void *ptr, int n){ return realloc(ptr, n); }
328
+
329
+ static void *tmLsmEnvMalloc(lsm_env *p, size_t n){
330
+ return tmMalloc((TmGlobal *)(p->pMemCtx), n);
331
+ }
332
+ static void tmLsmEnvFree(lsm_env *p, void *ptr){
333
+ tmFree((TmGlobal *)(p->pMemCtx), ptr);
334
+ }
335
+ static void *tmLsmEnvRealloc(lsm_env *p, void *ptr, size_t n){
336
+ return tmRealloc((TmGlobal *)(p->pMemCtx), ptr, n);
337
+ }
338
+
339
+ void testMallocInstall(lsm_env *pEnv){
340
+ TmGlobal *pGlobal;
341
+ LsmMutex *pMutex;
342
+ assert( pEnv->pMemCtx==0 );
343
+
344
+ /* Allocate and populate a TmGlobal structure. */
345
+ pGlobal = (TmGlobal *)tmLsmMalloc(sizeof(TmGlobal));
346
+ memset(pGlobal, 0, sizeof(TmGlobal));
347
+ pGlobal->xMalloc = tmLsmMalloc;
348
+ pGlobal->xRealloc = tmLsmRealloc;
349
+ pGlobal->xFree = tmLsmFree;
350
+ pMutex = (LsmMutex *)pGlobal->xMalloc(sizeof(LsmMutex));
351
+ pMutex->pEnv = pEnv;
352
+ pEnv->xMutexStatic(pEnv, LSM_MUTEX_HEAP, &pMutex->pMutex);
353
+ pGlobal->xEnterMutex = tmLsmMutexEnter;
354
+ pGlobal->xLeaveMutex = tmLsmMutexLeave;
355
+ pGlobal->xDelMutex = tmLsmMutexDel;
356
+ pGlobal->pMutex = (void *)pMutex;
357
+
358
+ pGlobal->xSaveMalloc = pEnv->xMalloc;
359
+ pGlobal->xSaveRealloc = pEnv->xRealloc;
360
+ pGlobal->xSaveFree = pEnv->xFree;
361
+
362
+ /* Set up pEnv to the use the new TmGlobal */
363
+ pEnv->pMemCtx = (void *)pGlobal;
364
+ pEnv->xMalloc = tmLsmEnvMalloc;
365
+ pEnv->xRealloc = tmLsmEnvRealloc;
366
+ pEnv->xFree = tmLsmEnvFree;
367
+ }
368
+
369
+ void testMallocUninstall(lsm_env *pEnv){
370
+ TmGlobal *p = (TmGlobal *)pEnv->pMemCtx;
371
+ pEnv->pMemCtx = 0;
372
+ if( p ){
373
+ pEnv->xMalloc = p->xSaveMalloc;
374
+ pEnv->xRealloc = p->xSaveRealloc;
375
+ pEnv->xFree = p->xSaveFree;
376
+ p->xDelMutex(p);
377
+ tmLsmFree(p);
378
+ }
379
+ }
380
+
381
+ void testMallocCheck(
382
+ lsm_env *pEnv,
383
+ int *pnLeakAlloc,
384
+ int *pnLeakByte,
385
+ FILE *pFile
386
+ ){
387
+ if( pEnv->pMemCtx==0 ){
388
+ *pnLeakAlloc = 0;
389
+ *pnLeakByte = 0;
390
+ }else{
391
+ tmMallocCheck((TmGlobal *)(pEnv->pMemCtx), pnLeakAlloc, pnLeakByte, pFile);
392
+ }
393
+ }
394
+
395
+ void testMallocOom(
396
+ lsm_env *pEnv,
397
+ int nCountdown,
398
+ int bPersist,
399
+ void (*xHook)(void *),
400
+ void *pHookCtx
401
+ ){
402
+ TmGlobal *pTm = (TmGlobal *)(pEnv->pMemCtx);
403
+ tmMallocOom(pTm, nCountdown, bPersist, xHook, pHookCtx);
404
+ }
405
+
406
+ void testMallocOomEnable(lsm_env *pEnv, int bEnable){
407
+ TmGlobal *pTm = (TmGlobal *)(pEnv->pMemCtx);
408
+ tmMallocOomEnable(pTm, bEnable);
409
+ }
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb.c ADDED
@@ -0,0 +1,846 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /*
3
+ ** This program attempts to test the correctness of some facets of the
4
+ ** LSM database library. Specifically, that the contents of the database
5
+ ** are maintained correctly during a series of inserts and deletes.
6
+ */
7
+
8
+
9
+ #include "lsmtest_tdb.h"
10
+ #include "lsm.h"
11
+
12
+ #include "lsmtest.h"
13
+
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+ #include <assert.h>
17
+ #ifndef _WIN32
18
+ # include <unistd.h>
19
+ #endif
20
+ #include <stdio.h>
21
+
22
+
23
+ typedef struct SqlDb SqlDb;
24
+
25
+ static int error_transaction_function(TestDb *p, int iLevel){
26
+ unused_parameter(p);
27
+ unused_parameter(iLevel);
28
+ return -1;
29
+ }
30
+
31
+
32
+ /*************************************************************************
33
+ ** Begin wrapper for LevelDB.
34
+ */
35
+ #ifdef HAVE_LEVELDB
36
+
37
+ #include <leveldb/c.h>
38
+
39
+ typedef struct LevelDb LevelDb;
40
+ struct LevelDb {
41
+ TestDb base;
42
+ leveldb_t *db;
43
+ leveldb_options_t *pOpt;
44
+ leveldb_writeoptions_t *pWriteOpt;
45
+ leveldb_readoptions_t *pReadOpt;
46
+
47
+ char *pVal;
48
+ };
49
+
50
+ static int test_leveldb_close(TestDb *pTestDb){
51
+ LevelDb *pDb = (LevelDb *)pTestDb;
52
+
53
+ leveldb_close(pDb->db);
54
+ leveldb_writeoptions_destroy(pDb->pWriteOpt);
55
+ leveldb_readoptions_destroy(pDb->pReadOpt);
56
+ leveldb_options_destroy(pDb->pOpt);
57
+ free(pDb->pVal);
58
+ free(pDb);
59
+
60
+ return 0;
61
+ }
62
+
63
+ static int test_leveldb_write(
64
+ TestDb *pTestDb,
65
+ void *pKey,
66
+ int nKey,
67
+ void *pVal,
68
+ int nVal
69
+ ){
70
+ LevelDb *pDb = (LevelDb *)pTestDb;
71
+ char *zErr = 0;
72
+ leveldb_put(pDb->db, pDb->pWriteOpt, pKey, nKey, pVal, nVal, &zErr);
73
+ return (zErr!=0);
74
+ }
75
+
76
+ static int test_leveldb_delete(TestDb *pTestDb, void *pKey, int nKey){
77
+ LevelDb *pDb = (LevelDb *)pTestDb;
78
+ char *zErr = 0;
79
+ leveldb_delete(pDb->db, pDb->pWriteOpt, pKey, nKey, &zErr);
80
+ return (zErr!=0);
81
+ }
82
+
83
+ static int test_leveldb_fetch(
84
+ TestDb *pTestDb,
85
+ void *pKey,
86
+ int nKey,
87
+ void **ppVal,
88
+ int *pnVal
89
+ ){
90
+ LevelDb *pDb = (LevelDb *)pTestDb;
91
+ char *zErr = 0;
92
+ size_t nVal = 0;
93
+
94
+ if( pKey==0 ) return 0;
95
+ free(pDb->pVal);
96
+ pDb->pVal = leveldb_get(pDb->db, pDb->pReadOpt, pKey, nKey, &nVal, &zErr);
97
+ *ppVal = (void *)(pDb->pVal);
98
+ if( pDb->pVal==0 ){
99
+ *pnVal = -1;
100
+ }else{
101
+ *pnVal = (int)nVal;
102
+ }
103
+
104
+ return (zErr!=0);
105
+ }
106
+
107
+ static int test_leveldb_scan(
108
+ TestDb *pTestDb,
109
+ void *pCtx,
110
+ int bReverse,
111
+ void *pKey1, int nKey1, /* Start of search */
112
+ void *pKey2, int nKey2, /* End of search */
113
+ void (*xCallback)(void *, void *, int , void *, int)
114
+ ){
115
+ LevelDb *pDb = (LevelDb *)pTestDb;
116
+ leveldb_iterator_t *iter;
117
+
118
+ iter = leveldb_create_iterator(pDb->db, pDb->pReadOpt);
119
+
120
+ if( bReverse==0 ){
121
+ if( pKey1 ){
122
+ leveldb_iter_seek(iter, pKey1, nKey1);
123
+ }else{
124
+ leveldb_iter_seek_to_first(iter);
125
+ }
126
+ }else{
127
+ if( pKey2 ){
128
+ leveldb_iter_seek(iter, pKey2, nKey2);
129
+
130
+ if( leveldb_iter_valid(iter)==0 ){
131
+ leveldb_iter_seek_to_last(iter);
132
+ }else{
133
+ const char *k; size_t n;
134
+ int res;
135
+ k = leveldb_iter_key(iter, &n);
136
+ res = memcmp(k, pKey2, MIN(n, nKey2));
137
+ if( res==0 ) res = n - nKey2;
138
+ assert( res>=0 );
139
+ if( res>0 ){
140
+ leveldb_iter_prev(iter);
141
+ }
142
+ }
143
+ }else{
144
+ leveldb_iter_seek_to_last(iter);
145
+ }
146
+ }
147
+
148
+
149
+ while( leveldb_iter_valid(iter) ){
150
+ const char *k; size_t n;
151
+ const char *v; size_t n2;
152
+ int res;
153
+
154
+ k = leveldb_iter_key(iter, &n);
155
+ if( bReverse==0 && pKey2 ){
156
+ res = memcmp(k, pKey2, MIN(n, nKey2));
157
+ if( res==0 ) res = n - nKey2;
158
+ if( res>0 ) break;
159
+ }
160
+ if( bReverse!=0 && pKey1 ){
161
+ res = memcmp(k, pKey1, MIN(n, nKey1));
162
+ if( res==0 ) res = n - nKey1;
163
+ if( res<0 ) break;
164
+ }
165
+
166
+ v = leveldb_iter_value(iter, &n2);
167
+
168
+ xCallback(pCtx, (void *)k, n, (void *)v, n2);
169
+
170
+ if( bReverse==0 ){
171
+ leveldb_iter_next(iter);
172
+ }else{
173
+ leveldb_iter_prev(iter);
174
+ }
175
+ }
176
+
177
+ leveldb_iter_destroy(iter);
178
+ return 0;
179
+ }
180
+
181
+ static int test_leveldb_open(
182
+ const char *zSpec,
183
+ const char *zFilename,
184
+ int bClear,
185
+ TestDb **ppDb
186
+ ){
187
+ static const DatabaseMethods LeveldbMethods = {
188
+ test_leveldb_close,
189
+ test_leveldb_write,
190
+ test_leveldb_delete,
191
+ 0,
192
+ test_leveldb_fetch,
193
+ test_leveldb_scan,
194
+ error_transaction_function,
195
+ error_transaction_function,
196
+ error_transaction_function
197
+ };
198
+
199
+ LevelDb *pLevelDb;
200
+ char *zErr = 0;
201
+
202
+ if( bClear ){
203
+ char *zCmd = sqlite3_mprintf("rm -rf %s\n", zFilename);
204
+ system(zCmd);
205
+ sqlite3_free(zCmd);
206
+ }
207
+
208
+ pLevelDb = (LevelDb *)malloc(sizeof(LevelDb));
209
+ memset(pLevelDb, 0, sizeof(LevelDb));
210
+
211
+ pLevelDb->pOpt = leveldb_options_create();
212
+ leveldb_options_set_create_if_missing(pLevelDb->pOpt, 1);
213
+ pLevelDb->pWriteOpt = leveldb_writeoptions_create();
214
+ pLevelDb->pReadOpt = leveldb_readoptions_create();
215
+
216
+ pLevelDb->db = leveldb_open(pLevelDb->pOpt, zFilename, &zErr);
217
+
218
+ if( zErr ){
219
+ test_leveldb_close((TestDb *)pLevelDb);
220
+ *ppDb = 0;
221
+ return 1;
222
+ }
223
+
224
+ *ppDb = (TestDb *)pLevelDb;
225
+ pLevelDb->base.pMethods = &LeveldbMethods;
226
+ return 0;
227
+ }
228
+ #endif /* HAVE_LEVELDB */
229
+ /*
230
+ ** End wrapper for LevelDB.
231
+ *************************************************************************/
232
+
233
+ #ifdef HAVE_KYOTOCABINET
234
+ static int kc_close(TestDb *pTestDb){
235
+ return test_kc_close(pTestDb);
236
+ }
237
+
238
+ static int kc_write(
239
+ TestDb *pTestDb,
240
+ void *pKey,
241
+ int nKey,
242
+ void *pVal,
243
+ int nVal
244
+ ){
245
+ return test_kc_write(pTestDb, pKey, nKey, pVal, nVal);
246
+ }
247
+
248
+ static int kc_delete(TestDb *pTestDb, void *pKey, int nKey){
249
+ return test_kc_delete(pTestDb, pKey, nKey);
250
+ }
251
+
252
+ static int kc_delete_range(
253
+ TestDb *pTestDb,
254
+ void *pKey1, int nKey1,
255
+ void *pKey2, int nKey2
256
+ ){
257
+ return test_kc_delete_range(pTestDb, pKey1, nKey1, pKey2, nKey2);
258
+ }
259
+
260
+ static int kc_fetch(
261
+ TestDb *pTestDb,
262
+ void *pKey,
263
+ int nKey,
264
+ void **ppVal,
265
+ int *pnVal
266
+ ){
267
+ if( pKey==0 ) return LSM_OK;
268
+ return test_kc_fetch(pTestDb, pKey, nKey, ppVal, pnVal);
269
+ }
270
+
271
+ static int kc_scan(
272
+ TestDb *pTestDb,
273
+ void *pCtx,
274
+ int bReverse,
275
+ void *pFirst, int nFirst,
276
+ void *pLast, int nLast,
277
+ void (*xCallback)(void *, void *, int , void *, int)
278
+ ){
279
+ return test_kc_scan(
280
+ pTestDb, pCtx, bReverse, pFirst, nFirst, pLast, nLast, xCallback
281
+ );
282
+ }
283
+
284
+ static int kc_open(
285
+ const char *zSpec,
286
+ const char *zFilename,
287
+ int bClear,
288
+ TestDb **ppDb
289
+ ){
290
+ static const DatabaseMethods KcdbMethods = {
291
+ kc_close,
292
+ kc_write,
293
+ kc_delete,
294
+ kc_delete_range,
295
+ kc_fetch,
296
+ kc_scan,
297
+ error_transaction_function,
298
+ error_transaction_function,
299
+ error_transaction_function
300
+ };
301
+
302
+ int rc;
303
+ TestDb *pTestDb = 0;
304
+
305
+ rc = test_kc_open(zFilename, bClear, &pTestDb);
306
+ if( rc!=0 ){
307
+ *ppDb = 0;
308
+ return rc;
309
+ }
310
+ pTestDb->pMethods = &KcdbMethods;
311
+ *ppDb = pTestDb;
312
+ return 0;
313
+ }
314
+ #endif /* HAVE_KYOTOCABINET */
315
+ /*
316
+ ** End wrapper for Kyoto cabinet.
317
+ *************************************************************************/
318
+
319
+ #ifdef HAVE_MDB
320
+ static int mdb_close(TestDb *pTestDb){
321
+ return test_mdb_close(pTestDb);
322
+ }
323
+
324
+ static int mdb_write(
325
+ TestDb *pTestDb,
326
+ void *pKey,
327
+ int nKey,
328
+ void *pVal,
329
+ int nVal
330
+ ){
331
+ return test_mdb_write(pTestDb, pKey, nKey, pVal, nVal);
332
+ }
333
+
334
+ static int mdb_delete(TestDb *pTestDb, void *pKey, int nKey){
335
+ return test_mdb_delete(pTestDb, pKey, nKey);
336
+ }
337
+
338
+ static int mdb_fetch(
339
+ TestDb *pTestDb,
340
+ void *pKey,
341
+ int nKey,
342
+ void **ppVal,
343
+ int *pnVal
344
+ ){
345
+ if( pKey==0 ) return LSM_OK;
346
+ return test_mdb_fetch(pTestDb, pKey, nKey, ppVal, pnVal);
347
+ }
348
+
349
+ static int mdb_scan(
350
+ TestDb *pTestDb,
351
+ void *pCtx,
352
+ int bReverse,
353
+ void *pFirst, int nFirst,
354
+ void *pLast, int nLast,
355
+ void (*xCallback)(void *, void *, int , void *, int)
356
+ ){
357
+ return test_mdb_scan(
358
+ pTestDb, pCtx, bReverse, pFirst, nFirst, pLast, nLast, xCallback
359
+ );
360
+ }
361
+
362
+ static int mdb_open(
363
+ const char *zSpec,
364
+ const char *zFilename,
365
+ int bClear,
366
+ TestDb **ppDb
367
+ ){
368
+ static const DatabaseMethods KcdbMethods = {
369
+ mdb_close,
370
+ mdb_write,
371
+ mdb_delete,
372
+ 0,
373
+ mdb_fetch,
374
+ mdb_scan,
375
+ error_transaction_function,
376
+ error_transaction_function,
377
+ error_transaction_function
378
+ };
379
+
380
+ int rc;
381
+ TestDb *pTestDb = 0;
382
+
383
+ rc = test_mdb_open(zSpec, zFilename, bClear, &pTestDb);
384
+ if( rc!=0 ){
385
+ *ppDb = 0;
386
+ return rc;
387
+ }
388
+ pTestDb->pMethods = &KcdbMethods;
389
+ *ppDb = pTestDb;
390
+ return 0;
391
+ }
392
+ #endif /* HAVE_MDB */
393
+
394
+ /*************************************************************************
395
+ ** Begin wrapper for SQLite.
396
+ */
397
+
398
+ /*
399
+ ** nOpenTrans:
400
+ ** The number of open nested transactions, in the same sense as used
401
+ ** by the tdb_begin/commit/rollback and SQLite 4 KV interfaces. If this
402
+ ** value is 0, there are no transactions open at all. If it is 1, then
403
+ ** there is a read transaction. If it is 2 or greater, then there are
404
+ ** (nOpenTrans-1) nested write transactions open.
405
+ */
406
+ struct SqlDb {
407
+ TestDb base;
408
+ sqlite3 *db;
409
+ sqlite3_stmt *pInsert;
410
+ sqlite3_stmt *pDelete;
411
+ sqlite3_stmt *pDeleteRange;
412
+ sqlite3_stmt *pFetch;
413
+ sqlite3_stmt *apScan[8];
414
+
415
+ int nOpenTrans;
416
+
417
+ /* Used by sql_fetch() to allocate space for results */
418
+ int nAlloc;
419
+ u8 *aAlloc;
420
+ };
421
+
422
+ static int sql_close(TestDb *pTestDb){
423
+ SqlDb *pDb = (SqlDb *)pTestDb;
424
+ sqlite3_finalize(pDb->pInsert);
425
+ sqlite3_finalize(pDb->pDelete);
426
+ sqlite3_finalize(pDb->pDeleteRange);
427
+ sqlite3_finalize(pDb->pFetch);
428
+ sqlite3_finalize(pDb->apScan[0]);
429
+ sqlite3_finalize(pDb->apScan[1]);
430
+ sqlite3_finalize(pDb->apScan[2]);
431
+ sqlite3_finalize(pDb->apScan[3]);
432
+ sqlite3_finalize(pDb->apScan[4]);
433
+ sqlite3_finalize(pDb->apScan[5]);
434
+ sqlite3_finalize(pDb->apScan[6]);
435
+ sqlite3_finalize(pDb->apScan[7]);
436
+ sqlite3_close(pDb->db);
437
+ free((char *)pDb->aAlloc);
438
+ free((char *)pDb);
439
+ return SQLITE_OK;
440
+ }
441
+
442
+ static int sql_write(
443
+ TestDb *pTestDb,
444
+ void *pKey,
445
+ int nKey,
446
+ void *pVal,
447
+ int nVal
448
+ ){
449
+ SqlDb *pDb = (SqlDb *)pTestDb;
450
+ sqlite3_bind_blob(pDb->pInsert, 1, pKey, nKey, SQLITE_STATIC);
451
+ sqlite3_bind_blob(pDb->pInsert, 2, pVal, nVal, SQLITE_STATIC);
452
+ sqlite3_step(pDb->pInsert);
453
+ return sqlite3_reset(pDb->pInsert);
454
+ }
455
+
456
+ static int sql_delete(TestDb *pTestDb, void *pKey, int nKey){
457
+ SqlDb *pDb = (SqlDb *)pTestDb;
458
+ sqlite3_bind_blob(pDb->pDelete, 1, pKey, nKey, SQLITE_STATIC);
459
+ sqlite3_step(pDb->pDelete);
460
+ return sqlite3_reset(pDb->pDelete);
461
+ }
462
+
463
+ static int sql_delete_range(
464
+ TestDb *pTestDb,
465
+ void *pKey1, int nKey1,
466
+ void *pKey2, int nKey2
467
+ ){
468
+ SqlDb *pDb = (SqlDb *)pTestDb;
469
+ sqlite3_bind_blob(pDb->pDeleteRange, 1, pKey1, nKey1, SQLITE_STATIC);
470
+ sqlite3_bind_blob(pDb->pDeleteRange, 2, pKey2, nKey2, SQLITE_STATIC);
471
+ sqlite3_step(pDb->pDeleteRange);
472
+ return sqlite3_reset(pDb->pDeleteRange);
473
+ }
474
+
475
+ static int sql_fetch(
476
+ TestDb *pTestDb,
477
+ void *pKey,
478
+ int nKey,
479
+ void **ppVal,
480
+ int *pnVal
481
+ ){
482
+ SqlDb *pDb = (SqlDb *)pTestDb;
483
+ int rc;
484
+
485
+ sqlite3_reset(pDb->pFetch);
486
+ if( pKey==0 ){
487
+ assert( ppVal==0 );
488
+ assert( pnVal==0 );
489
+ return LSM_OK;
490
+ }
491
+
492
+ sqlite3_bind_blob(pDb->pFetch, 1, pKey, nKey, SQLITE_STATIC);
493
+ rc = sqlite3_step(pDb->pFetch);
494
+ if( rc==SQLITE_ROW ){
495
+ int nVal = sqlite3_column_bytes(pDb->pFetch, 0);
496
+ u8 *aVal = (void *)sqlite3_column_blob(pDb->pFetch, 0);
497
+
498
+ if( nVal>pDb->nAlloc ){
499
+ free(pDb->aAlloc);
500
+ pDb->aAlloc = (u8 *)malloc(nVal*2);
501
+ pDb->nAlloc = nVal*2;
502
+ }
503
+ memcpy(pDb->aAlloc, aVal, nVal);
504
+ *pnVal = nVal;
505
+ *ppVal = (void *)pDb->aAlloc;
506
+ }else{
507
+ *pnVal = -1;
508
+ *ppVal = 0;
509
+ }
510
+
511
+ rc = sqlite3_reset(pDb->pFetch);
512
+ return rc;
513
+ }
514
+
515
+ static int sql_scan(
516
+ TestDb *pTestDb,
517
+ void *pCtx,
518
+ int bReverse,
519
+ void *pFirst, int nFirst,
520
+ void *pLast, int nLast,
521
+ void (*xCallback)(void *, void *, int , void *, int)
522
+ ){
523
+ SqlDb *pDb = (SqlDb *)pTestDb;
524
+ sqlite3_stmt *pScan;
525
+
526
+ assert( bReverse==1 || bReverse==0 );
527
+ pScan = pDb->apScan[(pFirst==0) + (pLast==0)*2 + bReverse*4];
528
+
529
+ if( pFirst ) sqlite3_bind_blob(pScan, 1, pFirst, nFirst, SQLITE_STATIC);
530
+ if( pLast ) sqlite3_bind_blob(pScan, 2, pLast, nLast, SQLITE_STATIC);
531
+
532
+ while( SQLITE_ROW==sqlite3_step(pScan) ){
533
+ void *pKey; int nKey;
534
+ void *pVal; int nVal;
535
+
536
+ nKey = sqlite3_column_bytes(pScan, 0);
537
+ pKey = (void *)sqlite3_column_blob(pScan, 0);
538
+ nVal = sqlite3_column_bytes(pScan, 1);
539
+ pVal = (void *)sqlite3_column_blob(pScan, 1);
540
+
541
+ xCallback(pCtx, pKey, nKey, pVal, nVal);
542
+ }
543
+ return sqlite3_reset(pScan);
544
+ }
545
+
546
+ static int sql_begin(TestDb *pTestDb, int iLevel){
547
+ int i;
548
+ SqlDb *pDb = (SqlDb *)pTestDb;
549
+
550
+ /* iLevel==0 is a no-op */
551
+ if( iLevel==0 ) return 0;
552
+
553
+ /* If there are no transactions at all open, open a read transaction. */
554
+ if( pDb->nOpenTrans==0 ){
555
+ int rc = sqlite3_exec(pDb->db,
556
+ "BEGIN; SELECT * FROM sqlite_schema LIMIT 1;" , 0, 0, 0
557
+ );
558
+ if( rc!=0 ) return rc;
559
+ pDb->nOpenTrans = 1;
560
+ }
561
+
562
+ /* Open any required write transactions */
563
+ for(i=pDb->nOpenTrans; i<iLevel; i++){
564
+ char *zSql = sqlite3_mprintf("SAVEPOINT x%d", i);
565
+ int rc = sqlite3_exec(pDb->db, zSql, 0, 0, 0);
566
+ sqlite3_free(zSql);
567
+ if( rc!=SQLITE_OK ) return rc;
568
+ }
569
+
570
+ pDb->nOpenTrans = iLevel;
571
+ return 0;
572
+ }
573
+
574
+ static int sql_commit(TestDb *pTestDb, int iLevel){
575
+ SqlDb *pDb = (SqlDb *)pTestDb;
576
+ assert( iLevel>=0 );
577
+
578
+ /* Close the read transaction if requested. */
579
+ if( pDb->nOpenTrans>=1 && iLevel==0 ){
580
+ int rc = sqlite3_exec(pDb->db, "COMMIT", 0, 0, 0);
581
+ if( rc!=0 ) return rc;
582
+ pDb->nOpenTrans = 0;
583
+ }
584
+
585
+ /* Close write transactions as required */
586
+ if( pDb->nOpenTrans>iLevel ){
587
+ char *zSql = sqlite3_mprintf("RELEASE x%d", iLevel);
588
+ int rc = sqlite3_exec(pDb->db, zSql, 0, 0, 0);
589
+ sqlite3_free(zSql);
590
+ if( rc!=0 ) return rc;
591
+ }
592
+
593
+ pDb->nOpenTrans = iLevel;
594
+ return 0;
595
+ }
596
+
597
+ static int sql_rollback(TestDb *pTestDb, int iLevel){
598
+ SqlDb *pDb = (SqlDb *)pTestDb;
599
+ assert( iLevel>=0 );
600
+
601
+ if( pDb->nOpenTrans>=1 && iLevel==0 ){
602
+ /* Close the read transaction if requested. */
603
+ int rc = sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
604
+ if( rc!=0 ) return rc;
605
+ }else if( pDb->nOpenTrans>1 && iLevel==1 ){
606
+ /* Or, rollback and close the top-level write transaction */
607
+ int rc = sqlite3_exec(pDb->db, "ROLLBACK TO x1; RELEASE x1;", 0, 0, 0);
608
+ if( rc!=0 ) return rc;
609
+ }else{
610
+ /* Or, just roll back some nested transactions */
611
+ char *zSql = sqlite3_mprintf("ROLLBACK TO x%d", iLevel-1);
612
+ int rc = sqlite3_exec(pDb->db, zSql, 0, 0, 0);
613
+ sqlite3_free(zSql);
614
+ if( rc!=0 ) return rc;
615
+ }
616
+
617
+ pDb->nOpenTrans = iLevel;
618
+ return 0;
619
+ }
620
+
621
+ static int sql_open(
622
+ const char *zSpec,
623
+ const char *zFilename,
624
+ int bClear,
625
+ TestDb **ppDb
626
+ ){
627
+ static const DatabaseMethods SqlMethods = {
628
+ sql_close,
629
+ sql_write,
630
+ sql_delete,
631
+ sql_delete_range,
632
+ sql_fetch,
633
+ sql_scan,
634
+ sql_begin,
635
+ sql_commit,
636
+ sql_rollback
637
+ };
638
+ const char *zCreate = "CREATE TABLE IF NOT EXISTS t1(k PRIMARY KEY, v)";
639
+ const char *zInsert = "REPLACE INTO t1 VALUES(?, ?)";
640
+ const char *zDelete = "DELETE FROM t1 WHERE k = ?";
641
+ const char *zRange = "DELETE FROM t1 WHERE k>? AND k<?";
642
+ const char *zFetch = "SELECT v FROM t1 WHERE k = ?";
643
+
644
+ const char *zScan0 = "SELECT * FROM t1 WHERE k BETWEEN ?1 AND ?2 ORDER BY k";
645
+ const char *zScan1 = "SELECT * FROM t1 WHERE k <= ?2 ORDER BY k";
646
+ const char *zScan2 = "SELECT * FROM t1 WHERE k >= ?1 ORDER BY k";
647
+ const char *zScan3 = "SELECT * FROM t1 ORDER BY k";
648
+
649
+ const char *zScan4 =
650
+ "SELECT * FROM t1 WHERE k BETWEEN ?1 AND ?2 ORDER BY k DESC";
651
+ const char *zScan5 = "SELECT * FROM t1 WHERE k <= ?2 ORDER BY k DESC";
652
+ const char *zScan6 = "SELECT * FROM t1 WHERE k >= ?1 ORDER BY k DESC";
653
+ const char *zScan7 = "SELECT * FROM t1 ORDER BY k DESC";
654
+
655
+ int rc;
656
+ SqlDb *pDb;
657
+ char *zPragma;
658
+
659
+ if( bClear && zFilename && zFilename[0] ){
660
+ unlink(zFilename);
661
+ }
662
+
663
+ pDb = (SqlDb *)malloc(sizeof(SqlDb));
664
+ memset(pDb, 0, sizeof(SqlDb));
665
+ pDb->base.pMethods = &SqlMethods;
666
+
667
+ if( 0!=(rc = sqlite3_open(zFilename, &pDb->db))
668
+ || 0!=(rc = sqlite3_exec(pDb->db, zCreate, 0, 0, 0))
669
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zInsert, -1, &pDb->pInsert, 0))
670
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zDelete, -1, &pDb->pDelete, 0))
671
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zRange, -1, &pDb->pDeleteRange, 0))
672
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zFetch, -1, &pDb->pFetch, 0))
673
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan0, -1, &pDb->apScan[0], 0))
674
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan1, -1, &pDb->apScan[1], 0))
675
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan2, -1, &pDb->apScan[2], 0))
676
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan3, -1, &pDb->apScan[3], 0))
677
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan4, -1, &pDb->apScan[4], 0))
678
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan5, -1, &pDb->apScan[5], 0))
679
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan6, -1, &pDb->apScan[6], 0))
680
+ || 0!=(rc = sqlite3_prepare_v2(pDb->db, zScan7, -1, &pDb->apScan[7], 0))
681
+ ){
682
+ *ppDb = 0;
683
+ sql_close((TestDb *)pDb);
684
+ return rc;
685
+ }
686
+
687
+ zPragma = sqlite3_mprintf("PRAGMA page_size=%d", TESTDB_DEFAULT_PAGE_SIZE);
688
+ sqlite3_exec(pDb->db, zPragma, 0, 0, 0);
689
+ sqlite3_free(zPragma);
690
+ zPragma = sqlite3_mprintf("PRAGMA cache_size=%d", TESTDB_DEFAULT_CACHE_SIZE);
691
+ sqlite3_exec(pDb->db, zPragma, 0, 0, 0);
692
+ sqlite3_free(zPragma);
693
+
694
+ /* sqlite3_exec(pDb->db, "PRAGMA locking_mode=EXCLUSIVE", 0, 0, 0); */
695
+ sqlite3_exec(pDb->db, "PRAGMA synchronous=OFF", 0, 0, 0);
696
+ sqlite3_exec(pDb->db, "PRAGMA journal_mode=WAL", 0, 0, 0);
697
+ sqlite3_exec(pDb->db, "PRAGMA wal_autocheckpoint=4096", 0, 0, 0);
698
+ if( zSpec ){
699
+ rc = sqlite3_exec(pDb->db, zSpec, 0, 0, 0);
700
+ if( rc!=SQLITE_OK ){
701
+ sql_close((TestDb *)pDb);
702
+ return rc;
703
+ }
704
+ }
705
+
706
+ *ppDb = (TestDb *)pDb;
707
+ return 0;
708
+ }
709
+ /*
710
+ ** End wrapper for SQLite.
711
+ *************************************************************************/
712
+
713
+ /*************************************************************************
714
+ ** Begin exported functions.
715
+ */
716
+ static struct Lib {
717
+ const char *zName;
718
+ const char *zDefaultDb;
719
+ int (*xOpen)(const char *, const char *zFilename, int bClear, TestDb **ppDb);
720
+ } aLib[] = {
721
+ { "sqlite3", "testdb.sqlite", sql_open },
722
+ { "lsm_small", "testdb.lsm_small", test_lsm_small_open },
723
+ { "lsm_lomem", "testdb.lsm_lomem", test_lsm_lomem_open },
724
+ { "lsm_lomem2", "testdb.lsm_lomem2", test_lsm_lomem2_open },
725
+ #ifdef HAVE_ZLIB
726
+ { "lsm_zip", "testdb.lsm_zip", test_lsm_zip_open },
727
+ #endif
728
+ { "lsm", "testdb.lsm", test_lsm_open },
729
+ #ifdef LSM_MUTEX_PTHREADS
730
+ { "lsm_mt2", "testdb.lsm_mt2", test_lsm_mt2 },
731
+ { "lsm_mt3", "testdb.lsm_mt3", test_lsm_mt3 },
732
+ #endif
733
+ #ifdef HAVE_LEVELDB
734
+ { "leveldb", "testdb.leveldb", test_leveldb_open },
735
+ #endif
736
+ #ifdef HAVE_KYOTOCABINET
737
+ { "kyotocabinet", "testdb.kc", kc_open },
738
+ #endif
739
+ #ifdef HAVE_MDB
740
+ { "mdb", "./testdb.mdb", mdb_open }
741
+ #endif
742
+ };
743
+
744
+ const char *tdb_system_name(int i){
745
+ if( i<0 || i>=ArraySize(aLib) ) return 0;
746
+ return aLib[i].zName;
747
+ }
748
+
749
+ const char *tdb_default_db(const char *zSys){
750
+ int i;
751
+ for(i=0; i<ArraySize(aLib); i++){
752
+ if( strcmp(aLib[i].zName, zSys)==0 ) return aLib[i].zDefaultDb;
753
+ }
754
+ return 0;
755
+ }
756
+
757
+ int tdb_open(const char *zLib, const char *zDb, int bClear, TestDb **ppDb){
758
+ int i;
759
+ int rc = 1;
760
+ const char *zSpec = 0;
761
+
762
+ int nLib = 0;
763
+ while( zLib[nLib] && zLib[nLib]!=' ' ){
764
+ nLib++;
765
+ }
766
+ zSpec = &zLib[nLib];
767
+ while( *zSpec==' ' ) zSpec++;
768
+ if( *zSpec=='\0' ) zSpec = 0;
769
+
770
+ for(i=0; i<ArraySize(aLib); i++){
771
+ if( (int)strlen(aLib[i].zName)==nLib
772
+ && 0==memcmp(zLib, aLib[i].zName, nLib) ){
773
+ rc = aLib[i].xOpen(zSpec, (zDb ? zDb : aLib[i].zDefaultDb), bClear, ppDb);
774
+ if( rc==0 ){
775
+ (*ppDb)->zLibrary = aLib[i].zName;
776
+ }
777
+ break;
778
+ }
779
+ }
780
+
781
+ if( rc ){
782
+ /* Failed to find the requested database library. Return an error. */
783
+ *ppDb = 0;
784
+ }
785
+ return rc;
786
+ }
787
+
788
+ int tdb_close(TestDb *pDb){
789
+ if( pDb ){
790
+ return pDb->pMethods->xClose(pDb);
791
+ }
792
+ return 0;
793
+ }
794
+
795
+ int tdb_write(TestDb *pDb, void *pKey, int nKey, void *pVal, int nVal){
796
+ return pDb->pMethods->xWrite(pDb, pKey, nKey, pVal, nVal);
797
+ }
798
+
799
+ int tdb_delete(TestDb *pDb, void *pKey, int nKey){
800
+ return pDb->pMethods->xDelete(pDb, pKey, nKey);
801
+ }
802
+
803
+ int tdb_delete_range(
804
+ TestDb *pDb, void *pKey1, int nKey1, void *pKey2, int nKey2
805
+ ){
806
+ return pDb->pMethods->xDeleteRange(pDb, pKey1, nKey1, pKey2, nKey2);
807
+ }
808
+
809
+ int tdb_fetch(TestDb *pDb, void *pKey, int nKey, void **ppVal, int *pnVal){
810
+ return pDb->pMethods->xFetch(pDb, pKey, nKey, ppVal, pnVal);
811
+ }
812
+
813
+ int tdb_scan(
814
+ TestDb *pDb, /* Database handle */
815
+ void *pCtx, /* Context pointer to pass to xCallback */
816
+ int bReverse, /* True to scan in reverse order */
817
+ void *pKey1, int nKey1, /* Start of search */
818
+ void *pKey2, int nKey2, /* End of search */
819
+ void (*xCallback)(void *pCtx, void *pKey, int nKey, void *pVal, int nVal)
820
+ ){
821
+ return pDb->pMethods->xScan(
822
+ pDb, pCtx, bReverse, pKey1, nKey1, pKey2, nKey2, xCallback
823
+ );
824
+ }
825
+
826
+ int tdb_begin(TestDb *pDb, int iLevel){
827
+ return pDb->pMethods->xBegin(pDb, iLevel);
828
+ }
829
+ int tdb_commit(TestDb *pDb, int iLevel){
830
+ return pDb->pMethods->xCommit(pDb, iLevel);
831
+ }
832
+ int tdb_rollback(TestDb *pDb, int iLevel){
833
+ return pDb->pMethods->xRollback(pDb, iLevel);
834
+ }
835
+
836
+ int tdb_transaction_support(TestDb *pDb){
837
+ return (pDb->pMethods->xBegin != error_transaction_function);
838
+ }
839
+
840
+ const char *tdb_library_name(TestDb *pDb){
841
+ return pDb->zLibrary;
842
+ }
843
+
844
+ /*
845
+ ** End exported functions.
846
+ *************************************************************************/
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb.h ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /*
3
+ ** This file is the interface to a very simple database library used for
4
+ ** testing. The interface is similar to that of the LSM. The main virtue
5
+ ** of this library is that the same API may be used to access a key-value
6
+ ** store implemented by LSM, SQLite or another database system. Which
7
+ ** makes it easy to use for correctness and performance tests.
8
+ */
9
+
10
+ #ifndef __WRAPPER_H_
11
+ #define __WRAPPER_H_
12
+
13
+ #ifdef __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ #include "lsm.h"
18
+
19
+ typedef struct TestDb TestDb;
20
+
21
+ /*
22
+ ** Open a new database connection. The first argument is the name of the
23
+ ** database library to use. e.g. something like:
24
+ **
25
+ ** "sqlite3"
26
+ ** "lsm"
27
+ **
28
+ ** See function tdb_system_name() for a list of available database systems.
29
+ **
30
+ ** The second argument is the name of the database to open (e.g. a filename).
31
+ **
32
+ ** If the third parameter is non-zero, then any existing database by the
33
+ ** name of zDb is removed before opening a new one. If it is zero, then an
34
+ ** existing database may be opened.
35
+ */
36
+ int tdb_open(const char *zLibrary, const char *zDb, int bClear, TestDb **ppDb);
37
+
38
+ /*
39
+ ** Close a database handle.
40
+ */
41
+ int tdb_close(TestDb *pDb);
42
+
43
+ /*
44
+ ** Write a new key/value into the database.
45
+ */
46
+ int tdb_write(TestDb *pDb, void *pKey, int nKey, void *pVal, int nVal);
47
+
48
+ /*
49
+ ** Delete a key from the database.
50
+ */
51
+ int tdb_delete(TestDb *pDb, void *pKey, int nKey);
52
+
53
+ /*
54
+ ** Delete a range of keys from the database.
55
+ */
56
+ int tdb_delete_range(TestDb *, void *pKey1, int nKey1, void *pKey2, int nKey2);
57
+
58
+ /*
59
+ ** Query the database for key (pKey/nKey). If no entry is found, set *ppVal
60
+ ** to 0 and *pnVal to -1 before returning. Otherwise, set *ppVal and *pnVal
61
+ ** to a pointer to and size of the value associated with (pKey/nKey).
62
+ */
63
+ int tdb_fetch(TestDb *pDb, void *pKey, int nKey, void **ppVal, int *pnVal);
64
+
65
+ /*
66
+ ** Open and close nested transactions. Currently, these functions only
67
+ ** work for SQLite3 and LSM systems. Use the tdb_transaction_support()
68
+ ** function to determine if a given TestDb handle supports these methods.
69
+ **
70
+ ** These functions and the iLevel parameter follow the same conventions as
71
+ ** the SQLite 4 transaction interface. Note that this is slightly different
72
+ ** from the way LSM does things. As follows:
73
+ **
74
+ ** tdb_begin():
75
+ ** A successful call to tdb_begin() with (iLevel>1) guarantees that
76
+ ** there are at least (iLevel-1) write transactions open. If iLevel==1,
77
+ ** then it guarantees that at least a read-transaction is open. Calling
78
+ ** tdb_begin() with iLevel==0 is a no-op.
79
+ **
80
+ ** tdb_commit():
81
+ ** A successful call to tdb_commit() with (iLevel>1) guarantees that
82
+ ** there are at most (iLevel-1) write transactions open. If iLevel==1,
83
+ ** then it guarantees that there are no write transactions open (although
84
+ ** a read-transaction may remain open). Calling tdb_commit() with
85
+ ** iLevel==0 ensures that all transactions, read or write, have been
86
+ ** closed and committed.
87
+ **
88
+ ** tdb_rollback():
89
+ ** This call is similar to tdb_commit(), except that instead of committing
90
+ ** transactions, it reverts them. For example, calling tdb_rollback() with
91
+ ** iLevel==2 ensures that there is at most one write transaction open, and
92
+ ** restores the database to the state that it was in when that transaction
93
+ ** was opened.
94
+ **
95
+ ** In other words, tdb_commit() just closes transactions - tdb_rollback()
96
+ ** closes transactions and then restores the database to the state it
97
+ ** was in before those transactions were even opened.
98
+ */
99
+ int tdb_begin(TestDb *pDb, int iLevel);
100
+ int tdb_commit(TestDb *pDb, int iLevel);
101
+ int tdb_rollback(TestDb *pDb, int iLevel);
102
+
103
+ /*
104
+ ** Return true if transactions are supported, or false otherwise.
105
+ */
106
+ int tdb_transaction_support(TestDb *pDb);
107
+
108
+ /*
109
+ ** Return the name of the database library (as passed to tdb_open()) used
110
+ ** by the handled passed as the first argument.
111
+ */
112
+ const char *tdb_library_name(TestDb *pDb);
113
+
114
+ /*
115
+ ** Scan a range of database keys. Invoke the callback function for each
116
+ ** key visited.
117
+ */
118
+ int tdb_scan(
119
+ TestDb *pDb, /* Database handle */
120
+ void *pCtx, /* Context pointer to pass to xCallback */
121
+ int bReverse, /* True to scan in reverse order */
122
+ void *pKey1, int nKey1, /* Start of search */
123
+ void *pKey2, int nKey2, /* End of search */
124
+ void (*xCallback)(void *pCtx, void *pKey, int nKey, void *pVal, int nVal)
125
+ );
126
+
127
+ const char *tdb_system_name(int i);
128
+ const char *tdb_default_db(const char *zSys);
129
+
130
+ int tdb_lsm_open(const char *zCfg, const char *zDb, int bClear, TestDb **ppDb);
131
+
132
+ /*
133
+ ** If the TestDb handle passed as an argument is a wrapper around an LSM
134
+ ** database, return the LSM handle. Otherwise, if the argument is some other
135
+ ** database system, return NULL.
136
+ */
137
+ lsm_db *tdb_lsm(TestDb *pDb);
138
+
139
+ /*
140
+ ** Return true if the db passed as an argument is a multi-threaded LSM
141
+ ** connection.
142
+ */
143
+ int tdb_lsm_multithread(TestDb *pDb);
144
+
145
+ /*
146
+ ** Return a pointer to the lsm_env object used by all lsm database
147
+ ** connections initialized as a copy of the object returned by
148
+ ** lsm_default_env(). It may be modified (e.g. to override functions)
149
+ ** if the caller can guarantee that it is not already in use.
150
+ */
151
+ lsm_env *tdb_lsm_env(void);
152
+
153
+ /*
154
+ ** The following functions only work with LSM database handles. It is
155
+ ** illegal to call them with any other type of database handle specified
156
+ ** as an argument.
157
+ */
158
+ void tdb_lsm_enable_log(TestDb *pDb, int bEnable);
159
+ void tdb_lsm_application_crash(TestDb *pDb);
160
+ void tdb_lsm_prepare_system_crash(TestDb *pDb);
161
+ void tdb_lsm_system_crash(TestDb *pDb);
162
+ void tdb_lsm_prepare_sync_crash(TestDb *pDb, int iSync);
163
+
164
+
165
+ void tdb_lsm_safety(TestDb *pDb, int eMode);
166
+ void tdb_lsm_config_work_hook(TestDb *pDb, void (*)(lsm_db *, void *), void *);
167
+ void tdb_lsm_write_hook(TestDb *, void(*)(void*,int,lsm_i64,int,int), void*);
168
+ int tdb_lsm_config_str(TestDb *pDb, const char *zStr);
169
+
170
+ #ifdef __cplusplus
171
+ } /* End of the 'extern "C"' block */
172
+ #endif
173
+
174
+ #endif
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb2.cc ADDED
@@ -0,0 +1,369 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ #include "lsmtest.h"
4
+ #include <stdlib.h>
5
+
6
+ #ifdef HAVE_KYOTOCABINET
7
+ #include "kcpolydb.h"
8
+ extern "C" {
9
+ struct KcDb {
10
+ TestDb base;
11
+ kyotocabinet::TreeDB* db;
12
+ char *pVal;
13
+ };
14
+ }
15
+
16
+ int test_kc_open(const char *zFilename, int bClear, TestDb **ppDb){
17
+ KcDb *pKcDb;
18
+ int ok;
19
+ int rc = 0;
20
+
21
+ if( bClear ){
22
+ char *zCmd = sqlite3_mprintf("rm -rf %s\n", zFilename);
23
+ system(zCmd);
24
+ sqlite3_free(zCmd);
25
+ }
26
+
27
+ pKcDb = (KcDb *)malloc(sizeof(KcDb));
28
+ memset(pKcDb, 0, sizeof(KcDb));
29
+
30
+
31
+ pKcDb->db = new kyotocabinet::TreeDB();
32
+ pKcDb->db->tune_page(TESTDB_DEFAULT_PAGE_SIZE);
33
+ pKcDb->db->tune_page_cache(
34
+ TESTDB_DEFAULT_PAGE_SIZE * TESTDB_DEFAULT_CACHE_SIZE
35
+ );
36
+ ok = pKcDb->db->open(zFilename,
37
+ kyotocabinet::PolyDB::OWRITER | kyotocabinet::PolyDB::OCREATE
38
+ );
39
+ if( ok==0 ){
40
+ free(pKcDb);
41
+ pKcDb = 0;
42
+ rc = 1;
43
+ }
44
+
45
+ *ppDb = (TestDb *)pKcDb;
46
+ return rc;
47
+ }
48
+
49
+ int test_kc_close(TestDb *pDb){
50
+ KcDb *pKcDb = (KcDb *)pDb;
51
+ if( pKcDb->pVal ){
52
+ delete [] pKcDb->pVal;
53
+ }
54
+ pKcDb->db->close();
55
+ delete pKcDb->db;
56
+ free(pKcDb);
57
+ return 0;
58
+ }
59
+
60
+ int test_kc_write(TestDb *pDb, void *pKey, int nKey, void *pVal, int nVal){
61
+ KcDb *pKcDb = (KcDb *)pDb;
62
+ int ok;
63
+
64
+ ok = pKcDb->db->set((const char *)pKey, nKey, (const char *)pVal, nVal);
65
+ return (ok ? 0 : 1);
66
+ }
67
+
68
+ int test_kc_delete(TestDb *pDb, void *pKey, int nKey){
69
+ KcDb *pKcDb = (KcDb *)pDb;
70
+ int ok;
71
+
72
+ ok = pKcDb->db->remove((const char *)pKey, nKey);
73
+ return (ok ? 0 : 1);
74
+ }
75
+
76
+ int test_kc_delete_range(
77
+ TestDb *pDb,
78
+ void *pKey1, int nKey1,
79
+ void *pKey2, int nKey2
80
+ ){
81
+ int res;
82
+ KcDb *pKcDb = (KcDb *)pDb;
83
+ kyotocabinet::DB::Cursor* pCur = pKcDb->db->cursor();
84
+
85
+ if( pKey1 ){
86
+ res = pCur->jump((const char *)pKey1, nKey1);
87
+ }else{
88
+ res = pCur->jump();
89
+ }
90
+
91
+ while( 1 ){
92
+ const char *pKey; size_t nKey;
93
+ const char *pVal; size_t nVal;
94
+
95
+ pKey = pCur->get(&nKey, &pVal, &nVal);
96
+ if( pKey==0 ) break;
97
+
98
+ #ifndef NDEBUG
99
+ if( pKey1 ){
100
+ res = memcmp(pKey, pKey1, MIN((size_t)nKey1, nKey));
101
+ assert( res>0 || (res==0 && nKey>nKey1) );
102
+ }
103
+ #endif
104
+
105
+ if( pKey2 ){
106
+ res = memcmp(pKey, pKey2, MIN((size_t)nKey2, nKey));
107
+ if( res>0 || (res==0 && (size_t)nKey2<nKey) ){
108
+ delete [] pKey;
109
+ break;
110
+ }
111
+ }
112
+ pCur->remove();
113
+ delete [] pKey;
114
+ }
115
+
116
+ delete pCur;
117
+ return 0;
118
+ }
119
+
120
+ int test_kc_fetch(
121
+ TestDb *pDb,
122
+ void *pKey,
123
+ int nKey,
124
+ void **ppVal,
125
+ int *pnVal
126
+ ){
127
+ KcDb *pKcDb = (KcDb *)pDb;
128
+ size_t nVal;
129
+
130
+ if( pKcDb->pVal ){
131
+ delete [] pKcDb->pVal;
132
+ pKcDb->pVal = 0;
133
+ }
134
+
135
+ pKcDb->pVal = pKcDb->db->get((const char *)pKey, nKey, &nVal);
136
+ if( pKcDb->pVal ){
137
+ *ppVal = pKcDb->pVal;
138
+ *pnVal = nVal;
139
+ }else{
140
+ *ppVal = 0;
141
+ *pnVal = -1;
142
+ }
143
+
144
+ return 0;
145
+ }
146
+
147
+ int test_kc_scan(
148
+ TestDb *pDb, /* Database handle */
149
+ void *pCtx, /* Context pointer to pass to xCallback */
150
+ int bReverse, /* True for a reverse order scan */
151
+ void *pKey1, int nKey1, /* Start of search */
152
+ void *pKey2, int nKey2, /* End of search */
153
+ void (*xCallback)(void *pCtx, void *pKey, int nKey, void *pVal, int nVal)
154
+ ){
155
+ KcDb *pKcDb = (KcDb *)pDb;
156
+ kyotocabinet::DB::Cursor* pCur = pKcDb->db->cursor();
157
+ int res;
158
+
159
+ if( bReverse==0 ){
160
+ if( pKey1 ){
161
+ res = pCur->jump((const char *)pKey1, nKey1);
162
+ }else{
163
+ res = pCur->jump();
164
+ }
165
+ }else{
166
+ if( pKey2 ){
167
+ res = pCur->jump_back((const char *)pKey2, nKey2);
168
+ }else{
169
+ res = pCur->jump_back();
170
+ }
171
+ }
172
+
173
+ while( res ){
174
+ const char *pKey; size_t nKey;
175
+ const char *pVal; size_t nVal;
176
+ pKey = pCur->get(&nKey, &pVal, &nVal);
177
+
178
+ if( bReverse==0 && pKey2 ){
179
+ res = memcmp(pKey, pKey2, MIN((size_t)nKey2, nKey));
180
+ if( res>0 || (res==0 && (size_t)nKey2<nKey) ){
181
+ delete [] pKey;
182
+ break;
183
+ }
184
+ }else if( bReverse!=0 && pKey1 ){
185
+ res = memcmp(pKey, pKey1, MIN((size_t)nKey1, nKey));
186
+ if( res<0 || (res==0 && (size_t)nKey1>nKey) ){
187
+ delete [] pKey;
188
+ break;
189
+ }
190
+ }
191
+
192
+ xCallback(pCtx, (void *)pKey, (int)nKey, (void *)pVal, (int)nVal);
193
+ delete [] pKey;
194
+
195
+ if( bReverse ){
196
+ res = pCur->step_back();
197
+ }else{
198
+ res = pCur->step();
199
+ }
200
+ }
201
+
202
+ delete pCur;
203
+ return 0;
204
+ }
205
+ #endif /* HAVE_KYOTOCABINET */
206
+
207
+ #ifdef HAVE_MDB
208
+ #include "lmdb.h"
209
+
210
+ extern "C" {
211
+ struct MdbDb {
212
+ TestDb base;
213
+ MDB_env *env;
214
+ MDB_dbi dbi;
215
+ };
216
+ }
217
+
218
+ int test_mdb_open(
219
+ const char *zSpec,
220
+ const char *zFilename,
221
+ int bClear,
222
+ TestDb **ppDb
223
+ ){
224
+ MDB_txn *txn;
225
+ MdbDb *pMdb;
226
+ int rc;
227
+
228
+ if( bClear ){
229
+ char *zCmd = sqlite3_mprintf("rm -rf %s\n", zFilename);
230
+ system(zCmd);
231
+ sqlite3_free(zCmd);
232
+ }
233
+
234
+ pMdb = (MdbDb *)malloc(sizeof(MdbDb));
235
+ memset(pMdb, 0, sizeof(MdbDb));
236
+
237
+ rc = mdb_env_create(&pMdb->env);
238
+ if( rc==0 ) rc = mdb_env_set_mapsize(pMdb->env, 1*1024*1024*1024);
239
+ if( rc==0 ) rc = mdb_env_open(pMdb->env, zFilename, MDB_NOSYNC|MDB_NOSUBDIR, 0600);
240
+ if( rc==0 ) rc = mdb_txn_begin(pMdb->env, NULL, 0, &txn);
241
+ if( rc==0 ){
242
+ rc = mdb_open(txn, NULL, 0, &pMdb->dbi);
243
+ mdb_txn_commit(txn);
244
+ }
245
+
246
+ *ppDb = (TestDb *)pMdb;
247
+ return rc;
248
+ }
249
+
250
+ int test_mdb_close(TestDb *pDb){
251
+ MdbDb *pMdb = (MdbDb *)pDb;
252
+
253
+ mdb_close(pMdb->env, pMdb->dbi);
254
+ mdb_env_close(pMdb->env);
255
+ free(pMdb);
256
+ return 0;
257
+ }
258
+
259
+ int test_mdb_write(TestDb *pDb, void *pKey, int nKey, void *pVal, int nVal){
260
+ int rc;
261
+ MdbDb *pMdb = (MdbDb *)pDb;
262
+ MDB_val val;
263
+ MDB_val key;
264
+ MDB_txn *txn;
265
+
266
+ val.mv_size = nVal;
267
+ val.mv_data = pVal;
268
+ key.mv_size = nKey;
269
+ key.mv_data = pKey;
270
+
271
+ rc = mdb_txn_begin(pMdb->env, NULL, 0, &txn);
272
+ if( rc==0 ){
273
+ rc = mdb_put(txn, pMdb->dbi, &key, &val, 0);
274
+ if( rc==0 ){
275
+ rc = mdb_txn_commit(txn);
276
+ }else{
277
+ mdb_txn_abort(txn);
278
+ }
279
+ }
280
+
281
+ return rc;
282
+ }
283
+
284
+ int test_mdb_delete(TestDb *pDb, void *pKey, int nKey){
285
+ int rc;
286
+ MdbDb *pMdb = (MdbDb *)pDb;
287
+ MDB_val key;
288
+ MDB_txn *txn;
289
+
290
+ key.mv_size = nKey;
291
+ key.mv_data = pKey;
292
+ rc = mdb_txn_begin(pMdb->env, NULL, 0, &txn);
293
+ if( rc==0 ){
294
+ rc = mdb_del(txn, pMdb->dbi, &key, 0);
295
+ if( rc==0 ){
296
+ rc = mdb_txn_commit(txn);
297
+ }else{
298
+ mdb_txn_abort(txn);
299
+ }
300
+ }
301
+
302
+ return rc;
303
+ }
304
+
305
+ int test_mdb_fetch(
306
+ TestDb *pDb,
307
+ void *pKey,
308
+ int nKey,
309
+ void **ppVal,
310
+ int *pnVal
311
+ ){
312
+ int rc;
313
+ MdbDb *pMdb = (MdbDb *)pDb;
314
+ MDB_val key;
315
+ MDB_txn *txn;
316
+
317
+ key.mv_size = nKey;
318
+ key.mv_data = pKey;
319
+
320
+ rc = mdb_txn_begin(pMdb->env, NULL, MDB_RDONLY, &txn);
321
+ if( rc==0 ){
322
+ MDB_val val = {0, 0};
323
+ rc = mdb_get(txn, pMdb->dbi, &key, &val);
324
+ if( rc==MDB_NOTFOUND ){
325
+ rc = 0;
326
+ *ppVal = 0;
327
+ *pnVal = -1;
328
+ }else{
329
+ *ppVal = val.mv_data;
330
+ *pnVal = val.mv_size;
331
+ }
332
+ mdb_txn_commit(txn);
333
+ }
334
+
335
+ return rc;
336
+ }
337
+
338
+ int test_mdb_scan(
339
+ TestDb *pDb, /* Database handle */
340
+ void *pCtx, /* Context pointer to pass to xCallback */
341
+ int bReverse, /* True for a reverse order scan */
342
+ void *pKey1, int nKey1, /* Start of search */
343
+ void *pKey2, int nKey2, /* End of search */
344
+ void (*xCallback)(void *pCtx, void *pKey, int nKey, void *pVal, int nVal)
345
+ ){
346
+ MdbDb *pMdb = (MdbDb *)pDb;
347
+ int rc;
348
+ MDB_cursor_op op = bReverse ? MDB_PREV : MDB_NEXT;
349
+ MDB_txn *txn;
350
+
351
+ rc = mdb_txn_begin(pMdb->env, NULL, MDB_RDONLY, &txn);
352
+ if( rc==0 ){
353
+ MDB_cursor *csr;
354
+ MDB_val key = {0, 0};
355
+ MDB_val val = {0, 0};
356
+
357
+ rc = mdb_cursor_open(txn, pMdb->dbi, &csr);
358
+ if( rc==0 ){
359
+ while( mdb_cursor_get(csr, &key, &val, op)==0 ){
360
+ xCallback(pCtx, key.mv_data, key.mv_size, val.mv_data, val.mv_size);
361
+ }
362
+ mdb_cursor_close(csr);
363
+ }
364
+ }
365
+
366
+ return rc;
367
+ }
368
+
369
+ #endif /* HAVE_MDB */
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb3.c ADDED
@@ -0,0 +1,1429 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include "lsmtest_tdb.h"
3
+ #include "lsm.h"
4
+ #include "lsmtest.h"
5
+
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+ #include <assert.h>
9
+ #ifndef _WIN32
10
+ # include <unistd.h>
11
+ #endif
12
+ #include <stdio.h>
13
+
14
+ #ifndef _WIN32
15
+ # include <sys/time.h>
16
+ #endif
17
+
18
+ typedef struct LsmDb LsmDb;
19
+ typedef struct LsmWorker LsmWorker;
20
+ typedef struct LsmFile LsmFile;
21
+
22
+ #define LSMTEST_DFLT_MT_MAX_CKPT (8*1024)
23
+ #define LSMTEST_DFLT_MT_MIN_CKPT (2*1024)
24
+
25
+ #ifdef LSM_MUTEX_PTHREADS
26
+ #include <pthread.h>
27
+
28
+ #define LSMTEST_THREAD_CKPT 1
29
+ #define LSMTEST_THREAD_WORKER 2
30
+ #define LSMTEST_THREAD_WORKER_AC 3
31
+
32
+ /*
33
+ ** There are several different types of worker threads that run in different
34
+ ** test configurations, depending on the value of LsmWorker.eType.
35
+ **
36
+ ** 1. Checkpointer.
37
+ ** 2. Worker with auto-checkpoint.
38
+ ** 3. Worker without auto-checkpoint.
39
+ */
40
+ struct LsmWorker {
41
+ LsmDb *pDb; /* Main database structure */
42
+ lsm_db *pWorker; /* Worker database handle */
43
+ pthread_t worker_thread; /* Worker thread */
44
+ pthread_cond_t worker_cond; /* Condition var the worker waits on */
45
+ pthread_mutex_t worker_mutex; /* Mutex used with worker_cond */
46
+ int bDoWork; /* Set to true by client when there is work */
47
+ int worker_rc; /* Store error code here */
48
+ int eType; /* LSMTEST_THREAD_XXX constant */
49
+ int bBlock;
50
+ };
51
+ #else
52
+ struct LsmWorker { int worker_rc; int bBlock; };
53
+ #endif
54
+
55
+ static void mt_shutdown(LsmDb *);
56
+
57
+ lsm_env *tdb_lsm_env(void){
58
+ static int bInit = 0;
59
+ static lsm_env env;
60
+ if( bInit==0 ){
61
+ memcpy(&env, lsm_default_env(), sizeof(env));
62
+ bInit = 1;
63
+ }
64
+ return &env;
65
+ }
66
+
67
+ typedef struct FileSector FileSector;
68
+ typedef struct FileData FileData;
69
+
70
+ struct FileSector {
71
+ u8 *aOld; /* Old data for this sector */
72
+ };
73
+
74
+ struct FileData {
75
+ int nSector; /* Allocated size of apSector[] array */
76
+ FileSector *aSector; /* Array of file sectors */
77
+ };
78
+
79
+ /*
80
+ ** bPrepareCrash:
81
+ ** If non-zero, the file wrappers maintain enough in-memory data to
82
+ ** simulate the effect of a power-failure on the file-system (i.e. that
83
+ ** unsynced sectors may be written, not written, or overwritten with
84
+ ** arbitrary data when the crash occurs).
85
+ **
86
+ ** bCrashed:
87
+ ** Set to true after a crash is simulated. Once this variable is true, all
88
+ ** VFS methods other than xClose() return LSM_IOERR as soon as they are
89
+ ** called (without affecting the contents of the file-system).
90
+ **
91
+ ** env:
92
+ ** The environment object used by all lsm_db* handles opened by this
93
+ ** object (i.e. LsmDb.db plus any worker connections). Variable env.pVfsCtx
94
+ ** always points to the containing LsmDb structure.
95
+ */
96
+ struct LsmDb {
97
+ TestDb base; /* Base class - methods table */
98
+ lsm_env env; /* Environment used by connection db */
99
+ char *zName; /* Database file name */
100
+ lsm_db *db; /* LSM database handle */
101
+
102
+ lsm_cursor *pCsr; /* Cursor held open during read transaction */
103
+ void *pBuf; /* Buffer for tdb_fetch() output */
104
+ int nBuf; /* Allocated (not used) size of pBuf */
105
+
106
+ /* Crash testing related state */
107
+ int bCrashed; /* True once a crash has occurred */
108
+ int nAutoCrash; /* Number of syncs until a crash */
109
+ int bPrepareCrash; /* True to store writes in memory */
110
+
111
+ /* Unsynced data (while crash testing) */
112
+ int szSector; /* Assumed size of disk sectors (512B) */
113
+ FileData aFile[2]; /* Database and log file data */
114
+
115
+ /* Other test instrumentation */
116
+ int bNoRecovery; /* If true, assume DMS2 is locked */
117
+
118
+ /* Work hook redirection */
119
+ void (*xWork)(lsm_db *, void *);
120
+ void *pWorkCtx;
121
+
122
+ /* IO logging hook */
123
+ void (*xWriteHook)(void *, int, lsm_i64, int, int);
124
+ void *pWriteCtx;
125
+
126
+ /* Worker threads (for lsm_mt) */
127
+ int nMtMinCkpt;
128
+ int nMtMaxCkpt;
129
+ int eMode;
130
+ int nWorker;
131
+ LsmWorker *aWorker;
132
+ };
133
+
134
+ #define LSMTEST_MODE_SINGLETHREAD 1
135
+ #define LSMTEST_MODE_BACKGROUND_CKPT 2
136
+ #define LSMTEST_MODE_BACKGROUND_WORK 3
137
+ #define LSMTEST_MODE_BACKGROUND_BOTH 4
138
+
139
+ /*************************************************************************
140
+ **************************************************************************
141
+ ** Begin test VFS code.
142
+ */
143
+
144
+ struct LsmFile {
145
+ lsm_file *pReal; /* Real underlying file */
146
+ int bLog; /* True for log file. False for db file */
147
+ LsmDb *pDb; /* Database handle that uses this file */
148
+ };
149
+
150
+ static int testEnvFullpath(
151
+ lsm_env *pEnv, /* Environment for current LsmDb */
152
+ const char *zFile, /* Relative path name */
153
+ char *zOut, /* Output buffer */
154
+ int *pnOut /* IN/OUT: Size of output buffer */
155
+ ){
156
+ lsm_env *pRealEnv = tdb_lsm_env();
157
+ return pRealEnv->xFullpath(pRealEnv, zFile, zOut, pnOut);
158
+ }
159
+
160
+ static int testEnvOpen(
161
+ lsm_env *pEnv, /* Environment for current LsmDb */
162
+ const char *zFile, /* Name of file to open */
163
+ int flags,
164
+ lsm_file **ppFile /* OUT: New file handle object */
165
+ ){
166
+ lsm_env *pRealEnv = tdb_lsm_env();
167
+ LsmDb *pDb = (LsmDb *)pEnv->pVfsCtx;
168
+ int rc; /* Return Code */
169
+ LsmFile *pRet; /* The new file handle */
170
+ int nFile; /* Length of string zFile in bytes */
171
+
172
+ nFile = strlen(zFile);
173
+ pRet = (LsmFile *)testMalloc(sizeof(LsmFile));
174
+ pRet->pDb = pDb;
175
+ pRet->bLog = (nFile > 4 && 0==memcmp("-log", &zFile[nFile-4], 4));
176
+
177
+ rc = pRealEnv->xOpen(pRealEnv, zFile, flags, &pRet->pReal);
178
+ if( rc!=LSM_OK ){
179
+ testFree(pRet);
180
+ pRet = 0;
181
+ }
182
+
183
+ *ppFile = (lsm_file *)pRet;
184
+ return rc;
185
+ }
186
+
187
+ static int testEnvRead(lsm_file *pFile, lsm_i64 iOff, void *pData, int nData){
188
+ lsm_env *pRealEnv = tdb_lsm_env();
189
+ LsmFile *p = (LsmFile *)pFile;
190
+ if( p->pDb->bCrashed ) return LSM_IOERR;
191
+ return pRealEnv->xRead(p->pReal, iOff, pData, nData);
192
+ }
193
+
194
+ static int testEnvWrite(lsm_file *pFile, lsm_i64 iOff, void *pData, int nData){
195
+ lsm_env *pRealEnv = tdb_lsm_env();
196
+ LsmFile *p = (LsmFile *)pFile;
197
+ LsmDb *pDb = p->pDb;
198
+
199
+ if( pDb->bCrashed ) return LSM_IOERR;
200
+
201
+ if( pDb->bPrepareCrash ){
202
+ FileData *pData2 = &pDb->aFile[p->bLog];
203
+ int iFirst;
204
+ int iLast;
205
+ int iSector;
206
+
207
+ iFirst = (int)(iOff / pDb->szSector);
208
+ iLast = (int)((iOff + nData - 1) / pDb->szSector);
209
+
210
+ if( pData2->nSector<(iLast+1) ){
211
+ int nNew = ( ((iLast + 1) + 63) / 64 ) * 64;
212
+ assert( nNew>iLast );
213
+ pData2->aSector = (FileSector *)testRealloc(
214
+ pData2->aSector, nNew*sizeof(FileSector)
215
+ );
216
+ memset(&pData2->aSector[pData2->nSector],
217
+ 0, (nNew - pData2->nSector) * sizeof(FileSector)
218
+ );
219
+ pData2->nSector = nNew;
220
+ }
221
+
222
+ for(iSector=iFirst; iSector<=iLast; iSector++){
223
+ if( pData2->aSector[iSector].aOld==0 ){
224
+ u8 *aOld = (u8 *)testMalloc(pDb->szSector);
225
+ pRealEnv->xRead(
226
+ p->pReal, (lsm_i64)iSector*pDb->szSector, aOld, pDb->szSector
227
+ );
228
+ pData2->aSector[iSector].aOld = aOld;
229
+ }
230
+ }
231
+ }
232
+
233
+ if( pDb->xWriteHook ){
234
+ int rc;
235
+ int nUs;
236
+ struct timeval t1;
237
+ struct timeval t2;
238
+
239
+ gettimeofday(&t1, 0);
240
+ assert( nData>0 );
241
+ rc = pRealEnv->xWrite(p->pReal, iOff, pData, nData);
242
+ gettimeofday(&t2, 0);
243
+
244
+ nUs = (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
245
+ pDb->xWriteHook(pDb->pWriteCtx, p->bLog, iOff, nData, nUs);
246
+ return rc;
247
+ }
248
+
249
+ return pRealEnv->xWrite(p->pReal, iOff, pData, nData);
250
+ }
251
+
252
+ static void doSystemCrash(LsmDb *pDb);
253
+
254
+ static int testEnvSync(lsm_file *pFile){
255
+ lsm_env *pRealEnv = tdb_lsm_env();
256
+ LsmFile *p = (LsmFile *)pFile;
257
+ LsmDb *pDb = p->pDb;
258
+ FileData *pData = &pDb->aFile[p->bLog];
259
+ int i;
260
+
261
+ if( pDb->bCrashed ) return LSM_IOERR;
262
+
263
+ if( pDb->nAutoCrash ){
264
+ pDb->nAutoCrash--;
265
+ if( pDb->nAutoCrash==0 ){
266
+ doSystemCrash(pDb);
267
+ pDb->bCrashed = 1;
268
+ return LSM_IOERR;
269
+ }
270
+ }
271
+
272
+ if( pDb->bPrepareCrash ){
273
+ for(i=0; i<pData->nSector; i++){
274
+ testFree(pData->aSector[i].aOld);
275
+ pData->aSector[i].aOld = 0;
276
+ }
277
+ }
278
+
279
+ if( pDb->xWriteHook ){
280
+ int rc;
281
+ int nUs;
282
+ struct timeval t1;
283
+ struct timeval t2;
284
+
285
+ gettimeofday(&t1, 0);
286
+ rc = pRealEnv->xSync(p->pReal);
287
+ gettimeofday(&t2, 0);
288
+
289
+ nUs = (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
290
+ pDb->xWriteHook(pDb->pWriteCtx, p->bLog, 0, 0, nUs);
291
+ return rc;
292
+ }
293
+
294
+ return pRealEnv->xSync(p->pReal);
295
+ }
296
+
297
+ static int testEnvTruncate(lsm_file *pFile, lsm_i64 iOff){
298
+ lsm_env *pRealEnv = tdb_lsm_env();
299
+ LsmFile *p = (LsmFile *)pFile;
300
+ if( p->pDb->bCrashed ) return LSM_IOERR;
301
+ return pRealEnv->xTruncate(p->pReal, iOff);
302
+ }
303
+
304
+ static int testEnvSectorSize(lsm_file *pFile){
305
+ lsm_env *pRealEnv = tdb_lsm_env();
306
+ LsmFile *p = (LsmFile *)pFile;
307
+ return pRealEnv->xSectorSize(p->pReal);
308
+ }
309
+
310
+ static int testEnvRemap(
311
+ lsm_file *pFile,
312
+ lsm_i64 iMin,
313
+ void **ppOut,
314
+ lsm_i64 *pnOut
315
+ ){
316
+ lsm_env *pRealEnv = tdb_lsm_env();
317
+ LsmFile *p = (LsmFile *)pFile;
318
+ return pRealEnv->xRemap(p->pReal, iMin, ppOut, pnOut);
319
+ }
320
+
321
+ static int testEnvFileid(
322
+ lsm_file *pFile,
323
+ void *ppOut,
324
+ int *pnOut
325
+ ){
326
+ lsm_env *pRealEnv = tdb_lsm_env();
327
+ LsmFile *p = (LsmFile *)pFile;
328
+ return pRealEnv->xFileid(p->pReal, ppOut, pnOut);
329
+ }
330
+
331
+ static int testEnvClose(lsm_file *pFile){
332
+ lsm_env *pRealEnv = tdb_lsm_env();
333
+ LsmFile *p = (LsmFile *)pFile;
334
+
335
+ pRealEnv->xClose(p->pReal);
336
+ testFree(p);
337
+ return LSM_OK;
338
+ }
339
+
340
+ static int testEnvUnlink(lsm_env *pEnv, const char *zFile){
341
+ lsm_env *pRealEnv = tdb_lsm_env();
342
+ unused_parameter(pEnv);
343
+ return pRealEnv->xUnlink(pRealEnv, zFile);
344
+ }
345
+
346
+ static int testEnvLock(lsm_file *pFile, int iLock, int eType){
347
+ LsmFile *p = (LsmFile *)pFile;
348
+ lsm_env *pRealEnv = tdb_lsm_env();
349
+
350
+ if( iLock==2 && eType==LSM_LOCK_EXCL && p->pDb->bNoRecovery ){
351
+ return LSM_BUSY;
352
+ }
353
+ return pRealEnv->xLock(p->pReal, iLock, eType);
354
+ }
355
+
356
+ static int testEnvTestLock(lsm_file *pFile, int iLock, int nLock, int eType){
357
+ LsmFile *p = (LsmFile *)pFile;
358
+ lsm_env *pRealEnv = tdb_lsm_env();
359
+
360
+ if( iLock==2 && eType==LSM_LOCK_EXCL && p->pDb->bNoRecovery ){
361
+ return LSM_BUSY;
362
+ }
363
+ return pRealEnv->xTestLock(p->pReal, iLock, nLock, eType);
364
+ }
365
+
366
+ static int testEnvShmMap(lsm_file *pFile, int iRegion, int sz, void **pp){
367
+ LsmFile *p = (LsmFile *)pFile;
368
+ lsm_env *pRealEnv = tdb_lsm_env();
369
+ return pRealEnv->xShmMap(p->pReal, iRegion, sz, pp);
370
+ }
371
+
372
+ static void testEnvShmBarrier(void){
373
+ }
374
+
375
+ static int testEnvShmUnmap(lsm_file *pFile, int bDel){
376
+ LsmFile *p = (LsmFile *)pFile;
377
+ lsm_env *pRealEnv = tdb_lsm_env();
378
+ return pRealEnv->xShmUnmap(p->pReal, bDel);
379
+ }
380
+
381
+ static int testEnvSleep(lsm_env *pEnv, int us){
382
+ lsm_env *pRealEnv = tdb_lsm_env();
383
+ return pRealEnv->xSleep(pRealEnv, us);
384
+ }
385
+
386
+ static void doSystemCrash(LsmDb *pDb){
387
+ lsm_env *pEnv = tdb_lsm_env();
388
+ int iFile;
389
+ int iSeed = pDb->aFile[0].nSector + pDb->aFile[1].nSector;
390
+
391
+ char *zFile = pDb->zName;
392
+ char *zFree = 0;
393
+
394
+ for(iFile=0; iFile<2; iFile++){
395
+ lsm_file *pFile = 0;
396
+ int i;
397
+
398
+ pEnv->xOpen(pEnv, zFile, 0, &pFile);
399
+ for(i=0; i<pDb->aFile[iFile].nSector; i++){
400
+ u8 *aOld = pDb->aFile[iFile].aSector[i].aOld;
401
+ if( aOld ){
402
+ int iOpt = testPrngValue(iSeed++) % 3;
403
+ switch( iOpt ){
404
+ case 0:
405
+ break;
406
+
407
+ case 1:
408
+ testPrngArray(iSeed++, (u32 *)aOld, pDb->szSector/4);
409
+ /* Fall-through */
410
+
411
+ case 2:
412
+ pEnv->xWrite(
413
+ pFile, (lsm_i64)i * pDb->szSector, aOld, pDb->szSector
414
+ );
415
+ break;
416
+ }
417
+ testFree(aOld);
418
+ pDb->aFile[iFile].aSector[i].aOld = 0;
419
+ }
420
+ }
421
+ pEnv->xClose(pFile);
422
+ zFree = zFile = sqlite3_mprintf("%s-log", pDb->zName);
423
+ }
424
+
425
+ sqlite3_free(zFree);
426
+ }
427
+ /*
428
+ ** End test VFS code.
429
+ **************************************************************************
430
+ *************************************************************************/
431
+
432
+ /*************************************************************************
433
+ **************************************************************************
434
+ ** Begin test compression hooks.
435
+ */
436
+
437
+ #ifdef HAVE_ZLIB
438
+ #include <zlib.h>
439
+
440
+ static int testZipBound(void *pCtx, int nSrc){
441
+ return compressBound(nSrc);
442
+ }
443
+
444
+ static int testZipCompress(
445
+ void *pCtx, /* Context pointer */
446
+ char *aOut, int *pnOut, /* OUT: Buffer containing compressed data */
447
+ const char *aIn, int nIn /* Buffer containing input data */
448
+ ){
449
+ uLongf n = *pnOut; /* In/out buffer size for compress() */
450
+ int rc; /* compress() return code */
451
+
452
+ rc = compress((Bytef*)aOut, &n, (Bytef*)aIn, nIn);
453
+ *pnOut = n;
454
+ return (rc==Z_OK ? 0 : LSM_ERROR);
455
+ }
456
+
457
+ static int testZipUncompress(
458
+ void *pCtx, /* Context pointer */
459
+ char *aOut, int *pnOut, /* OUT: Buffer containing uncompressed data */
460
+ const char *aIn, int nIn /* Buffer containing input data */
461
+ ){
462
+ uLongf n = *pnOut; /* In/out buffer size for uncompress() */
463
+ int rc; /* uncompress() return code */
464
+
465
+ rc = uncompress((Bytef*)aOut, &n, (Bytef*)aIn, nIn);
466
+ *pnOut = n;
467
+ return (rc==Z_OK ? 0 : LSM_ERROR);
468
+ }
469
+
470
+ static int testConfigureCompression(lsm_db *pDb){
471
+ static lsm_compress zip = {
472
+ 0, /* Context pointer (unused) */
473
+ 1, /* Id value */
474
+ testZipBound, /* xBound method */
475
+ testZipCompress, /* xCompress method */
476
+ testZipUncompress /* xUncompress method */
477
+ };
478
+ return lsm_config(pDb, LSM_CONFIG_SET_COMPRESSION, &zip);
479
+ }
480
+ #endif /* ifdef HAVE_ZLIB */
481
+
482
+ /*
483
+ ** End test compression hooks.
484
+ **************************************************************************
485
+ *************************************************************************/
486
+
487
+ static int test_lsm_close(TestDb *pTestDb){
488
+ int i;
489
+ int rc = LSM_OK;
490
+ LsmDb *pDb = (LsmDb *)pTestDb;
491
+
492
+ lsm_csr_close(pDb->pCsr);
493
+ lsm_close(pDb->db);
494
+
495
+ /* If this is a multi-threaded database, wait on the worker threads. */
496
+ mt_shutdown(pDb);
497
+ for(i=0; i<pDb->nWorker && rc==LSM_OK; i++){
498
+ rc = pDb->aWorker[i].worker_rc;
499
+ }
500
+
501
+ for(i=0; i<pDb->aFile[0].nSector; i++){
502
+ testFree(pDb->aFile[0].aSector[i].aOld);
503
+ }
504
+ testFree(pDb->aFile[0].aSector);
505
+ for(i=0; i<pDb->aFile[1].nSector; i++){
506
+ testFree(pDb->aFile[1].aSector[i].aOld);
507
+ }
508
+ testFree(pDb->aFile[1].aSector);
509
+
510
+ memset(pDb, sizeof(LsmDb), 0x11);
511
+ testFree((char *)pDb->pBuf);
512
+ testFree((char *)pDb);
513
+ return rc;
514
+ }
515
+
516
+ static void mt_signal_worker(LsmDb*, int);
517
+
518
+ static int waitOnCheckpointer(LsmDb *pDb, lsm_db *db){
519
+ int nSleep = 0;
520
+ int nKB;
521
+ int rc;
522
+
523
+ do {
524
+ nKB = 0;
525
+ rc = lsm_info(db, LSM_INFO_CHECKPOINT_SIZE, &nKB);
526
+ if( rc!=LSM_OK || nKB<pDb->nMtMaxCkpt ) break;
527
+ #ifdef LSM_MUTEX_PTHREADS
528
+ mt_signal_worker(pDb,
529
+ (pDb->eMode==LSMTEST_MODE_BACKGROUND_CKPT ? 0 : 1)
530
+ );
531
+ #endif
532
+ usleep(5000);
533
+ nSleep += 5;
534
+ }while( 1 );
535
+
536
+ #if 0
537
+ if( nSleep ) printf("# waitOnCheckpointer(): nSleep=%d\n", nSleep);
538
+ #endif
539
+
540
+ return rc;
541
+ }
542
+
543
+ static int waitOnWorker(LsmDb *pDb){
544
+ int rc;
545
+ int nLimit = -1;
546
+ int nSleep = 0;
547
+
548
+ rc = lsm_config(pDb->db, LSM_CONFIG_AUTOFLUSH, &nLimit);
549
+ do {
550
+ int nOld, nNew, rc2;
551
+ rc2 = lsm_info(pDb->db, LSM_INFO_TREE_SIZE, &nOld, &nNew);
552
+ if( rc2!=LSM_OK ) return rc2;
553
+ if( nOld==0 || nNew<(nLimit/2) ) break;
554
+ #ifdef LSM_MUTEX_PTHREADS
555
+ mt_signal_worker(pDb, 0);
556
+ #endif
557
+ usleep(5000);
558
+ nSleep += 5;
559
+ }while( 1 );
560
+
561
+ #if 0
562
+ if( nSleep ) printf("# waitOnWorker(): nSleep=%d\n", nSleep);
563
+ #endif
564
+
565
+ return rc;
566
+ }
567
+
568
+ static int test_lsm_write(
569
+ TestDb *pTestDb,
570
+ void *pKey,
571
+ int nKey,
572
+ void *pVal,
573
+ int nVal
574
+ ){
575
+ LsmDb *pDb = (LsmDb *)pTestDb;
576
+ int rc = LSM_OK;
577
+
578
+ if( pDb->eMode==LSMTEST_MODE_BACKGROUND_CKPT ){
579
+ rc = waitOnCheckpointer(pDb, pDb->db);
580
+ }else if(
581
+ pDb->eMode==LSMTEST_MODE_BACKGROUND_WORK
582
+ || pDb->eMode==LSMTEST_MODE_BACKGROUND_BOTH
583
+ ){
584
+ rc = waitOnWorker(pDb);
585
+ }
586
+
587
+ if( rc==LSM_OK ){
588
+ rc = lsm_insert(pDb->db, pKey, nKey, pVal, nVal);
589
+ }
590
+ return rc;
591
+ }
592
+
593
+ static int test_lsm_delete(TestDb *pTestDb, void *pKey, int nKey){
594
+ LsmDb *pDb = (LsmDb *)pTestDb;
595
+ return lsm_delete(pDb->db, pKey, nKey);
596
+ }
597
+
598
+ static int test_lsm_delete_range(
599
+ TestDb *pTestDb,
600
+ void *pKey1, int nKey1,
601
+ void *pKey2, int nKey2
602
+ ){
603
+ LsmDb *pDb = (LsmDb *)pTestDb;
604
+ return lsm_delete_range(pDb->db, pKey1, nKey1, pKey2, nKey2);
605
+ }
606
+
607
+ static int test_lsm_fetch(
608
+ TestDb *pTestDb,
609
+ void *pKey,
610
+ int nKey,
611
+ void **ppVal,
612
+ int *pnVal
613
+ ){
614
+ int rc;
615
+ LsmDb *pDb = (LsmDb *)pTestDb;
616
+ lsm_cursor *csr;
617
+
618
+ if( pKey==0 ) return LSM_OK;
619
+
620
+ if( pDb->pCsr==0 ){
621
+ rc = lsm_csr_open(pDb->db, &csr);
622
+ if( rc!=LSM_OK ) return rc;
623
+ }else{
624
+ csr = pDb->pCsr;
625
+ }
626
+
627
+ rc = lsm_csr_seek(csr, pKey, nKey, LSM_SEEK_EQ);
628
+ if( rc==LSM_OK ){
629
+ if( lsm_csr_valid(csr) ){
630
+ const void *pVal; int nVal;
631
+ rc = lsm_csr_value(csr, &pVal, &nVal);
632
+ if( nVal>pDb->nBuf ){
633
+ testFree(pDb->pBuf);
634
+ pDb->pBuf = testMalloc(nVal*2);
635
+ pDb->nBuf = nVal*2;
636
+ }
637
+ memcpy(pDb->pBuf, pVal, nVal);
638
+ *ppVal = pDb->pBuf;
639
+ *pnVal = nVal;
640
+ }else{
641
+ *ppVal = 0;
642
+ *pnVal = -1;
643
+ }
644
+ }
645
+ if( pDb->pCsr==0 ){
646
+ lsm_csr_close(csr);
647
+ }
648
+ return rc;
649
+ }
650
+
651
+ static int test_lsm_scan(
652
+ TestDb *pTestDb,
653
+ void *pCtx,
654
+ int bReverse,
655
+ void *pFirst, int nFirst,
656
+ void *pLast, int nLast,
657
+ void (*xCallback)(void *, void *, int , void *, int)
658
+ ){
659
+ LsmDb *pDb = (LsmDb *)pTestDb;
660
+ lsm_cursor *csr;
661
+ lsm_cursor *csr2 = 0;
662
+ int rc;
663
+
664
+ if( pDb->pCsr==0 ){
665
+ rc = lsm_csr_open(pDb->db, &csr);
666
+ if( rc!=LSM_OK ) return rc;
667
+ }else{
668
+ rc = LSM_OK;
669
+ csr = pDb->pCsr;
670
+ }
671
+
672
+ /* To enhance testing, if both pLast and pFirst are defined, seek the
673
+ ** cursor to the "end" boundary here. Then the next block seeks it to
674
+ ** the "start" ready for the scan. The point is to test that cursors
675
+ ** can be reused. */
676
+ if( pLast && pFirst ){
677
+ if( bReverse ){
678
+ rc = lsm_csr_seek(csr, pFirst, nFirst, LSM_SEEK_LE);
679
+ }else{
680
+ rc = lsm_csr_seek(csr, pLast, nLast, LSM_SEEK_GE);
681
+ }
682
+ }
683
+
684
+ if( bReverse ){
685
+ if( pLast ){
686
+ rc = lsm_csr_seek(csr, pLast, nLast, LSM_SEEK_LE);
687
+ }else{
688
+ rc = lsm_csr_last(csr);
689
+ }
690
+ }else{
691
+ if( pFirst ){
692
+ rc = lsm_csr_seek(csr, pFirst, nFirst, LSM_SEEK_GE);
693
+ }else{
694
+ rc = lsm_csr_first(csr);
695
+ }
696
+ }
697
+
698
+ while( rc==LSM_OK && lsm_csr_valid(csr) ){
699
+ const void *pKey; int nKey;
700
+ const void *pVal; int nVal;
701
+ int cmp;
702
+
703
+ lsm_csr_key(csr, &pKey, &nKey);
704
+ lsm_csr_value(csr, &pVal, &nVal);
705
+
706
+ if( bReverse && pFirst ){
707
+ cmp = memcmp(pFirst, pKey, MIN(nKey, nFirst));
708
+ if( cmp>0 || (cmp==0 && nFirst>nKey) ) break;
709
+ }else if( bReverse==0 && pLast ){
710
+ cmp = memcmp(pLast, pKey, MIN(nKey, nLast));
711
+ if( cmp<0 || (cmp==0 && nLast<nKey) ) break;
712
+ }
713
+
714
+ xCallback(pCtx, (void *)pKey, nKey, (void *)pVal, nVal);
715
+
716
+ if( bReverse ){
717
+ rc = lsm_csr_prev(csr);
718
+ }else{
719
+ rc = lsm_csr_next(csr);
720
+ }
721
+ }
722
+
723
+ if( pDb->pCsr==0 ){
724
+ lsm_csr_close(csr);
725
+ }
726
+ return rc;
727
+ }
728
+
729
+ static int test_lsm_begin(TestDb *pTestDb, int iLevel){
730
+ int rc = LSM_OK;
731
+ LsmDb *pDb = (LsmDb *)pTestDb;
732
+
733
+ /* iLevel==0 is a no-op. */
734
+ if( iLevel==0 ) return 0;
735
+
736
+ if( pDb->pCsr==0 ) rc = lsm_csr_open(pDb->db, &pDb->pCsr);
737
+ if( rc==LSM_OK && iLevel>1 ){
738
+ rc = lsm_begin(pDb->db, iLevel-1);
739
+ }
740
+
741
+ return rc;
742
+ }
743
+ static int test_lsm_commit(TestDb *pTestDb, int iLevel){
744
+ LsmDb *pDb = (LsmDb *)pTestDb;
745
+
746
+ /* If iLevel==0, close any open read transaction */
747
+ if( iLevel==0 && pDb->pCsr ){
748
+ lsm_csr_close(pDb->pCsr);
749
+ pDb->pCsr = 0;
750
+ }
751
+
752
+ /* If iLevel==0, close any open read transaction */
753
+ return lsm_commit(pDb->db, MAX(0, iLevel-1));
754
+ }
755
+ static int test_lsm_rollback(TestDb *pTestDb, int iLevel){
756
+ LsmDb *pDb = (LsmDb *)pTestDb;
757
+
758
+ /* If iLevel==0, close any open read transaction */
759
+ if( iLevel==0 && pDb->pCsr ){
760
+ lsm_csr_close(pDb->pCsr);
761
+ pDb->pCsr = 0;
762
+ }
763
+
764
+ return lsm_rollback(pDb->db, MAX(0, iLevel-1));
765
+ }
766
+
767
+ /*
768
+ ** A log message callback registered with lsm connections. Prints all
769
+ ** messages to stderr.
770
+ */
771
+ static void xLog(void *pCtx, int rc, const char *z){
772
+ unused_parameter(rc);
773
+ /* fprintf(stderr, "lsm: rc=%d \"%s\"\n", rc, z); */
774
+ if( pCtx ) fprintf(stderr, "%s: ", (char *)pCtx);
775
+ fprintf(stderr, "%s\n", z);
776
+ fflush(stderr);
777
+ }
778
+
779
+ static void xWorkHook(lsm_db *db, void *pArg){
780
+ LsmDb *p = (LsmDb *)pArg;
781
+ if( p->xWork ) p->xWork(db, p->pWorkCtx);
782
+ }
783
+
784
+ #define TEST_NO_RECOVERY -1
785
+ #define TEST_COMPRESSION -3
786
+
787
+ #define TEST_MT_MODE -2
788
+ #define TEST_MT_MIN_CKPT -4
789
+ #define TEST_MT_MAX_CKPT -5
790
+
791
+
792
+ int test_lsm_config_str(
793
+ LsmDb *pLsm,
794
+ lsm_db *db,
795
+ int bWorker,
796
+ const char *zStr,
797
+ int *pnThread
798
+ ){
799
+ struct CfgParam {
800
+ const char *zParam;
801
+ int bWorker;
802
+ int eParam;
803
+ } aParam[] = {
804
+ { "autoflush", 0, LSM_CONFIG_AUTOFLUSH },
805
+ { "page_size", 0, LSM_CONFIG_PAGE_SIZE },
806
+ { "block_size", 0, LSM_CONFIG_BLOCK_SIZE },
807
+ { "safety", 0, LSM_CONFIG_SAFETY },
808
+ { "autowork", 0, LSM_CONFIG_AUTOWORK },
809
+ { "autocheckpoint", 0, LSM_CONFIG_AUTOCHECKPOINT },
810
+ { "mmap", 0, LSM_CONFIG_MMAP },
811
+ { "use_log", 0, LSM_CONFIG_USE_LOG },
812
+ { "automerge", 0, LSM_CONFIG_AUTOMERGE },
813
+ { "max_freelist", 0, LSM_CONFIG_MAX_FREELIST },
814
+ { "multi_proc", 0, LSM_CONFIG_MULTIPLE_PROCESSES },
815
+ { "worker_automerge", 1, LSM_CONFIG_AUTOMERGE },
816
+ { "test_no_recovery", 0, TEST_NO_RECOVERY },
817
+ { "bg_min_ckpt", 0, TEST_NO_RECOVERY },
818
+
819
+ { "mt_mode", 0, TEST_MT_MODE },
820
+ { "mt_min_ckpt", 0, TEST_MT_MIN_CKPT },
821
+ { "mt_max_ckpt", 0, TEST_MT_MAX_CKPT },
822
+
823
+ #ifdef HAVE_ZLIB
824
+ { "compression", 0, TEST_COMPRESSION },
825
+ #endif
826
+ { 0, 0 }
827
+ };
828
+ const char *z = zStr;
829
+ int nThread = 1;
830
+
831
+ if( zStr==0 ) return 0;
832
+
833
+ assert( db );
834
+ while( z[0] ){
835
+ const char *zStart;
836
+
837
+ /* Skip whitespace */
838
+ while( *z==' ' ) z++;
839
+ zStart = z;
840
+
841
+ while( *z && *z!='=' ) z++;
842
+ if( *z ){
843
+ int eParam;
844
+ int i;
845
+ int iVal;
846
+ int iMul = 1;
847
+ int rc;
848
+ char zParam[32];
849
+ int nParam = z-zStart;
850
+ if( nParam==0 || nParam>sizeof(zParam)-1 ) goto syntax_error;
851
+
852
+ memcpy(zParam, zStart, nParam);
853
+ zParam[nParam] = '\0';
854
+ rc = testArgSelect(aParam, "param", zParam, &i);
855
+ if( rc!=0 ) return rc;
856
+ eParam = aParam[i].eParam;
857
+
858
+ z++;
859
+ zStart = z;
860
+ while( *z>='0' && *z<='9' ) z++;
861
+ if( *z=='k' || *z=='K' ){
862
+ iMul = 1;
863
+ z++;
864
+ }else if( *z=='M' || *z=='M' ){
865
+ iMul = 1024;
866
+ z++;
867
+ }
868
+ nParam = z-zStart;
869
+ if( nParam==0 || nParam>sizeof(zParam)-1 ) goto syntax_error;
870
+ memcpy(zParam, zStart, nParam);
871
+ zParam[nParam] = '\0';
872
+ iVal = atoi(zParam) * iMul;
873
+
874
+ if( eParam>0 ){
875
+ if( bWorker || aParam[i].bWorker==0 ){
876
+ lsm_config(db, eParam, &iVal);
877
+ }
878
+ }else{
879
+ switch( eParam ){
880
+ case TEST_NO_RECOVERY:
881
+ if( pLsm ) pLsm->bNoRecovery = iVal;
882
+ break;
883
+ case TEST_MT_MODE:
884
+ if( pLsm ) nThread = iVal;
885
+ break;
886
+ case TEST_MT_MIN_CKPT:
887
+ if( pLsm && iVal>0 ) pLsm->nMtMinCkpt = iVal*1024;
888
+ break;
889
+ case TEST_MT_MAX_CKPT:
890
+ if( pLsm && iVal>0 ) pLsm->nMtMaxCkpt = iVal*1024;
891
+ break;
892
+ #ifdef HAVE_ZLIB
893
+ case TEST_COMPRESSION:
894
+ testConfigureCompression(db);
895
+ break;
896
+ #endif
897
+ }
898
+ }
899
+ }else if( z!=zStart ){
900
+ goto syntax_error;
901
+ }
902
+ }
903
+
904
+ if( pnThread ) *pnThread = nThread;
905
+ if( pLsm && pLsm->nMtMaxCkpt < pLsm->nMtMinCkpt ){
906
+ pLsm->nMtMinCkpt = pLsm->nMtMaxCkpt;
907
+ }
908
+
909
+ return 0;
910
+ syntax_error:
911
+ testPrintError("syntax error at: \"%s\"\n", z);
912
+ return 1;
913
+ }
914
+
915
+ int tdb_lsm_config_str(TestDb *pDb, const char *zStr){
916
+ int rc = 0;
917
+ if( tdb_lsm(pDb) ){
918
+ #ifdef LSM_MUTEX_PTHREADS
919
+ int i;
920
+ #endif
921
+ LsmDb *pLsm = (LsmDb *)pDb;
922
+
923
+ rc = test_lsm_config_str(pLsm, pLsm->db, 0, zStr, 0);
924
+ #ifdef LSM_MUTEX_PTHREADS
925
+ for(i=0; rc==0 && i<pLsm->nWorker; i++){
926
+ rc = test_lsm_config_str(0, pLsm->aWorker[i].pWorker, 1, zStr, 0);
927
+ }
928
+ #endif
929
+ }
930
+ return rc;
931
+ }
932
+
933
+ int tdb_lsm_configure(lsm_db *db, const char *zConfig){
934
+ return test_lsm_config_str(0, db, 0, zConfig, 0);
935
+ }
936
+
937
+ static int testLsmStartWorkers(LsmDb *, int, const char *, const char *);
938
+
939
+ static int testLsmOpen(
940
+ const char *zCfg,
941
+ const char *zFilename,
942
+ int bClear,
943
+ TestDb **ppDb
944
+ ){
945
+ static const DatabaseMethods LsmMethods = {
946
+ test_lsm_close,
947
+ test_lsm_write,
948
+ test_lsm_delete,
949
+ test_lsm_delete_range,
950
+ test_lsm_fetch,
951
+ test_lsm_scan,
952
+ test_lsm_begin,
953
+ test_lsm_commit,
954
+ test_lsm_rollback
955
+ };
956
+
957
+ int rc;
958
+ int nFilename;
959
+ LsmDb *pDb;
960
+
961
+ /* If the bClear flag is set, delete any existing database. */
962
+ assert( zFilename);
963
+ if( bClear ) testDeleteLsmdb(zFilename);
964
+ nFilename = strlen(zFilename);
965
+
966
+ pDb = (LsmDb *)testMalloc(sizeof(LsmDb) + nFilename + 1);
967
+ memset(pDb, 0, sizeof(LsmDb));
968
+ pDb->base.pMethods = &LsmMethods;
969
+ pDb->zName = (char *)&pDb[1];
970
+ memcpy(pDb->zName, zFilename, nFilename + 1);
971
+
972
+ /* Default the sector size used for crash simulation to 512 bytes.
973
+ ** Todo: There should be an OS method to obtain this value - just as
974
+ ** there is in SQLite. For now, LSM assumes that it is smaller than
975
+ ** the page size (default 4KB).
976
+ */
977
+ pDb->szSector = 256;
978
+
979
+ /* Default values for the mt_min_ckpt and mt_max_ckpt parameters. */
980
+ pDb->nMtMinCkpt = LSMTEST_DFLT_MT_MIN_CKPT;
981
+ pDb->nMtMaxCkpt = LSMTEST_DFLT_MT_MAX_CKPT;
982
+
983
+ memcpy(&pDb->env, tdb_lsm_env(), sizeof(lsm_env));
984
+ pDb->env.pVfsCtx = (void *)pDb;
985
+ pDb->env.xFullpath = testEnvFullpath;
986
+ pDb->env.xOpen = testEnvOpen;
987
+ pDb->env.xRead = testEnvRead;
988
+ pDb->env.xWrite = testEnvWrite;
989
+ pDb->env.xTruncate = testEnvTruncate;
990
+ pDb->env.xSync = testEnvSync;
991
+ pDb->env.xSectorSize = testEnvSectorSize;
992
+ pDb->env.xRemap = testEnvRemap;
993
+ pDb->env.xFileid = testEnvFileid;
994
+ pDb->env.xClose = testEnvClose;
995
+ pDb->env.xUnlink = testEnvUnlink;
996
+ pDb->env.xLock = testEnvLock;
997
+ pDb->env.xTestLock = testEnvTestLock;
998
+ pDb->env.xShmBarrier = testEnvShmBarrier;
999
+ pDb->env.xShmMap = testEnvShmMap;
1000
+ pDb->env.xShmUnmap = testEnvShmUnmap;
1001
+ pDb->env.xSleep = testEnvSleep;
1002
+
1003
+ rc = lsm_new(&pDb->env, &pDb->db);
1004
+ if( rc==LSM_OK ){
1005
+ int nThread = 1;
1006
+ lsm_config_log(pDb->db, xLog, 0);
1007
+ lsm_config_work_hook(pDb->db, xWorkHook, (void *)pDb);
1008
+
1009
+ rc = test_lsm_config_str(pDb, pDb->db, 0, zCfg, &nThread);
1010
+ if( rc==LSM_OK ) rc = lsm_open(pDb->db, zFilename);
1011
+
1012
+ pDb->eMode = nThread;
1013
+ #ifdef LSM_MUTEX_PTHREADS
1014
+ if( rc==LSM_OK && nThread>1 ){
1015
+ testLsmStartWorkers(pDb, nThread, zFilename, zCfg);
1016
+ }
1017
+ #endif
1018
+
1019
+ if( rc!=LSM_OK ){
1020
+ test_lsm_close((TestDb *)pDb);
1021
+ pDb = 0;
1022
+ }
1023
+ }
1024
+
1025
+ *ppDb = (TestDb *)pDb;
1026
+ return rc;
1027
+ }
1028
+
1029
+ int test_lsm_open(
1030
+ const char *zSpec,
1031
+ const char *zFilename,
1032
+ int bClear,
1033
+ TestDb **ppDb
1034
+ ){
1035
+ return testLsmOpen(zSpec, zFilename, bClear, ppDb);
1036
+ }
1037
+
1038
+ int test_lsm_small_open(
1039
+ const char *zSpec,
1040
+ const char *zFile,
1041
+ int bClear,
1042
+ TestDb **ppDb
1043
+ ){
1044
+ const char *zCfg = "page_size=256 block_size=64 mmap=1024";
1045
+ return testLsmOpen(zCfg, zFile, bClear, ppDb);
1046
+ }
1047
+
1048
+ int test_lsm_lomem_open(
1049
+ const char *zSpec,
1050
+ const char *zFilename,
1051
+ int bClear,
1052
+ TestDb **ppDb
1053
+ ){
1054
+ /* "max_freelist=4 autocheckpoint=32" */
1055
+ const char *zCfg =
1056
+ "page_size=256 block_size=64 autoflush=16 "
1057
+ "autocheckpoint=32"
1058
+ "mmap=0 "
1059
+ ;
1060
+ return testLsmOpen(zCfg, zFilename, bClear, ppDb);
1061
+ }
1062
+
1063
+ int test_lsm_lomem2_open(
1064
+ const char *zSpec,
1065
+ const char *zFilename,
1066
+ int bClear,
1067
+ TestDb **ppDb
1068
+ ){
1069
+ /* "max_freelist=4 autocheckpoint=32" */
1070
+ const char *zCfg =
1071
+ "page_size=512 block_size=64 autoflush=0 mmap=0 "
1072
+ ;
1073
+ return testLsmOpen(zCfg, zFilename, bClear, ppDb);
1074
+ }
1075
+
1076
+ int test_lsm_zip_open(
1077
+ const char *zSpec,
1078
+ const char *zFilename,
1079
+ int bClear,
1080
+ TestDb **ppDb
1081
+ ){
1082
+ const char *zCfg =
1083
+ "page_size=256 block_size=64 autoflush=16 "
1084
+ "autocheckpoint=32 compression=1 mmap=0 "
1085
+ ;
1086
+ return testLsmOpen(zCfg, zFilename, bClear, ppDb);
1087
+ }
1088
+
1089
+ lsm_db *tdb_lsm(TestDb *pDb){
1090
+ if( pDb->pMethods->xClose==test_lsm_close ){
1091
+ return ((LsmDb *)pDb)->db;
1092
+ }
1093
+ return 0;
1094
+ }
1095
+
1096
+ int tdb_lsm_multithread(TestDb *pDb){
1097
+ int ret = 0;
1098
+ if( tdb_lsm(pDb) ){
1099
+ ret = ((LsmDb*)pDb)->eMode!=LSMTEST_MODE_SINGLETHREAD;
1100
+ }
1101
+ return ret;
1102
+ }
1103
+
1104
+ void tdb_lsm_enable_log(TestDb *pDb, int bEnable){
1105
+ lsm_db *db = tdb_lsm(pDb);
1106
+ if( db ){
1107
+ lsm_config_log(db, (bEnable ? xLog : 0), (void *)"client");
1108
+ }
1109
+ }
1110
+
1111
+ void tdb_lsm_application_crash(TestDb *pDb){
1112
+ if( tdb_lsm(pDb) ){
1113
+ LsmDb *p = (LsmDb *)pDb;
1114
+ p->bCrashed = 1;
1115
+ }
1116
+ }
1117
+
1118
+ void tdb_lsm_prepare_system_crash(TestDb *pDb){
1119
+ if( tdb_lsm(pDb) ){
1120
+ LsmDb *p = (LsmDb *)pDb;
1121
+ p->bPrepareCrash = 1;
1122
+ }
1123
+ }
1124
+
1125
+ void tdb_lsm_system_crash(TestDb *pDb){
1126
+ if( tdb_lsm(pDb) ){
1127
+ LsmDb *p = (LsmDb *)pDb;
1128
+ p->bCrashed = 1;
1129
+ doSystemCrash(p);
1130
+ }
1131
+ }
1132
+
1133
+ void tdb_lsm_safety(TestDb *pDb, int eMode){
1134
+ assert( eMode==LSM_SAFETY_OFF
1135
+ || eMode==LSM_SAFETY_NORMAL
1136
+ || eMode==LSM_SAFETY_FULL
1137
+ );
1138
+ if( tdb_lsm(pDb) ){
1139
+ int iParam = eMode;
1140
+ LsmDb *p = (LsmDb *)pDb;
1141
+ lsm_config(p->db, LSM_CONFIG_SAFETY, &iParam);
1142
+ }
1143
+ }
1144
+
1145
+ void tdb_lsm_prepare_sync_crash(TestDb *pDb, int iSync){
1146
+ assert( iSync>0 );
1147
+ if( tdb_lsm(pDb) ){
1148
+ LsmDb *p = (LsmDb *)pDb;
1149
+ p->nAutoCrash = iSync;
1150
+ p->bPrepareCrash = 1;
1151
+ }
1152
+ }
1153
+
1154
+ void tdb_lsm_config_work_hook(
1155
+ TestDb *pDb,
1156
+ void (*xWork)(lsm_db *, void *),
1157
+ void *pWorkCtx
1158
+ ){
1159
+ if( tdb_lsm(pDb) ){
1160
+ LsmDb *p = (LsmDb *)pDb;
1161
+ p->xWork = xWork;
1162
+ p->pWorkCtx = pWorkCtx;
1163
+ }
1164
+ }
1165
+
1166
+ void tdb_lsm_write_hook(
1167
+ TestDb *pDb,
1168
+ void (*xWrite)(void *, int, lsm_i64, int, int),
1169
+ void *pWriteCtx
1170
+ ){
1171
+ if( tdb_lsm(pDb) ){
1172
+ LsmDb *p = (LsmDb *)pDb;
1173
+ p->xWriteHook = xWrite;
1174
+ p->pWriteCtx = pWriteCtx;
1175
+ }
1176
+ }
1177
+
1178
+ int tdb_lsm_open(const char *zCfg, const char *zDb, int bClear, TestDb **ppDb){
1179
+ return testLsmOpen(zCfg, zDb, bClear, ppDb);
1180
+ }
1181
+
1182
+ #ifdef LSM_MUTEX_PTHREADS
1183
+
1184
+ /*
1185
+ ** Signal worker thread iWorker that there may be work to do.
1186
+ */
1187
+ static void mt_signal_worker(LsmDb *pDb, int iWorker){
1188
+ LsmWorker *p = &pDb->aWorker[iWorker];
1189
+ pthread_mutex_lock(&p->worker_mutex);
1190
+ p->bDoWork = 1;
1191
+ pthread_cond_signal(&p->worker_cond);
1192
+ pthread_mutex_unlock(&p->worker_mutex);
1193
+ }
1194
+
1195
+ /*
1196
+ ** This routine is used as the main() for all worker threads.
1197
+ */
1198
+ static void *worker_main(void *pArg){
1199
+ LsmWorker *p = (LsmWorker *)pArg;
1200
+ lsm_db *pWorker; /* Connection to access db through */
1201
+
1202
+ pthread_mutex_lock(&p->worker_mutex);
1203
+ while( (pWorker = p->pWorker) ){
1204
+ int rc = LSM_OK;
1205
+
1206
+ /* Do some work. If an error occurs, exit. */
1207
+
1208
+ pthread_mutex_unlock(&p->worker_mutex);
1209
+ if( p->eType==LSMTEST_THREAD_CKPT ){
1210
+ int nKB = 0;
1211
+ rc = lsm_info(pWorker, LSM_INFO_CHECKPOINT_SIZE, &nKB);
1212
+ if( rc==LSM_OK && nKB>=p->pDb->nMtMinCkpt ){
1213
+ rc = lsm_checkpoint(pWorker, 0);
1214
+ }
1215
+ }else{
1216
+ int nWrite;
1217
+ do {
1218
+
1219
+ if( p->eType==LSMTEST_THREAD_WORKER ){
1220
+ waitOnCheckpointer(p->pDb, pWorker);
1221
+ }
1222
+
1223
+ nWrite = 0;
1224
+ rc = lsm_work(pWorker, 0, 256, &nWrite);
1225
+
1226
+ if( p->eType==LSMTEST_THREAD_WORKER && nWrite ){
1227
+ mt_signal_worker(p->pDb, 1);
1228
+ }
1229
+ }while( nWrite && p->pWorker );
1230
+ }
1231
+ pthread_mutex_lock(&p->worker_mutex);
1232
+
1233
+ if( rc!=LSM_OK && rc!=LSM_BUSY ){
1234
+ p->worker_rc = rc;
1235
+ break;
1236
+ }
1237
+
1238
+ /* The thread will wake up when it is signaled either because another
1239
+ ** thread has created some work for this one or because the connection
1240
+ ** is being closed. */
1241
+ if( p->pWorker && p->bDoWork==0 ){
1242
+ pthread_cond_wait(&p->worker_cond, &p->worker_mutex);
1243
+ }
1244
+ p->bDoWork = 0;
1245
+ }
1246
+ pthread_mutex_unlock(&p->worker_mutex);
1247
+
1248
+ return 0;
1249
+ }
1250
+
1251
+
1252
+ static void mt_stop_worker(LsmDb *pDb, int iWorker){
1253
+ LsmWorker *p = &pDb->aWorker[iWorker];
1254
+ if( p->pWorker ){
1255
+ void *pDummy;
1256
+ lsm_db *pWorker;
1257
+
1258
+ /* Signal the worker to stop */
1259
+ pthread_mutex_lock(&p->worker_mutex);
1260
+ pWorker = p->pWorker;
1261
+ p->pWorker = 0;
1262
+ pthread_cond_signal(&p->worker_cond);
1263
+ pthread_mutex_unlock(&p->worker_mutex);
1264
+
1265
+ /* Join the worker thread. */
1266
+ pthread_join(p->worker_thread, &pDummy);
1267
+
1268
+ /* Free resources allocated in mt_start_worker() */
1269
+ pthread_cond_destroy(&p->worker_cond);
1270
+ pthread_mutex_destroy(&p->worker_mutex);
1271
+ lsm_close(pWorker);
1272
+ }
1273
+ }
1274
+
1275
+ static void mt_shutdown(LsmDb *pDb){
1276
+ int i;
1277
+ for(i=0; i<pDb->nWorker; i++){
1278
+ mt_stop_worker(pDb, i);
1279
+ }
1280
+ }
1281
+
1282
+ /*
1283
+ ** This callback is invoked by LSM when the client database writes to
1284
+ ** the database file (i.e. to flush the contents of the in-memory tree).
1285
+ ** This implies there may be work to do on the database, so signal
1286
+ ** the worker threads.
1287
+ */
1288
+ static void mt_client_work_hook(lsm_db *db, void *pArg){
1289
+ LsmDb *pDb = (LsmDb *)pArg; /* LsmDb database handle */
1290
+
1291
+ /* Invoke the user level work-hook, if any. */
1292
+ if( pDb->xWork ) pDb->xWork(db, pDb->pWorkCtx);
1293
+
1294
+ /* Wake up worker thread 0. */
1295
+ mt_signal_worker(pDb, 0);
1296
+ }
1297
+
1298
+ static void mt_worker_work_hook(lsm_db *db, void *pArg){
1299
+ LsmDb *pDb = (LsmDb *)pArg; /* LsmDb database handle */
1300
+
1301
+ /* Invoke the user level work-hook, if any. */
1302
+ if( pDb->xWork ) pDb->xWork(db, pDb->pWorkCtx);
1303
+ }
1304
+
1305
+ /*
1306
+ ** Launch worker thread iWorker for database connection pDb.
1307
+ */
1308
+ static int mt_start_worker(
1309
+ LsmDb *pDb, /* Main database structure */
1310
+ int iWorker, /* Worker number to start */
1311
+ const char *zFilename, /* File name of database to open */
1312
+ const char *zCfg, /* Connection configuration string */
1313
+ int eType /* Type of worker thread */
1314
+ ){
1315
+ int rc = 0; /* Return code */
1316
+ LsmWorker *p; /* Object to initialize */
1317
+
1318
+ assert( iWorker<pDb->nWorker );
1319
+ assert( eType==LSMTEST_THREAD_CKPT
1320
+ || eType==LSMTEST_THREAD_WORKER
1321
+ || eType==LSMTEST_THREAD_WORKER_AC
1322
+ );
1323
+
1324
+ p = &pDb->aWorker[iWorker];
1325
+ p->eType = eType;
1326
+ p->pDb = pDb;
1327
+
1328
+ /* Open the worker connection */
1329
+ if( rc==0 ) rc = lsm_new(&pDb->env, &p->pWorker);
1330
+ if( zCfg ){
1331
+ test_lsm_config_str(pDb, p->pWorker, 1, zCfg, 0);
1332
+ }
1333
+ if( rc==0 ) rc = lsm_open(p->pWorker, zFilename);
1334
+ lsm_config_log(p->pWorker, xLog, (void *)"worker");
1335
+
1336
+ /* Configure the work-hook */
1337
+ if( rc==0 ){
1338
+ lsm_config_work_hook(p->pWorker, mt_worker_work_hook, (void *)pDb);
1339
+ }
1340
+
1341
+ if( eType==LSMTEST_THREAD_WORKER ){
1342
+ test_lsm_config_str(0, p->pWorker, 1, "autocheckpoint=0", 0);
1343
+ }
1344
+
1345
+ /* Kick off the worker thread. */
1346
+ if( rc==0 ) rc = pthread_cond_init(&p->worker_cond, 0);
1347
+ if( rc==0 ) rc = pthread_mutex_init(&p->worker_mutex, 0);
1348
+ if( rc==0 ) rc = pthread_create(&p->worker_thread, 0, worker_main, (void *)p);
1349
+
1350
+ return rc;
1351
+ }
1352
+
1353
+
1354
+ static int testLsmStartWorkers(
1355
+ LsmDb *pDb, int eModel, const char *zFilename, const char *zCfg
1356
+ ){
1357
+ int rc;
1358
+
1359
+ if( eModel<1 || eModel>4 ) return 1;
1360
+ if( eModel==1 ) return 0;
1361
+
1362
+ /* Configure a work-hook for the client connection. Worker 0 is signalled
1363
+ ** every time the users connection writes to the database. */
1364
+ lsm_config_work_hook(pDb->db, mt_client_work_hook, (void *)pDb);
1365
+
1366
+ /* Allocate space for two worker connections. They may not both be
1367
+ ** used, but both are allocated. */
1368
+ pDb->aWorker = (LsmWorker *)testMalloc(sizeof(LsmWorker) * 2);
1369
+ memset(pDb->aWorker, 0, sizeof(LsmWorker) * 2);
1370
+
1371
+ switch( eModel ){
1372
+ case LSMTEST_MODE_BACKGROUND_CKPT:
1373
+ pDb->nWorker = 1;
1374
+ test_lsm_config_str(0, pDb->db, 0, "autocheckpoint=0", 0);
1375
+ rc = mt_start_worker(pDb, 0, zFilename, zCfg, LSMTEST_THREAD_CKPT);
1376
+ break;
1377
+
1378
+ case LSMTEST_MODE_BACKGROUND_WORK:
1379
+ pDb->nWorker = 1;
1380
+ test_lsm_config_str(0, pDb->db, 0, "autowork=0", 0);
1381
+ rc = mt_start_worker(pDb, 0, zFilename, zCfg, LSMTEST_THREAD_WORKER_AC);
1382
+ break;
1383
+
1384
+ case LSMTEST_MODE_BACKGROUND_BOTH:
1385
+ pDb->nWorker = 2;
1386
+ test_lsm_config_str(0, pDb->db, 0, "autowork=0", 0);
1387
+ rc = mt_start_worker(pDb, 0, zFilename, zCfg, LSMTEST_THREAD_WORKER);
1388
+ if( rc==0 ){
1389
+ rc = mt_start_worker(pDb, 1, zFilename, zCfg, LSMTEST_THREAD_CKPT);
1390
+ }
1391
+ break;
1392
+ }
1393
+
1394
+ return rc;
1395
+ }
1396
+
1397
+
1398
+ int test_lsm_mt2(
1399
+ const char *zSpec,
1400
+ const char *zFilename,
1401
+ int bClear,
1402
+ TestDb **ppDb
1403
+ ){
1404
+ const char *zCfg = "mt_mode=2";
1405
+ return testLsmOpen(zCfg, zFilename, bClear, ppDb);
1406
+ }
1407
+
1408
+ int test_lsm_mt3(
1409
+ const char *zSpec,
1410
+ const char *zFilename,
1411
+ int bClear,
1412
+ TestDb **ppDb
1413
+ ){
1414
+ const char *zCfg = "mt_mode=4";
1415
+ return testLsmOpen(zCfg, zFilename, bClear, ppDb);
1416
+ }
1417
+
1418
+ #else
1419
+ static void mt_shutdown(LsmDb *pDb) {
1420
+ unused_parameter(pDb);
1421
+ }
1422
+ int test_lsm_mt(const char *zFilename, int bClear, TestDb **ppDb){
1423
+ unused_parameter(zFilename);
1424
+ unused_parameter(bClear);
1425
+ unused_parameter(ppDb);
1426
+ testPrintError("threads unavailable - recompile with LSM_MUTEX_PTHREADS\n");
1427
+ return 1;
1428
+ }
1429
+ #endif
local-test-sqlite3-delta-02/afc-sqlite3/ext/lsm1/lsm-test/lsmtest_tdb4.c ADDED
@@ -0,0 +1,980 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /*
3
+ ** This file contains the TestDb bt wrapper.
4
+ */
5
+
6
+ #include "lsmtest_tdb.h"
7
+ #include "lsmtest.h"
8
+ #include <unistd.h>
9
+ #include "bt.h"
10
+
11
+ #include <pthread.h>
12
+
13
+ typedef struct BtDb BtDb;
14
+ typedef struct BtFile BtFile;
15
+
16
+ /* Background checkpointer interface (see implementations below). */
17
+ typedef struct bt_ckpter bt_ckpter;
18
+ static int bgc_attach(BtDb *pDb, const char*);
19
+ static int bgc_detach(BtDb *pDb);
20
+
21
+ /*
22
+ ** Each database or log file opened by a database handle is wrapped by
23
+ ** an object of the following type.
24
+ */
25
+ struct BtFile {
26
+ BtDb *pBt; /* Database handle that opened this file */
27
+ bt_env *pVfs; /* Underlying VFS */
28
+ bt_file *pFile; /* File handle belonging to underlying VFS */
29
+ int nSectorSize; /* Size of sectors in bytes */
30
+ int nSector; /* Allocated size of nSector array */
31
+ u8 **apSector; /* Original sector data */
32
+ };
33
+
34
+ /*
35
+ ** nCrashSync:
36
+ ** If this value is non-zero, then a "crash-test" is running. If
37
+ ** nCrashSync==1, then the crash is simulated during the very next
38
+ ** call to the xSync() VFS method (on either the db or log file).
39
+ ** If nCrashSync==2, the following call to xSync(), and so on.
40
+ **
41
+ ** bCrash:
42
+ ** After a crash is simulated, this variable is set. Any subsequent
43
+ ** attempts to write to a file or modify the file system in any way
44
+ ** fail once this is set. All the caller can do is close the connection.
45
+ **
46
+ ** bFastInsert:
47
+ ** If this variable is set to true, then a BT_CONTROL_FAST_INSERT_OP
48
+ ** control is issued before each callto BtReplace() or BtCsrOpen().
49
+ */
50
+ struct BtDb {
51
+ TestDb base; /* Base class */
52
+ bt_db *pBt; /* bt database handle */
53
+ sqlite4_env *pEnv; /* SQLite environment (for malloc/free) */
54
+ bt_env *pVfs; /* Underlying VFS */
55
+ int bFastInsert; /* True to use fast-insert */
56
+
57
+ /* Space for bt_fetch() results */
58
+ u8 *aBuffer; /* Space to store results */
59
+ int nBuffer; /* Allocated size of aBuffer[] in bytes */
60
+ int nRef;
61
+
62
+ /* Background checkpointer used by mt connections */
63
+ bt_ckpter *pCkpter;
64
+
65
+ /* Stuff used for crash test simulation */
66
+ BtFile *apFile[2]; /* Database and log files used by pBt */
67
+ bt_env env; /* Private VFS for this object */
68
+ int nCrashSync; /* Number of syncs until crash (see above) */
69
+ int bCrash; /* True once a crash has been simulated */
70
+ };
71
+
72
+ static int btVfsFullpath(
73
+ sqlite4_env *pEnv,
74
+ bt_env *pVfs,
75
+ const char *z,
76
+ char **pzOut
77
+ ){
78
+ BtDb *pBt = (BtDb*)pVfs->pVfsCtx;
79
+ if( pBt->bCrash ) return SQLITE4_IOERR;
80
+ return pBt->pVfs->xFullpath(pEnv, pBt->pVfs, z, pzOut);
81
+ }
82
+
83
+ static int btVfsOpen(
84
+ sqlite4_env *pEnv,
85
+ bt_env *pVfs,
86
+ const char *zFile,
87
+ int flags, bt_file **ppFile
88
+ ){
89
+ BtFile *p;
90
+ BtDb *pBt = (BtDb*)pVfs->pVfsCtx;
91
+ int rc;
92
+
93
+ if( pBt->bCrash ) return SQLITE4_IOERR;
94
+
95
+ p = (BtFile*)testMalloc(sizeof(BtFile));
96
+ if( !p ) return SQLITE4_NOMEM;
97
+ if( flags & BT_OPEN_DATABASE ){
98
+ pBt->apFile[0] = p;
99
+ }else if( flags & BT_OPEN_LOG ){
100
+ pBt->apFile[1] = p;
101
+ }
102
+ if( (flags & BT_OPEN_SHARED)==0 ){
103
+ p->pBt = pBt;
104
+ }
105
+ p->pVfs = pBt->pVfs;
106
+
107
+ rc = pBt->pVfs->xOpen(pEnv, pVfs, zFile, flags, &p->pFile);
108
+ if( rc!=SQLITE4_OK ){
109
+ testFree(p);
110
+ p = 0;
111
+ }else{
112
+ pBt->nRef++;
113
+ }
114
+
115
+ *ppFile = (bt_file*)p;
116
+ return rc;
117
+ }
118
+
119
+ static int btVfsSize(bt_file *pFile, sqlite4_int64 *piRes){
120
+ BtFile *p = (BtFile*)pFile;
121
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
122
+ return p->pVfs->xSize(p->pFile, piRes);
123
+ }
124
+
125
+ static int btVfsRead(bt_file *pFile, sqlite4_int64 iOff, void *pBuf, int nBuf){
126
+ BtFile *p = (BtFile*)pFile;
127
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
128
+ return p->pVfs->xRead(p->pFile, iOff, pBuf, nBuf);
129
+ }
130
+
131
+ static int btFlushSectors(BtFile *p, int iFile){
132
+ sqlite4_int64 iSz;
133
+ int rc;
134
+ int i;
135
+ u8 *aTmp = 0;
136
+
137
+ rc = p->pBt->pVfs->xSize(p->pFile, &iSz);
138
+ for(i=0; rc==SQLITE4_OK && i<p->nSector; i++){
139
+ if( p->pBt->bCrash && p->apSector[i] ){
140
+
141
+ /* The system is simulating a crash. There are three choices for
142
+ ** this sector:
143
+ **
144
+ ** 1) Leave it as it is (simulating a successful write),
145
+ ** 2) Restore the original data (simulating a lost write),
146
+ ** 3) Populate the disk sector with garbage data.
147
+ */
148
+ sqlite4_int64 iSOff = p->nSectorSize*i;
149
+ int nWrite = MIN(p->nSectorSize, iSz - iSOff);
150
+
151
+ if( nWrite ){
152
+ u8 *aWrite = 0;
153
+ int iOpt = (testPrngValue(i) % 3) + 1;
154
+ if( iOpt==1 ){
155
+ aWrite = p->apSector[i];
156
+ }else if( iOpt==3 ){
157
+ if( aTmp==0 ) aTmp = testMalloc(p->nSectorSize);
158
+ aWrite = aTmp;
159
+ testPrngArray(i*13, (u32*)aWrite, nWrite/sizeof(u32));
160
+ }
161
+
162
+ #if 0
163
+ fprintf(stderr, "handle sector %d of %s with %s\n", i,
164
+ iFile==0 ? "db" : "log",
165
+ iOpt==1 ? "rollback" : iOpt==2 ? "write" : "omit"
166
+ );
167
+ fflush(stderr);
168
+ #endif
169
+
170
+ if( aWrite ){
171
+ rc = p->pBt->pVfs->xWrite(p->pFile, iSOff, aWrite, nWrite);
172
+ }
173
+ }
174
+ }
175
+ testFree(p->apSector[i]);
176
+ p->apSector[i] = 0;
177
+ }
178
+
179
+ testFree(aTmp);
180
+ return rc;
181
+ }
182
+
183
+ static int btSaveSectors(BtFile *p, sqlite4_int64 iOff, int nBuf){
184
+ int rc;
185
+ sqlite4_int64 iSz; /* Size of file on disk */
186
+ int iFirst; /* First sector affected */
187
+ int iSector; /* Current sector */
188
+ int iLast; /* Last sector affected */
189
+
190
+ if( p->nSectorSize==0 ){
191
+ p->nSectorSize = p->pBt->pVfs->xSectorSize(p->pFile);
192
+ if( p->nSectorSize<512 ) p->nSectorSize = 512;
193
+ }
194
+ iLast = (iOff+nBuf-1) / p->nSectorSize;
195
+ iFirst = iOff / p->nSectorSize;
196
+
197
+ rc = p->pBt->pVfs->xSize(p->pFile, &iSz);
198
+ for(iSector=iFirst; rc==SQLITE4_OK && iSector<=iLast; iSector++){
199
+ int nRead;
200
+ sqlite4_int64 iSOff = iSector * p->nSectorSize;
201
+ u8 *aBuf = testMalloc(p->nSectorSize);
202
+ nRead = MIN(p->nSectorSize, (iSz - iSOff));
203
+ if( nRead>0 ){
204
+ rc = p->pBt->pVfs->xRead(p->pFile, iSOff, aBuf, nRead);
205
+ }
206
+
207
+ while( rc==SQLITE4_OK && iSector>=p->nSector ){
208
+ int nNew = p->nSector + 32;
209
+ u8 **apNew = (u8**)testMalloc(nNew * sizeof(u8*));
210
+ memcpy(apNew, p->apSector, p->nSector*sizeof(u8*));
211
+ testFree(p->apSector);
212
+ p->apSector = apNew;
213
+ p->nSector = nNew;
214
+ }
215
+
216
+ p->apSector[iSector] = aBuf;
217
+ }
218
+
219
+ return rc;
220
+ }
221
+
222
+ static int btVfsWrite(bt_file *pFile, sqlite4_int64 iOff, void *pBuf, int nBuf){
223
+ BtFile *p = (BtFile*)pFile;
224
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
225
+ if( p->pBt && p->pBt->nCrashSync ){
226
+ btSaveSectors(p, iOff, nBuf);
227
+ }
228
+ return p->pVfs->xWrite(p->pFile, iOff, pBuf, nBuf);
229
+ }
230
+
231
+ static int btVfsTruncate(bt_file *pFile, sqlite4_int64 iOff){
232
+ BtFile *p = (BtFile*)pFile;
233
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
234
+ return p->pVfs->xTruncate(p->pFile, iOff);
235
+ }
236
+
237
+ static int btVfsSync(bt_file *pFile){
238
+ int rc = SQLITE4_OK;
239
+ BtFile *p = (BtFile*)pFile;
240
+ BtDb *pBt = p->pBt;
241
+
242
+ if( pBt ){
243
+ if( pBt->bCrash ) return SQLITE4_IOERR;
244
+ if( pBt->nCrashSync ){
245
+ pBt->nCrashSync--;
246
+ pBt->bCrash = (pBt->nCrashSync==0);
247
+ if( pBt->bCrash ){
248
+ btFlushSectors(pBt->apFile[0], 0);
249
+ btFlushSectors(pBt->apFile[1], 1);
250
+ rc = SQLITE4_IOERR;
251
+ }else{
252
+ btFlushSectors(p, 0);
253
+ }
254
+ }
255
+ }
256
+
257
+ if( rc==SQLITE4_OK ){
258
+ rc = p->pVfs->xSync(p->pFile);
259
+ }
260
+ return rc;
261
+ }
262
+
263
+ static int btVfsSectorSize(bt_file *pFile){
264
+ BtFile *p = (BtFile*)pFile;
265
+ return p->pVfs->xSectorSize(p->pFile);
266
+ }
267
+
268
+ static void btDeref(BtDb *p){
269
+ p->nRef--;
270
+ assert( p->nRef>=0 );
271
+ if( p->nRef<=0 ) testFree(p);
272
+ }
273
+
274
+ static int btVfsClose(bt_file *pFile){
275
+ BtFile *p = (BtFile*)pFile;
276
+ BtDb *pBt = p->pBt;
277
+ int rc;
278
+ if( pBt ){
279
+ btFlushSectors(p, 0);
280
+ if( p==pBt->apFile[0] ) pBt->apFile[0] = 0;
281
+ if( p==pBt->apFile[1] ) pBt->apFile[1] = 0;
282
+ }
283
+ testFree(p->apSector);
284
+ rc = p->pVfs->xClose(p->pFile);
285
+ #if 0
286
+ btDeref(p->pBt);
287
+ #endif
288
+ testFree(p);
289
+ return rc;
290
+ }
291
+
292
+ static int btVfsUnlink(sqlite4_env *pEnv, bt_env *pVfs, const char *zFile){
293
+ BtDb *pBt = (BtDb*)pVfs->pVfsCtx;
294
+ if( pBt->bCrash ) return SQLITE4_IOERR;
295
+ return pBt->pVfs->xUnlink(pEnv, pBt->pVfs, zFile);
296
+ }
297
+
298
+ static int btVfsLock(bt_file *pFile, int iLock, int eType){
299
+ BtFile *p = (BtFile*)pFile;
300
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
301
+ return p->pVfs->xLock(p->pFile, iLock, eType);
302
+ }
303
+
304
+ static int btVfsTestLock(bt_file *pFile, int iLock, int nLock, int eType){
305
+ BtFile *p = (BtFile*)pFile;
306
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
307
+ return p->pVfs->xTestLock(p->pFile, iLock, nLock, eType);
308
+ }
309
+
310
+ static int btVfsShmMap(bt_file *pFile, int iChunk, int sz, void **ppOut){
311
+ BtFile *p = (BtFile*)pFile;
312
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
313
+ return p->pVfs->xShmMap(p->pFile, iChunk, sz, ppOut);
314
+ }
315
+
316
+ static void btVfsShmBarrier(bt_file *pFile){
317
+ BtFile *p = (BtFile*)pFile;
318
+ return p->pVfs->xShmBarrier(p->pFile);
319
+ }
320
+
321
+ static int btVfsShmUnmap(bt_file *pFile, int bDelete){
322
+ BtFile *p = (BtFile*)pFile;
323
+ if( p->pBt && p->pBt->bCrash ) return SQLITE4_IOERR;
324
+ return p->pVfs->xShmUnmap(p->pFile, bDelete);
325
+ }
326
+
327
+ static int bt_close(TestDb *pTestDb){
328
+ BtDb *p = (BtDb*)pTestDb;
329
+ int rc = sqlite4BtClose(p->pBt);
330
+ free(p->aBuffer);
331
+ if( p->apFile[0] ) p->apFile[0]->pBt = 0;
332
+ if( p->apFile[1] ) p->apFile[1]->pBt = 0;
333
+ bgc_detach(p);
334
+ testFree(p);
335
+ return rc;
336
+ }
337
+
338
+ static int btMinTransaction(BtDb *p, int iMin, int *piLevel){
339
+ int iLevel;
340
+ int rc = SQLITE4_OK;
341
+
342
+ iLevel = sqlite4BtTransactionLevel(p->pBt);
343
+ if( iLevel<iMin ){
344
+ rc = sqlite4BtBegin(p->pBt, iMin);
345
+ *piLevel = iLevel;
346
+ }else{
347
+ *piLevel = -1;
348
+ }
349
+
350
+ return rc;
351
+ }
352
+ static int btRestoreTransaction(BtDb *p, int iLevel, int rcin){
353
+ int rc = rcin;
354
+ if( iLevel>=0 ){
355
+ if( rc==SQLITE4_OK ){
356
+ rc = sqlite4BtCommit(p->pBt, iLevel);
357
+ }else{
358
+ sqlite4BtRollback(p->pBt, iLevel);
359
+ }
360
+ assert( iLevel==sqlite4BtTransactionLevel(p->pBt) );
361
+ }
362
+ return rc;
363
+ }
364
+
365
+ static int bt_write(TestDb *pTestDb, void *pK, int nK, void *pV, int nV){
366
+ BtDb *p = (BtDb*)pTestDb;
367
+ int iLevel;
368
+ int rc;
369
+
370
+ rc = btMinTransaction(p, 2, &iLevel);
371
+ if( rc==SQLITE4_OK ){
372
+ if( p->bFastInsert ) sqlite4BtControl(p->pBt, BT_CONTROL_FAST_INSERT_OP, 0);
373
+ rc = sqlite4BtReplace(p->pBt, pK, nK, pV, nV);
374
+ rc = btRestoreTransaction(p, iLevel, rc);
375
+ }
376
+ return rc;
377
+ }
378
+
379
+ static int bt_delete(TestDb *pTestDb, void *pK, int nK){
380
+ return bt_write(pTestDb, pK, nK, 0, -1);
381
+ }
382
+
383
+ static int bt_delete_range(
384
+ TestDb *pTestDb,
385
+ void *pKey1, int nKey1,
386
+ void *pKey2, int nKey2
387
+ ){
388
+ BtDb *p = (BtDb*)pTestDb;
389
+ bt_cursor *pCsr = 0;
390
+ int rc = SQLITE4_OK;
391
+ int iLevel;
392
+
393
+ rc = btMinTransaction(p, 2, &iLevel);
394
+ if( rc==SQLITE4_OK ){
395
+ if( p->bFastInsert ) sqlite4BtControl(p->pBt, BT_CONTROL_FAST_INSERT_OP, 0);
396
+ rc = sqlite4BtCsrOpen(p->pBt, 0, &pCsr);
397
+ }
398
+ while( rc==SQLITE4_OK ){
399
+ const void *pK;
400
+ int n;
401
+ int nCmp;
402
+ int res;
403
+
404
+ rc = sqlite4BtCsrSeek(pCsr, pKey1, nKey1, BT_SEEK_GE);
405
+ if( rc==SQLITE4_INEXACT ) rc = SQLITE4_OK;
406
+ if( rc!=SQLITE4_OK ) break;
407
+
408
+ rc = sqlite4BtCsrKey(pCsr, &pK, &n);
409
+ if( rc!=SQLITE4_OK ) break;
410
+
411
+ nCmp = MIN(n, nKey1);
412
+ res = memcmp(pKey1, pK, nCmp);
413
+ assert( res<0 || (res==0 && nKey1<=n) );
414
+ if( res==0 && nKey1==n ){
415
+ rc = sqlite4BtCsrNext(pCsr);
416
+ if( rc!=SQLITE4_OK ) break;
417
+ rc = sqlite4BtCsrKey(pCsr, &pK, &n);
418
+ if( rc!=SQLITE4_OK ) break;
419
+ }
420
+
421
+ nCmp = MIN(n, nKey2);
422
+ res = memcmp(pKey2, pK, nCmp);
423
+ if( res<0 || (res==0 && nKey2<=n) ) break;
424
+
425
+ rc = sqlite4BtDelete(pCsr);
426
+ }
427
+ if( rc==SQLITE4_NOTFOUND ) rc = SQLITE4_OK;
428
+
429
+ sqlite4BtCsrClose(pCsr);
430
+
431
+ rc = btRestoreTransaction(p, iLevel, rc);
432
+ return rc;
433
+ }
434
+
435
+ static int bt_fetch(
436
+ TestDb *pTestDb,
437
+ void *pK, int nK,
438
+ void **ppVal, int *pnVal
439
+ ){
440
+ BtDb *p = (BtDb*)pTestDb;
441
+ bt_cursor *pCsr = 0;
442
+ int iLevel;
443
+ int rc = SQLITE4_OK;
444
+
445
+ iLevel = sqlite4BtTransactionLevel(p->pBt);
446
+ if( iLevel==0 ){
447
+ rc = sqlite4BtBegin(p->pBt, 1);
448
+ if( rc!=SQLITE4_OK ) return rc;
449
+ }
450
+
451
+ if( p->bFastInsert ) sqlite4BtControl(p->pBt, BT_CONTROL_FAST_INSERT_OP, 0);
452
+ rc = sqlite4BtCsrOpen(p->pBt, 0, &pCsr);
453
+ if( rc==SQLITE4_OK ){
454
+ rc = sqlite4BtCsrSeek(pCsr, pK, nK, BT_SEEK_EQ);
455
+ if( rc==SQLITE4_OK ){
456
+ const void *pV = 0;
457
+ int nV = 0;
458
+ rc = sqlite4BtCsrData(pCsr, 0, -1, &pV, &nV);
459
+ if( rc==SQLITE4_OK ){
460
+ if( nV>p->nBuffer ){
461
+ free(p->aBuffer);
462
+ p->aBuffer = (u8*)malloc(nV*2);
463
+ p->nBuffer = nV*2;
464
+ }
465
+ memcpy(p->aBuffer, pV, nV);
466
+ *pnVal = nV;
467
+ *ppVal = (void*)(p->aBuffer);
468
+ }
469
+
470
+ }else if( rc==SQLITE4_INEXACT || rc==SQLITE4_NOTFOUND ){
471
+ *ppVal = 0;
472
+ *pnVal = -1;
473
+ rc = SQLITE4_OK;
474
+ }
475
+ sqlite4BtCsrClose(pCsr);
476
+ }
477
+
478
+ if( iLevel==0 ) sqlite4BtCommit(p->pBt, 0);
479
+ return rc;
480
+ }
481
+
482
+ static int bt_scan(
483
+ TestDb *pTestDb,
484
+ void *pCtx,
485
+ int bReverse,
486
+ void *pFirst, int nFirst,
487
+ void *pLast, int nLast,
488
+ void (*xCallback)(void *, void *, int , void *, int)
489
+ ){
490
+ BtDb *p = (BtDb*)pTestDb;
491
+ bt_cursor *pCsr = 0;
492
+ int rc;
493
+ int iLevel;
494
+
495
+ rc = btMinTransaction(p, 1, &iLevel);
496
+
497
+ if( rc==SQLITE4_OK ){
498
+ if( p->bFastInsert ) sqlite4BtControl(p->pBt, BT_CONTROL_FAST_INSERT_OP, 0);
499
+ rc = sqlite4BtCsrOpen(p->pBt, 0, &pCsr);
500
+ }
501
+ if( rc==SQLITE4_OK ){
502
+ if( bReverse ){
503
+ if( pLast ){
504
+ rc = sqlite4BtCsrSeek(pCsr, pLast, nLast, BT_SEEK_LE);
505
+ }else{
506
+ rc = sqlite4BtCsrLast(pCsr);
507
+ }
508
+ }else{
509
+ rc = sqlite4BtCsrSeek(pCsr, pFirst, nFirst, BT_SEEK_GE);
510
+ }
511
+ if( rc==SQLITE4_INEXACT ) rc = SQLITE4_OK;
512
+
513
+ while( rc==SQLITE4_OK ){
514
+ const void *pK = 0; int nK = 0;
515
+ const void *pV = 0; int nV = 0;
516
+
517
+ rc = sqlite4BtCsrKey(pCsr, &pK, &nK);
518
+ if( rc==SQLITE4_OK ){
519
+ rc = sqlite4BtCsrData(pCsr, 0, -1, &pV, &nV);
520
+ }
521
+
522
+ if( rc!=SQLITE4_OK ) break;
523
+ if( bReverse ){
524
+ if( pFirst ){
525
+ int res;
526
+ int nCmp = MIN(nK, nFirst);
527
+ res = memcmp(pFirst, pK, nCmp);
528
+ if( res>0 || (res==0 && nK<nFirst) ) break;
529
+ }
530
+ }else{
531
+ if( pLast ){
532
+ int res;
533
+ int nCmp = MIN(nK, nLast);
534
+ res = memcmp(pLast, pK, nCmp);
535
+ if( res<0 || (res==0 && nK>nLast) ) break;
536
+ }
537
+ }
538
+
539
+ xCallback(pCtx, (void*)pK, nK, (void*)pV, nV);
540
+ if( bReverse ){
541
+ rc = sqlite4BtCsrPrev(pCsr);
542
+ }else{
543
+ rc = sqlite4BtCsrNext(pCsr);
544
+ }
545
+ }
546
+ if( rc==SQLITE4_NOTFOUND ) rc = SQLITE4_OK;
547
+
548
+ sqlite4BtCsrClose(pCsr);
549
+ }
550
+
551
+ rc = btRestoreTransaction(p, iLevel, rc);
552
+ return rc;
553
+ }
554
+
555
+ static int bt_begin(TestDb *pTestDb, int iLvl){
556
+ BtDb *p = (BtDb*)pTestDb;
557
+ int rc = sqlite4BtBegin(p->pBt, iLvl);
558
+ return rc;
559
+ }
560
+
561
+ static int bt_commit(TestDb *pTestDb, int iLvl){
562
+ BtDb *p = (BtDb*)pTestDb;
563
+ int rc = sqlite4BtCommit(p->pBt, iLvl);
564
+ return rc;
565
+ }
566
+
567
+ static int bt_rollback(TestDb *pTestDb, int iLvl){
568
+ BtDb *p = (BtDb*)pTestDb;
569
+ int rc = sqlite4BtRollback(p->pBt, iLvl);
570
+ return rc;
571
+ }
572
+
573
+ static int testParseOption(
574
+ const char **pzIn, /* IN/OUT: pointer to next option */
575
+ const char **pzOpt, /* OUT: nul-terminated option name */
576
+ const char **pzArg, /* OUT: nul-terminated option argument */
577
+ char *pSpace /* Temporary space for output params */
578
+ ){
579
+ const char *p = *pzIn;
580
+ const char *pStart;
581
+ int n;
582
+
583
+ char *pOut = pSpace;
584
+
585
+ while( *p==' ' ) p++;
586
+ pStart = p;
587
+ while( *p && *p!='=' ) p++;
588
+ if( *p==0 ) return 1;
589
+
590
+ n = (p - pStart);
591
+ memcpy(pOut, pStart, n);
592
+ *pzOpt = pOut;
593
+ pOut += n;
594
+ *pOut++ = '\0';
595
+
596
+ p++;
597
+ pStart = p;
598
+ while( *p && *p!=' ' ) p++;
599
+ n = (p - pStart);
600
+
601
+ memcpy(pOut, pStart, n);
602
+ *pzArg = pOut;
603
+ pOut += n;
604
+ *pOut++ = '\0';
605
+
606
+ *pzIn = p;
607
+ return 0;
608
+ }
609
+
610
+ static int testParseInt(const char *z, int *piVal){
611
+ int i = 0;
612
+ const char *p = z;
613
+
614
+ while( *p>='0' && *p<='9' ){
615
+ i = i*10 + (*p - '0');
616
+ p++;
617
+ }
618
+ if( *p=='K' || *p=='k' ){
619
+ i = i * 1024;
620
+ p++;
621
+ }else if( *p=='M' || *p=='m' ){
622
+ i = i * 1024 * 1024;
623
+ p++;
624
+ }
625
+
626
+ if( *p ) return SQLITE4_ERROR;
627
+ *piVal = i;
628
+ return SQLITE4_OK;
629
+ }
630
+
631
+ static int testBtConfigure(BtDb *pDb, const char *zCfg, int *pbMt){
632
+ int rc = SQLITE4_OK;
633
+
634
+ if( zCfg ){
635
+ struct CfgParam {
636
+ const char *zParam;
637
+ int eParam;
638
+ } aParam[] = {
639
+ { "safety", BT_CONTROL_SAFETY },
640
+ { "autockpt", BT_CONTROL_AUTOCKPT },
641
+ { "multiproc", BT_CONTROL_MULTIPROC },
642
+ { "blksz", BT_CONTROL_BLKSZ },
643
+ { "pagesz", BT_CONTROL_PAGESZ },
644
+ { "mt", -1 },
645
+ { "fastinsert", -2 },
646
+ { 0, 0 }
647
+ };
648
+ const char *z = zCfg;
649
+ int n = strlen(z);
650
+ char *aSpace;
651
+ const char *zOpt;
652
+ const char *zArg;
653
+
654
+ aSpace = (char*)testMalloc(n+2);
655
+ while( rc==SQLITE4_OK && 0==testParseOption(&z, &zOpt, &zArg, aSpace) ){
656
+ int i;
657
+ int iVal;
658
+ rc = testArgSelect(aParam, "param", zOpt, &i);
659
+ if( rc!=SQLITE4_OK ) break;
660
+
661
+ rc = testParseInt(zArg, &iVal);
662
+ if( rc!=SQLITE4_OK ) break;
663
+
664
+ switch( aParam[i].eParam ){
665
+ case -1:
666
+ *pbMt = iVal;
667
+ break;
668
+ case -2:
669
+ pDb->bFastInsert = 1;
670
+ break;
671
+ default:
672
+ rc = sqlite4BtControl(pDb->pBt, aParam[i].eParam, (void*)&iVal);
673
+ break;
674
+ }
675
+ }
676
+ testFree(aSpace);
677
+ }
678
+
679
+ return rc;
680
+ }
681
+
682
+
683
+ int test_bt_open(
684
+ const char *zSpec,
685
+ const char *zFilename,
686
+ int bClear,
687
+ TestDb **ppDb
688
+ ){
689
+
690
+ static const DatabaseMethods SqlMethods = {
691
+ bt_close,
692
+ bt_write,
693
+ bt_delete,
694
+ bt_delete_range,
695
+ bt_fetch,
696
+ bt_scan,
697
+ bt_begin,
698
+ bt_commit,
699
+ bt_rollback
700
+ };
701
+ BtDb *p = 0;
702
+ bt_db *pBt = 0;
703
+ int rc;
704
+ sqlite4_env *pEnv = sqlite4_env_default();
705
+
706
+ if( bClear && zFilename && zFilename[0] ){
707
+ char *zLog = sqlite3_mprintf("%s-wal", zFilename);
708
+ unlink(zFilename);
709
+ unlink(zLog);
710
+ sqlite3_free(zLog);
711
+ }
712
+
713
+ rc = sqlite4BtNew(pEnv, 0, &pBt);
714
+ if( rc==SQLITE4_OK ){
715
+ int mt = 0; /* True for multi-threaded connection */
716
+
717
+ p = (BtDb*)testMalloc(sizeof(BtDb));
718
+ p->base.pMethods = &SqlMethods;
719
+ p->pBt = pBt;
720
+ p->pEnv = pEnv;
721
+ p->nRef = 1;
722
+
723
+ p->env.pVfsCtx = (void*)p;
724
+ p->env.xFullpath = btVfsFullpath;
725
+ p->env.xOpen = btVfsOpen;
726
+ p->env.xSize = btVfsSize;
727
+ p->env.xRead = btVfsRead;
728
+ p->env.xWrite = btVfsWrite;
729
+ p->env.xTruncate = btVfsTruncate;
730
+ p->env.xSync = btVfsSync;
731
+ p->env.xSectorSize = btVfsSectorSize;
732
+ p->env.xClose = btVfsClose;
733
+ p->env.xUnlink = btVfsUnlink;
734
+ p->env.xLock = btVfsLock;
735
+ p->env.xTestLock = btVfsTestLock;
736
+ p->env.xShmMap = btVfsShmMap;
737
+ p->env.xShmBarrier = btVfsShmBarrier;
738
+ p->env.xShmUnmap = btVfsShmUnmap;
739
+
740
+ sqlite4BtControl(pBt, BT_CONTROL_GETVFS, (void*)&p->pVfs);
741
+ sqlite4BtControl(pBt, BT_CONTROL_SETVFS, (void*)&p->env);
742
+
743
+ rc = testBtConfigure(p, zSpec, &mt);
744
+ if( rc==SQLITE4_OK ){
745
+ rc = sqlite4BtOpen(pBt, zFilename);
746
+ }
747
+
748
+ if( rc==SQLITE4_OK && mt ){
749
+ int nAuto = 0;
750
+ rc = bgc_attach(p, zSpec);
751
+ sqlite4BtControl(pBt, BT_CONTROL_AUTOCKPT, (void*)&nAuto);
752
+ }
753
+ }
754
+
755
+ if( rc!=SQLITE4_OK && p ){
756
+ bt_close(&p->base);
757
+ }
758
+
759
+ *ppDb = &p->base;
760
+ return rc;
761
+ }
762
+
763
+ int test_fbt_open(
764
+ const char *zSpec,
765
+ const char *zFilename,
766
+ int bClear,
767
+ TestDb **ppDb
768
+ ){
769
+ return test_bt_open("fast=1", zFilename, bClear, ppDb);
770
+ }
771
+
772
+ int test_fbts_open(
773
+ const char *zSpec,
774
+ const char *zFilename,
775
+ int bClear,
776
+ TestDb **ppDb
777
+ ){
778
+ return test_bt_open("fast=1 blksz=32K pagesz=512", zFilename, bClear, ppDb);
779
+ }
780
+
781
+
782
+ void tdb_bt_prepare_sync_crash(TestDb *pTestDb, int iSync){
783
+ BtDb *p = (BtDb*)pTestDb;
784
+ assert( pTestDb->pMethods->xClose==bt_close );
785
+ assert( p->bCrash==0 );
786
+ p->nCrashSync = iSync;
787
+ }
788
+
789
+ bt_db *tdb_bt(TestDb *pDb){
790
+ if( pDb->pMethods->xClose==bt_close ){
791
+ return ((BtDb *)pDb)->pBt;
792
+ }
793
+ return 0;
794
+ }
795
+
796
+ /*************************************************************************
797
+ ** Beginning of code for background checkpointer.
798
+ */
799
+
800
+ struct bt_ckpter {
801
+ sqlite4_buffer file; /* File name */
802
+ sqlite4_buffer spec; /* Options */
803
+ int nLogsize; /* Minimum log size to checkpoint */
804
+ int nRef; /* Number of clients */
805
+
806
+ int bDoWork; /* Set by client threads */
807
+ pthread_t ckpter_thread; /* Checkpointer thread */
808
+ pthread_cond_t ckpter_cond; /* Condition var the ckpter waits on */
809
+ pthread_mutex_t ckpter_mutex; /* Mutex used with ckpter_cond */
810
+
811
+ bt_ckpter *pNext; /* Next object in list at gBgc.pCkpter */
812
+ };
813
+
814
+ static struct GlobalBackgroundCheckpointer {
815
+ bt_ckpter *pCkpter; /* Linked list of checkpointers */
816
+ } gBgc;
817
+
818
+ static void *bgc_main(void *pArg){
819
+ BtDb *pDb = 0;
820
+ int rc;
821
+ int mt;
822
+ bt_ckpter *pCkpter = (bt_ckpter*)pArg;
823
+
824
+ rc = test_bt_open("", (char*)pCkpter->file.p, 0, (TestDb**)&pDb);
825
+ assert( rc==SQLITE4_OK );
826
+ rc = testBtConfigure(pDb, (char*)pCkpter->spec.p, &mt);
827
+
828
+ while( pCkpter->nRef>0 ){
829
+ bt_db *db = pDb->pBt;
830
+ int nLog = 0;
831
+
832
+ sqlite4BtBegin(db, 1);
833
+ sqlite4BtCommit(db, 0);
834
+ sqlite4BtControl(db, BT_CONTROL_LOGSIZE, (void*)&nLog);
835
+
836
+ if( nLog>=pCkpter->nLogsize ){
837
+ int rc;
838
+ bt_checkpoint ckpt;
839
+ memset(&ckpt, 0, sizeof(bt_checkpoint));
840
+ ckpt.nFrameBuffer = nLog/2;
841
+ rc = sqlite4BtControl(db, BT_CONTROL_CHECKPOINT, (void*)&ckpt);
842
+ assert( rc==SQLITE4_OK );
843
+ sqlite4BtControl(db, BT_CONTROL_LOGSIZE, (void*)&nLog);
844
+ }
845
+
846
+ /* The thread will wake up when it is signaled either because another
847
+ ** thread has created some work for this one or because the connection
848
+ ** is being closed. */
849
+ pthread_mutex_lock(&pCkpter->ckpter_mutex);
850
+ if( pCkpter->bDoWork==0 ){
851
+ pthread_cond_wait(&pCkpter->ckpter_cond, &pCkpter->ckpter_mutex);
852
+ }
853
+ pCkpter->bDoWork = 0;
854
+ pthread_mutex_unlock(&pCkpter->ckpter_mutex);
855
+ }
856
+
857
+ if( pDb ) bt_close((TestDb*)pDb);
858
+ return 0;
859
+ }
860
+
861
+ static void bgc_logsize_cb(void *pCtx, int nLogsize){
862
+ bt_ckpter *p = (bt_ckpter*)pCtx;
863
+ if( nLogsize>=p->nLogsize ){
864
+ pthread_mutex_lock(&p->ckpter_mutex);
865
+ p->bDoWork = 1;
866
+ pthread_cond_signal(&p->ckpter_cond);
867
+ pthread_mutex_unlock(&p->ckpter_mutex);
868
+ }
869
+ }
870
+
871
+ static int bgc_attach(BtDb *pDb, const char *zSpec){
872
+ int rc;
873
+ int n;
874
+ bt_info info;
875
+ bt_ckpter *pCkpter;
876
+
877
+ /* Figure out the full path to the database opened by handle pDb. */
878
+ info.eType = BT_INFO_FILENAME;
879
+ info.pgno = 0;
880
+ sqlite4_buffer_init(&info.output, 0);
881
+ rc = sqlite4BtControl(pDb->pBt, BT_CONTROL_INFO, (void*)&info);
882
+ if( rc!=SQLITE4_OK ) return rc;
883
+
884
+ sqlite4_mutex_enter(sqlite4_mutex_alloc(pDb->pEnv, SQLITE4_MUTEX_STATIC_KV));
885
+
886
+ /* Search for an existing bt_ckpter object. */
887
+ n = info.output.n;
888
+ for(pCkpter=gBgc.pCkpter; pCkpter; pCkpter=pCkpter->pNext){
889
+ if( n==pCkpter->file.n && 0==memcmp(info.output.p, pCkpter->file.p, n) ){
890
+ break;
891
+ }
892
+ }
893
+
894
+ /* Failed to find a suitable checkpointer. Create a new one. */
895
+ if( pCkpter==0 ){
896
+ bt_logsizecb cb;
897
+
898
+ pCkpter = testMalloc(sizeof(bt_ckpter));
899
+ memcpy(&pCkpter->file, &info.output, sizeof(sqlite4_buffer));
900
+ info.output.p = 0;
901
+ pCkpter->pNext = gBgc.pCkpter;
902
+ pCkpter->nLogsize = 1000;
903
+ gBgc.pCkpter = pCkpter;
904
+ pCkpter->nRef = 1;
905
+
906
+ sqlite4_buffer_init(&pCkpter->spec, 0);
907
+ rc = sqlite4_buffer_set(&pCkpter->spec, zSpec, strlen(zSpec)+1);
908
+ assert( rc==SQLITE4_OK );
909
+
910
+ /* Kick off the checkpointer thread. */
911
+ if( rc==0 ) rc = pthread_cond_init(&pCkpter->ckpter_cond, 0);
912
+ if( rc==0 ) rc = pthread_mutex_init(&pCkpter->ckpter_mutex, 0);
913
+ if( rc==0 ){
914
+ rc = pthread_create(&pCkpter->ckpter_thread, 0, bgc_main, (void*)pCkpter);
915
+ }
916
+ assert( rc==0 ); /* todo: Fix this */
917
+
918
+ /* Set up the logsize callback for the client thread */
919
+ cb.pCtx = (void*)pCkpter;
920
+ cb.xLogsize = bgc_logsize_cb;
921
+ sqlite4BtControl(pDb->pBt, BT_CONTROL_LOGSIZECB, (void*)&cb);
922
+ }else{
923
+ pCkpter->nRef++;
924
+ }
925
+
926
+ /* Assuming a checkpointer was encountered or effected, attach the
927
+ ** connection to it. */
928
+ if( pCkpter ){
929
+ pDb->pCkpter = pCkpter;
930
+ }
931
+
932
+ sqlite4_mutex_leave(sqlite4_mutex_alloc(pDb->pEnv, SQLITE4_MUTEX_STATIC_KV));
933
+ sqlite4_buffer_clear(&info.output);
934
+ return rc;
935
+ }
936
+
937
+ static int bgc_detach(BtDb *pDb){
938
+ int rc = SQLITE4_OK;
939
+ bt_ckpter *pCkpter = pDb->pCkpter;
940
+ if( pCkpter ){
941
+ int bShutdown = 0; /* True if this is the last reference */
942
+
943
+ sqlite4_mutex_enter(sqlite4_mutex_alloc(pDb->pEnv,SQLITE4_MUTEX_STATIC_KV));
944
+ pCkpter->nRef--;
945
+ if( pCkpter->nRef==0 ){
946
+ bt_ckpter **pp;
947
+
948
+ *pp = pCkpter->pNext;
949
+ for(pp=&gBgc.pCkpter; *pp!=pCkpter; pp=&((*pp)->pNext));
950
+ bShutdown = 1;
951
+ }
952
+ sqlite4_mutex_leave(sqlite4_mutex_alloc(pDb->pEnv,SQLITE4_MUTEX_STATIC_KV));
953
+
954
+ if( bShutdown ){
955
+ void *pDummy;
956
+
957
+ /* Signal the checkpointer thread. */
958
+ pthread_mutex_lock(&pCkpter->ckpter_mutex);
959
+ pCkpter->bDoWork = 1;
960
+ pthread_cond_signal(&pCkpter->ckpter_cond);
961
+ pthread_mutex_unlock(&pCkpter->ckpter_mutex);
962
+
963
+ /* Join the checkpointer thread. */
964
+ pthread_join(pCkpter->ckpter_thread, &pDummy);
965
+ pthread_cond_destroy(&pCkpter->ckpter_cond);
966
+ pthread_mutex_destroy(&pCkpter->ckpter_mutex);
967
+
968
+ sqlite4_buffer_clear(&pCkpter->file);
969
+ sqlite4_buffer_clear(&pCkpter->spec);
970
+ testFree(pCkpter);
971
+ }
972
+
973
+ pDb->pCkpter = 0;
974
+ }
975
+ return rc;
976
+ }
977
+
978
+ /*
979
+ ** End of background checkpointer.
980
+ *************************************************************************/