-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpattern.py
More file actions
1170 lines (950 loc) · 43.7 KB
/
Copy pathpattern.py
File metadata and controls
1170 lines (950 loc) · 43.7 KB
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Three-Phase CDC Pattern for SQLite
This module provides a Change Data Capture (CDC) implementation using a three-phase
versioning system. The pattern maintains up to three versions of each row across
different phases (0, 1, 2) to enable atomic changeset generation and delta compression.
This code is not intended for production use, but serves as a demonstration of
the pattern and tests its correctness.
"""
import argparse
import random
import sqlite3
import sys
import time
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Iterator, List, Literal, Tuple, Union
import fossil_delta
# Add parent directory to path to import testlib
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from testlib import (
assert_tables_equal,
compute_table_hash,
generate_random_workload,
sqlite3_test_db,
)
@dataclass
class InsertOp:
"""Represents an insert operation in a changeset."""
id: int
data: str
@dataclass
class UpdateOp:
"""Represents an update operation with delta compression in a changeset."""
id: int
delta: bytes
@dataclass
class DeleteOp:
"""Represents a delete operation in a changeset."""
id: int
# Type alias for changeset operations
ChangesetOp = Union[InsertOp, UpdateOp, DeleteOp]
# Type alias for a changeset, mapping table names to lists of operations
Changeset = Dict[str, List[ChangesetOp]]
SAMPLE_TABLE_NAME = "AppTable"
SAMPLE_BASELINE_ROWS: List[Tuple[int, str]] = [
(1, "order-100:status=pending"),
(2, "order-200:status=pending"),
(3, "order-300:status=paid"),
(4, "order-400:status=pending"),
(5, "order-500:status=cancelled"),
]
SAMPLE_PENDING_UPSERTS: List[Tuple[int, str]] = [
(2, "order-200:status=shipped"),
(6, "order-600:status=pending"),
]
SAMPLE_PENDING_DELETES: List[int] = [4]
def setup_three_phase_table(conn: sqlite3.Connection, table_name: str) -> str:
"""
Create a three-phase table with phase and deleted columns.
This creates a simple table with:
- id INTEGER: primary key
- data BLOB: single data column
- phase/deleted: three-phase pattern columns
Args:
conn: SQLite database connection
table_name: Name of the table to create
Returns:
The name of the created table.
"""
sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
-- Application columns
id INTEGER,
data BLOB,
-- Three-phase pattern columns:
-- phase represents the current phase of the row
-- 0: new row, not seen by any changeset
-- 1: row version used by an in-progress changeset
-- 2: stable row version
phase INTEGER NOT NULL DEFAULT 0,
-- deleted indicates whether the row is logically deleted
-- the row will be removed after reaching phase 2
deleted BOOL NOT NULL DEFAULT 0,
-- The primary key must include phase, as there are now up to 3 copies
-- of each row depending on snapshot state
PRIMARY KEY (id, phase)
)
"""
conn.execute(sql)
return table_name
def setup_regular_table(conn: sqlite3.Connection, table_name: str) -> str:
"""
Create a regular table without three-phase columns.
This creates a simple table with:
- id INTEGER: primary key
- data BLOB: single data column
Args:
conn: SQLite database connection
table_name: Name of the table to create
Returns:
The name of the created table.
"""
sql = f"""
CREATE TABLE IF NOT EXISTS {table_name} (
id INTEGER PRIMARY KEY,
data BLOB
)
"""
conn.execute(sql)
return table_name
def setup_sample_database(db_path: Path, table_name: str = SAMPLE_TABLE_NAME) -> Path:
"""
Create a new SQLite database with schema and sample three-phase data.
The database includes a phase-2 baseline plus a few pending phase-0 mutations
so it is immediately useful for testing changeset generation.
Args:
db_path: Filesystem path to create the database at
table_name: Name of the sample table to create
Returns:
The created database path.
Raises:
FileExistsError: If the database path already exists
FileNotFoundError: If the parent directory does not exist
sqlite3.Error: If SQLite setup fails
"""
target_path = db_path.expanduser().resolve(strict=False)
if target_path.exists():
raise FileExistsError(f"Database already exists: {target_path}")
if not target_path.parent.exists():
raise FileNotFoundError(f"Parent directory does not exist: {target_path.parent}")
conn = sqlite3.connect(target_path)
try:
setup_three_phase_table(conn, table_name)
for row_id, data_value in SAMPLE_BASELINE_ROWS:
insert_or_update(conn, table_name, row_id, data_value)
# Promote baseline rows to phase 2.
with changeset(conn, table_name):
pass
# Leave pending phase-0 writes to demonstrate a realistic in-flight state.
for row_id, data_value in SAMPLE_PENDING_UPSERTS:
insert_or_update(conn, table_name, row_id, data_value)
for row_id in SAMPLE_PENDING_DELETES:
logical_delete(conn, table_name, row_id)
conn.commit()
except Exception:
conn.close()
target_path.unlink(missing_ok=True)
raise
conn.close()
return target_path
def insert_or_update(conn: sqlite3.Connection, table_name: str, row_id: int, data: str) -> None:
"""
Insert or update a row using upsert operation targeting phase 0.
Note: This operation must set `deleted=0` in case the row was previously
logically deleted and is still in phase 0.
Args:
conn: SQLite database connection
table_name: Name of the table
row_id: Primary key value
data: Data value to insert/update
"""
sql = f"""
INSERT INTO {table_name} (id, data)
VALUES (?, ?)
ON CONFLICT (id, phase) DO UPDATE SET data = excluded.data, deleted = 0
"""
conn.execute(sql, (row_id, data))
def logical_delete(conn: sqlite3.Connection, table_name: str, row_id: int) -> None:
"""
Logically delete a row by setting deleted=1 with phase=0.
Note: We also clear the data column to save space. This is an optimization
that is not needed for correctness as the row is no longer logically visible
to queries and will eventually be removed either during compaction or after
it reaches phase 2.
Args:
conn: SQLite database connection
table_name: Name of the table
row_id: Primary key value to delete
"""
sql = f"""
INSERT INTO {table_name} (id, deleted)
VALUES (?, 1)
ON CONFLICT (id, phase) DO UPDATE SET
deleted = excluded.deleted,
data = NULL
"""
conn.execute(sql, (row_id,))
def read_latest(conn: sqlite3.Connection, table_name: str, row_id: int) -> str | None:
"""
Read the latest version of a row (lowest phase, not deleted).
Args:
conn: SQLite database connection
table_name: Name of the table
row_id: Primary key value to read
Returns:
Data value, or None if not found/deleted
"""
sql = f"""
SELECT data FROM (
SELECT * FROM {table_name}
WHERE id = ?
ORDER BY phase ASC
LIMIT 1
) WHERE deleted = 0
"""
cursor = conn.execute(sql, (row_id,))
row = cursor.fetchone()
if row is None:
return None
return row[0]
def read_all_latest(conn: sqlite3.Connection, table_name: str) -> List[Tuple[str, str]]:
"""
Read the latest version of every row (lowest phase, not deleted).
Args:
conn: SQLite database connection
table_name: Name of the table
Returns:
List of data values
"""
# This query takes advantage of how SQLite handles bare-columns in an
# aggregate query to select the data and deleted columns corresponding to
# the minimum phase for each unique id.
# Documented here: https://www.sqlite.org/lang_select.html#bareagg
sql = f"""
SELECT id, data
FROM (
SELECT id, data, deleted, MIN(phase) AS min_phase
FROM {table_name}
GROUP BY id
)
WHERE deleted = 0
"""
cursor = conn.execute(sql)
rows = cursor.fetchall()
return rows
@contextmanager
def changeset(conn: sqlite3.Connection, table_name: str) -> Iterator[List[ChangesetOp]]:
"""
Context manager for atomic changeset generation with delta compression.
This context manager:
1. Transitions all phase 0 rows to phase 1
2. Generates changeset by comparing phase 1 and phase 2 rows
3. Computes deltas for updates using fossil delta algorithm
4. Automatically cleans up processed changes
Args:
conn: SQLite database connection
table_name: Name of the table to generate changeset for
Yields:
List of changeset operations (InsertOp, UpdateOp, DeleteOp)
"""
operations = []
# Step 1: Transition phase 0 rows to phase 1
#
# Note: If any rows exist in phase 1 then the last changeset operation failed to complete
# In this case we have to recover
with conn:
# Remove any rows in phase 1 which were overwritten in phase 0
conn.execute(f"""
DELETE FROM {table_name}
WHERE phase = 1 AND id IN (SELECT id FROM {table_name} WHERE phase = 0)
""")
# Finally migrate phase 0 rows to phase 1
conn.execute(f"UPDATE {table_name} SET phase = 1 WHERE phase = 0")
# Step 2: Generate changeset using single query per SQL pattern
with conn:
changeset_sql = f"""
SELECT
IFNULL(before.id, after.id) as id,
before.data as data_before,
after.data as data_after,
after.deleted as deleted
FROM
(SELECT * FROM {table_name} WHERE phase = 2) as before
RIGHT JOIN
(SELECT * FROM {table_name} WHERE phase = 1) as after
USING (id)
"""
cursor = conn.execute(changeset_sql)
rows = cursor.fetchall()
for row in rows:
row_id, data_before, data_after, deleted = row
# Determine operation type
if deleted:
operations.append(DeleteOp(id=row_id))
elif data_before is None:
# New row (insert) - no corresponding phase 2 row
operations.append(InsertOp(id=row_id, data=data_after))
else:
# Updated row - compute delta
before_bytes = (
data_before
if isinstance(data_before, bytes)
else str(data_before).encode("utf-8")
)
after_bytes = (
data_after if isinstance(data_after, bytes) else str(data_after).encode("utf-8")
)
delta = fossil_delta.create_delta(before_bytes, after_bytes)
operations.append(UpdateOp(id=row_id, delta=delta))
try:
yield operations
except:
# If we crash while yielding the changeset do nothing - we don't want to
# lose changes
raise
else:
# Step 3: Cleanup: remove deleted rows and then move all alive rows to phase=2
with conn:
# First we need to remove three classes of rows:
# 1. deleted phase=1 rows
# 2. phase=2 rows which are being updated/deleted by a phase=1 row
conn.execute(f"""
DELETE FROM {table_name} as outer
WHERE
(
-- First case: deleted phase=1 rows
phase = 1 AND deleted = 1
) OR (
-- Second case: phase=2 rows which are in phase=1
phase = 2 AND EXISTS (
SELECT * FROM {table_name} as inner
WHERE outer.id = inner.id AND phase = 1
)
)
""")
# Migrate phase 1 rows to phase 2
conn.execute(f"UPDATE {table_name} SET phase = 2 WHERE phase = 1")
def compact(conn: sqlite3.Connection, table_name: str) -> None:
"""
Compact a three-phase table down to only phase=2 rows.
This operation keeps the latest version of each row and sets its phase to 2.
It should be used to take periodic checkpoints to prevent delta histories
from growing too large.
Args:
conn: SQLite database connection
table_name: Name of the table to compact
"""
with conn:
# First delete all rows which are either logically deleted or not the latest version
conn.execute(f"""
DELETE FROM {table_name}
WHERE (id, phase) IN (
SELECT id, phase FROM (
SELECT id, phase, deleted, ROW_NUMBER() OVER (PARTITION BY id ORDER BY phase ASC) AS rn
FROM {table_name}
)
WHERE (rn = 1 AND deleted = 1) OR (rn > 1)
)
""")
# Then update the remaining rows to phase 2
conn.execute(f"UPDATE {table_name} SET phase = 2")
def apply_changeset_operation(
conn: sqlite3.Connection, table_name: str, operation: ChangesetOp
) -> None:
"""
Apply a single changeset operation directly to phase=2.
This function is used for replication scenarios where changesets are applied
on top of a clean checkpoint without intermediate operations.
Args:
conn: SQLite database connection
table_name: Name of the table
operation: The operation to apply
"""
if isinstance(operation, InsertOp):
# Insert directly as phase=2
sql = f"""
INSERT INTO {table_name} (id, data, phase)
VALUES (?, ?, 2)
"""
conn.execute(sql, (operation.id, operation.data))
elif isinstance(operation, UpdateOp):
# Read current data and apply delta, then update phase=2 row
current_data = read_latest(conn, table_name, operation.id)
if current_data is not None:
current_bytes = (
current_data
if isinstance(current_data, bytes)
else str(current_data).encode("utf-8")
)
updated_bytes = fossil_delta.apply_delta(current_bytes, operation.delta)
updated_data = (
updated_bytes.decode("utf-8") if isinstance(updated_bytes, bytes) else updated_bytes
)
# Update the phase=2 row directly
sql = f"UPDATE {table_name} SET data = ? WHERE id = ? AND phase = 2"
conn.execute(sql, (updated_data, operation.id))
elif isinstance(operation, DeleteOp):
# Remove the phase=2 row completely for deletes
sql = f"DELETE FROM {table_name} WHERE id = ? AND phase = 2"
conn.execute(sql, (operation.id,))
def apply_operation_to_regular_table(
conn: sqlite3.Connection,
table_name: str,
operation: Literal["upsert", "delete"],
rowid: int,
data: str = "",
) -> None:
"""
Apply an operation to a regular table (without three-phase columns).
Args:
conn: SQLite database connection
table_name: Name of the regular table
operation: The operation to apply
rowid: Primary key value for the operation
data: Data value for insert/update operations (default empty string)
"""
if operation == "upsert":
# Insert or update (upsert)
sql = f"""
INSERT INTO {table_name} (id, data)
VALUES (?, ?)
ON CONFLICT (id) DO UPDATE SET data = excluded.data
"""
conn.execute(sql, (rowid, data))
elif operation == "delete":
# Delete the row
sql = f"DELETE FROM {table_name} WHERE id = ?"
conn.execute(sql, (rowid,))
def test_random_workload(
seed: int | None = None,
operations: int = 200_000,
max_id: int = 100,
replication_probability: float = 0.1,
) -> None:
"""
Test the three-phase pattern with a random workload.
This test:
1. Applies a random workload stream to a three-phase table
2. Applies the same stream to a regular table (without three-phase columns)
3. Replicates the three-phase table to a second database using changesets
4. Sends changesets to the replica with some probability after each operation
5. Runs for a configurable duration
6. Compacts both three-phase tables after the workload
7. Compares the hash of all three tables
Args:
seed: Random seed for reproducible results
duration_seconds: How long to run the workload
max_id: Maximum ID value to use in workload
replication_probability: Probability of sending changeset to replica after each op
"""
if seed is None:
seed = random.randint(0, 2**32 - 1)
print(f" Running random workload test with {operations} ops...")
print(
f" Using max_id={max_id}, replication_probability={replication_probability}, seed={seed}"
)
# Create databases
with (
sqlite3_test_db() as writer_conn,
sqlite3_test_db() as replica_conn,
sqlite3_test_db() as regular_conn,
):
# Set up tables
writer_table = setup_three_phase_table(writer_conn, "WorkloadTable")
replica_table = setup_three_phase_table(replica_conn, "WorkloadTable")
regular_table = setup_regular_table(regular_conn, "WorkloadTable")
# Counters
operation_count = 0
changeset_count = 0
# Generate workload and apply it
start_time = time.time()
workload_gen = generate_random_workload(max_id, seed)
while operation_count < operations:
# Get next operation
operation_type, row_id, data = next(workload_gen)
operation_count += 1
# Apply to three-phase table
if operation_type == "upsert":
insert_or_update(writer_conn, writer_table, row_id, data)
elif operation_type == "delete":
logical_delete(writer_conn, writer_table, row_id)
# Apply operation to regular table
apply_operation_to_regular_table(
regular_conn, regular_table, operation_type, row_id, data
)
# Randomly decide whether to replicate
if random.random() < replication_probability:
changeset_count += 1
# Generate changeset and apply to replica
with changeset(writer_conn, writer_table) as changeset_ops:
for op in changeset_ops:
apply_changeset_operation(replica_conn, replica_table, op)
# Final replication to ensure replica is up to date
with changeset(writer_conn, writer_table) as final_changeset:
changeset_count += 1
for op in final_changeset:
apply_changeset_operation(replica_conn, replica_table, op)
print(f" Applied {operation_count} operations in {time.time() - start_time:.2f} seconds")
print(f" Applied {changeset_count} changesets to replica")
# Compact both three-phase tables
compact(writer_conn, writer_table)
compact(replica_conn, replica_table)
# Verify all three tables are equal
assert_tables_equal(
"writer and replica tables mismatch",
writer_conn,
writer_table,
replica_conn,
replica_table,
)
assert_tables_equal(
"writer and regular tables mismatch",
writer_conn,
writer_table,
regular_conn,
regular_table,
["id", "data"],
)
print(" ✓ All table hashes match! Random workload test passed.")
def run_example() -> None:
"""
Demonstrate the three-phase CDC pattern with a complete example.
"""
with sqlite3_test_db() as conn:
# Create example table
table_name = setup_three_phase_table(conn, SAMPLE_TABLE_NAME)
# Insert initial data
initial_data = [
(1, "data 1; revision 1"),
(2, "data 2; revision 1"),
(3, "data 3; revision 1"),
(4, "data 4; revision 1"),
(5, "data 5; revision 1"),
]
for row_id, data_value in initial_data:
insert_or_update(conn, table_name, row_id, data_value)
print("Initial data inserted")
# Create initial changeset to establish phase 2 baseline
with changeset(conn, table_name) as ops:
print(f"Initial changeset created with {len(ops)} inserts")
# Make some modifications
insert_or_update(conn, table_name, 1, "data 1; revision 2")
insert_or_update(conn, table_name, 3, "data 3; revision 2")
insert_or_update(conn, table_name, 5, "data 5; revision 2")
insert_or_update(conn, table_name, 6, "data 6; revision 1")
insert_or_update(conn, table_name, 7, "data 7; revision 1")
logical_delete(conn, table_name, 2)
print("Modifications made")
# Generate changeset
with changeset(conn, table_name) as operations:
print(f"\nGenerated changeset with {len(operations)} operations:")
for op in operations:
if isinstance(op, InsertOp):
print(f" Insert id={op.id}: {op.data}")
elif isinstance(op, UpdateOp):
print(f" Update id={op.id}: delta={len(op.delta)} bytes")
elif isinstance(op, DeleteOp):
print(f" Delete id={op.id}")
print("\nChangeset completed and cleanup performed")
# Demonstrate compaction
print("\nBefore compaction:")
cursor = conn.execute(f"SELECT COUNT(*) FROM {table_name}")
row_count = cursor.fetchone()[0]
print(f"Total rows: {row_count}")
compact(conn, table_name)
print("After compaction:")
cursor = conn.execute(f"SELECT COUNT(*) FROM {table_name}")
row_count = cursor.fetchone()[0]
print(f"Total rows: {row_count}")
cursor = conn.execute(f"SELECT COUNT(*) FROM {table_name} WHERE phase = 2")
phase2_count = cursor.fetchone()[0]
print(f"Phase 2 rows: {phase2_count}")
print("Table compacted to latest versions only")
def test_pattern():
"""
Test suite focused on three-phase pattern correctness.
"""
def test_basic_operations():
"""Test basic insert, update, and delete operations."""
with sqlite3_test_db() as conn:
table_name = setup_three_phase_table(conn, SAMPLE_TABLE_NAME)
# Test insert
insert_or_update(conn, table_name, 1, "test data")
result = read_latest(conn, table_name, 1)
assert result == "test data", f"Expected 'test data', got {result}"
# Test update
insert_or_update(conn, table_name, 1, "updated data")
result = read_latest(conn, table_name, 1)
assert result == "updated data", f"Expected 'updated data', got {result}"
# Test delete
logical_delete(conn, table_name, 1)
result = read_latest(conn, table_name, 1)
assert result is None, f"Expected None after delete, got {result}"
print("✓ Basic operations test passed")
def test_read_all_latest():
"""Test read_all_latest."""
with sqlite3_test_db() as conn:
table_name = setup_three_phase_table(conn, SAMPLE_TABLE_NAME)
# insert two rows
insert_or_update(conn, table_name, 1, "bob")
insert_or_update(conn, table_name, 2, "alice")
rows = read_all_latest(conn, table_name)
assert len(rows) == 2, f"Expected 2 rows, got {len(rows)}"
assert rows[0] == (1, "bob"), f"Expected (1, 'bob'), got {rows[0]}"
assert rows[1] == (2, "alice"), f"Expected (2, 'alice'), got {rows[1]}"
# compact them to phase = 2
with changeset(conn, table_name):
pass
rows = read_all_latest(conn, table_name)
assert len(rows) == 2, f"Expected 2 rows, got {len(rows)}"
assert rows[0] == (1, "bob"), f"Expected (1, 'bob'), got {rows[0]}"
assert rows[1] == (2, "alice"), f"Expected (2, 'alice'), got {rows[1]}"
# delete row 1, update row 2, insert row 3
logical_delete(conn, table_name, 1)
insert_or_update(conn, table_name, 2, "alice++")
insert_or_update(conn, table_name, 3, "jones")
rows = read_all_latest(conn, table_name)
assert len(rows) == 2, f"Expected 2 rows, got {len(rows)}"
assert rows[0] == (2, "alice++"), f"Expected (2, 'alice++'), got {rows[0]}"
assert rows[1] == (3, "jones"), f"Expected (3, 'jones'), got {rows[1]}"
print("✓ read all latest test passed")
def test_changeset_generation():
"""Test changeset generation with mixed operations."""
with sqlite3_test_db() as conn:
table_name = setup_three_phase_table(conn, SAMPLE_TABLE_NAME)
# Insert initial data
insert_or_update(conn, table_name, 1, "data1")
insert_or_update(conn, table_name, 2, "data2")
insert_or_update(conn, table_name, 3, "data3")
# Create first changeset to establish phase 2 rows
with changeset(conn, table_name) as ops:
assert len(ops) == 3, f"Expected 3 initial operations, got {len(ops)}"
# Make changes
insert_or_update(conn, table_name, 1, "updated1") # Update
insert_or_update(conn, table_name, 4, "data4") # Insert
logical_delete(conn, table_name, 3) # Delete
# Generate changeset
with changeset(conn, table_name) as operations:
assert len(operations) == 3, f"Expected 3 operations, got {len(operations)}"
# Check operation types
inserts = [op for op in operations if isinstance(op, InsertOp)]
updates = [op for op in operations if isinstance(op, UpdateOp)]
deletes = [op for op in operations if isinstance(op, DeleteOp)]
assert len(inserts) == 1, f"Expected 1 insert, got {len(inserts)}"
assert len(updates) == 1, f"Expected 1 update, got {len(updates)}"
assert len(deletes) == 1, f"Expected 1 delete, got {len(deletes)}"
# Verify specific operations
insert_op = inserts[0]
assert insert_op.id == 4, f"Expected insert id=4, got {insert_op.id}"
assert insert_op.data == "data4"
update_op = updates[0]
assert update_op.id == 1, f"Expected update id=1, got {update_op.id}"
delete_op = deletes[0]
assert delete_op.id == 3, f"Expected delete id=3, got {delete_op.id}"
print("✓ Changeset generation test passed")
def test_replication():
"""Test replication between writer and replica using changesets and compaction."""
# Create writer and replica databases
with sqlite3_test_db() as writer_conn, sqlite3_test_db() as replica_conn:
writer_table = setup_three_phase_table(writer_conn, SAMPLE_TABLE_NAME)
replica_table = setup_three_phase_table(replica_conn, SAMPLE_TABLE_NAME)
# === CHECKPOINT 1: Initial data ===
print(" Checkpoint 1: Initial data")
initial_data = [
(1, "data1_v1"),
(2, "data2_v1"),
(3, "data3_v1"),
(4, "data4_v1"),
]
for row_id, data_value in initial_data:
insert_or_update(writer_conn, writer_table, row_id, data_value)
# Create checkpoint and replicate
with changeset(writer_conn, writer_table) as operations:
for op in operations:
apply_changeset_operation(replica_conn, replica_table, op)
# Verify tables match
compact(writer_conn, writer_table)
compact(replica_conn, replica_table)
writer_hash = compute_table_hash(writer_conn, writer_table)
replica_hash = compute_table_hash(replica_conn, replica_table)
assert writer_hash == replica_hash, "Tables don't match after checkpoint 1"
# === CHECKPOINT 2: Updates and new data ===
print(" Checkpoint 2: Updates and new data")
insert_or_update(writer_conn, writer_table, 1, "data1_v2") # Update
insert_or_update(writer_conn, writer_table, 2, "data2_v2") # Update
insert_or_update(writer_conn, writer_table, 5, "data5_v1") # Insert
logical_delete(writer_conn, writer_table, 4) # Delete
with changeset(writer_conn, writer_table) as operations:
for op in operations:
apply_changeset_operation(replica_conn, replica_table, op)
# Verify tables match
compact(writer_conn, writer_table)
compact(replica_conn, replica_table)
writer_hash = compute_table_hash(writer_conn, writer_table)
replica_hash = compute_table_hash(replica_conn, replica_table)
assert writer_hash == replica_hash, "Tables don't match after checkpoint 2"
# === CHECKPOINT 3: Replicate multiple changesets ===
print(" Checkpoint 3: Complex changes")
insert_or_update(writer_conn, writer_table, 1, "data1_v3") # Update again
insert_or_update(writer_conn, writer_table, 6, "data6_v1") # Insert
insert_or_update(writer_conn, writer_table, 7, "data7_v1") # Insert
with changeset(writer_conn, writer_table) as operations:
for op in operations:
apply_changeset_operation(replica_conn, replica_table, op)
logical_delete(writer_conn, writer_table, 3) # Delete
insert_or_update(writer_conn, writer_table, 8, "data8_v1") # Insert
logical_delete(writer_conn, writer_table, 7) # Delete what we just inserted
with changeset(writer_conn, writer_table) as operations:
for op in operations:
apply_changeset_operation(replica_conn, replica_table, op)
# Final verification
compact(writer_conn, writer_table)
compact(replica_conn, replica_table)
writer_hash = compute_table_hash(writer_conn, writer_table)
replica_hash = compute_table_hash(replica_conn, replica_table)
assert writer_hash == replica_hash, "Tables don't match after checkpoint 3"
# Verify final state manually
expected_data = {
1: "data1_v3", # Updated twice
2: "data2_v2", # Updated once
5: "data5_v1", # Inserted
6: "data6_v1", # Inserted
8: "data8_v1", # Inserted
# 3, 4, 7 were deleted
}
for row_id, expected_value in expected_data.items():
writer_result = read_latest(writer_conn, writer_table, row_id)
replica_result = read_latest(replica_conn, replica_table, row_id)
assert writer_result == expected_value, (
f"Writer row {row_id}: expected {expected_value}, got {writer_result}"
)
assert replica_result == expected_value, (
f"Replica row {row_id}: expected {expected_value}, got {replica_result}"
)
# Verify deleted rows
for deleted_id in [3, 4, 7]:
assert read_latest(writer_conn, writer_table, deleted_id) is None
assert read_latest(replica_conn, replica_table, deleted_id) is None
print("✓ Replication test passed")
def test_phase_isolation():
"""Test that concurrent writes don't interfere with changeset generation."""
with sqlite3_test_db() as conn:
table_name = setup_three_phase_table(conn, SAMPLE_TABLE_NAME)
# Insert initial data
insert_or_update(conn, table_name, 1, "data1")
# Create baseline changeset
with changeset(conn, table_name):
pass
# Make a change
insert_or_update(conn, table_name, 1, "changed")
# Start changeset generation (this transitions phase 0 -> 1)
with conn:
conn.execute(f"UPDATE {table_name} SET phase = 1 WHERE phase = 0")
# Simulate concurrent write (should go to phase 0)
insert_or_update(conn, table_name, 1, "concurrent change")
insert_or_update(conn, table_name, 2, "new row")
# Verify phase isolation
cursor = conn.execute(
f"SELECT phase, data FROM {table_name} WHERE id = 1 ORDER BY phase"
)
rows = cursor.fetchall()
# Should have phase 0 (concurrent) and phase 1 (changeset) versions
phases = [row[0] for row in rows]
assert 0 in phases, "Expected phase 0 row from concurrent write"
assert 1 in phases, "Expected phase 1 row from changeset"
print("✓ Phase isolation test passed")
def test_compact():
"""Test table compaction functionality."""
with sqlite3_test_db() as conn:
table_name = setup_three_phase_table(conn, SAMPLE_TABLE_NAME)
# Insert initial data
insert_or_update(conn, table_name, 1, "data1")
insert_or_update(conn, table_name, 2, "data2")
insert_or_update(conn, table_name, 3, "data3")
# Create baseline changeset
with changeset(conn, table_name):
pass
# Make modifications to create multiple phases
insert_or_update(conn, table_name, 1, "updated1") # Update
insert_or_update(conn, table_name, 4, "data4") # Insert
logical_delete(conn, table_name, 3) # Delete
# Before compact - should have multiple copies of rows
cursor = conn.execute(f"SELECT COUNT(*) FROM {table_name}")
before_count = cursor.fetchone()[0]
assert before_count > 4, f"Expected more than 4 rows before compact, got {before_count}"
# Compact the table
compact(conn, table_name)
# verify the table only contains the latest version of non-deleted rows
cursor = conn.execute(f"SELECT COUNT(*) FROM {table_name}")
after_count = cursor.fetchone()[0]
# Should have ID 1 (updated1), ID 2 (data2), ID 4 (data4) = 3 rows
# ID 3 is completely removed because its latest version was deleted
assert after_count == 3, f"Expected 3 rows after compact, got {after_count}"
# All remaining rows should be phase=2
cursor = conn.execute(f"SELECT COUNT(*) FROM {table_name} WHERE phase = 2")
phase2_count = cursor.fetchone()[0]
assert phase2_count == 3, f"Expected 3 phase=2 rows, got {phase2_count}"
# Verify data integrity - latest versions are preserved
assert read_latest(conn, table_name, 1) == "updated1" # Kept latest version
assert read_latest(conn, table_name, 2) == "data2" # Kept (single, non-deleted)
assert read_latest(conn, table_name, 3) is None # Removed (latest was deleted)
assert read_latest(conn, table_name, 4) == "data4" # Kept (single, non-deleted)
print("✓ Compact test passed")
def test_crash_safety():
"""Test that changes are not lost when application crashes during changeset generation."""
with sqlite3_test_db() as writer_conn, sqlite3_test_db() as replica_conn:
writer_table = setup_three_phase_table(writer_conn, SAMPLE_TABLE_NAME)
replica_table = setup_three_phase_table(replica_conn, SAMPLE_TABLE_NAME)
# === SETUP: Initial data ===
print(" Setup: Creating baseline data")
initial_data = [
(1, "data1_v1"),
(2, "data2_v1"),
(3, "data3_v1"),
(4, "data4_v1"),
(5, "data5_v1"),
]
for row_id, data_value in initial_data:
insert_or_update(writer_conn, writer_table, row_id, data_value)