# Copyright (c) reifydb.com 2025
# This file is licensed under the AGPL-3.0-or-later

# This file includes and modifies code from the toydb project (https://github.com/erikgrinaker/toydb),
# originally licensed under the Apache License, Version 2.0.
# Original copyright:
#   Copyright (c) 2024 Erik Grinaker
#
# The original Apache License can be found at:
#   http://www.apache.org/licenses/LICENSE-2.0

# Begin creates new transactions at increasing versions, with concurrent
# transactions in their active sets.

# Start t1 at v1, with an empty active set. Dump raw engine operations to ensure
# it bumps the next version and registers itself as active.
t1: begin [ops]
t1: state
---
t1: engine set mvcc:NextVersion → 2 ["\x00" → "\x02"]
t1: engine set mvcc:TxActive(1) → "" ["\x01\x00\x00\x00\x00\x00\x00\x00\x01" → ""]
t1: v1 rw active={}

# t2 should have v2, and t1 in its active set. It should persist a snapshot of
# its active set.
t2: begin [ops]
t2: state
---
t2: engine set mvcc:NextVersion → 3 ["\x00" → "\x03"]
t2: engine set mvcc:TxActiveSnapshot(2) → {1} ["\x02\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x01"]
t2: engine set mvcc:TxActive(2) → "" ["\x01\x00\x00\x00\x00\x00\x00\x00\x02" → ""]
t2: v2 rw active={1}

# Similarly for t3.
t3: begin [ops]
t3: state
---
t3: engine set mvcc:NextVersion → 4 ["\x00" → "\x04"]
t3: engine set mvcc:TxActiveSnapshot(3) → {1,2} ["\x02\x00\x00\x00\x00\x00\x00\x00\x03" → "\x02\x01\x02"]
t3: engine set mvcc:TxActive(3) → "" ["\x01\x00\x00\x00\x00\x00\x00\x00\x03" → ""]
t3: v3 rw active={1,2}

# Now, commit t2, which unregisters it.
t2: commit [ops]
---
t2: engine remove mvcc:TxActive(2) ["\x01\x00\x00\x00\x00\x00\x00\x00\x02"]

# It should still be in t3's active set.
t3: state
---
t3: v3 rw active={1,2}

# But not in a new t4.
t4: begin [ops]
t4: state
---
t4: engine set mvcc:NextVersion → 5 ["\x00" → "\x05"]
t4: engine set mvcc:TxActiveSnapshot(4) → {1,3} ["\x02\x00\x00\x00\x00\x00\x00\x00\x04" → "\x02\x01\x03"]
t4: engine set mvcc:TxActive(4) → "" ["\x01\x00\x00\x00\x00\x00\x00\x00\x04" → ""]
t4: v4 rw active={1,3}
