File: truncate.test.lua

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (172 lines) | stat: -rw-r--r-- 4,817 bytes parent folder | download | duplicates (3)
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
test_run = require('test_run').new()
engine = test_run:get_cfg('engine')

fiber =  require('fiber')

--
-- Check that space truncation works fine in a transaction.
--
s = box.schema.create_space('test', {engine = engine})
_ = s:create_index('pk')
_ = s:insert{123}
box.begin() s:truncate() box.commit()
s:select()
s:drop()

--
-- Check that space truncation works for spaces created via
-- the internal API.
--
_ = box.space._space:insert{512, 1, 'test', engine, 0, {temporary = false}, {}}
_ = box.space._index:insert{512, 0, 'pk', 'tree', {unique = true}, {{0, 'unsigned'}}}
_ = box.space.test:insert{123}
box.space.test:select()
box.space.test:truncate()
box.space.test:select()
box.space.test:drop()

--
-- Check that a space cannot be dropped if it has a record
-- in _truncate space.
--
s = box.schema.create_space('test', {engine = engine})
s:truncate()
_ = box.space._space:delete{s.id} -- error
_ = box.space._truncate:delete{s.id}
_ = box.space._space:delete{s.id} -- ok

--
-- Check that truncation of system spaces is not permitted.
--
box.space._space:truncate()
box.space._index:truncate()

--
-- Truncate space with no indexes.
--
s = box.schema.create_space('test', {engine = engine})
s:truncate()
s:drop()

--
-- Truncate empty space.
--
s = box.schema.create_space('test', {engine = engine})
_ = s:create_index('pk')
s:truncate()
s:select()
s:drop()

--
-- Truncate non-empty space.
--
s = box.schema.create_space('test', {engine = engine})
_ = s:create_index('i1', {parts = {1, 'unsigned'}})
_ = s:create_index('i2', {parts = {2, 'unsigned'}})
_ = s:create_index('i3', {parts = {3, 'string'}})
_ = s:insert{1, 3, 'a'}
_ = s:insert{2, 2, 'b'}
_ = s:insert{3, 1, 'c'}
s:truncate()
s.index.i1:select()
s.index.i2:select()
s.index.i3:select()
_ = s:insert{10, 30, 'x'}
_ = s:insert{20, 20, 'y'}
_ = s:insert{30, 10, 'z'}
s.index.i1:select()
s.index.i2:select()
s.index.i3:select()
s:drop()

--
-- Calling space.truncate concurrently.
--
s = box.schema.create_space('test', {engine = engine})
_ = s:create_index('pk')
c = fiber.channel(5)
for i = 1, 5 do fiber.create(function() s:truncate() c:put(true) end) end
for i = 1, 5 do c:get() end
s:drop()

--
-- Check that space truncation is persistent.
--
-- The test checks the following cases:
-- - Create and truncate before snapshot
-- - Create before snapshot, truncate after snapshot
-- - Create and truncate after snapshot
--
s1 = box.schema.create_space('test1', {engine = engine})
_ = s1:create_index('i1', {parts = {1, 'unsigned'}})
_ = s1:create_index('i2', {parts = {2, 'unsigned'}})
_ = s1:insert{1, 3}
_ = s1:insert{2, 2}
_ = s1:insert{3, 1}
s1:truncate()
_ = s1:insert{123, 321}
s2 = box.schema.create_space('test2', {engine = engine})
_ = s2:create_index('i1', {parts = {1, 'unsigned'}})
_ = s2:create_index('i2', {parts = {2, 'unsigned'}})
_ = s2:insert{10, 30}
_ = s2:insert{20, 20}
_ = s2:insert{30, 10}
box.snapshot()
_ = s1:insert{321, 123}
s2:truncate()
_ = s2:insert{456, 654}
s3 = box.schema.create_space('test3', {engine = engine})
_ = s3:create_index('i1', {parts = {1, 'unsigned'}})
_ = s3:create_index('i2', {parts = {2, 'unsigned'}})
_ = s3:insert{100, 300}
_ = s3:insert{200, 200}
_ = s3:insert{300, 100}
s3:truncate()
_ = s3:insert{789, 987}
-- Check that index drop, create, and alter called after space
-- truncate do not break recovery (gh-2615)
s4 = box.schema.create_space('test4', {engine = engine})
_ = s4:create_index('i1', {parts = {1, 'string'}})
_ = s4:create_index('i3', {parts = {3, 'string'}})
_ = s4:insert{'zzz', 111, 'yyy'}
s4:truncate()
s4.index.i3:drop()
_ = s4:create_index('i2', {parts = {2, 'string'}})
s4.index.i1:alter({parts = {1, 'string', 2, 'string'}})
_ = s4:insert{'abc', 'cba'}
test_run:cmd('restart server default')
s1 = box.space.test1
s2 = box.space.test2
s3 = box.space.test3
s4 = box.space.test4
s1.index.i1:select()
s1.index.i2:select()
s2.index.i1:select()
s2.index.i2:select()
s3.index.i1:select()
s3.index.i2:select()
s4.index.i1:select()
s4.index.i2:select()
s1:drop()
s2:drop()
s3:drop()
s4:drop()

-- Truncate should fail in no write access for the space
engine = test_run:get_cfg('engine')
s = box.schema.create_space('access_truncate', {engine = engine})
_ = s:create_index('pk')
s:insert({1, 2, 3})
s:insert({3, 2, 1})
box.schema.user.grant('guest', 'execute', 'universe')
box.schema.user.grant('guest', 'read', 'space', 'access_truncate')
con = require('net.box').connect(box.cfg.listen)
con:eval([[box.space.access_truncate:truncate()]])
con.space.access_truncate:select()
box.schema.user.grant('guest', 'write', 'space', 'access_truncate')
con:eval([[box.space.access_truncate:truncate()]])
con.space.access_truncate:select()
con:close()
box.schema.user.revoke('guest', 'execute', 'universe')
box.schema.user.revoke('guest', 'read,write', 'space', 'access_truncate')
s:drop()