#>cpp
#pragma once

#include "communication/rpc/messages.hpp"
#include "transactions/commit_log.hpp"
#include "transactions/distributed/engine_rpc_messages.capnp.h"
#include "transactions/snapshot.hpp"
#include "transactions/type.hpp"
cpp<#

(load "transactions/distributed/serialization.lcp")

(lcp:namespace tx)

(lcp:capnp-namespace "tx")

(lcp:capnp-type-conversion "TransactionId" "UInt64")
(lcp:capnp-type-conversion "CommandId" "UInt32")

(lcp:define-struct tx-and-snapshot ()
  ((tx-id "TransactionId")
   (snapshot "Snapshot"
             :slk-save #'slk-save-snapshot
             :slk-load #'slk-load-snapshot
             :capnp-type "List(UInt64)"
             :capnp-init nil
             :capnp-save #'save-snapshot
             :capnp-load #'load-snapshot))
  (:serialize (:slk) (:capnp)))

(lcp:define-rpc begin
    (:request ())
  (:response ((member "TxAndSnapshot"))))

(lcp:define-rpc advance
    (:request ((member "TransactionId")))
  (:response ((member "CommandId"))))

(lcp:define-rpc commit
    (:request ((member "TransactionId")))
  (:response ()))

(lcp:define-rpc abort
    (:request ((member "TransactionId")))
  (:response ()))

(lcp:define-rpc snapshot
    (:request ((member "TransactionId")))
  (:response ((member "Snapshot"
                      :slk-save #'slk-save-snapshot
                      :slk-load #'slk-load-snapshot
                      :capnp-type "List(UInt64)"
                      :capnp-init nil
                      :capnp-save #'save-snapshot
                      :capnp-load #'load-snapshot))))

(lcp:define-rpc command
    (:request ((member "TransactionId")))
  (:response ((member "CommandId"))))

(lcp:define-rpc gc-snapshot
    (:request ())
  (:response ((member "Snapshot"
                      :slk-save #'slk-save-snapshot
                      :slk-load #'slk-load-snapshot
                      :capnp-type "List(UInt64)"
                      :capnp-init nil
                      :capnp-save #'save-snapshot
                      :capnp-load #'load-snapshot))))

(lcp:define-rpc clog-info
    (:request ((member "TransactionId")))
  (:response ((member "CommitLog::Info"
                      :slk-save #'slk-save-commitlog-info
                      :slk-load #'slk-load-commitlog-info
                      :capnp-type "UInt8"
                      :capnp-init nil
                      :capnp-save #'save-commitlog-info
                      :capnp-load #'load-commitlog-info))))

(lcp:define-rpc active-transactions
    (:request ())
  (:response ((member "Snapshot"
                      :slk-save #'slk-save-snapshot
                      :slk-load #'slk-load-snapshot
                      :capnp-type "List(UInt64)"
                      :capnp-init nil
                      :capnp-save #'save-snapshot
                      :capnp-load #'load-snapshot))))

(lcp:define-rpc ensure-next-id-greater
    (:request ((member "TransactionId")))
  (:response ()))

(lcp:define-rpc global-last
    (:request ())
  (:response ((member "TransactionId"))))

(lcp:define-rpc notify-committed
    (:request ((member "TransactionId")))
  (:response ()))

(lcp:pop-namespace) ;; tx
