xref: /sqlite-3.40.0/test/mmap1.test (revision eecc3983)
1# 2013 March 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
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15source $testdir/lock_common.tcl
16set testprefix mmap1
17
18proc nRead {db} {
19  set bt [btree_from_db $db]
20  db_enter $db
21  array set stats [btree_pager_stats $bt]
22  db_leave $db
23  return $stats(read)
24}
25
26foreach {t mmap_size nRead} {
27  1 { PRAGMA mmap_size = -65536 }   4
28  2 { PRAGMA mmap_size = -50    } 156
29  3 { PRAGMA mmap_size = 0      } 344
30} {
31  do_multiclient_test tn {
32    sql1 $mmap_size
33
34    code2 {
35      set ::rcnt 0
36      proc rblob {n} {
37        set ::rcnt [expr (($::rcnt << 3) + $::rcnt + 456) & 0xFFFFFFFF]
38        set str [format %.8x [expr $::rcnt ^ 0xbdf20da3]]
39        string range [string repeat $str [expr $n/4]] 1 $n
40      }
41      db2 func rblob rblob
42    }
43
44    sql2 {
45      PRAGMA auto_vacuum = 1;
46      CREATE TABLE t1(a, b, UNIQUE(a, b));
47      INSERT INTO t1 VALUES(rblob(500), rblob(500));
48      INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; --    2
49      INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; --    4
50      INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; --    8
51      INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; --   16
52      INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1; --   32
53    }
54    do_test $t.$tn.1 {
55      sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
56    } {32 ok 77}
57
58    # Have connection 2 shrink the file. Check connection 1 can still read it.
59    sql2 { DELETE FROM t1 WHERE rowid%2; }
60    do_test $t.$tn.2 {
61      sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
62    } {16 ok 42}
63
64    # Have connection 2 grow the file. Check connection 1 can still read it.
65    sql2 { INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1 }
66    do_test $t.$tn.3 {
67      sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
68    } {32 ok 79}
69
70    # Have connection 2 grow the file again. Check connection 1 is still ok.
71    sql2 { INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1 }
72    do_test $t.$tn.4 {
73      sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count"
74    } {64 ok 149}
75
76    # Check that the number of pages read by connection 1 indicates that the
77    # "PRAGMA mmap_size" command worked.
78    do_test $t.$tn.5 { nRead db } $nRead
79  }
80}
81
82
83finish_test
84
85