mz_persist_types/
txn.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Types for the txn-wal crate.
11
12use std::fmt::Debug;
13
14use serde::{Deserialize, Serialize};
15
16use crate::stats::PartStats;
17use crate::{Codec, Codec64, ShardId};
18
19/// The in-mem representation of an update in the txns shard.
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub enum TxnsEntry {
22    /// A data shard register operation.
23    ///
24    /// The `[u8; 8]` is a Codec64 encoded timestamp.
25    Register(ShardId, [u8; 8]),
26    /// A batch written to a data shard in a txn.
27    ///
28    /// The `[u8; 8]` is a Codec64 encoded timestamp.
29    Append(ShardId, [u8; 8], Vec<u8>),
30}
31
32impl TxnsEntry {
33    /// Returns the ShardId of the data shard targeted by this entry.
34    pub fn data_id(&self) -> &ShardId {
35        match self {
36            TxnsEntry::Register(data_id, _) => data_id,
37            TxnsEntry::Append(data_id, _, _) => data_id,
38        }
39    }
40
41    /// Returns the decoded timestamp of this entry.
42    pub fn ts<T: Codec64>(&self) -> T {
43        match self {
44            TxnsEntry::Register(_, ts) => T::decode(*ts),
45            TxnsEntry::Append(_, ts, _) => T::decode(*ts),
46        }
47    }
48}
49
50/// An abstraction over the encoding format of [TxnsEntry].
51///
52/// This enables users of this crate to control how data is written to the txns
53/// shard (which will allow mz to present it as a normal introspection source).
54pub trait TxnsCodec: Debug {
55    /// The `K` type used in the txns shard.
56    type Key: Debug + Codec;
57    /// The `V` type used in the txns shard.
58    type Val: Debug + Codec;
59
60    /// Returns the Schemas to use with [Self::Key] and [Self::Val].
61    fn schemas() -> (<Self::Key as Codec>::Schema, <Self::Val as Codec>::Schema);
62    /// Encodes a [TxnsEntry] in the format persisted in the txns shard.
63    fn encode(e: TxnsEntry) -> (Self::Key, Self::Val);
64    /// Decodes a [TxnsEntry] from the format persisted in the txns shard.
65    ///
66    /// Implementations should panic if the values are invalid.
67    fn decode(key: Self::Key, val: Self::Val) -> TxnsEntry;
68
69    /// Returns if a part might include the given data shard based on pushdown
70    /// stats.
71    ///
72    /// False positives are okay (needless fetches) but false negatives are not
73    /// (incorrectness). Returns an Option to make `?` convenient, `None` is
74    /// treated the same as `Some(true)`.
75    fn should_fetch_part(data_id: &ShardId, stats: &PartStats) -> Option<bool>;
76}