CMDCMemoryPg.CheckpointBackend (cmdc_memory_pg v0.1.0)

Copy Markdown View Source

PostgreSQL 实现 CMDC.Checkpoint.Backend behaviour。

序列化策略

CMDC.Checkpoint.Snapshot struct 通过 :erlang.term_to_binary(snap, [:compressed]) 写入 cmdc_checkpoints.snapshot bytea 字段,避免 jsonb 对 atom / Message struct / 嵌套 plugin_states 等不规则数据的序列化痛点

CheckpointBackend.ETS 一致性

本模块复用 cmdc 主库 CMDC.Checkpoint.Backend.ETS 同套 behaviour 契约, 上游测试 suite 可一字不改替换 backend 跑通。

接 Cloak

encryption at rest 留给集成方在 backend wrapper 层处理:

defmodule MyApp.EncryptedCheckpointBackend do
  @behaviour CMDC.Checkpoint.Backend

  @impl true
  def save(sid, snap, opts) do
    # snap.state 可能含 user_data.api_key 等敏感字段
    sanitized = CMDC.Checkpoint.Snapshot.redact(snap, &MyApp.Vault.encrypt/1)
    CMDCMemoryPg.CheckpointBackend.save(sid, sanitized, opts)
  end

  @impl true
  def load(sid, opts) do
    case CMDCMemoryPg.CheckpointBackend.load(sid, opts) do
      {:ok, snap} ->
        decrypted = CMDC.Checkpoint.Snapshot.redact(snap, &MyApp.Vault.decrypt/1)
        {:ok, decrypted}

      other -> other
    end
  end

  # ... list / delete 透传
end