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