# 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

# Sets should work on both existing, missing, and removed keys.

import a=1 b=1 x=
---
ok

# Can replace an existing key and tombstone.
t1: begin
t1: set a=2 x=2 [ops]
---
t1: engine set mvcc:TxWrite(2, "a") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02a\x00\x00" → ""]
t1: engine set mvcc:Version("a", 2) → "2" ["\x04a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x012"]
t1: engine set mvcc:TxWrite(2, "x") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02x\x00\x00" → ""]
t1: engine set mvcc:Version("x", 2) → "2" ["\x04x\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x012"]

t1: scan
---
t1: "a" → "2"
t1: "b" → "1"
t1: "x" → "2"

# Can write a new key, replace it, and be idempotent.
t1: set c=1 c=2 c=2 [ops]
---
t1: engine set mvcc:TxWrite(2, "c") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02c\x00\x00" → ""]
t1: engine set mvcc:Version("c", 2) → "1" ["\x04c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x011"]
t1: engine set mvcc:TxWrite(2, "c") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02c\x00\x00" → ""]
t1: engine set mvcc:Version("c", 2) → "2" ["\x04c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x012"]
t1: engine set mvcc:TxWrite(2, "c") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02c\x00\x00" → ""]
t1: engine set mvcc:Version("c", 2) → "2" ["\x04c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x012"]

t1: scan
---
t1: "a" → "2"
t1: "b" → "1"
t1: "c" → "2"
t1: "x" → "2"

dump
---
mvcc:NextVersion → 3 ["\x00" → "\x03"]
mvcc:TxActive(2) → "" ["\x01\x00\x00\x00\x00\x00\x00\x00\x02" → ""]
mvcc:TxWrite(2, "a") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02a\x00\x00" → ""]
mvcc:TxWrite(2, "c") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02c\x00\x00" → ""]
mvcc:TxWrite(2, "x") → "" ["\x03\x00\x00\x00\x00\x00\x00\x00\x02x\x00\x00" → ""]
mvcc:Version("a", 1) → "1" ["\x04a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" → "\x01\x011"]
mvcc:Version("a", 2) → "2" ["\x04a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x012"]
mvcc:Version("b", 1) → "1" ["\x04b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" → "\x01\x011"]
mvcc:Version("c", 2) → "2" ["\x04c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x012"]
mvcc:Version("x", 1) → None ["\x04x\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" → "\x00"]
mvcc:Version("x", 2) → "2" ["\x04x\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" → "\x01\x012"]
