-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
660 lines (609 loc) · 27.8 KB
/
test.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import collections
import itertools
import os
import sqlite3
import tempfile
import unittest
import zlib
from stream_sqlite import stream_sqlite
column_constructor = collections.namedtuple('Column', ('cid', 'name', 'type', 'notnull', 'dflt_value', 'pk'))
class TestStreamSqlite(unittest.TestCase):
def test_empty_database(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size, chunk_size=chunk_size):
all_chunks = tables_list(stream_sqlite(db([('VACUUM;', ())], page_size, chunk_size), max_buffer_size=page_size))
self.assertEqual([], all_chunks)
def test_small_table(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = [
("CREATE TABLE \"my_table_'1\" (my_text_col_a text, my_text_col_b text);", ()),
("CREATE TABLE \"my_table_'2\" (my_text_col_a text, my_text_col_b text);", ()),
("INSERT INTO \"my_table_'1\" VALUES ('some-text-a', 'some-text-b')", ()),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=page_size))
self.assertEqual([(
"my_table_'1",
(
column_constructor(cid=0, name='my_text_col_a', type='text', notnull=0, dflt_value=None, pk=0),
column_constructor(cid=1, name='my_text_col_b', type='text', notnull=0, dflt_value=None, pk=0),
),
[('some-text-a', 'some-text-b')],
)], all_chunks)
def test_overflow_master(self):
for page_size, chunk_size in itertools.product(
[512],
[131072],
):
column_name = "my_text_col_" + ('a' * 10000)
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = [
("CREATE TABLE my_table_1 ({} text);".format(column_name), ()),
("INSERT INTO my_table_1 VALUES ('a')".format(column_name), ()),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=20971520))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name=column_name, type='text', notnull=0, dflt_value=None, pk=0),
),
[
('a',)
],
)], all_chunks)
def test_overflow_non_master(self):
for page_size, chunk_size in itertools.product(
[512],
[131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
for i in range(1, 10001):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a text);", ()),
("INSERT INTO my_table_1 VALUES ('" + ('-' * i) + "')", ()),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=20971520))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_text_col_a', type='text', notnull=0, dflt_value=None, pk=0),
),
[
('-' * i,)
],
)], all_chunks)
def test_integers(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a integer);", ()),
("INSERT INTO my_table_1 VALUES (0),(1),(2),(65536),(16777216),(4294967296),(1099511627776),(281474976710656),(72057594037927936)", ()),
("INSERT INTO my_table_1 VALUES (0),(-1),(-2),(-65536),(-16777216),(-4294967296),(-1099511627776),(-281474976710656),(-72057594037927936)", ()),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=page_size))
self.assertEqual([(
'my_table_1',
(
column_constructor(cid=0, name='my_text_col_a', type='integer', notnull=0, dflt_value=None, pk=0),
),
[
(0,),
(1,),
(2,),
(65536,),
(16777216,),
(4294967296,),
(1099511627776,),
(281474976710656,),
(72057594037927936,),
(0,),
(-1,),
(-2,),
(-65536,),
(-16777216,),
(-4294967296,),
(-1099511627776,),
(-281474976710656,),
(-72057594037927936,)]
)], all_chunks)
def test_integers_primary_key(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = [
("CREATE TABLE my_table_1 (m integer primary key);", ()),
("INSERT INTO my_table_1 VALUES (0),(1),(2),(65536),(16777216),(4294967296),(1099511627776),(281474976710656),(72057594037927936)", ()),
("INSERT INTO my_table_1 VALUES (-1),(-2),(-65536),(-16777216),(-4294967296),(-1099511627776),(-281474976710656),(-72057594037927936)", ()),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=page_size))
self.assertEqual([(
'my_table_1',
(
column_constructor(cid=0, name='m', type='integer', notnull=0, dflt_value=None, pk=1),
),
[
(-72057594037927936,),
(-281474976710656,),
(-1099511627776,),
(-4294967296,),
(-16777216,),
(-65536,),
(-2,),
(-1,),
(0,),
(1,),
(2,),
(65536,),
(16777216,),
(4294967296,),
(1099511627776,),
(281474976710656,),
(72057594037927936,),
]
)], all_chunks)
def test_floats(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = [
("CREATE TABLE my_table_1 (my_col_a real);", ()),
("INSERT INTO my_table_1 VALUES (0.5123), (-0.1)", ()),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=page_size))
self.assertEqual([(
'my_table_1',
(
column_constructor(cid=0, name='my_col_a', type='real', notnull=0, dflt_value=None, pk=0),
),
[
(0.5123,),
(-0.1,),
]
)], all_chunks)
def test_blobs(self):
small_blob = b'Something'
big_blob = b'E' * 99999999 # Can't be much bigger on CircleCI
for page_size, chunk_size in itertools.product(
[512, 65536,],
[131072,],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = [
("CREATE TABLE my_table_1 (my_col_a blob);", ()),
("INSERT INTO my_table_1 VALUES (?), (?)", (b'Something', big_blob)),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1048576000))
self.assertEqual([(
'my_table_1',
(
column_constructor(cid=0, name='my_col_a', type='blob', notnull=0, dflt_value=None, pk=0),
),
[
(small_blob,),
(big_blob,),
]
)], all_chunks)
def test_many_small_tables(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = flatten((
[
("CREATE TABLE my_table_{} (my_text_col_a text, my_text_col_b text);".format(i), ()),
("INSERT INTO my_table_{} VALUES ('some-text-a', 'some-text-b');".format(i), ()),
]
for i in range(1, 101)
))
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1000000))
self.assertEqual([(
'my_table_{}'.format(i),
(
column_constructor(cid=0, name='my_text_col_a', type='text', notnull=0, dflt_value=None, pk=0),
column_constructor(cid=1, name='my_text_col_b', type='text', notnull=0, dflt_value=None, pk=0),
),
[('some-text-a', 'some-text-b')],
) for i in range(1, 101)], all_chunks)
def test_many_large_tables_intermingled_no_recursion_error(self):
# This test runs a particular risk of recursion error
blob = b'E' * 1000000
for page_size, chunk_size in itertools.product(
[512, 65536],
[131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = flatten((
[
("CREATE TABLE my_table_{} (my_blob blob);".format(i), ()),
("INSERT INTO my_table_{} VALUES (?);".format(i), (blob,)),
]
for i in range(1, 1001)
))
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1000000000))
self.assertEqual([(
'my_table_{}'.format(i),
(
column_constructor(cid=0, name='my_blob', type='blob', notnull=0, dflt_value=None, pk=0),
),
[(blob,)],
) for i in range(1, 1001)], all_chunks)
def test_large_table(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a text, my_text_col_b text);", ()),
] + [
("INSERT INTO my_table_1 VALUES ('some-text-a', 'some-text-b')", ()),
] * 1000
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=page_size))
self.assertEqual(
[('some-text-a', 'some-text-b') for i in range (1, 1001)],
all_chunks[0][2],
)
def test_index(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("CREATE TABLE my_table_1 (my_col_a integer);", ())] +
[
("INSERT INTO my_table_1 VALUES ({});".format(i), ())
for i in range(0, 1024)
] +
[("CREATE INDEX my_index ON my_table_1(my_col_a);", ())]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=page_size))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='integer', notnull=0, dflt_value=None, pk=0),
),
[(i,) for i in range(0, 1024)],
)], all_chunks)
def test_index_overflow_few(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("CREATE TABLE my_table_1 (my_col_a text);", ())] +
[
("INSERT INTO my_table_1 VALUES ('{}'), ('{}');".format('a' * 20000, 'b' * 20000), ()),
] +
[("CREATE INDEX my_index ON my_table_1(my_col_a);", ())]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1048576))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='text', notnull=0, dflt_value=None, pk=0),
),
[
('a' * 20000,),
('b' * 20000,),
],
)], all_chunks)
def test_index_interior_overflow_many(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("CREATE TABLE my_table_1 (my_col_a text);", ())] +
[
("INSERT INTO my_table_1 VALUES ('{}');".format(str(i) * 2000), ())
for i in range(0, 100)
] +
[("CREATE INDEX my_index ON my_table_1(my_col_a);", ())]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1048576))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='text', notnull=0, dflt_value=None, pk=0),
),
[
(str(i) * 2000,)
for i in range(0, 100)
],
)], all_chunks)
def test_integer_primary_key(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("CREATE TABLE my_table_1 (my_col_a integer primary key);", ())] +
[
("INSERT INTO my_table_1 VALUES ({});".format(i), ())
for i in range(0, 100)
]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1048576))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='integer', notnull=0, dflt_value=None, pk=1),
),
[
(i,)
for i in range(0, 100)
],
)], all_chunks)
def test_alter_table_integer(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("CREATE TABLE my_table_1 (my_col_a integer);", ())] +
[
("INSERT INTO my_table_1 VALUES ({});".format(i), ())
for i in range(0, 100)
] +
[("ALTER TABLE my_table_1 ADD COLUMN my_col_b integer DEFAULT 10;", ())]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1048576))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='integer', notnull=0, dflt_value=None, pk=0),
column_constructor(cid=1, name='my_col_b', type='integer', notnull=0, dflt_value='10', pk=0),
),
[
(i, 10)
for i in range(0, 100)
],
)], all_chunks)
def test_alter_table_text(self):
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192, 16384, 32768, 65536],
[1, 2, 3, 5, 7, 32, 131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("CREATE TABLE my_table_1 (my_col_a integer);", ())] +
[
("INSERT INTO my_table_1 VALUES ({});".format(i), ())
for i in range(0, 100)
] +
[("ALTER TABLE my_table_1 ADD COLUMN my_col_b integer DEFAULT '10';", ())]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1048576))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='integer', notnull=0, dflt_value=None, pk=0),
column_constructor(cid=1, name='my_col_b', type='integer', notnull=0, dflt_value="'10'", pk=0),
),
[
(i, '10')
for i in range(0, 100)
],
)], all_chunks)
def test_analyze(self):
for page_size, chunk_size in itertools.product(
[65536],
[131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("CREATE TABLE my_table_1 (my_col_a integer primary key);", ())] +
[
("INSERT INTO my_table_1 VALUES ({});".format(i), ())
for i in range(0, 100)
] + [
("ANALYZE;", ()),
]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=1048576))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='integer', notnull=0, dflt_value=None, pk=1),
),
[
(i,)
for i in range(0, 100)
],
)], all_chunks)
def test_with_pointermap_pages(self):
string = '-' * 100000
for page_size, chunk_size in itertools.product(
[512, 1024, 4096, 8192],
[512],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
sqls = (
[("PRAGMA auto_vacuum = FULL;", ())] +
[("CREATE TABLE my_table_1 (my_col_a text);", ())] +
[
("INSERT INTO my_table_1 VALUES ('{}');".format(string), ())
for i in range(0, 200)
]
)
all_chunks = tables_list(stream_sqlite(db(sqls, page_size, chunk_size), max_buffer_size=20971520))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='text', notnull=0, dflt_value=None, pk=0),
),
[(string,) for i in range(0, 200)],
)], all_chunks)
def test_iterable_finishes(self):
for page_size, chunk_size in itertools.product(
[65536],
[131072],
):
with self.subTest(page_size=page_size, chunk_size=chunk_size):
finished = False
def with_finished(db):
nonlocal finished
yield from db
finished = True
sqls = (
[("CREATE TABLE my_table_1 (my_col_a integer primary key);", ())] +
[
("INSERT INTO my_table_1 VALUES ({});".format(i), ())
for i in range(0, 100)
]
)
all_chunks = tables_list(stream_sqlite(with_finished(db(sqls, page_size, chunk_size)), max_buffer_size=1048576))
self.assertEqual([(
"my_table_1",
(
column_constructor(cid=0, name='my_col_a', type='integer', notnull=0, dflt_value=None, pk=1),
),
[
(i,)
for i in range(0, 100)
],
)], all_chunks)
self.assertTrue(finished)
def test_freelist(self):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a text, my_text_col_b text);", ()),
] + [
("INSERT INTO my_table_1 VALUES " + ','.join(["('some-text-a', 'some-text-b')"] * 500), ()),
] * 1000 + [
("DELETE FROM my_table_1", ()),
]
all_chunks = tables_list(stream_sqlite(db(sqls, page_size=1024, chunk_size=131072), max_buffer_size=20971520))
self.assertEqual([], all_chunks)
with self.assertRaises(ValueError):
all_chunks = tables_list(stream_sqlite(db(sqls, page_size=1024, chunk_size=131072), max_buffer_size=1048576))
def test_truncated(self):
with self.assertRaises(ValueError):
next(stream_sqlite([b'too-short'], max_buffer_size=20971520))
def test_bad_header(self):
with self.assertRaises(ValueError):
next(stream_sqlite([b'0123456789'] * 10, max_buffer_size=20971520))
def test_bad_encoding(self):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a text, my_text_col_b text);", ()),
] + [
("INSERT INTO my_table_1 VALUES ('some-text-a', 'some-text-b')", ()),
]
db_bytes = bytearray(b''.join(db(sqls, page_size=1024, chunk_size=131072)))
db_bytes[56] = 99
with self.assertRaises(ValueError):
next(tables_list(stream_sqlite([db_bytes], max_buffer_size=20971520)))
def test_bad_usable_space(self):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a text, my_text_col_b text);", ()),
] + [
("INSERT INTO my_table_1 VALUES ('some-text-a', 'some-text-b')", ()),
]
db_bytes = bytearray(b''.join(db(sqls, page_size=1024, chunk_size=131072)))
db_bytes[20] = 1
with self.assertRaises(ValueError):
next(stream_sqlite([db_bytes], max_buffer_size=20971520))
def test_unused_page(self):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a text, my_text_col_b text);", ()),
] + [
("INSERT INTO my_table_1 VALUES " + ','.join(["('some-text-a', 'some-text-b')"] * 500), ()),
] * 1000 + [
("DELETE FROM my_table_1", ()),
]
db_bytes = bytearray(b''.join(db(sqls, page_size=1024, chunk_size=131072)))
db_bytes[32:36] = b'\x99\x00\x00\x00'
with self.assertRaises(ValueError):
next(tables_list(stream_sqlite([db_bytes], max_buffer_size=20971520)))
def test_expected_page_unprocessed(self):
sqls = [
("CREATE TABLE my_table_1 (my_text_col_a text, my_text_col_b text);", ()),
]
db_bytes = bytearray(b''.join(db(sqls, page_size=1024, chunk_size=131072)))
db_bytes[32:36] = b'\x99\x00\x00\x00'
with self.assertRaises(ValueError):
next(tables_list(stream_sqlite([db_bytes], max_buffer_size=20971520)))
def test_lock_byte_page(self):
def get_file_bytes():
with open('fixtures/large.sqlite.gz', 'rb') as f:
while True:
chunk = f.read(131072)
if not chunk:
break
yield chunk
def get_sqlite_bytes(file_bytes):
obj = zlib.decompressobj(32 + zlib.MAX_WBITS)
for gzipped_chunk in file_bytes:
chunk = obj.decompress(gzipped_chunk)
if chunk:
yield chunk
file_bytes = get_file_bytes()
sqlite_bytes = get_sqlite_bytes(file_bytes)
count = 0
for table_row, table_info, rows in stream_sqlite(sqlite_bytes, max_buffer_size=838860800):
for row in rows:
count += 1
self.assertEqual(count, 1200000)
def test_lock_byte_page_with_autovaccum(self):
def get_file_bytes():
with open('fixtures/large-autovacuum-1024.sqlite.gz', 'rb') as f:
while True:
chunk = f.read(131072)
if not chunk:
break
yield chunk
def get_sqlite_bytes(file_bytes):
obj = zlib.decompressobj(32 + zlib.MAX_WBITS)
for gzipped_chunk in file_bytes:
chunk = obj.decompress(gzipped_chunk)
if chunk:
yield chunk
file_bytes = get_file_bytes()
sqlite_bytes = get_sqlite_bytes(file_bytes)
count = 0
for table_row, table_info, rows in stream_sqlite(sqlite_bytes, max_buffer_size=209715200):
for row in rows:
count += 1
self.assertEqual(count, 1200000)
def db(sqls, page_size, chunk_size):
with tempfile.NamedTemporaryFile() as fp:
with sqlite3.connect(fp.name, isolation_level=None) as con:
cur = con.cursor()
cur.execute('PRAGMA page_size = {};'.format(page_size))
for sql, bindings in sqls:
cur.execute(sql, bindings)
with open(fp.name, 'rb') as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
yield chunk
def tables_list(table_iter):
return [
(table_name, table_info, list(table_rows))
for table_name, table_info, table_rows in table_iter
]
def flatten(l):
return [
item
for sublist in l
for item in sublist
]