chore: 新增项目规则文档、构建别名及忽略规则 添加 AGENTS.md 项目规则文件,涵盖语言与工具链、模块设计、 Git 提交规范及验证流程等内容。 变更要点: - .cargo/config.toml 新增 xtask 命令别名 - .gitignore 忽略运行时地图缓存、接收器配置文件、 构建锁文件及本地 docs/ 目录 - 新增 AGENTS.md 定义项目编码、提交与审查规则 ```
212 lines
6.6 KiB
Rust
212 lines
6.6 KiB
Rust
//! Bounded latest-state store, independent of SQLite and archive generation.
|
|
use rid_shared::{Frame, Metrics, Observation, PROTOCOL_VERSION, Target};
|
|
use std::collections::{HashMap, HashSet, VecDeque};
|
|
|
|
struct Track {
|
|
target: Target,
|
|
sources: HashMap<String, (u64, u64)>,
|
|
}
|
|
|
|
pub struct Store {
|
|
tracks: HashMap<String, Track>,
|
|
changes: VecDeque<(u64, String)>,
|
|
journal_capacity: usize,
|
|
epoch: u64,
|
|
ttl_ms: u64,
|
|
metrics: Metrics,
|
|
}
|
|
|
|
impl Store {
|
|
pub fn new(capacity: usize, journal_capacity: usize, ttl_ms: u64, epoch: u64) -> Self {
|
|
assert!(capacity > 0 && journal_capacity > 0 && ttl_ms > 0);
|
|
Self {
|
|
tracks: HashMap::new(),
|
|
changes: VecDeque::new(),
|
|
journal_capacity,
|
|
epoch,
|
|
ttl_ms,
|
|
metrics: Metrics {
|
|
capacity,
|
|
..Metrics::default()
|
|
},
|
|
}
|
|
}
|
|
|
|
fn changed(&mut self, id: String) {
|
|
self.metrics.sequence += 1;
|
|
if self.changes.len() == self.journal_capacity {
|
|
self.changes.pop_front();
|
|
}
|
|
self.changes.push_back((self.metrics.sequence, id));
|
|
}
|
|
|
|
/// Multiple receivers use arrival order; no unverified sensor-time fusion is implied.
|
|
pub fn apply(&mut self, observation: Observation, now_ms: u64) -> bool {
|
|
if !observation.validate() {
|
|
self.metrics.rejected += 1;
|
|
return false;
|
|
}
|
|
let id = observation.target_id.clone();
|
|
if let Some(track) = self.tracks.get_mut(&id) {
|
|
if let Some(&(epoch, sequence)) = track.sources.get(&observation.receiver_id) {
|
|
if observation.source_epoch < epoch
|
|
|| (observation.source_epoch == epoch && observation.sequence <= sequence)
|
|
{
|
|
self.metrics.stale += 1;
|
|
return false;
|
|
}
|
|
} else if track.sources.len() >= 8 {
|
|
self.metrics.rejected += 1;
|
|
return false;
|
|
}
|
|
track.sources.insert(
|
|
observation.receiver_id.clone(),
|
|
(observation.source_epoch, observation.sequence),
|
|
);
|
|
track.target = Target {
|
|
observation,
|
|
received_at_ms: now_ms,
|
|
};
|
|
} else {
|
|
if self.tracks.len() >= self.metrics.capacity {
|
|
self.metrics.rejected += 1;
|
|
return false;
|
|
}
|
|
let sources = HashMap::from([(
|
|
observation.receiver_id.clone(),
|
|
(observation.source_epoch, observation.sequence),
|
|
)]);
|
|
self.tracks.insert(
|
|
id.clone(),
|
|
Track {
|
|
target: Target {
|
|
observation,
|
|
received_at_ms: now_ms,
|
|
},
|
|
sources,
|
|
},
|
|
);
|
|
}
|
|
self.metrics.accepted += 1;
|
|
self.changed(id);
|
|
true
|
|
}
|
|
|
|
pub fn expire(&mut self, now_ms: u64) {
|
|
let expired: Vec<String> = self
|
|
.tracks
|
|
.iter()
|
|
.filter(|(_, t)| now_ms.saturating_sub(t.target.received_at_ms) >= self.ttl_ms)
|
|
.map(|(id, _)| id.clone())
|
|
.collect();
|
|
for id in expired {
|
|
self.tracks.remove(&id);
|
|
self.metrics.expired += 1;
|
|
self.changed(id);
|
|
}
|
|
}
|
|
|
|
pub fn frame(&self, since: Option<u64>, now_ms: u64) -> Frame {
|
|
let snapshot = since.is_none_or(|s| {
|
|
s > self.metrics.sequence
|
|
|| self
|
|
.changes
|
|
.front()
|
|
.is_some_and(|(oldest, _)| s.saturating_add(1) < *oldest)
|
|
});
|
|
let mut targets = Vec::new();
|
|
let mut removed = Vec::new();
|
|
if snapshot {
|
|
targets.extend(self.tracks.values().map(|t| t.target.clone()));
|
|
} else {
|
|
let mut ids = HashSet::new();
|
|
for (_, id) in self
|
|
.changes
|
|
.iter()
|
|
.rev()
|
|
.take_while(|(seq, _)| *seq > since.unwrap())
|
|
{
|
|
if ids.insert(id) {
|
|
if let Some(track) = self.tracks.get(id) {
|
|
targets.push(track.target.clone());
|
|
} else {
|
|
removed.push(id.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Frame {
|
|
version: PROTOCOL_VERSION,
|
|
epoch: self.epoch,
|
|
sequence: self.metrics.sequence,
|
|
snapshot,
|
|
server_time_ms: now_ms,
|
|
targets,
|
|
removed,
|
|
}
|
|
}
|
|
|
|
pub fn metrics(&self) -> Metrics {
|
|
Metrics {
|
|
active_targets: self.tracks.len(),
|
|
..self.metrics.clone()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
fn sample(id: &str, sequence: u64) -> Observation {
|
|
Observation {
|
|
target_id: id.into(),
|
|
receiver_id: "station-1".into(),
|
|
source_epoch: 1,
|
|
sequence,
|
|
longitude: 120.0,
|
|
latitude: 30.0,
|
|
altitude_m: 100.0,
|
|
heading_deg: 90.0,
|
|
}
|
|
}
|
|
#[test]
|
|
fn stale_and_capacity_are_bounded() {
|
|
let mut s = Store::new(1, 4, 100, 1);
|
|
assert!(s.apply(sample("a", 2), 0));
|
|
assert!(!s.apply(sample("a", 1), 1));
|
|
assert!(!s.apply(sample("b", 1), 1));
|
|
assert_eq!(s.metrics().active_targets, 1);
|
|
assert_eq!(s.metrics().stale, 1);
|
|
s.expire(100);
|
|
assert!(s.apply(sample("b", 1), 100));
|
|
}
|
|
#[test]
|
|
fn coalesces_and_resynchronizes_after_journal_overflow() {
|
|
let mut s = Store::new(4, 2, 100, 42);
|
|
for seq in 1..=4 {
|
|
s.apply(sample("a", seq), seq);
|
|
}
|
|
let frame = s.frame(Some(2), 4);
|
|
assert!(!frame.snapshot);
|
|
assert_eq!(frame.targets.len(), 1);
|
|
assert_eq!(frame.targets[0].observation.sequence, 4);
|
|
assert!(s.frame(Some(0), 4).snapshot);
|
|
s.expire(104);
|
|
let frame = s.frame(Some(4), 104);
|
|
assert_eq!(frame.removed, ["a"]);
|
|
assert_eq!(frame.epoch, 42);
|
|
}
|
|
#[test]
|
|
fn rejects_invalid_values_and_previous_source_session() {
|
|
let mut s = Store::new(2, 4, 100, 1);
|
|
let mut a = sample("a", 1);
|
|
a.longitude = f64::NAN;
|
|
assert!(!s.apply(a, 0));
|
|
let mut a = sample("a", 1);
|
|
a.source_epoch = 2;
|
|
assert!(s.apply(a, 1));
|
|
assert!(!s.apply(sample("a", 100), 2));
|
|
assert_eq!(s.frame(None, 2).targets[0].observation.source_epoch, 2);
|
|
}
|
|
}
|