forked from uktrade/sqlite-s3vfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_sqlite_ddbvfs.py
441 lines (339 loc) · 14.5 KB
/
test_sqlite_ddbvfs.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import os
import tempfile
import uuid
from contextlib import closing, contextmanager
import apsw
import boto3
import sqlite3
import pytest
from concurrent import futures
from sqlite_ddbvfs import DDBVFS
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 table():
session = boto3.Session(
aws_access_key_id='AKIAIDIDIDIDIDIDIDID',
aws_secret_access_key='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
region_name='us-east-1',
)
dynamo = session.resource('dynamodb' , endpoint_url='http://localhost:8000')
#dynamo = boto3.resource('dynamodb' )
table = dynamo.create_table(
TableName=f'ddbvfs-table-{str(uuid.uuid4())}',
KeySchema=[
{'AttributeName': 'key', 'KeyType': 'HASH'},
{'AttributeName': 'range', 'KeyType': 'RANGE'},
]
,
AttributeDefinitions=[
{'AttributeName': 'key', 'AttributeType': 'S'},
{'AttributeName': 'range', 'AttributeType': 'S'},
],
BillingMode='PAY_PER_REQUEST',
)
table.wait_until_exists()
yield table
#drop the dynamodb table
table.delete()
@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-bucket-{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_ddbvfs(bucket, table, page_size, block_size, journal_mode):
ddbvfs = DDBVFS(table=table, block_size=block_size)
# Create a database and query it
with closing(apsw.Connection("a-test/cool.db", vfs=ddbvfs.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=ddbvfs.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_ddbvfs:
target_obj = bucket.Object('target/cool.sqlite')
target_obj.upload_fileobj(ddbvfs.serialize_fileobj(key_prefix='a-test/cool.db'))
fp_ddbvfs.write(target_obj.get()['Body'].read())
fp_ddbvfs.flush()
with \
closing(sqlite3.connect(fp_ddbvfs.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_ddbvfs, \
tempfile.NamedTemporaryFile() as fp_sqlite3:
for chunk in ddbvfs.serialize_iter(pathname='a-test/cool.db'):
# Empty chunks can be treated as EOF, so never output those
assert bool(chunk)
fp_ddbvfs.write(chunk)
fp_ddbvfs.flush()
with \
closing(sqlite3.connect(fp_ddbvfs.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_ddbvfs.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=ddbvfs.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_ddbvfs.truncate(0)
fp_ddbvfs.seek(0)
for chunk in ddbvfs.serialize_iter(pathname = 'a-test/cool.db'):
assert bool(chunk)
fp_ddbvfs.write(chunk)
fp_ddbvfs.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_ddbvfs.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(table, page_size, block_size, journal_mode):
ddbvfs = DDBVFS(table=table, 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)
ddbvfs.deserialize_iter(key_prefix='another-test/cool.db', bytes_iter=fp_sqlite3)
with \
closing(apsw.Connection('another-test/cool.db', vfs=ddbvfs.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(table, page_size, block_size):
ddbvfs = DDBVFS(table=table, block_size=block_size)
empty = (bytes(4050),)
with closing(apsw.Connection('another-test/cool.db', vfs=ddbvfs.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]
@pytest.mark.parametrize(
'page_size', [6144]
)
@pytest.mark.parametrize(
'block_size', [6144]
)
def test_simple_read(table, page_size, block_size):
ddbvfs = DDBVFS(table=table, block_size=block_size)
canary = (uuid.uuid4().hex * 100,)
with closing(apsw.Connection('another-test/cool.db', vfs=ddbvfs.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 (?);', (canary for _ in range(0, 10)))
cursor.execute('SELECT * FROM foo LIMIT 1;')
assert cursor.fetchall() == [canary]
cursor.execute('SELECT count(*) FROM foo;')
assert cursor.fetchall() == [(10,)]
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 (?);', (canary for _ in range(0, 100)))
cursor.execute('SELECT * FROM foo LIMIT 1;')
assert cursor.fetchall() == [canary]
def transaction_loop(table, page_size, block_size):
canary = (uuid.uuid4().hex * 10,)
ddbvfs = DDBVFS(table=table, block_size=block_size)
with closing(apsw.Connection('another-test/cool.db', vfs=ddbvfs.name)) as db:
db.cursor().execute(f'PRAGMA busy_timeout = {10000};')
for r in range(10):
with transaction(db.cursor()) as cursor:
cursor.executemany('INSERT INTO foo VALUES (?);', (canary for _ in range(0, 10)))
with closing(db.cursor()) as cursor:
cursor.execute('SELECT * FROM foo where content = (?) LIMIT 1;', canary)
assert cursor.fetchall() == [canary]
cursor.execute('SELECT count(*) FROM foo where content = (?);', canary)
assert cursor.fetchall()[0] == (10,)
with closing(db.cursor()) as cursor:
cursor.execute('DELETE FROM foo where content = (?);', canary)
cursor.execute('SELECT * FROM foo where content = (?) LIMIT 1;', canary)
assert cursor.fetchall() == []
cursor.execute('SELECT count(*) FROM foo where content = (?);', canary)
assert cursor.fetchall()[0] == (0,)
with closing(db.cursor()) as cursor:
cursor.executemany('INSERT INTO foo VALUES (?);', (canary for _ in range(0, 10)))
cursor.execute('SELECT * FROM foo where content = (?) LIMIT 1;', canary)
assert cursor.fetchall() == [canary]
cursor.execute('SELECT count(*) FROM foo where content = (?);', canary)
assert cursor.fetchall()[0] == (10,)
with transaction(db.cursor()) as cursor:
cursor.execute('DELETE FROM foo where content = (?);',canary)
cursor.execute('SELECT * FROM foo where content = (?) LIMIT 1;', canary)
assert cursor.fetchall() == []
cursor.execute('SELECT count(*) FROM foo where content = (?);', canary)
assert cursor.fetchall()[0] == (0,)
return True
@pytest.mark.parametrize(
'page_size', [6144]
)
@pytest.mark.parametrize(
'block_size', [6144]
)
def test_threaded_pattern(table, page_size, block_size):
ddbvfs = DDBVFS(table=table, block_size=block_size)
future_result = []
with closing(apsw.Connection('another-test/cool.db', vfs=ddbvfs.name)) as db:
db.cursor().execute(f'PRAGMA page_size = {page_size};')
db.cursor().execute('CREATE TABLE IF NOT EXISTS foo(content BLOB);')
count_process = 2
#execute transaction_loop in 10 threads
with futures.ThreadPoolExecutor(max_workers=count_process) as executor:
for _ in range(count_process):
future_result.append(executor.submit(transaction_loop, table, page_size, block_size))
#check all futures response when completed
for future in future_result:
assert future.result()
def test_set_temp_store_which_calls_xaccess(table):
ddbvfs = DDBVFS(table=table)
with closing(apsw.Connection('another-test/cool.db', vfs=ddbvfs.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(table, page_size, block_size, journal_mode):
ddbvfs = DDBVFS(table=table, block_size=block_size)
with closing(apsw.Connection('another-test/cool.db', vfs=ddbvfs.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',)]