summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/suite/test_truncate21.py
blob: b9016b3788f42f1dd7b2e6e13559ddd60b81cc3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
#
# Public Domain 2014-present MongoDB, Inc.
# Public Domain 2008-2014 WiredTiger, Inc.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import wiredtiger, wttest
import os
from helper import copy_wiredtiger_home
from wtscenario import make_scenarios
from wiredtiger import WT_NOTFOUND

# test_truncate21.py
# Test truncate, logging and recovery when truncate has no work to do. 
class test_truncate21(wttest.WiredTigerTestCase):
    conn_config = 'cache_size=2MB,log=(enabled)'
    dir = "newdir"
    nentries = 1000

    uri_fix = 'table:trunc_fix'
    uri_row = 'table:trunc_row'
    create_fix = 'key_format=r,value_format=8t'
    create_row = 'key_format=i,value_format=S'

    start_key = nentries // 4
    end_key = nentries // 2
    # Pick a key in the range we will truncate to re-insert.
    insert_key = (start_key + end_key) // 2

    def trunc_range(self):
        # In a transaction, truncate the same range from all three tables.
        # Then truncate the same range again, also inserting one key into the range for
        # the row-store table. Delete a range from the middle of the table.
        cfix_start = self.session.open_cursor(self.uri_fix)
        crow_start = self.session.open_cursor(self.uri_row)
        cfix_end = self.session.open_cursor(self.uri_fix)
        crow_end = self.session.open_cursor(self.uri_row)
        cfix_start.set_key(self.start_key)
        crow_start.set_key(self.start_key)
        cfix_end.set_key(self.end_key)
        crow_end.set_key(self.end_key)
        # Do the truncate on teach table.
        self.session.truncate(None, cfix_start, cfix_end, None)
        self.session.truncate(None, crow_start, crow_end, None)
        cfix_start.close()
        crow_start.close()
        cfix_end.close()
        crow_end.close()

    def test_truncate21(self):

        # Create one table of each type: FLCS, row-store and VLCS.
        # Put the same data into each (per allowed by type). 
        self.session.create(self.uri_fix, self.create_fix)
        self.session.create(self.uri_row, self.create_row)

        cfix = self.session.open_cursor(self.uri_fix)
        crow = self.session.open_cursor(self.uri_row)
        for i in range(1, self.nentries):
            self.session.begin_transaction()
            cfix[i] = 97
            crow[i] = 'rowval'
            self.session.commit_transaction()
        cfix.close()
        crow.close()
        self.session.checkpoint()

        # In a transaction, truncate the same range from all three tables.
        # Then truncate the same range again, also inserting one key into the range for
        # the row-store table. Delete a range from the middle of the table.
        self.session.begin_transaction()
        self.trunc_range()
        self.session.commit_transaction()

        # Open a second session and transaction. In one we truncate the same range again.
        # In the other we insert into the FLCS and row-store tables. (The VLCS table will be
        # used in a later test.)
        session2 = self.conn.open_session()

        self.session.begin_transaction()
        session2.begin_transaction()
        # In the other session, truncate the same range again.
        self.trunc_range()
        # Commit the insert.
        # With overlapping transactions, insert into the key range for FLCS and row.
        cfix = session2.open_cursor(self.uri_fix)
        crow = session2.open_cursor(self.uri_row)
        cfix[self.insert_key] = 98
        crow[self.insert_key] = 'newval'

        session2.commit_transaction()
        # Commit the truncate.
        self.session.commit_transaction()

        cfix.close()
        crow.close()
        session2.close()

        # Flush all log records.
        self.session.log_flush('sync=on')

        # Make a copy of the database and open it. That forces recovery to be run, which should
        # replay both truncate calls and then find the keys.
        os.mkdir(self.dir)
        copy_wiredtiger_home(self, '.', self.dir)

        new_conn = self.wiredtiger_open(self.dir, self.conn_config)
        new_sess = self.setUpSessionOpen(new_conn)
        cfix = new_sess.open_cursor(self.uri_fix)
        crow = new_sess.open_cursor(self.uri_row)
        cfix.set_key(self.insert_key)
        crow.set_key(self.insert_key)
        ret_fix = cfix.search()
        ret_row = crow.search()
        # The key should not exist in both. In FLCS the record number always exists but the value
        # should be zero.
        val_fix = cfix.get_value()
        self.pr('ret_row ' + str(ret_row))
        self.pr('val_fix ' + str(val_fix))
        self.assertEqual(ret_row, wiredtiger.WT_NOTFOUND)
        self.assertEqual(val_fix, 0)
        cfix.close()
        crow.close()
        new_conn.close()

if __name__ == '__main__':
    wttest.run()