MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / next

Method next

nodedb-array/src/sync/hlc.rs:169–195  ·  view source on GitHub ↗

Generate the next [`Hlc`], guaranteeing strict monotonicity. Implements the standard HLC advancement algorithm: - `new_physical = max(now_ms, last_physical)` - if `new_physical == last_physical`: `new_logical = last_logical + 1` - else: `new_logical = 0`

(&self)

Source from the content-addressed store, hash-verified

167 /// - if `new_physical == last_physical`: `new_logical = last_logical + 1`
168 /// - else: `new_logical = 0`
169 pub fn next(&self) -> ArrayResult<Hlc> {
170 let now_ms = Self::now_ms()?;
171 if now_ms > MAX_PHYSICAL_MS {
172 return Err(ArrayError::InvalidHlc {
173 detail: format!("system clock {now_ms} exceeds MAX_PHYSICAL_MS"),
174 });
175 }
176
177 let mut guard = self.state.lock().map_err(|_| ArrayError::HlcLockPoisoned)?;
178 let (last_physical, last_logical) = *guard;
179
180 let new_physical = now_ms.max(last_physical);
181 let new_logical = if new_physical == last_physical {
182 last_logical
183 .checked_add(1)
184 .ok_or_else(|| ArrayError::InvalidHlc {
185 detail: "logical counter overflow within one millisecond".into(),
186 })?
187 } else {
188 0
189 };
190
191 *guard = (new_physical, new_logical);
192 drop(guard);
193
194 Hlc::new(new_physical, new_logical, self.replica_id)
195 }
196
197 /// Advance local state after observing a remote [`Hlc`].
198 ///

Callers 5

from_schemaMethod · 0.45
import_snapshotMethod · 0.45
replace_schemaMethod · 0.45

Calls 2

lockMethod · 0.80
now_msFunction · 0.50

Tested by 2