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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#!/usr/bin/env python
#
# Public Domain 2014-2015 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
# test_txn01.py
# Transactions: basic functionality
class test_txn01(wttest.WiredTigerTestCase):
nentries = 1000
scenarios = [
('col-f', dict(uri='file:text_txn01',key_format='r',value_format='S')),
('col-t', dict(uri='table:text_txn01',key_format='r',value_format='S')),
('fix-f', dict(uri='file:text_txn01',key_format='r',value_format='8t')),
('fix-t', dict(uri='table:text_txn01',key_format='r',value_format='8t')),
('row-f', dict(uri='file:text_txn01',key_format='S',value_format='S')),
('row-t', dict(uri='table:text_txn01',key_format='S',value_format='S')),
]
# Overrides WiredTigerTestCase
def setUpConnectionOpen(self, dir):
conn = wiredtiger.wiredtiger_open(dir, 'create,' +
('error_prefix="%s: ",' % self.shortid()))
self.pr(`conn`)
return conn
# Return the number of records visible to the cursor.
def cursor_count(self, cursor):
count = 0
# Column-store appends result in phantoms, ignore records unless they
# have our flag value.
for r in cursor:
if self.value_format == 'S' or cursor.get_value() == 0xab:
count += 1
return count
# Checkpoint the database and assert the number of records visible to the
# checkpoint matches the expected value.
def check_checkpoint(self, expected):
s = self.conn.open_session()
s.checkpoint("name=test")
cursor = s.open_cursor(self.uri, None, "checkpoint=test")
self.assertEqual(self.cursor_count(cursor), expected)
s.close()
# Open a cursor with snapshot isolation, and assert the number of records
# visible to the cursor matches the expected value.
def check_txn_cursor(self, level, expected):
s = self.conn.open_session()
cursor = s.open_cursor(self.uri, None)
s.begin_transaction(level)
self.assertEqual(self.cursor_count(cursor), expected)
s.close()
# Open a session with snapshot isolation, and assert the number of records
# visible to the cursor matches the expected value.
def check_txn_session(self, level, expected):
s = self.conn.open_session(level)
cursor = s.open_cursor(self.uri, None)
s.begin_transaction()
self.assertEqual(self.cursor_count(cursor), expected)
s.close()
def check(self, cursor, committed, total):
# The cursor itself should see all of the records.
if cursor != None:
cursor.reset()
self.assertEqual(self.cursor_count(cursor), total)
# Read-uncommitted should see all of the records.
# Snapshot and read-committed should see only committed records.
self.check_txn_cursor('isolation=read-uncommitted', total)
self.check_txn_session('isolation=read-uncommitted', total)
self.check_txn_cursor('isolation=snapshot', committed)
self.check_txn_session('isolation=snapshot', committed)
self.check_txn_cursor('isolation=read-committed', committed)
self.check_txn_session('isolation=read-committed', committed)
# Checkpoints should only write committed items.
self.check_checkpoint(committed)
# Loop through a set of inserts, periodically committing; before each
# commit, verify the number of visible records matches the expected value.
def test_visibility(self):
self.session.create(self.uri,
'key_format=' + self.key_format +
',value_format=' + self.value_format)
committed = 0
cursor = self.session.open_cursor(self.uri, None)
self.check(cursor, 0, 0)
self.session.begin_transaction()
for i in xrange(self.nentries):
if i > 0 and i % (self.nentries / 37) == 0:
self.check(cursor, committed, i)
self.session.commit_transaction()
committed = i
self.session.begin_transaction()
if self.key_format == 'S':
cursor.set_key("key: %06d" % i)
else:
cursor.set_key(i + 1)
if self.value_format == 'S':
cursor.set_value("value: %06d" % i)
else:
cursor.set_value(0xab)
cursor.insert()
self.check(cursor, committed, self.nentries)
self.session.commit_transaction()
self.check(cursor, self.nentries, self.nentries)
# Test that read-committed is the default isolation level.
class test_read_committed_default(wttest.WiredTigerTestCase):
uri = 'table:test_txn'
# Return the number of records visible to the cursor.
def cursor_count(self, cursor):
count = 0
for r in cursor:
count += 1
return count
def test_read_committed_default(self):
self.session.create(self.uri, 'key_format=S,value_format=S')
cursor = self.session.open_cursor(self.uri, None)
self.session.begin_transaction()
cursor.set_key('key: aaa')
cursor.set_value('value: aaa')
cursor.insert()
self.session.commit_transaction()
self.session.begin_transaction()
cursor.set_key('key: bbb')
cursor.set_value('value: bbb')
cursor.insert()
s = self.conn.open_session()
cursor = s.open_cursor(self.uri, None)
s.begin_transaction("isolation=read-committed")
self.assertEqual(self.cursor_count(cursor), 1)
s.commit_transaction()
s.begin_transaction(None)
self.assertEqual(self.cursor_count(cursor), 1)
s.commit_transaction()
s.close()
if __name__ == '__main__':
wttest.run()
|