Skip to main content

iota_graphql_rpc/
consistency.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use async_graphql::connection::CursorType;
6use serde::{Deserialize, Serialize};
7
8use crate::types::cursor::{JsonCursor, ScanLimited};
9
10/// The checkpoint sequence number for entities not available for view.
11pub(crate) const UNAVAILABLE_CHECKPOINT_SEQUENCE_NUMBER: u64 = u64::MAX;
12
13/// The consistent cursor for an index into a `Vec` field is constructed from
14/// the index of the element and the checkpoint the cursor was constructed at.
15#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
16pub(crate) struct ConsistentIndexCursor {
17    #[serde(rename = "i")]
18    pub ix: usize,
19    /// The checkpoint sequence number at which the entity corresponding to this
20    /// cursor was viewed at.
21    pub c: u64,
22}
23
24/// The consistent cursor for an index into a `Map` field is constructed from
25/// the name or key of the element and the checkpoint the cursor was constructed
26/// at.
27#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
28pub(crate) struct ConsistentNamedCursor {
29    #[serde(rename = "n")]
30    pub name: String,
31    /// The checkpoint sequence number at which the entity corresponding to this
32    /// cursor was viewed at.
33    pub c: u64,
34}
35
36/// Trait for cursors that have a checkpoint sequence number associated with
37/// them.
38pub(crate) trait Checkpointed: CursorType {
39    fn checkpoint_viewed_at(&self) -> u64;
40}
41
42impl Checkpointed for JsonCursor<ConsistentIndexCursor> {
43    fn checkpoint_viewed_at(&self) -> u64 {
44        self.c
45    }
46}
47
48impl Checkpointed for JsonCursor<ConsistentNamedCursor> {
49    fn checkpoint_viewed_at(&self) -> u64 {
50        self.c
51    }
52}
53
54impl ScanLimited for JsonCursor<ConsistentIndexCursor> {}
55
56impl ScanLimited for JsonCursor<ConsistentNamedCursor> {}