forked from uktrade/sqlite-s3vfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_sqlite_s3vfs.py
309 lines (235 loc) · 9.49 KB
/
test_sqlite_s3vfs.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import tempfile
import uuid
from contextlib import closing, contextmanager
import apsw
import boto3
import sqlite3
import pytest
from sqlite_s3vfs import S3VFS
PAGE_SIZES = [512, 1024, 2048, 4096, 8192, 16384, 32768, 65536]
BLOCK_SIZES = [4095, 4096, 4097]
JOURNAL_MODES = ['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'OFF']
@pytest.fixture
def bucket():
session = boto3.Session(
aws_access_key_id='AKIAIDIDIDIDIDIDIDID',
aws_secret_access_key='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
region_name='us-east-1',
)
s3 = session.resource('s3',
endpoint_url='http://localhost:9000/'
)
bucket = s3.create_bucket(Bucket=f's3vfs-{str(uuid.uuid4())}')
yield bucket
bucket.objects.all().delete()
bucket.delete()
@contextmanager
def transaction(cursor):
cursor.execute('BEGIN;')
try:
yield cursor
except:
cursor.execute('ROLLBACK;')
raise
else:
cursor.execute('COMMIT;')
def set_pragmas(cursor, page_size, journal_mode):
sqls = [
f'PRAGMA page_size = {page_size};',
f'PRAGMA journal_mode = {journal_mode};',
]
for sql in sqls:
cursor.execute(sql)
def create_db(cursor):
sqls = [
'CREATE TABLE foo(x,y);',
'INSERT INTO foo VALUES ' + ','.join('(1,2)' for _ in range(0, 100)) + ';',
] + [
f'CREATE TABLE foo_{i}(x,y);' for i in range(0, 10)
]
for sql in sqls:
cursor.execute(sql)
def empty_db(cursor):
sqls = [
'DROP TABLE foo;'
] + [
f'DROP TABLE foo_{i};' for i in range(0, 10)
]
for sql in sqls:
cursor.execute(sql)
@pytest.mark.parametrize(
'page_size', PAGE_SIZES
)
@pytest.mark.parametrize(
'block_size', BLOCK_SIZES
)
@pytest.mark.parametrize(
'journal_mode', JOURNAL_MODES
)
def test_s3vfs(bucket, page_size, block_size, journal_mode):
s3vfs = S3VFS(bucket=bucket, block_size=block_size)
# Create a database and query it
with closing(apsw.Connection("a-test/cool.db", vfs=s3vfs.name)) as db:
set_pragmas(db.cursor(), page_size, journal_mode)
with transaction(db.cursor()) as cursor:
create_db(cursor)
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [(1, 2)] * 100
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
# Query an existing database
with \
closing(apsw.Connection("a-test/cool.db", vfs=s3vfs.name)) as db, \
transaction(db.cursor()) as cursor:
cursor = db.cursor()
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [(1, 2)] * 100
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
# Serialize a database with serialize_fileobj, upload to S3, download it, and query it
with \
tempfile.NamedTemporaryFile() as fp_s3vfs:
target_obj = bucket.Object('target/cool.sqlite')
target_obj.upload_fileobj(s3vfs.serialize_fileobj(key_prefix='a-test/cool.db'))
fp_s3vfs.write(target_obj.get()['Body'].read())
fp_s3vfs.flush()
with \
closing(sqlite3.connect(fp_s3vfs.name)) as db, \
transaction(db.cursor()) as cursor:
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [(1, 2)] * 100
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
# Serialize a database with serialize_iter and query it
with \
tempfile.NamedTemporaryFile() as fp_s3vfs, \
tempfile.NamedTemporaryFile() as fp_sqlite3:
for chunk in s3vfs.serialize_iter(key_prefix='a-test/cool.db'):
# Empty chunks can be treated as EOF, so never output those
assert bool(chunk)
fp_s3vfs.write(chunk)
fp_s3vfs.flush()
with \
closing(sqlite3.connect(fp_s3vfs.name)) as db, \
transaction(db.cursor()) as cursor:
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [(1, 2)] * 100
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
# Serialized form should be the same length as one constructed without the VFS...
with closing(sqlite3.connect(fp_sqlite3.name)) as db:
set_pragmas(db.cursor(), page_size, journal_mode)
with transaction(db.cursor()) as cursor:
create_db(cursor)
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
assert os.path.getsize(fp_s3vfs.name) == os.path.getsize(fp_sqlite3.name)
# ...including after a VACUUM (which cannot be in a transaction)
with closing(apsw.Connection("a-test/cool.db", vfs=s3vfs.name)) as db:
with transaction(db.cursor()) as cursor:
empty_db(cursor)
db.cursor().execute('VACUUM;')
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
fp_s3vfs.truncate(0)
fp_s3vfs.seek(0)
for chunk in s3vfs.serialize_iter(key_prefix='a-test/cool.db'):
assert bool(chunk)
fp_s3vfs.write(chunk)
fp_s3vfs.flush()
with closing(sqlite3.connect(fp_sqlite3.name)) as db:
with transaction(db.cursor()) as cursor:
empty_db(cursor)
db.cursor().execute('VACUUM;')
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
assert os.path.getsize(fp_s3vfs.name) == os.path.getsize(fp_sqlite3.name)
@pytest.mark.parametrize(
'page_size', PAGE_SIZES
)
@pytest.mark.parametrize(
'block_size', BLOCK_SIZES
)
@pytest.mark.parametrize(
'journal_mode', JOURNAL_MODES
)
def test_deserialize_iter(bucket, page_size, block_size, journal_mode):
s3vfs = S3VFS(bucket=bucket, block_size=block_size)
with tempfile.NamedTemporaryFile() as fp_sqlite3:
with closing(sqlite3.connect(fp_sqlite3.name)) as db:
set_pragmas(db.cursor(), page_size, journal_mode)
with transaction(db.cursor()) as cursor:
create_db(cursor)
s3vfs.deserialize_iter(key_prefix='another-test/cool.db', bytes_iter=fp_sqlite3)
with \
closing(apsw.Connection('another-test/cool.db', vfs=s3vfs.name)) as db, \
transaction(db.cursor()) as cursor:
cursor = db.cursor()
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [(1, 2)] * 100
cursor.execute('PRAGMA integrity_check;')
assert cursor.fetchall() == [('ok',)]
@pytest.mark.parametrize(
'page_size', [65536]
)
@pytest.mark.parametrize(
'block_size', [65536]
)
def test_byte_lock_page(bucket, page_size, block_size):
s3vfs = S3VFS(bucket=bucket, block_size=block_size)
empty = (bytes(4050),)
with closing(apsw.Connection('another-test/cool.db', vfs=s3vfs.name)) as db:
db.cursor().execute(f'PRAGMA page_size = {page_size};')
with transaction(db.cursor()) as cursor:
cursor.execute('CREATE TABLE foo(content BLOB);')
cursor.executemany('INSERT INTO foo VALUES (?);', (empty for _ in range(0, 300000)))
cursor.execute('SELECT * FROM foo LIMIT 1;')
assert cursor.fetchall() == [empty]
cursor.execute('DELETE FROM foo;')
cursor.execute('SELECT * FROM foo LIMIT 1;')
assert cursor.fetchall() == []
with transaction(db.cursor()) as cursor:
cursor.executemany('INSERT INTO foo VALUES (?);', (empty for _ in range(0, 300000)))
cursor.execute('SELECT * FROM foo LIMIT 1;')
assert cursor.fetchall() == [empty]
def test_set_temp_store_which_calls_xaccess(bucket):
s3vfs = S3VFS(bucket=bucket)
with closing(apsw.Connection('another-test/cool.db', vfs=s3vfs.name)) as db:
db.cursor().execute("pragma temp_store_directory = 'my-temp-store'")
@pytest.mark.parametrize(
'page_size', [4096]
)
@pytest.mark.parametrize(
'block_size', [4095, 4096, 4097]
)
@pytest.mark.parametrize(
'journal_mode', [journal_mode for journal_mode in JOURNAL_MODES if journal_mode != 'OFF']
)
def test_rollback(bucket, page_size, block_size, journal_mode):
s3vfs = S3VFS(bucket=bucket, block_size=block_size)
with closing(apsw.Connection('another-test/cool.db', vfs=s3vfs.name)) as db:
db.cursor().execute(f'PRAGMA page_size = {page_size};')
db.cursor().execute(f'PRAGMA journal_mode = {journal_mode};')
db.cursor().execute('CREATE TABLE foo(content text);')
try:
with transaction(db.cursor()) as cursor:
cursor.execute("INSERT INTO foo VALUES ('hello');");
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [('hello',)]
raise Exception()
except:
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == []
cursor.execute("INSERT INTO foo VALUES ('hello');");
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [('hello',)]
try:
with transaction(db.cursor()) as cursor:
cursor.execute("UPDATE foo SET content='goodbye'");
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [('goodbye',)]
raise Exception()
except:
cursor.execute('SELECT * FROM foo;')
assert cursor.fetchall() == [('hello',)]