MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / get_change_at_sequence

Function get_change_at_sequence

atomic-repository/src/history/operations.rs:134–180  ·  view source on GitHub ↗

Get a specific change by sequence number. # Arguments `txn` - Read transaction `view` - View to query `sequence` - Sequence number to retrieve # Returns The history entry at the given sequence, or an error if out of range.

(
    txn: &T,
    view: &ViewState,
    sequence: u64,
)

Source from the content-addressed store, hash-verified

132///
133/// The history entry at the given sequence, or an error if out of range.
134pub fn get_change_at_sequence<T: ViewTxnT>(
135 txn: &T,
136 view: &ViewState,
137 sequence: u64,
138) -> HistoryResult<HistoryEntry> {
139 if sequence >= view.change_count {
140 return Err(HistoryError::SequenceOutOfRange {
141 sequence,
142 max: view.change_count.saturating_sub(1),
143 });
144 }
145
146 let node_id = txn
147 .get_change_at_seq(view, sequence)
148 .map_err(|e| HistoryError::Database(e.to_string()))?
149 .ok_or_else(|| HistoryError::SequenceOutOfRange {
150 sequence,
151 max: view.change_count.saturating_sub(1),
152 })?;
153
154 let hash = txn
155 .get_external(node_id)
156 .map_err(|e| HistoryError::Database(e.to_string()))?
157 .ok_or_else(|| HistoryError::ChangeNotFound {
158 hash: format!("{:?}", node_id),
159 })?;
160
161 // Get the Merkle state - we need to iterate to find it
162 // This is a bit inefficient but maintains correctness
163 let iter = txn
164 .iter_changes(view, sequence)
165 .map_err(|e| HistoryError::Database(e.to_string()))?;
166
167 for result in iter {
168 match result {
169 Ok((seq, id, merkle)) if seq == sequence && id == node_id => {
170 return Ok(HistoryEntry::new(sequence, node_id, hash, merkle));
171 }
172 Ok((seq, _, _)) if seq > sequence => break,
173 Err(e) => return Err(HistoryError::Database(e.to_string())),
174 _ => continue,
175 }
176 }
177
178 // Fallback with zero merkle if we couldn't find it
179 Ok(HistoryEntry::new(sequence, node_id, hash, Merkle::ZERO))
180}
181
182/// Find the sequence number for a change by its hash.
183///

Callers 1

get_state_before_changeFunction · 0.70

Calls 3

get_change_at_seqMethod · 0.45
get_externalMethod · 0.45
iter_changesMethod · 0.45

Tested by

no test coverage detected