zaun_community_protocol/client/
cursors.rs1mod bodies;
4mod cursor;
5
6pub use cursor::Cursor;
7pub(super) use cursor::Held;
8
9use std::collections::{BTreeMap, BTreeSet};
10
11use crate::command::object::Cursors;
12use crate::envelope::MAX_JSON_INTEGER;
13use crate::ids::ChannelId;
14
15const MAX_HELD: usize = 1_024;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub(super) enum Key {
21 Member,
22 Channel(ChannelId),
23}
24
25#[derive(Debug)]
27pub(super) enum Offer {
28 Duplicate,
30 Ready(Vec<Held>),
32 Gap { expected: u64 },
34}
35
36#[derive(Debug, Clone, Default)]
42pub struct CursorTable {
43 member: Cursor,
44 channels: BTreeMap<ChannelId, Cursor>,
45 dirty: BTreeSet<ChannelId>,
46}
47
48impl CursorTable {
49 #[must_use]
51 pub const fn member(&self) -> u64 {
52 self.member.applied
53 }
54
55 #[must_use]
57 pub fn channel(&self, id: &ChannelId) -> Option<u64> {
58 self.channels.get(id).map(Cursor::applied)
59 }
60
61 pub fn set_member(&mut self, seq: u64) {
63 self.member = Cursor {
64 applied: seq,
65 held: BTreeMap::new(),
66 };
67 }
68
69 pub fn set_channel(&mut self, id: ChannelId, seq: u64) {
71 let cursor = Cursor {
72 applied: seq,
73 held: BTreeMap::new(),
74 };
75 self.channels.insert(id, cursor);
76 self.dirty.insert(id);
77 }
78
79 pub fn remove_channel(&mut self, id: &ChannelId) {
81 self.channels.remove(id);
82 self.dirty.remove(id);
83 }
84
85 pub fn seed(&mut self, cursors: &Cursors) {
87 self.raise(Key::Member, cursors.member);
88 for c in &cursors.channels {
89 self.raise(Key::Channel(c.channel_id), c.seq);
90 }
91 }
92
93 fn cursor(&mut self, key: Key) -> &mut Cursor {
94 match key {
95 Key::Member => &mut self.member,
96 Key::Channel(id) => self.channels.entry(id).or_default(),
97 }
98 }
99
100 pub(super) fn offer(&mut self, key: Key, seq: u64, held: Held) -> Offer {
101 if let Key::Channel(id) = key {
102 self.dirty.insert(id);
103 }
104 let cursor = self.cursor(key);
105 if seq <= cursor.applied {
106 return Offer::Duplicate;
107 }
108 if seq > cursor.applied.saturating_add(1) {
109 if cursor.held.len() < MAX_HELD {
110 cursor.held.entry(seq).or_insert(held);
111 }
112 return Offer::Gap {
113 expected: cursor.applied.saturating_add(1),
114 };
115 }
116 cursor.applied = seq;
117 let mut ready = vec![held];
118 ready.extend(cursor.release());
119 Offer::Ready(ready)
120 }
121
122 pub(super) fn raise(&mut self, key: Key, floor: u64) -> Vec<Held> {
125 let floor = floor.min(MAX_JSON_INTEGER);
126 let cursor = self.cursor(key);
127 cursor.applied = cursor.applied.max(floor);
128 cursor.release()
129 }
130
131 pub(super) fn skip_gap(&mut self, key: Key) -> Vec<Held> {
134 let cursor = self.cursor(key);
135 match cursor.held.keys().next() {
136 Some(first) => {
137 cursor.applied = first - 1;
138 cursor.release()
139 }
140 None => Vec::new(),
141 }
142 }
143
144 pub(super) fn gap_keys(&self) -> Vec<Key> {
146 let member = (!self.member.held.is_empty()).then_some(Key::Member);
147 let channels = self
148 .channels
149 .iter()
150 .filter(|(_, c)| !c.held.is_empty())
151 .map(|(id, _)| Key::Channel(*id));
152 member.into_iter().chain(channels).collect()
153 }
154
155 pub(super) fn drop_held(&mut self, key: Key) {
156 self.cursor(key).held.clear();
157 }
158}