tracks lexicons and how many times they appeared on the jetstream

refactor(server): use MAX_HITS capacity for buffer on hits api

ptr.pet 3a6fd033 78fdb32d

verified
+13 -14
+13 -14
server/src/api.rs
··· 1 1 use std::{ 2 + collections::VecDeque, 2 3 fmt::Display, 3 4 net::SocketAddr, 4 5 ops::{Bound, Deref, RangeBounds}, ··· 176 177 ) -> AppResult<Json<Vec<Hit>>> { 177 178 let from = params.to.map(Bound::Included).unwrap_or(Bound::Unbounded); 178 179 let to = params.from.map(Bound::Included).unwrap_or(Bound::Unbounded); 179 - let maybe_hits = db 180 - .get_hits(&params.nsid, HitsRange { from, to }, MAX_HITS) 181 - .take(MAX_HITS); 182 - let mut hits = Vec::with_capacity(maybe_hits.size_hint().0); 183 180 184 - for maybe_hit in maybe_hits { 185 - let hit = maybe_hit?; 186 - let hit_data = hit.deser()?; 187 - 188 - hits.push(Hit { 189 - timestamp: hit.timestamp, 190 - deleted: hit_data.deleted, 191 - }); 192 - } 181 + db.get_hits(&params.nsid, HitsRange { from, to }, MAX_HITS) 182 + .take(MAX_HITS) 183 + .try_fold(Vec::with_capacity(MAX_HITS), |mut acc, hit| { 184 + let hit = hit?; 185 + let hit_data = hit.deser()?; 193 186 194 - Ok(Json(hits)) 187 + acc.push(Hit { 188 + timestamp: hit.timestamp, 189 + deleted: hit_data.deleted, 190 + }); 191 + Ok(acc) 192 + }) 193 + .map(Json) 195 194 } 196 195 197 196 async fn stream_events(db: State<Arc<Db>>, ws: WebSocketUpgrade) -> Response {