moontxnkit

    Deterministic atomic state transitions, MVCC transactions and logical recovery for MoonBit.

    mvcc
    transaction
    atomic-state
    workflow
    wal
    recovery
    Download zip
    Version
    0.4.0
    License
    Apache-2.0
    Last updated
    3 months ago
    Downloads
    47

    No dependencies

    #MoonTxnKit

    MoonTxnKit 是面向 MoonBit 的确定性 MVCC 状态事务与恢复内核,适用于工作流、 规则引擎、模拟器、内存服务和测试替身。

    #Serializable 前缀扫描

    let engine = @moontxnkit.Engine::new()
    let transaction = engine.begin(
    isolation=@moontxnkit.IsolationLevel::Serializable,
    )

    let rows = transaction.scan_prefix("task:queued:")
    ignore(transaction.put("audit:scan", "\{rows.length()}"))

    match transaction.commit() {
    @moontxnkit.CommitResult::CommittedAt(version) =>
    println("committed at \{version}")
    @moontxnkit.CommitResult::Rejected(conflict) =>
    println(conflict.to_json())
    }

    扫描结果来自事务快照,并叠加事务自己的新增、修改和删除,最终按键稳定排序。 Serializable 提交会检测前缀内的并发新增、更新和删除,返回结构化 PredicateWrite 冲突。

    #其他能力

    • Snapshot Isolation 和点读 Serializable 校验;
    • 声明式原子状态计划;
    • 保存点和局部回滚;
    • 逻辑 WAL、校验、幂等恢复;
    • 历史读取、低水位压缩和 JSON 报告。

    完整说明见仓库 README.md 与 docs/。

    AtomicPlan

    pub struct AtomicPlan {
    name : String
    isolation : IsolationLevel
    conditions : Array[PlanCondition]
    writes : Array[WriteIntent]
    }

    AtomicPlan::condition_count

    fn AtomicPlan::condition_count(self : AtomicPlan) -> Int

    AtomicPlan::delete

    fn AtomicPlan::delete(self : AtomicPlan, key : String) -> AtomicPlan

    AtomicPlan::expect

    fn AtomicPlan::expect(self : AtomicPlan, key : String, value : String) -> AtomicPlan

    AtomicPlan::expect_missing

    fn AtomicPlan::expect_missing(self : AtomicPlan, key : String) -> AtomicPlan

    AtomicPlan::name

    fn AtomicPlan::name(self : AtomicPlan) -> String

    AtomicPlan::new

    fn AtomicPlan::new(name : String, isolation? : IsolationLevel) -> AtomicPlan

    AtomicPlan::put

    fn AtomicPlan::put(self : AtomicPlan, key : String, value : String) -> AtomicPlan

    AtomicPlan::write_count

    fn AtomicPlan::write_count(self : AtomicPlan) -> Int

    CommitResult

    pub(all) enum CommitResult {
    CommittedAt(Int)
    Rejected(TxnConflict)
    } derive(Eq,
    Debug
    )

    CommitResult::to_json

    fn CommitResult::to_json(self : CommitResult) -> String

    CompactionReport

    pub(all) struct CompactionReport {
    low_watermark : Int
    removed_versions : Int
    retained_versions : Int
    removed_keys : Int
    } derive(Eq,
    Debug
    )

    CompactionReport::to_json

    fn CompactionReport::to_json(self : CompactionReport) -> String

    ConditionFailure

    pub(all) struct ConditionFailure {
    plan : String
    key : String
    expected : String?
    actual : String?
    snapshot_version : Int
    } derive(Eq,
    Debug
    )

    ConditionFailure::to_json

    fn ConditionFailure::to_json(self : ConditionFailure) -> String

    ConflictKind

    pub(all) enum ConflictKind {
    WriteWrite
    ReadWrite
    PredicateWrite
    InvalidState
    InvalidSavepoint
    CorruptWal
    } derive(Eq,
    Debug
    )

    Engine

    pub struct Engine {
    current_version : Int
    next_txn_id : Int
    histories : Map[String, Array[VersionedValue]]
    active_snapshots : Map[Int, Int]
    committed_transactions : Int
    aborted_transactions : Int
    wal_records : Array[WalRecord]
    }

    Engine::begin

    fn Engine::begin(self : Engine, isolation? : IsolationLevel) -> Transaction

    Engine::compact

    fn Engine::compact(self : Engine) -> CompactionReport

    Engine::execute

    fn Engine::execute(self : Engine, plan : AtomicPlan) -> PlanResult

    Engine::from_snapshot

    fn Engine::from_snapshot(snapshot : EngineSnapshot) -> Engine

    Rebuilds an engine from an adapter-owned checkpoint. The caller can inspect validate() afterwards and choose its own serialization, encryption, and distributed-locking policy without adding platform I/O to the core.

    Engine::get

    fn Engine::get(self : Engine, key : String) -> String?

    Engine::history

    fn Engine::history(self : Engine, key : String) -> Array[VersionedValue]

    Engine::history_json

    fn Engine::history_json(self : Engine, key : String) -> String

    Engine::new

    fn Engine::new() -> Engine

    Engine::oldest_active_snapshot

    fn Engine::oldest_active_snapshot(self : Engine) -> Int

    Engine::read_at

    fn Engine::read_at(self : Engine, key : String, version : Int) -> String?

    Engine::recover

    fn Engine::recover(records : Array[WalRecord]) -> (Engine, ReplayReport)

    Engine::replay

    fn Engine::replay(self : Engine, records : Array[WalRecord]) -> ReplayReport

    Engine::snapshot

    fn Engine::snapshot(self : Engine) -> EngineSnapshot

    Captures committed MVCC state for an adapter-owned checkpoint. Active transactions are deliberately excluded: callers must abort or finish them before persisting, then start new transactions against the restored engine.

    Engine::stats

    fn Engine::stats(self : Engine) -> EngineStats

    Engine::validate

    fn Engine::validate(self : Engine) -> Array[ValidationIssue]

    Engine::version

    fn Engine::version(self : Engine) -> Int

    Engine::wal

    fn Engine::wal(self : Engine) -> Array[WalRecord]

    EngineSnapshot

    pub(all) struct EngineSnapshot {
    current_version : Int
    next_txn_id : Int
    committed_transactions : Int
    aborted_transactions : Int
    histories : Array[SnapshotHistory]
    wal_records : Array[WalRecord]
    } derive(Eq,
    Debug
    )

    Deterministic committed-state checkpoint. It contains no active transaction handles and therefore has a clear recovery boundary for storage adapters.

    EngineStats

    pub(all) struct EngineStats {
    current_version : Int
    keys : Int
    versions : Int
    active_transactions : Int
    committed_transactions : Int
    aborted_transactions : Int
    wal_records : Int
    } derive(Eq,
    Debug
    )

    EngineStats::to_json

    fn EngineStats::to_json(self : EngineStats) -> String

    IsolationLevel

    pub(all) enum IsolationLevel {
    Snapshot
    Serializable
    } derive(Eq,
    Debug
    )

    KeyValue

    pub(all) struct KeyValue {
    key : String
    value : String
    } derive(Eq,
    Debug
    )

    PlanCondition

    pub(all) struct PlanCondition {
    key : String
    expected : String?
    } derive(Eq,
    Debug
    )

    PlanResult

    pub(all) enum PlanResult {
    AppliedAt(Int)
    ConditionFailed(ConditionFailure)
    CommitRejected(TxnConflict)
    } derive(Eq,
    Debug
    )

    PlanResult::to_json

    fn PlanResult::to_json(self : PlanResult) -> String

    PrefixObservation

    pub(all) struct PrefixObservation {
    prefix : String
    } derive(Eq,
    Debug
    )

    ReadObservation

    pub(all) struct ReadObservation {
    key : String
    observed_version : Int
    } derive(Eq,
    Debug
    )

    ReplayReport

    pub(all) struct ReplayReport {
    applied_records : Int
    skipped_records : Int
    last_version : Int
    valid : Bool
    error : String
    } derive(Eq,
    Debug
    )

    ReplayReport::to_json

    fn ReplayReport::to_json(self : ReplayReport) -> String

    Savepoint

    pub(all) struct Savepoint {
    name : String
    write_count : Int
    read_count : Int
    writes : Array[WriteIntent]
    reads : Array[ReadObservation]
    prefix_reads : Array[PrefixObservation]
    } derive(Eq,
    Debug
    )

    SnapshotHistory

    pub(all) struct SnapshotHistory {
    key : String
    versions : Array[VersionedValue]
    } derive(Eq,
    Debug
    )

    A serializable history entry used by EngineSnapshot.

    Transaction

    pub struct Transaction {
    engine : Engine
    id : Int
    snapshot_version : Int
    isolation : IsolationLevel
    state : TxnState
    writes : Array[WriteIntent]
    reads : Array[ReadObservation]
    prefix_reads : Array[PrefixObservation]
    savepoints : Array[Savepoint]
    }

    Transaction::abort

    fn Transaction::abort(self : Transaction) -> Bool

    Transaction::commit

    fn Transaction::commit(self : Transaction) -> CommitResult

    Transaction::delete

    fn Transaction::delete(self : Transaction, key : String) -> Bool

    Transaction::get

    fn Transaction::get(self : Transaction, key : String) -> String?

    Transaction::id

    fn Transaction::id(self : Transaction) -> Int

    Transaction::pending_writes

    fn Transaction::pending_writes(self : Transaction) -> Int

    Transaction::put

    fn Transaction::put(self : Transaction, key : String, value : String) -> Bool

    Transaction::release_savepoint

    fn Transaction::release_savepoint(self : Transaction, name : String) -> Bool

    Transaction::rollback_to

    fn Transaction::rollback_to(self : Transaction, name : String) -> Bool

    Transaction::savepoint

    fn Transaction::savepoint(self : Transaction, name : String) -> Bool

    Transaction::savepoint_count

    fn Transaction::savepoint_count(self : Transaction) -> Int

    Transaction::scan_prefix

    fn Transaction::scan_prefix(self : Transaction, prefix : String) -> Array[KeyValue]

    Returns a stable, sorted prefix view at the transaction snapshot.

    Pending writes are overlaid so a transaction observes its own inserts, updates, and deletes. Serializable transactions validate the whole prefix at commit time to detect phantoms.

    Transaction::snapshot

    fn Transaction::snapshot(self : Transaction) -> Int

    Transaction::status

    fn Transaction::status(self : Transaction) -> TxnState

    TxnConflict

    pub(all) struct TxnConflict {
    kind : ConflictKind
    key : String
    snapshot_version : Int
    current_version : Int
    message : String
    } derive(Eq,
    Debug
    )

    TxnConflict::to_json

    fn TxnConflict::to_json(self : TxnConflict) -> String

    TxnState

    pub(all) enum TxnState {
    Active
    Committed
    Aborted
    } derive(Eq,
    Debug
    )

    ValidationIssue

    pub(all) struct ValidationIssue {
    code : String
    key : String
    message : String
    } derive(Eq,
    Debug
    )

    ValidationIssue::to_json

    fn ValidationIssue::to_json(self : ValidationIssue) -> String

    VersionedValue

    pub(all) struct VersionedValue {
    version : Int
    value : String?
    } derive(Eq,
    Debug
    )

    VersionedValue::to_json

    fn VersionedValue::to_json(self : VersionedValue) -> String

    WalRecord

    pub(all) struct WalRecord {
    transaction_id : Int
    snapshot_version : Int
    commit_version : Int
    operations : Array[WriteIntent]
    checksum : Int
    } derive(Eq,
    Debug
    )

    WalRecord::is_valid

    fn WalRecord::is_valid(self : WalRecord) -> Bool

    WalRecord::new

    fn WalRecord::new(transaction_id : Int, snapshot_version : Int, commit_version : Int, operations : Array[WriteIntent]) -> WalRecord

    WalRecord::to_json

    fn WalRecord::to_json(self : WalRecord) -> String

    WriteIntent

    pub(all) struct WriteIntent {
    key : String
    value : String?
    } derive(Eq,
    Debug
    )

    WriteIntent::delete

    fn WriteIntent::delete(key : String) -> WriteIntent

    WriteIntent::put

    fn WriteIntent::put(key : String, value : String) -> WriteIntent

    WriteIntent::to_json

    fn WriteIntent::to_json(self : WriteIntent) -> String