1pub use indexmap::IndexMap as HashMap;
18use std::borrow::Cow;
19use std::sync::Arc;
20#[cfg(feature = "engine")]
21use crate::exchange_generated::ExchangeBase;
22
23#[derive(Debug, Clone, PartialEq)]
42pub enum Value {
43 Null,
44 Bool(bool),
45 Int(i64),
46 Float(f64),
47 Str(Cow<'static, str>),
48 Dict(Arc<HashMap<String, Value>>),
49 Arr(Arc<Vec<Value>>),
50}
51
52impl Default for Value {
53 fn default() -> Self {
54 Value::Null
55 }
56}
57
58impl Value {
59 pub fn reduce(&mut self) {
67 let m = match self { Value::Dict(m) if m.contains_key("__precise") => Arc::make_mut(m), _ => return };
68 let mut decimals: i64 = match m.get("decimals") {
69 Some(Value::Int(n)) => *n,
70 _ => return,
71 };
72 let mut integer: String = match m.get("integer") {
73 Some(Value::Str(s)) => s.to_string(),
74 _ => return,
75 };
76 if decimals < 0 {
78 let (sign, digits) = if let Some(rest) = integer.strip_prefix('-') {
80 ("-", rest.to_string())
81 } else { ("", integer.clone()) };
82 let padded = format!("{digits}{:0<width$}", "", width = (-decimals) as usize);
83 integer = format!("{sign}{padded}");
84 decimals = 0;
85 }
86 while decimals > 0 && integer.ends_with('0') && integer.len() > 1 {
88 integer.pop();
89 decimals -= 1;
90 }
91 m.insert("integer".to_string(), Value::Str(Cow::Owned(integer)));
92 m.insert("decimals".to_string(), Value::Int(decimals));
93 }
94
95 pub fn append(&mut self, item: Value) {
101 match self {
102 Value::Arr(a) => Arc::make_mut(a).push(item),
103 Value::Dict(m) if m.contains_key("__cacheKind") => {
104 cache_append(self, item);
105 }
106 _ => {}
107 }
108 }
109
110 pub fn clear(&mut self) {
113 match self {
114 Value::Arr(a) => Arc::make_mut(a).clear(),
115 Value::Dict(m) if m.contains_key("__cacheKind") => {
116 if let Some(id) = cache_id_of_m(m) {
117 with_cache_cell(id, |c| {
118 if let Some(Value::Arr(data)) = c.get_mut("_data") { Arc::make_mut(data).clear(); }
119 cache_reset_bookkeeping(c);
120 });
121 } else {
122 let m = Arc::make_mut(m);
123 if let Some(Value::Arr(data)) = m.get_mut("_data") {
124 Arc::make_mut(data).clear();
125 }
126 cache_reset_bookkeeping(m);
127 }
128 }
129 _ => {}
130 }
131 }
132
133 pub fn get_limit(&mut self, symbol: Value, limit: Value) -> Value {
138 if matches!(self, Value::Dict(d) if d.contains_key("__cacheKind")) {
139 cache_get_limit(self, symbol, limit)
140 } else {
141 limit
142 }
143 }
144
145 #[allow(non_snake_case)]
150 pub fn List(items: Vec<Value>) -> Value {
151 Value::Arr(Arc::new(items))
152 }
153
154 #[allow(non_snake_case)]
158 pub fn Map(m: HashMap<String, Value>) -> Value {
159 Value::Dict(Arc::new(m))
160 }
161
162 #[allow(non_snake_case)]
164 pub fn Array(a: Vec<Value>) -> Value {
165 Value::Arr(Arc::new(a))
166 }
167
168 pub fn str(s: impl Into<String>) -> Self {
171 Value::Str(Cow::Owned(s.into()))
172 }
173
174 pub fn int(n: i64) -> Self {
175 Value::Int(n)
176 }
177
178 pub fn float(f: f64) -> Self {
179 Value::Float(f)
180 }
181
182 pub fn bool(b: bool) -> Self {
183 Value::Bool(b)
184 }
185
186 pub fn map(m: HashMap<String, Value>) -> Self {
187 Value::Map(m)
188 }
189
190 pub fn array(a: Vec<Value>) -> Self {
191 Value::Array(a)
192 }
193
194 pub fn is_null(&self) -> bool { matches!(self, Value::Null) }
197 pub fn is_str(&self) -> bool { matches!(self, Value::Str(_)) }
198 pub fn is_int(&self) -> bool { matches!(self, Value::Int(_)) }
199 pub fn is_float(&self) -> bool { matches!(self, Value::Float(_)) }
200 pub fn is_number(&self) -> bool { matches!(self, Value::Int(_) | Value::Float(_)) }
201 pub fn is_bool(&self) -> bool { matches!(self, Value::Bool(_)) }
202 pub fn is_map(&self) -> bool { matches!(self, Value::Dict(_)) }
203 pub fn is_array(&self) -> bool { matches!(self, Value::Arr(_)) }
204 pub fn is_truthy(&self) -> bool {
205 match self {
206 Value::Null => false,
207 Value::Bool(b) => *b,
208 Value::Int(n) => *n != 0,
209 Value::Float(f) => *f != 0.0,
210 Value::Str(s) => !s.is_empty(),
211 Value::Dict(m) => !m.is_empty(),
212 Value::Arr(a) => !a.is_empty(),
213 }
214 }
215
216 pub fn as_str(&self) -> Option<&str> { if let Value::Str(s) = self { Some(s.as_ref()) } else { None } }
219 pub fn as_i64(&self) -> Option<i64> { if let Value::Int(n) = self { Some(*n) } else { None } }
220 pub fn as_f64(&self) -> Option<f64> {
221 match self {
222 Value::Float(f) => Some(*f),
223 Value::Int(n) => Some(*n as f64),
224 _ => None,
225 }
226 }
227 pub fn as_bool(&self) -> Option<bool> { if let Value::Bool(b) = self { Some(*b) } else { None } }
228 pub fn as_map(&self) -> Option<&HashMap<String, Value>> {
229 if let Value::Dict(m) = self { Some(&**m) } else { None }
230 }
231 pub fn as_array(&self) -> Option<&Vec<Value>> {
232 if let Value::Arr(a) = self { Some(&**a) } else { None }
233 }
234
235 pub fn len(&self) -> usize {
239 match self {
240 Value::Arr(a) => a.len(),
241 Value::Dict(m) => book_cache_len_of(m).or_else(|| cache_len_of_marker(m)).unwrap_or(m.len()),
246 Value::Str(s) => s.chars().count(),
247 _ => 0,
248 }
249 }
250
251 pub fn is_empty(&self) -> bool { self.len() == 0 }
252
253 pub fn describe(&self) -> Value { Value::Null }
259 pub fn reject(&self, args: &[Value]) -> Value {
263 crate::pro::ws_client::value_reject(self, args)
264 }
265 pub fn resolve(&self, args: &[Value]) -> Value {
268 crate::pro::ws_client::value_resolve(self, args)
269 }
270 pub fn future(&self, args: &[Value]) -> Value {
278 self.open_flight(args.get(0).cloned().unwrap_or(Value::Null))
279 }
280 pub fn reusable_future(&self, msg_hash: Value) -> Value {
283 self.open_flight(msg_hash)
284 }
285 fn open_flight(&self, msg_hash: Value) -> Value {
290 let hash = match &msg_hash {
291 Value::Str(h) => h.clone(),
292 _ => return msg_hash,
293 };
294 match crate::pro::ws_client::url_of(self) {
295 Some(url) => {
296 let led = crate::pro::ws_client::begin_flight(&url, &hash);
297 crate::pro::ws_client::flight_handle(&url, &hash, led)
298 }
299 None => msg_hash,
300 }
301 }
302 pub fn hashmap(&self) -> Value {
307 if let Value::Dict(d) = self {
308 if let Some(id) = cache_id_of(self) {
315 let hm = with_cache_cell(id, |m| m.get("hashmap").cloned().unwrap_or(Value::Null));
316 return tag_cache_hashmap_buckets(hm, id);
317 }
318 if let Some(v) = d.get("hashmap") { return v.clone(); }
319 }
320 Value::Null
321 }
322 pub fn send(&self, args: &[Value]) -> Value {
325 crate::pro::ws_client::value_send(self, args)
326 }
327 pub fn decode_proto_msg(&self, _args: &[Value]) -> Value { Value::Null }
330 pub fn on_pong(&self, _args: &[Value]) -> Value {
332 crate::pro::ws_client::value_on_pong(self)
333 }
334 pub fn call(&self, _args: &[Value]) -> Value { Value::Null }
336 pub fn is_dictionary(&self, value: Value) -> Value {
340 Value::Bool(matches!(value, Value::Dict(_)))
341 }
342 #[cfg(feature = "engine")]
348 fn snapshot_as_exchange(&self) -> crate::exchange::Exchange {
349 let mut ex = crate::exchange::Exchange::new(None);
350 ex.options = crate::runtime::get_value(self, &Value::Str("options".into()));
351 ex.currencies = crate::runtime::get_value(self, &Value::Str("currencies".into()));
352 ex
353 }
354
355 pub fn network_code_to_id(&self, code: Value, args: &[Value]) -> Value {
358 #[cfg(feature = "engine")]
359 {
360 let currency = args.first().cloned().unwrap_or(Value::Null);
361 return crate::exchange::BaseCore::new(self.snapshot_as_exchange()).network_code_to_id(code, &[currency]);
362 }
363 #[cfg(not(feature = "engine"))]
364 {
365 let _ = args;
366 code
367 }
368 }
369
370 pub fn network_id_to_code(&self, args: &[Value]) -> Value {
373 #[cfg(feature = "engine")]
374 {
375 return crate::exchange::BaseCore::new(self.snapshot_as_exchange()).network_id_to_code(args);
376 }
377 #[cfg(not(feature = "engine"))]
378 {
379 let _ = args;
380 Value::Null
381 }
382 }
383
384 pub fn extend(&self, a: Value, args: &[Value]) -> Value {
389 #[cfg(feature = "engine")]
390 {
391 return self.snapshot_as_exchange().extend(a, args);
392 }
393 #[cfg(not(feature = "engine"))]
394 {
395 let _ = args;
396 a
397 }
398 }
399
400 pub fn set_markets(&mut self, markets: Value) -> Value {
406 #[cfg(feature = "engine")]
407 {
408 if let Value::Dict(m) = &*self {
416 if let Some(Value::Str(id)) = m.get("__live_id") {
417 if let Some(cb) = live_set_markets_get() {
418 return cb(&id.clone(), markets);
419 }
420 }
421 }
422 let mut ex = crate::exchange::BaseCore::new(self.snapshot_as_exchange());
423 let result = ex.set_markets(markets, &[]);
424 crate::runtime::add_element_to_object(self, &Value::Str("markets".into()), ex.markets.clone());
425 crate::runtime::add_element_to_object(self, &Value::Str("markets_by_id".into()), ex.markets_by_id.clone());
426 crate::runtime::add_element_to_object(self, &Value::Str("symbols".into()), ex.symbols.clone());
427 return result;
428 }
429 #[cfg(not(feature = "engine"))]
430 {
431 let _ = markets;
432 Value::Null
433 }
434 }
435 pub fn store_array(&mut self, delta: Value) {
441 if side_kind(self).is_some() { side_store_array(self, delta); }
442 }
443 pub fn append_to_array(&self, _v: Value) {}
444
445 pub fn to_json(&self) -> serde_json::Value {
448 match self {
449 Value::Null => serde_json::Value::Null,
450 Value::Bool(b) => serde_json::Value::Bool(*b),
451 Value::Int(n) => serde_json::json!(*n),
452 Value::Float(f) => serde_json::json!(*f),
453 Value::Str(s) => serde_json::Value::String(s.to_string()),
454 Value::Arr(a) => serde_json::Value::Array(a.iter().map(Value::to_json).collect()),
455 Value::Dict(m) => {
456 if m.contains_key("__side_id") {
463 let entries = side_entries_view(m);
464 return serde_json::Value::Array(entries.iter().map(Value::to_json).collect());
465 }
466 let is_book = m.contains_key("__book_id") || m.contains_key("__bookKind");
471 let mut obj = serde_json::Map::new();
472 for (k, v) in m.iter() {
473 if is_book && matches!(k.as_str(), "__bookKind" | "__book_id" | "_depth") {
474 continue;
475 }
476 obj.insert(k.clone(), v.to_json());
477 }
478 if let Some(Value::Int(id)) = m.get("__book_id") {
479 for (k, v) in book_meta_snapshot(*id) { obj.insert(k, v.to_json()); }
480 }
481 if m.contains_key("__cacheKind") {
484 let data = cache_data_snapshot(m);
485 obj.insert("_data".to_string(),
486 serde_json::Value::Array(data.iter().map(Value::to_json).collect()));
487 }
488 serde_json::Value::Object(obj)
489 }
490 }
491 }
492
493 pub fn from_json(v: &serde_json::Value) -> Self {
494 match v {
495 serde_json::Value::Null => Value::Null,
496 serde_json::Value::Bool(b) => Value::Bool(*b),
497 serde_json::Value::Number(n) => {
498 if let Some(i) = n.as_i64() {
499 Value::Int(i)
500 } else if n.is_u64() {
501 Value::Str(n.to_string().into())
507 } else if let Some(f) = n.as_f64() {
508 Value::Float(f)
509 } else {
510 Value::Null
511 }
512 }
513 serde_json::Value::String(s) => Value::Str(s.clone().into()),
514 serde_json::Value::Array(a) => Value::Array(a.iter().map(Value::from_json).collect()),
515 serde_json::Value::Object(m) => Value::Map(
516 m.iter().map(|(k, v)| (k.clone(), Value::from_json(v))).collect()
517 ),
518 }
519 }
520}
521
522impl std::fmt::Display for Value {
523 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
524 match self {
525 Value::Null => write!(f, "null"),
526 Value::Bool(b) => write!(f, "{b}"),
527 Value::Int(n) => write!(f, "{n}"),
528 Value::Float(fl) => write!(f, "{fl}"),
529 Value::Str(s) => write!(f, "{s}"),
530 Value::Arr(a) => write!(f, "[{} items]", a.len()),
531 Value::Dict(m) => write!(f, "{{{} keys}}", m.len()),
532 }
533 }
534}
535
536impl std::ops::Not for Value {
543 type Output = Value;
544 fn not(self) -> Value { Value::Bool(!self.is_truthy()) }
545}
546
547impl std::ops::Not for &Value {
548 type Output = Value;
549 fn not(self) -> Value { Value::Bool(!self.is_truthy()) }
550}
551
552impl std::ops::BitAnd for Value {
553 type Output = Value;
554 fn bitand(self, rhs: Value) -> Value {
555 Value::Bool(self.is_truthy() && rhs.is_truthy())
556 }
557}
558
559impl std::ops::BitOr for Value {
560 type Output = Value;
561 fn bitor(self, rhs: Value) -> Value {
562 Value::Bool(self.is_truthy() || rhs.is_truthy())
563 }
564}
565
566impl From<&str> for Value { fn from(s: &str) -> Self { Value::Str(Cow::Owned(s.to_owned())) } }
567impl From<String> for Value { fn from(s: String) -> Self { Value::Str(Cow::Owned(s)) } }
568impl From<i64> for Value { fn from(n: i64) -> Self { Value::Int(n) } }
569impl From<f64> for Value { fn from(f: f64) -> Self { Value::Float(f) } }
570impl From<bool> for Value { fn from(b: bool) -> Self { Value::Bool(b) } }
571impl From<crate::ExchangeError> for Value {
577 fn from(e: crate::ExchangeError) -> Self {
578 Value::Str(format!("[{}] {}", e.kind, e.message).into())
579 }
580}
581impl From<HashMap<String, Value>> for Value {
582 fn from(m: HashMap<String, Value>) -> Self { Value::Map(m) }
583}
584impl From<Vec<Value>> for Value {
585 fn from(a: Vec<Value>) -> Self { Value::Array(a) }
586}
587impl From<serde_json::Value> for Value {
588 fn from(v: serde_json::Value) -> Self { Value::from_json(&v) }
589}
590
591type LiveLookupFn = fn(id: &str, key: &str) -> Option<Value>;
601static LIVE_LOOKUP_PTR: std::sync::atomic::AtomicUsize =
607 std::sync::atomic::AtomicUsize::new(0);
608
609pub fn set_live_lookup(f: LiveLookupFn) {
610 LIVE_LOOKUP_PTR.store(f as usize, std::sync::atomic::Ordering::Release);
611}
612
613fn live_lookup_get() -> Option<LiveLookupFn> {
614 let n = LIVE_LOOKUP_PTR.load(std::sync::atomic::Ordering::Acquire);
615 if n == 0 { None } else {
616 Some(unsafe { std::mem::transmute::<usize, LiveLookupFn>(n) })
619 }
620}
621
622type LiveSetMarketsFn = fn(id: &str, markets: Value) -> Value;
628static LIVE_SET_MARKETS_PTR: std::sync::atomic::AtomicUsize =
629 std::sync::atomic::AtomicUsize::new(0);
630
631pub fn set_live_set_markets(f: LiveSetMarketsFn) {
632 LIVE_SET_MARKETS_PTR.store(f as usize, std::sync::atomic::Ordering::Release);
633}
634
635fn live_set_markets_get() -> Option<LiveSetMarketsFn> {
636 let n = LIVE_SET_MARKETS_PTR.load(std::sync::atomic::Ordering::Acquire);
637 if n == 0 { None } else {
638 Some(unsafe { std::mem::transmute::<usize, LiveSetMarketsFn>(n) })
641 }
642}
643
644#[inline]
648pub fn get_value_k(obj: &Value, key: &str) -> Value {
649 if let Value::Dict(m) = obj {
650 if is_book_meta_key(key) {
654 if let Some(id) = book_id_of(m) { return book_meta_get(id, key); }
655 }
656 if key == "cache" {
657 if let Some(id) = book_id_of(m) { return book_cache_handle(id); }
658 }
659 if let Some(v) = m.get(key) {
660 if !matches!(v, Value::Null) {
661 return v.clone();
662 }
663 }
664 if let Some(id_val) = m.get("__live_id") {
665 if let (Value::Str(id), Some(cb)) = (id_val, live_lookup_get()) {
666 if let Some(v) = cb(id, key) { return v; }
667 }
668 }
669 }
670 Value::Null
671}
672
673pub fn get_value(obj: &Value, key: &Value) -> Value {
676 if let Value::Dict(m) = obj {
685 let idx = match key {
686 Value::Int(i) if *i >= 0 => Some(*i as usize),
687 Value::Str(s) => s.parse::<usize>().ok(),
688 _ => None,
689 };
690 if let Some(i) = idx {
691 if let Some(id) = book_cache_id_of(m) {
692 return book_cache_at(id, i);
693 }
694 if m.contains_key("__cacheKind") {
695 return cache_at(m, i);
696 }
697 if m.contains_key("__sideKind") {
698 return side_entry_at(m, i);
699 }
700 }
701 }
702 match (obj, key) {
703 (Value::Dict(m), Value::Str(k)) => {
704 if is_book_meta_key(k) {
707 if let Some(id) = book_id_of(m) { return book_meta_get(id, k); }
708 }
709 if k == "cache" {
710 if let Some(id) = book_id_of(m) { return book_cache_handle(id); }
711 }
712 if k == "hashmap" && m.contains_key("__cacheKind") {
717 if let Some(id) = cache_id_of_m(m) {
718 return with_cache_cell(id, |c| c.get("hashmap").cloned().unwrap_or(Value::Null));
719 }
720 return m.get("hashmap").cloned().unwrap_or(Value::Null);
721 }
722 if (k == "subscriptions" || k == "futures")
727 && m.contains_key("subscriptions") && m.contains_key("futures")
728 {
729 if let Some(Value::Str(url)) = m.get("url") {
730 return crate::pro::ws_client::client_field_live(url, k);
731 }
732 }
733 let snapshot_val = m.get(k.as_ref());
740 if let Some(v) = snapshot_val {
741 if !matches!(v, Value::Null) {
742 return v.clone();
743 }
744 }
745 if let Some(id_val) = m.get("__live_id") {
746 if let (Value::Str(id), Some(cb)) = (id_val, live_lookup_get()) {
747 if let Some(v) = cb(id, k) { return v; }
748 }
749 }
750 Value::Null
751 }
752 (Value::Arr(a), Value::Int(i)) => a.get(*i as usize).cloned().unwrap_or(Value::Null),
753 (Value::Arr(a), Value::Str(k)) => {
754 if let Ok(i) = k.parse::<usize>() {
755 a.get(i).cloned().unwrap_or(Value::Null)
756 } else {
757 Value::Null
758 }
759 }
760 (Value::Str(s), Value::Int(i)) => {
764 if *i < 0 {
765 return Value::Null;
766 }
767 match s.chars().nth(*i as usize) {
768 Some(c) => Value::Str(c.to_string().into()),
769 None => Value::Null,
770 }
771 }
772 _ => Value::Null,
773 }
774}
775
776pub fn set_value(obj: &mut Value, key: &Value, val: Value) {
778 match (obj, key) {
779 (Value::Dict(m), Value::Str(k)) => {
780 if try_book_meta_write(m, k, &val) { return; }
781 try_ws_subs_write(m, k, &val);
785 try_ws_sub_field_write(m, k, &val);
786 Arc::make_mut(m).insert(k.to_string(), val);
787 }
788 (Value::Arr(a), Value::Int(i)) => {
789 let idx = *i as usize;
790 let a = Arc::make_mut(a);
791 if idx < a.len() { a[idx] = val; }
792 }
793 _ => {}
794 }
795}
796
797pub fn safe_string(obj: &Value, key: &str, default: Option<&str>) -> Option<String> {
800 let v = get_value(obj, &Value::Str(key.to_owned().into()));
801 match v {
802 Value::Str(s) => Some(s.to_string()),
803 Value::Int(n) => Some(n.to_string()),
804 Value::Float(f)=> Some(f.to_string()),
805 Value::Null => default.map(str::to_owned),
808 _ => default.map(str::to_owned),
809 }
810}
811
812pub fn safe_number(obj: &Value, key: &str, default: Option<f64>) -> Option<f64> {
813 let v = get_value(obj, &Value::Str(key.to_owned().into()));
814 match v {
815 Value::Float(f) => Some(f),
816 Value::Int(n) => Some(n as f64),
817 Value::Str(s) => s.parse::<f64>().ok().or(default),
818 _ => default,
819 }
820}
821
822pub fn safe_integer(obj: &Value, key: &str, default: Option<i64>) -> Option<i64> {
823 let v = get_value(obj, &Value::Str(key.to_owned().into()));
824 match v {
825 Value::Int(n) => Some(n),
826 Value::Float(f) => Some(f as i64),
827 Value::Str(s) => s.parse::<i64>().ok().or(default),
828 _ => default,
829 }
830}
831
832pub fn safe_bool(obj: &Value, key: &str, default: Option<bool>) -> Option<bool> {
833 let v = get_value(obj, &Value::Str(key.to_owned().into()));
834 match v {
835 Value::Bool(b) => Some(b),
836 Value::Int(n) => Some(n != 0),
837 Value::Str(s) => match s.to_lowercase().as_str() {
838 "true" | "1" | "yes" => Some(true),
839 "false"| "0" | "no" => Some(false),
840 _ => default,
841 },
842 _ => default,
843 }
844}
845
846pub fn deep_extend(dst: Value, src: Value) -> Value {
849 match (dst, src) {
850 (Value::Dict(mut d), Value::Dict(s)) => {
851 let dm = Arc::make_mut(&mut d);
852 let sm = Arc::try_unwrap(s).unwrap_or_else(|arc| (*arc).clone());
853 for (k, v) in sm {
854 let entry = dm.shift_remove(&k).unwrap_or(Value::Null);
855 dm.insert(k, deep_extend(entry, v));
856 }
857 Value::Dict(d)
858 }
859 (_, src) => src,
860 }
861}
862
863fn cache_str_field(item: &Value, key: &str) -> Option<String> {
869 match item {
870 Value::Dict(d) => match d.get(key) {
871 Some(Value::Str(s)) => Some(s.to_string()),
876 Some(Value::Int(n)) => Some(n.to_string()),
877 Some(Value::Float(f)) => Some(f.to_string()),
878 Some(Value::Bool(b)) => Some(b.to_string()),
879 _ => None,
880 },
881 _ => None,
882 }
883}
884
885fn tag_cache_hashmap_buckets(hm: Value, id: i64) -> Value {
890 if let Value::Dict(d) = &hm {
891 let mut out: HashMap<String, Value> = HashMap::new();
892 for (sym, bucket) in d.iter() {
893 if let Value::Dict(bd) = bucket {
894 let mut nb: HashMap<String, Value> = (**bd).clone();
895 nb.insert(
896 "__cache_backref".to_string(),
897 Value::Arr(std::sync::Arc::new(vec![Value::Int(id), Value::Str(sym.clone().into())])),
898 );
899 out.insert(sym.clone(), Value::Dict(std::sync::Arc::new(nb)));
900 } else {
901 out.insert(sym.clone(), bucket.clone());
902 }
903 }
904 return Value::Dict(std::sync::Arc::new(out));
905 }
906 hm
907}
908
909pub(crate) fn try_cache_hashmap_write(bucket: &HashMap<String, Value>, key: &str, item: &Value) -> bool {
916 let (id, sym) = match bucket.get("__cache_backref") {
917 Some(Value::Arr(a)) => match (a.first(), a.get(1)) {
918 (Some(Value::Int(id)), Some(Value::Str(s))) => (*id, s.clone()),
919 _ => return false,
920 },
921 _ => return false,
922 };
923 with_cache_cell(id, |m| {
924 {
926 let hm = cache_dict_field_mut(m, "hashmap");
927 let b = hm.entry(sym.to_string()).or_insert_with(|| Value::Map(HashMap::new()));
928 if let Value::Dict(bd) = b {
929 Arc::make_mut(bd).insert(key.to_string(), item.clone());
930 }
931 }
932 let data = cache_data_mut(m);
934 for x in data.iter_mut() {
935 if cache_str_field(x, "symbol").as_deref() == Some(&sym)
936 && (cache_str_field(x, "id").as_deref() == Some(key)
937 || cache_str_field(x, "side").as_deref() == Some(key))
938 {
939 *x = item.clone();
940 break;
941 }
942 }
943 });
944 true
945}
946
947fn cache_kind(v: &Value) -> Option<String> {
948 match v {
949 Value::Dict(d) => match d.get("__cacheKind") {
950 Some(Value::Str(s)) => Some(s.to_string()),
951 _ => None,
952 },
953 _ => None,
954 }
955}
956
957fn cache_max_size(v: &Value) -> Option<usize> {
958 match v {
959 Value::Dict(d) => match d.get("maxSize") {
960 Some(Value::Int(n)) if *n > 0 => Some(*n as usize),
961 _ => None,
962 },
963 _ => None,
964 }
965}
966
967fn cache_data_mut(m: &mut HashMap<String, Value>) -> &mut Vec<Value> {
970 let entry = m.entry("_data".to_string()).or_insert_with(|| Value::Array(Vec::new()));
971 if let Value::Arr(a) = entry {
972 return Arc::make_mut(a);
973 }
974 *entry = Value::Array(Vec::new());
975 if let Value::Arr(a) = entry {
976 return Arc::make_mut(a);
977 }
978 unreachable!()
979}
980
981fn cache_dict_field_mut<'a>(m: &'a mut HashMap<String, Value>, key: &str) -> &'a mut HashMap<String, Value> {
982 let entry = m.entry(key.to_string()).or_insert_with(|| Value::Map(HashMap::new()));
983 if let Value::Dict(d) = entry {
984 return Arc::make_mut(d);
985 }
986 *entry = Value::Map(HashMap::new());
987 if let Value::Dict(d) = entry {
988 return Arc::make_mut(d);
989 }
990 unreachable!()
991}
992
993fn cache_int_field(m: &HashMap<String, Value>, key: &str) -> i64 {
994 match m.get(key) { Some(Value::Int(n)) => *n, _ => 0 }
995}
996
997fn cache_set_int(m: &mut HashMap<String, Value>, key: &str, value: i64) {
998 m.insert(key.to_string(), Value::Int(value));
999}
1000
1001fn cache_bool_field(m: &HashMap<String, Value>, key: &str) -> bool {
1002 matches!(m.get(key), Some(Value::Bool(true)))
1003}
1004
1005fn cache_set_bool(m: &mut HashMap<String, Value>, key: &str, value: bool) {
1006 m.insert(key.to_string(), Value::Bool(value));
1007}
1008
1009fn cache_reset_bookkeeping(state: &mut HashMap<String, Value>) {
1018 for k in ["hashmap", "_newUpdatesBySymbol", "_seenUpdatesBySymbol", "_seenUpdatesAll",
1019 "_clearUpdatesBySymbol", "_sizeTracker"] {
1020 if state.contains_key(k) {
1021 state.insert(k.to_string(), Value::Map(HashMap::new()));
1022 }
1023 }
1024 if state.contains_key("_allNewUpdates") { cache_set_int(state, "_allNewUpdates", 0); }
1025 if state.contains_key("_newUpdates") { cache_set_int(state, "_newUpdates", 0); }
1026 if state.contains_key("_clearAllUpdates") { cache_set_bool(state, "_clearAllUpdates", false); }
1027 if state.contains_key("_clearUpdates") { cache_set_bool(state, "_clearUpdates", false); }
1028}
1029
1030fn cache_apply_clear_all(m: &mut HashMap<String, Value>) {
1039 if cache_bool_field(m, "_clearAllUpdates") {
1040 cache_set_bool(m, "_clearAllUpdates", false);
1041 cache_set_int(m, "_allNewUpdates", 0);
1042 cache_dict_field_mut(m, "_seenUpdatesAll").clear();
1043 }
1044}
1045
1046fn cache_take_symbol_clear(m: &mut HashMap<String, Value>, key1: &str) -> bool {
1048 let cs = cache_dict_field_mut(m, "_clearUpdatesBySymbol");
1049 if matches!(cs.get(key1), Some(Value::Bool(true))) {
1050 cs.insert(key1.to_string(), Value::Bool(false));
1051 true
1052 } else {
1053 false
1054 }
1055}
1056
1057fn cache_seen_add(m: &mut HashMap<String, Value>, field: &str, key1: &str, key2: &str) -> i64 {
1059 let outer = cache_dict_field_mut(m, field);
1060 let set = outer.entry(key1.to_string()).or_insert_with(|| Value::Map(HashMap::new()));
1061 if let Value::Dict(sd) = set {
1062 let sd = Arc::make_mut(sd);
1063 sd.insert(key2.to_string(), Value::Bool(true));
1064 sd.len() as i64
1065 } else {
1066 0
1067 }
1068}
1069
1070fn cache_seen_size(m: &mut HashMap<String, Value>, field: &str, key1: &str) -> i64 {
1072 match cache_dict_field_mut(m, field).get(key1) {
1073 Some(Value::Dict(sd)) => sd.len() as i64,
1074 _ => 0,
1075 }
1076}
1077
1078fn cache_seen_clear(m: &mut HashMap<String, Value>, field: &str, key1: &str) {
1080 if let Some(Value::Dict(sd)) = cache_dict_field_mut(m, field).get_mut(key1) {
1081 Arc::make_mut(sd).clear();
1082 }
1083}
1084
1085fn cache_seen_remove(m: &mut HashMap<String, Value>, field: &str, key1: &str, key2: &str) -> bool {
1088 let outer = cache_dict_field_mut(m, field);
1089 let (removed, empty) = if let Some(Value::Dict(sd)) = outer.get_mut(key1) {
1090 let sd = Arc::make_mut(sd);
1091 let removed = sd.shift_remove(key2).is_some();
1092 (removed, sd.is_empty())
1093 } else {
1094 (false, false)
1095 };
1096 if empty { outer.shift_remove(key1); }
1097 removed
1098}
1099
1100static CACHE_STORE: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, HashMap<String, Value>>>> =
1109 once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
1110static NEXT_CACHE_ID: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1);
1111
1112pub(crate) fn alloc_cache_id(initial: HashMap<String, Value>) -> i64 {
1114 let id = NEXT_CACHE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1115 CACHE_STORE.lock().unwrap().insert(id, initial);
1116 id
1117}
1118
1119fn cache_id_of(v: &Value) -> Option<i64> {
1120 match v {
1121 Value::Dict(d) => match d.get("__cache_id") { Some(Value::Int(n)) => Some(*n), _ => None },
1122 _ => None,
1123 }
1124}
1125fn cache_id_of_m(m: &HashMap<String, Value>) -> Option<i64> {
1126 match m.get("__cache_id") { Some(Value::Int(n)) => Some(*n), _ => None }
1127}
1128
1129fn with_cache_cell<R>(id: i64, f: impl FnOnce(&mut HashMap<String, Value>) -> R) -> R {
1130 let mut g = CACHE_STORE.lock().unwrap();
1131 let cell = g.entry(id).or_insert_with(HashMap::new);
1132 f(cell)
1133}
1134
1135fn cache_data_snapshot(m: &HashMap<String, Value>) -> Vec<Value> {
1138 if let Some(id) = cache_id_of_m(m) {
1139 return with_cache_cell(id, |c| match c.get("_data") {
1140 Some(Value::Arr(a)) => (**a).clone(),
1141 _ => Vec::new(),
1142 });
1143 }
1144 match m.get("_data") { Some(Value::Arr(a)) => (**a).clone(), _ => Vec::new() }
1145}
1146
1147fn cache_len_of(m: &HashMap<String, Value>) -> usize {
1148 if let Some(id) = cache_id_of_m(m) {
1149 return with_cache_cell(id, |c| match c.get("_data") { Some(Value::Arr(a)) => a.len(), _ => 0 });
1150 }
1151 match m.get("_data") { Some(Value::Arr(a)) => a.len(), _ => 0 }
1152}
1153
1154fn cache_at(m: &HashMap<String, Value>, i: usize) -> Value {
1155 if let Some(id) = cache_id_of_m(m) {
1156 return with_cache_cell(id, |c| match c.get("_data") {
1157 Some(Value::Arr(a)) => a.get(i).cloned().unwrap_or(Value::Null),
1158 _ => Value::Null,
1159 });
1160 }
1161 match m.get("_data") { Some(Value::Arr(a)) => a.get(i).cloned().unwrap_or(Value::Null), _ => Value::Null }
1162}
1163
1164pub(crate) fn cache_len_of_marker(m: &HashMap<String, Value>) -> Option<usize> {
1166 if m.contains_key("__cacheKind") { Some(cache_len_of(m)) } else { None }
1167}
1168
1169pub(crate) fn cache_append(target: &mut Value, item: Value) {
1170 let kind = cache_kind(target).unwrap_or_default();
1171 let cap = cache_max_size(target);
1172 if let Some(id) = cache_id_of(target) {
1173 with_cache_cell(id, |m| cache_append_inner(m, &kind, cap, item));
1174 } else if let Value::Dict(m_arc) = target {
1175 let m = Arc::make_mut(m_arc);
1176 cache_append_inner(m, &kind, cap, item);
1177 }
1178}
1179
1180fn cache_append_inner(m: &mut HashMap<String, Value>, kind: &str, cap: Option<usize>, item: Value) {
1181 {
1182 match kind {
1183 "ArrayCache" => {
1184 let data = cache_data_mut(m);
1185 if let Some(cap) = cap { if data.len() == cap { data.remove(0); } }
1186 data.push(item.clone());
1187 cache_apply_clear_all(m);
1189 let sym = cache_str_field(&item, "symbol").unwrap_or_default();
1190 if cache_take_symbol_clear(m, &sym) {
1191 cache_dict_field_mut(m, "_newUpdatesBySymbol").insert(sym.clone(), Value::Int(0));
1192 }
1193 {
1194 let nubs = cache_dict_field_mut(m, "_newUpdatesBySymbol");
1195 let cur = match nubs.get(&sym) { Some(Value::Int(n)) => *n, _ => 0 };
1196 nubs.insert(sym, Value::Int(cur + 1));
1197 }
1198 let all = cache_int_field(m, "_allNewUpdates");
1199 cache_set_int(m, "_allNewUpdates", all + 1);
1200 }
1201 "ArrayCacheByTimestamp" => {
1202 let ts_key = match item {
1203 Value::Arr(ref a) => match a.first() {
1204 Some(Value::Int(n)) => n.to_string(),
1205 Some(Value::Float(f)) => f.to_string(),
1206 Some(Value::Str(s)) => s.to_string(),
1207 _ => String::new(),
1208 },
1209 _ => String::new(),
1210 };
1211 let existing_idx = {
1212 let hm = cache_dict_field_mut(m, "hashmap");
1213 match hm.get(&ts_key) { Some(Value::Int(n)) => Some(*n as usize), _ => None }
1214 };
1215 if let Some(idx) = existing_idx {
1216 let data = cache_data_mut(m);
1217 if idx < data.len() && data[idx] != item { data[idx] = item.clone(); }
1218 } else {
1219 let evicting = match cap { Some(c) => {
1221 let data_len = cache_data_mut(m).len();
1222 data_len == c
1223 }, None => false };
1224 if evicting {
1225 let removed = {
1226 let data = cache_data_mut(m);
1227 if data.is_empty() { Value::Null } else { data.remove(0) }
1228 };
1229 let removed_key = match &removed {
1230 Value::Arr(a) => match a.first() {
1231 Some(Value::Int(n)) => n.to_string(),
1232 Some(Value::Float(f)) => f.to_string(),
1233 Some(Value::Str(s)) => s.to_string(),
1234 _ => String::new(),
1235 },
1236 _ => String::new(),
1237 };
1238 let hm = cache_dict_field_mut(m, "hashmap");
1239 hm.shift_remove(&removed_key);
1240 for v in hm.values_mut() {
1241 if let Value::Int(n) = v { *n = n.saturating_sub(1); }
1242 }
1243 }
1244 let new_idx = {
1245 let data = cache_data_mut(m);
1246 let i = data.len();
1247 data.push(item);
1248 i
1249 };
1250 let hm = cache_dict_field_mut(m, "hashmap");
1251 hm.insert(ts_key.clone(), Value::Int(new_idx as i64));
1252 }
1253 if cache_bool_field(m, "_clearUpdates") {
1255 cache_set_bool(m, "_clearUpdates", false);
1256 let st = cache_dict_field_mut(m, "_sizeTracker");
1257 st.clear();
1258 }
1259 {
1260 let st = cache_dict_field_mut(m, "_sizeTracker");
1261 st.insert(ts_key, Value::Bool(true));
1262 }
1263 let st_len = match m.get("_sizeTracker") {
1264 Some(Value::Dict(d)) => d.len() as i64,
1265 _ => 0,
1266 };
1267 cache_set_int(m, "_newUpdates", st_len);
1268 }
1269 "ArrayCacheBySymbolById" | "ArrayCacheBySymbolBySide" | "ArrayCacheByOutcomeById" => {
1270 let key1_name = if kind == "ArrayCacheByOutcomeById" { "outcome" } else { "symbol" };
1275 let symbol = cache_str_field(&item, key1_name).unwrap_or_default();
1276 let key2_name = if kind == "ArrayCacheBySymbolBySide" { "side" } else { "id" };
1277 let key2 = cache_str_field(&item, key2_name).unwrap_or_default();
1278 let was_duplicate = {
1279 let hm = cache_dict_field_mut(m, "hashmap");
1280 let bucket = hm.entry(symbol.clone())
1281 .or_insert_with(|| Value::Map(HashMap::new()));
1282 if let Value::Dict(bd) = bucket {
1283 Arc::make_mut(bd);
1284 }
1285 if let Some(Value::Dict(bd)) = hm.get(&symbol) {
1286 bd.contains_key(&key2)
1287 } else { false }
1288 };
1289 let item_to_store = if was_duplicate {
1290 let merged = {
1291 let hm = cache_dict_field_mut(m, "hashmap");
1292 let bucket = hm.get(&symbol).cloned();
1293 let existing = match bucket {
1294 Some(Value::Dict(bd)) => bd.get(&key2).cloned(),
1295 _ => None,
1296 };
1297 match (existing, item.clone()) {
1298 (Some(Value::Dict(old)), Value::Dict(new_)) => {
1299 let mut merged = (*old).clone();
1300 for (k, v) in new_.iter() { merged.insert(k.clone(), v.clone()); }
1301 Value::Dict(Arc::new(merged))
1302 }
1303 (_, new_) => new_,
1304 }
1305 };
1306 {
1307 let hm = cache_dict_field_mut(m, "hashmap");
1308 let bucket = hm.entry(symbol.clone())
1309 .or_insert_with(|| Value::Map(HashMap::new()));
1310 if let Value::Dict(bd) = bucket {
1311 Arc::make_mut(bd).insert(key2.clone(), merged.clone());
1312 }
1313 }
1314 {
1316 let data = cache_data_mut(m);
1317 if let Some(pos) = data.iter().position(|x| {
1318 cache_str_field(x, key2_name).as_deref() == Some(&key2)
1319 && cache_str_field(x, key1_name).as_deref() == Some(&symbol)
1320 }) { data.remove(pos); }
1321 }
1322 merged
1323 } else {
1324 let hm = cache_dict_field_mut(m, "hashmap");
1325 let bucket = hm.entry(symbol.clone())
1326 .or_insert_with(|| Value::Map(HashMap::new()));
1327 if let Value::Dict(bd) = bucket {
1328 Arc::make_mut(bd).insert(key2.clone(), item.clone());
1329 }
1330 item.clone()
1331 };
1332 let evicting = match cap { Some(c) => {
1334 cache_data_mut(m).len() == c
1335 }, None => false };
1336 if evicting {
1337 let removed = {
1338 let data = cache_data_mut(m);
1339 if data.is_empty() { Value::Null } else { data.remove(0) }
1340 };
1341 let r_sym = cache_str_field(&removed, key1_name).unwrap_or_default();
1342 let r_key2 = cache_str_field(&removed, key2_name).unwrap_or_default();
1343 {
1344 let hm = cache_dict_field_mut(m, "hashmap");
1345 let now_empty = if let Some(Value::Dict(bd)) = hm.get_mut(&r_sym) {
1346 let bd = Arc::make_mut(bd);
1347 bd.shift_remove(&r_key2);
1348 bd.is_empty()
1349 } else { false };
1350 if now_empty { hm.shift_remove(&r_sym); }
1354 }
1355 if cache_seen_remove(m, "_seenUpdatesBySymbol", &r_sym, &r_key2) {
1359 let nubs = cache_dict_field_mut(m, "_newUpdatesBySymbol");
1360 if let Some(Value::Int(n)) = nubs.get(&r_sym).cloned() {
1361 nubs.insert(r_sym.clone(), Value::Int(n - 1));
1362 }
1363 }
1364 if cache_seen_remove(m, "_seenUpdatesAll", &r_sym, &r_key2) {
1365 let all = cache_int_field(m, "_allNewUpdates");
1366 cache_set_int(m, "_allNewUpdates", all - 1);
1367 }
1368 }
1369 cache_data_mut(m).push(item_to_store);
1370
1371 cache_apply_clear_all(m);
1376 if cache_take_symbol_clear(m, &symbol) {
1377 cache_seen_clear(m, "_seenUpdatesBySymbol", &symbol);
1378 }
1379 let sz = cache_seen_add(m, "_seenUpdatesBySymbol", &symbol, &key2);
1380 cache_dict_field_mut(m, "_newUpdatesBySymbol").insert(symbol.clone(), Value::Int(sz));
1381 let before = cache_seen_size(m, "_seenUpdatesAll", &symbol);
1382 let after = cache_seen_add(m, "_seenUpdatesAll", &symbol, &key2);
1383 let all = cache_int_field(m, "_allNewUpdates");
1384 cache_set_int(m, "_allNewUpdates", all + (after - before));
1385 }
1386 _ => {}
1387 }
1388 }
1389}
1390
1391
1392pub(crate) fn cache_get_limit(target: &mut Value, symbol: Value, limit: Value) -> Value {
1393 let kind = cache_kind(target).unwrap_or_default();
1394 if let Some(id) = cache_id_of(target) {
1395 return with_cache_cell(id, |m| cache_get_limit_inner(m, &kind, symbol, limit));
1396 }
1397 if let Value::Dict(m_arc) = target {
1398 let m = Arc::make_mut(m_arc);
1399 cache_get_limit_inner(m, &kind, symbol, limit)
1400 } else {
1401 limit
1402 }
1403}
1404
1405fn cache_get_limit_inner(m: &mut HashMap<String, Value>, kind: &str, symbol: Value, limit: Value) -> Value {
1410 {
1411 if kind == "ArrayCacheByTimestamp" {
1415 let v = cache_int_field(m, "_newUpdates");
1416 cache_set_bool(m, "_clearUpdates", true);
1417 return match limit {
1418 Value::Int(l) => Value::Int(v.min(l)),
1419 _ => Value::Int(v),
1420 };
1421 }
1422 let new_updates_value: Option<i64> = match &symbol {
1423 Value::Null => {
1424 cache_set_bool(m, "_clearAllUpdates", true);
1425 Some(cache_int_field(m, "_allNewUpdates"))
1426 }
1427 Value::Str(sym) => {
1428 let v = match cache_dict_field_mut(m, "_newUpdatesBySymbol").get(sym.as_ref()) {
1429 Some(Value::Int(n)) => Some(*n),
1430 _ => None,
1431 };
1432 cache_dict_field_mut(m, "_clearUpdatesBySymbol")
1433 .insert(sym.to_string(), Value::Bool(true));
1434 v
1435 }
1436 _ => None,
1437 };
1438 match (new_updates_value, &limit) {
1439 (None, _) => limit.clone(),
1440 (Some(v), Value::Int(l)) => Value::Int(v.min(*l)),
1441 (Some(v), _) => Value::Int(v),
1442 }
1443 }
1444}
1445
1446fn as_f64(v: &Value) -> f64 {
1455 match v {
1456 Value::Float(f) => *f,
1457 Value::Int(n) => *n as f64,
1458 Value::Str(s) => s.parse().unwrap_or(0.0),
1459 _ => 0.0,
1460 }
1461}
1462
1463fn book_kind(v: &Value) -> Option<String> {
1464 match v { Value::Dict(d) => match d.get("__bookKind") {
1465 Some(Value::Str(s)) => Some(s.to_string()), _ => None,
1466 }, _ => None }
1467}
1468
1469static BOOK_META: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, HashMap<String, Value>>>> =
1479 once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
1480static NEXT_BOOK_ID: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1);
1481
1482fn is_book_meta_key(k: &str) -> bool {
1484 matches!(k, "timestamp" | "datetime" | "nonce" | "symbol" | "checksum")
1485}
1486
1487pub(crate) fn alloc_book_id() -> i64 {
1488 let id = NEXT_BOOK_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1489 BOOK_META.lock().unwrap().insert(id, HashMap::new());
1490 id
1491}
1492
1493fn book_id_of(m: &HashMap<String, Value>) -> Option<i64> {
1494 match m.get("__book_id") { Some(Value::Int(n)) => Some(*n), _ => None }
1495}
1496
1497fn book_meta_get(id: i64, k: &str) -> Value {
1498 BOOK_META.lock().unwrap().get(&id).and_then(|m| m.get(k).cloned()).unwrap_or(Value::Null)
1499}
1500
1501pub(crate) fn book_meta_set(id: i64, k: &str, v: Value) {
1502 let mut g = BOOK_META.lock().unwrap();
1503 g.entry(id).or_default().insert(k.to_string(), v);
1504}
1505
1506fn book_meta_snapshot(id: i64) -> Vec<(String, Value)> {
1507 BOOK_META.lock().unwrap().get(&id)
1508 .map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
1509 .unwrap_or_default()
1510}
1511
1512static BOOK_CACHE: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, Vec<Value>>>> =
1520 once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
1521
1522fn book_cache_handle(id: i64) -> Value {
1524 let mut h = HashMap::new();
1525 h.insert("__book_cache_id".to_string(), Value::Int(id));
1526 Value::Dict(Arc::new(h))
1527}
1528
1529fn book_cache_id_of(m: &HashMap<String, Value>) -> Option<i64> {
1530 match m.get("__book_cache_id") { Some(Value::Int(n)) => Some(*n), _ => None }
1531}
1532
1533pub(crate) fn book_cache_push(id: i64, v: Value) {
1534 BOOK_CACHE.lock().unwrap().entry(id).or_default().push(v);
1535}
1536
1537fn book_cache_len(id: i64) -> usize {
1538 BOOK_CACHE.lock().unwrap().get(&id).map(|c| c.len()).unwrap_or(0)
1539}
1540
1541pub(crate) fn book_cache_len_of(m: &HashMap<String, Value>) -> Option<usize> {
1543 book_cache_id_of(m).map(book_cache_len)
1544}
1545
1546pub(crate) fn try_ws_subs_write(m: &HashMap<String, Value>, key: &str, val: &Value) -> bool {
1549 if key == "__ws_subs_url" { return false; }
1550 if let Some(Value::Str(url)) = m.get("__ws_subs_url") {
1551 crate::pro::ws_client::value_subs_insert(url, key, val.clone());
1552 return true;
1553 }
1554 false
1555}
1556
1557pub(crate) fn try_ws_sub_field_write(m: &HashMap<String, Value>, key: &str, val: &Value) -> bool {
1560 if key == "__ws_sub_ref" { return false; }
1561 if let Some(Value::Str(subref)) = m.get("__ws_sub_ref") {
1562 crate::pro::ws_client::value_sub_field_write(subref, key, val.clone());
1563 return true;
1564 }
1565 false
1566}
1567
1568pub(crate) fn try_ws_subs_remove(m: &HashMap<String, Value>, key: &str) -> bool {
1571 if let Some(Value::Str(url)) = m.get("__ws_subs_url") {
1572 crate::pro::ws_client::value_subs_remove(url, key);
1573 return true;
1574 }
1575 false
1576}
1577
1578pub(crate) fn try_book_cache_push(obj: &Value, v: &Value) -> bool {
1580 if let Value::Dict(m) = obj {
1581 if let Some(id) = book_cache_id_of(m) { book_cache_push(id, v.clone()); return true; }
1582 }
1583 false
1584}
1585
1586fn book_cache_at(id: i64, i: usize) -> Value {
1587 BOOK_CACHE.lock().unwrap().get(&id).and_then(|c| c.get(i).cloned()).unwrap_or(Value::Null)
1588}
1589
1590pub(crate) fn book_cache_set(id: i64, items: Vec<Value>) {
1592 BOOK_CACHE.lock().unwrap().insert(id, items);
1593}
1594
1595pub fn cache_entries_as_value(v: &Value) -> Option<Value> {
1602 if let Value::Dict(m) = v {
1603 if let Some(id) = book_cache_id_of(m) {
1604 let items = BOOK_CACHE.lock().unwrap().get(&id).cloned().unwrap_or_default();
1605 return Some(Value::Array(items));
1606 }
1607 if m.contains_key("__cacheKind") {
1608 return Some(Value::Array(cache_data_snapshot(m)));
1609 }
1610 }
1611 None
1612}
1613
1614pub(crate) fn try_book_meta_write(m: &HashMap<String, Value>, k: &str, v: &Value) -> bool {
1617 if is_book_meta_key(k) {
1618 if let Some(id) = book_id_of(m) { book_meta_set(id, k, v.clone()); return true; }
1619 }
1620 if k == "cache" {
1622 if let Some(id) = book_id_of(m) {
1623 let items = match v { Value::Arr(a) => a.as_ref().clone(), _ => Vec::new() };
1624 book_cache_set(id, items);
1625 return true;
1626 }
1627 }
1628 false
1629}
1630
1631pub fn book_fields_as_value(v: &Value) -> Option<Value> {
1634 match v {
1635 Value::Dict(d) if d.contains_key("__book_id") => {
1636 let mut out: HashMap<String, Value> = (**d).clone();
1637 out.shift_remove("__book_id");
1638 if let Some(Value::Int(id)) = d.get("__book_id") {
1639 if let Some(meta) = BOOK_META.lock().unwrap().get(id) {
1640 for (k, val) in meta.iter() { out.insert(k.clone(), val.clone()); }
1641 }
1642 }
1643 Some(Value::Dict(Arc::new(out)))
1644 }
1645 _ => None,
1646 }
1647}
1648
1649struct Level {
1667 price: f64, entry: Value, }
1670struct SideCell {
1671 entries: Vec<Level>,
1672 hashmap: HashMap<String, Value>,
1673}
1674
1675static SIDE_STORE: once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<i64, SideCell>>> =
1676 once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
1677static NEXT_SIDE_ID: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1);
1678
1679fn alloc_side_id() -> i64 {
1681 let id = NEXT_SIDE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1682 SIDE_STORE.lock().unwrap().insert(id, SideCell { entries: Vec::new(), hashmap: HashMap::new() });
1683 id
1684}
1685
1686fn clear_side_cell(id: i64) {
1689 let mut g = SIDE_STORE.lock().unwrap();
1690 let cell = g.entry(id).or_insert_with(|| SideCell { entries: Vec::new(), hashmap: HashMap::new() });
1691 cell.entries.clear();
1692 cell.hashmap.clear();
1693}
1694
1695fn side_id_of(m: &HashMap<String, Value>) -> Option<i64> {
1696 match m.get("__side_id") { Some(Value::Int(n)) => Some(*n), _ => None }
1697}
1698
1699fn side_entries_view(m: &HashMap<String, Value>) -> Vec<Value> {
1701 match side_id_of(m) {
1702 Some(id) => SIDE_STORE.lock().unwrap().get(&id)
1703 .map(|c| c.entries.iter().map(|l| l.entry.clone()).collect()).unwrap_or_default(),
1704 None => match m.get("_entries") { Some(Value::Arr(a)) => a.as_ref().clone(), _ => Vec::new() },
1706 }
1707}
1708
1709fn side_entry_at(m: &HashMap<String, Value>, i: usize) -> Value {
1710 match side_id_of(m) {
1711 Some(id) => SIDE_STORE.lock().unwrap().get(&id)
1712 .and_then(|c| c.entries.get(i).map(|l| l.entry.clone())).unwrap_or(Value::Null),
1713 None => match m.get("_entries") {
1714 Some(Value::Arr(a)) => a.get(i).cloned().unwrap_or(Value::Null), _ => Value::Null },
1715 }
1716}
1717
1718pub(crate) fn side_entries_len(m: &HashMap<String, Value>) -> usize {
1719 match side_id_of(m) {
1720 Some(id) => SIDE_STORE.lock().unwrap().get(&id).map(|c| c.entries.len()).unwrap_or(0),
1721 None => match m.get("_entries") { Some(Value::Arr(a)) => a.len(), _ => 0 },
1722 }
1723}
1724
1725fn with_side_cell<R>(m: &HashMap<String, Value>, f: impl FnOnce(&mut SideCell) -> R) -> Option<R> {
1728 let id = side_id_of(m)?;
1729 let mut g = SIDE_STORE.lock().unwrap();
1730 let cell = g.entry(id).or_insert_with(|| SideCell { entries: Vec::new(), hashmap: HashMap::new() });
1731 Some(f(cell))
1732}
1733
1734pub fn side_entries_as_value(v: &Value) -> Option<Value> {
1739 match v {
1740 Value::Dict(d) if d.contains_key("__sideKind") => Some(Value::List(side_entries_view(d))),
1741 _ => None,
1742 }
1743}
1744
1745fn row_price_amount(row: &Value) -> Option<[f64; 2]> {
1748 let r = match row { Value::Arr(a) => a, _ => return None };
1749 let num = |cell: Option<&Value>| -> f64 {
1750 match cell {
1751 Some(Value::Float(f)) => *f,
1752 Some(Value::Int(n)) => *n as f64,
1753 Some(Value::Str(s)) => s.parse().unwrap_or(f64::NAN),
1754 _ => f64::NAN,
1755 }
1756 };
1757 let p = num(r.first());
1758 let a = num(r.get(1));
1759 if p.is_nan() || a.is_nan() { None } else { Some([p, a]) }
1760}
1761
1762pub fn side_price_amounts(v: &Value) -> Option<Vec<[f64; 2]>> {
1768 let d = match v {
1769 Value::Dict(d) if d.contains_key("__sideKind") => d,
1770 _ => return None,
1771 };
1772 let out = match side_id_of(d) {
1773 Some(id) => match SIDE_STORE.lock().unwrap().get(&id) {
1774 Some(cell) => cell.entries.iter().filter_map(|l| row_price_amount(&l.entry)).collect(),
1775 None => Vec::new(),
1776 },
1777 None => match d.get("_entries") {
1778 Some(Value::Arr(a)) => a.iter().filter_map(row_price_amount).collect(),
1779 _ => Vec::new(),
1780 },
1781 };
1782 Some(out)
1783}
1784
1785pub(crate) fn make_side_marker(side_kind: &str, is_bid: bool, depth: i64) -> Value {
1787 let sid = alloc_side_id();
1788 let mut m = HashMap::new();
1789 m.insert("__sideKind".to_string(), Value::Str(side_kind.to_string().into()));
1790 m.insert("_isBid".to_string(), Value::Bool(is_bid));
1791 m.insert("_depth".to_string(), Value::Int(depth));
1792 m.insert("__side_id".to_string(), Value::Int(sid));
1793 Value::Dict(Arc::new(m))
1794}
1795
1796fn side_kind(v: &Value) -> Option<String> {
1797 match v { Value::Dict(d) => match d.get("__sideKind") {
1798 Some(Value::Str(s)) => Some(s.to_string()), _ => None,
1799 }, _ => None }
1800}
1801
1802fn side_is_bid(m: &HashMap<String, Value>) -> bool {
1803 matches!(m.get("_isBid"), Some(Value::Bool(true)))
1804}
1805
1806fn side_depth(m: &HashMap<String, Value>) -> usize {
1807 match m.get("_depth") {
1808 Some(Value::Int(n)) if *n > 0 => *n as usize,
1809 _ => usize::MAX / 2,
1810 }
1811}
1812
1813fn level_price(entries: &[Level]) -> impl Fn(usize) -> f64 + '_ {
1814 move |i: usize| entries[i].price
1815}
1816
1817fn bisect_left_by(len: usize, target: f64, is_bid: bool, get_price: impl Fn(usize) -> f64) -> usize {
1818 let mut lo = 0usize;
1819 let mut hi = len;
1820 while lo < hi {
1821 let mid = (lo + hi) / 2;
1822 let p = get_price(mid);
1823 let ip = if is_bid { -p } else { p };
1824 if ip < target { lo = mid + 1; } else { hi = mid; }
1825 }
1826 lo
1827}
1828
1829fn delta_field<T>(d: &Value, idx: usize, parse: impl Fn(&Value) -> T, default: T) -> T {
1830 match d {
1831 Value::Arr(a) => match a.get(idx) { Some(v) => parse(v), None => default },
1832 _ => default,
1833 }
1834}
1835
1836fn entry_id(entries: &[Level], i: usize) -> String {
1837 if let Value::Arr(a) = &entries[i].entry {
1838 if let Some(v) = a.get(2) {
1839 return match v {
1840 Value::Str(s) => s.to_string(),
1841 Value::Int(n) => n.to_string(),
1842 _ => String::new(),
1843 };
1844 }
1845 }
1846 String::new()
1847}
1848
1849fn side_store_array(side: &Value, delta: Value) {
1850 let kind = side_kind(side).unwrap_or_default();
1851 let m = match side { Value::Dict(d) => d.as_ref(), _ => return };
1852 let is_bid = side_is_bid(m);
1853 with_side_cell(m, |cell| {
1854 match kind.as_str() {
1855 "OrderBookSide" => {
1856 let price = delta_field(&delta, 0, as_f64, 0.0);
1857 let size = delta_field(&delta, 1, as_f64, 0.0);
1858 let target = if is_bid { -price } else { price };
1859 let entries = &mut cell.entries;
1860 let idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
1861 let matches_here = idx < entries.len()
1862 && (if is_bid { -entries[idx].price } else { entries[idx].price }) == target;
1863 if size != 0.0 {
1864 if matches_here {
1865 entries[idx] = Level { price, entry: delta };
1866 } else {
1867 entries.insert(idx, Level { price, entry: delta });
1868 }
1869 } else if matches_here {
1870 entries.remove(idx);
1871 }
1872 }
1873 "CountedOrderBookSide" => {
1874 let price = delta_field(&delta, 0, as_f64, 0.0);
1875 let size = delta_field(&delta, 1, as_f64, 0.0);
1876 let count = delta_field(&delta, 2, as_f64, 0.0);
1877 let has_count = matches!(&delta,
1883 Value::Arr(a) if matches!(a.get(2), Some(Value::Int(_)) | Some(Value::Float(_))));
1884 let store_it = if has_count { size != 0.0 && count != 0.0 } else { size != 0.0 };
1885 let target = if is_bid { -price } else { price };
1886 let entries = &mut cell.entries;
1887 let idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
1888 let matches_here = idx < entries.len()
1889 && (if is_bid { -entries[idx].price } else { entries[idx].price }) == target;
1890 if store_it {
1891 if matches_here {
1892 entries[idx] = Level { price, entry: delta };
1893 } else {
1894 entries.insert(idx, Level { price, entry: delta });
1895 }
1896 } else if matches_here {
1897 entries.remove(idx);
1898 }
1899 }
1900 "IndexedOrderBookSide" => {
1901 let price_opt = match &delta {
1902 Value::Arr(a) => a.first().and_then(|v| match v {
1903 Value::Float(f) => Some(*f),
1904 Value::Int(n) => Some(*n as f64),
1905 _ => None,
1906 }),
1907 _ => None,
1908 };
1909 let size = delta_field(&delta, 1, as_f64, 0.0);
1910 let id = match &delta {
1911 Value::Arr(a) => match a.get(2) {
1912 Some(Value::Str(s)) => s.to_string(),
1913 Some(Value::Int(n)) => n.to_string(),
1914 _ => String::new(),
1915 },
1916 _ => String::new(),
1917 };
1918 let mut index_price = price_opt.map(|p| if is_bid { -p } else { p });
1919 let old_price_opt = {
1920 match cell.hashmap.get(&id) {
1921 Some(Value::Float(f)) => Some(*f),
1922 Some(Value::Int(n)) => Some(*n as f64),
1923 _ => None,
1924 }
1925 };
1926 if size != 0.0 {
1927 if let Some(old_price) = old_price_opt {
1928 index_price = index_price.or(Some(old_price));
1929 if Some(old_price) == index_price {
1930 let target = old_price;
1932 let raw = if is_bid { -old_price } else { old_price };
1933 let entries = &mut cell.entries;
1934 let mut idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
1935 while idx < entries.len() && entry_id(entries, idx) != id { idx += 1; }
1936 if idx < entries.len() { entries[idx] = Level { price: raw, entry: delta }; }
1937 return;
1938 }
1939 let target = old_price;
1941 let entries = &mut cell.entries;
1942 let mut old_idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
1943 while old_idx < entries.len() && entry_id(entries, old_idx) != id { old_idx += 1; }
1944 if old_idx < entries.len() { entries.remove(old_idx); }
1945 }
1946 let target = match index_price { Some(p) => p, None => return };
1947 let raw = if is_bid { -target } else { target };
1948 let entries = &mut cell.entries;
1950 let mut idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
1951 while idx < entries.len() {
1952 let ip = if is_bid { -entries[idx].price } else { entries[idx].price };
1953 if ip != target { break; }
1954 if entry_id(entries, idx) >= id { break; }
1955 idx += 1;
1956 }
1957 entries.insert(idx, Level { price: raw, entry: delta });
1958 cell.hashmap.insert(id, Value::Float(target));
1959 } else if let Some(old_price) = old_price_opt {
1960 let target = old_price;
1962 let entries = &mut cell.entries;
1963 let mut idx = bisect_left_by(entries.len(), target, is_bid, level_price(entries));
1964 while idx < entries.len() && entry_id(entries, idx) != id { idx += 1; }
1965 if idx < entries.len() { entries.remove(idx); }
1966 cell.hashmap.shift_remove(&id);
1967 }
1968 }
1969 _ => {}
1970 }
1971 });
1972}
1973
1974fn side_limit(side: &Value) {
1975 let m = match side { Value::Dict(d) => d.as_ref(), _ => return };
1976 let depth = side_depth(m);
1977 with_side_cell(m, |cell| {
1978 if cell.entries.len() > depth { cell.entries.truncate(depth); }
1979 });
1980}
1981
1982fn book_reseed_sides(book: &mut Value, snapshot: &Value) {
1983 let kind = book_kind(book).unwrap_or_default();
1984 let side_kind_str = match kind.as_str() {
1985 "IndexedOrderBook" => "IndexedOrderBookSide",
1986 "CountedOrderBook" => "CountedOrderBookSide",
1987 _ => "OrderBookSide",
1988 };
1989 let bids_deltas = crate::get_value(snapshot, &Value::Str("bids".into()));
1990 let asks_deltas = crate::get_value(snapshot, &Value::Str("asks".into()));
1991 if let Value::Dict(book_arc) = book {
1992 let book_m = Arc::make_mut(book_arc);
1993 let depth = match book_m.get("_depth") {
1994 Some(Value::Int(n)) => *n,
1995 _ => i64::MAX / 2,
1996 };
1997 for (side_key, deltas, is_bid) in [
2000 ("bids", &bids_deltas, true),
2001 ("asks", &asks_deltas, false),
2002 ] {
2003 let sid = match book_m.get(side_key) {
2004 Some(Value::Dict(d)) => match d.get("__side_id") {
2005 Some(Value::Int(n)) => { clear_side_cell(*n); *n }
2006 _ => alloc_side_id(),
2007 },
2008 _ => alloc_side_id(),
2009 };
2010 let mut m = HashMap::new();
2011 m.insert("__sideKind".to_string(), Value::Str(side_kind_str.to_string().into()));
2012 m.insert("_isBid".to_string(), Value::Bool(is_bid));
2013 m.insert("_depth".to_string(), Value::Int(depth));
2014 m.insert("__side_id".to_string(), Value::Int(sid));
2015 let side = Value::Map(m);
2016 if let Value::Arr(rows) = deltas {
2017 for row in rows.iter() {
2018 if let Value::Arr(_) = row { side_store_array(&side, row.clone()); }
2019 }
2020 }
2021 book_m.insert(side_key.to_string(), side);
2022 }
2023 }
2024}
2025
2026pub(crate) fn book_limit(book: &mut Value) {
2027 if book_kind(book).is_none() { return; }
2028 if let Value::Dict(arc) = book {
2029 let m = Arc::make_mut(arc);
2030 if let Some(side) = m.get_mut("bids") { side_limit(side); }
2031 if let Some(side) = m.get_mut("asks") { side_limit(side); }
2032 }
2033}
2034
2035pub(crate) fn book_reset(book: &mut Value, snapshot: Value) {
2036 if book_kind(book).is_none() { return; }
2037 book_reseed_sides(book, &snapshot);
2038 let id = match book { Value::Dict(d) => book_id_of(d), _ => None };
2039 if let Some(id) = id {
2040 let ts = match crate::get_value(&snapshot, &Value::Str("timestamp".into())) {
2041 Value::Int(n) => Some(n),
2042 Value::Float(f) => Some(f as i64),
2043 _ => None,
2044 };
2045 let dt = ts.and_then(|n| chrono::DateTime::<chrono::Utc>::from_timestamp_millis(n)
2046 .map(|t| t.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)));
2047 book_meta_set(id, "timestamp", ts.map(Value::Int).unwrap_or(Value::Null));
2048 book_meta_set(id, "datetime", dt.map(|s| Value::Str(s.into())).unwrap_or(Value::Null));
2049 book_meta_set(id, "nonce", crate::get_value(&snapshot, &Value::Str("nonce".into())));
2050 book_meta_set(id, "symbol", crate::get_value(&snapshot, &Value::Str("symbol".into())));
2051 }
2052}
2053
2054pub(crate) fn book_update(book: &mut Value, snapshot: Value) {
2055 if book_kind(book).is_none() { return; }
2056 let new_nonce = match crate::get_value(&snapshot, &Value::Str("nonce".into())) {
2058 Value::Int(n) => Some(n), _ => None,
2059 };
2060 let cur_nonce = match book {
2061 Value::Dict(d) => match book_id_of(d).map(|id| book_meta_get(id, "nonce")) {
2062 Some(Value::Int(n)) => Some(n), _ => None,
2063 },
2064 _ => None,
2065 };
2066 if let (Some(n), Some(cur)) = (new_nonce, cur_nonce) {
2067 if n <= cur { return; }
2068 }
2069 book_reset(book, snapshot);
2070}
2071
2072pub(crate) fn book_store_array_side(book: &mut Value, side_key: &str, delta: Value) {
2073 if let Value::Dict(arc) = book {
2074 let m = Arc::make_mut(arc);
2075 if let Some(side) = m.get_mut(side_key) {
2076 side_store_array(side, delta);
2077 }
2078 }
2079}
2080
2081
2082impl Value {
2085 pub fn limit(&mut self) -> Value {
2089 book_limit(self);
2090 self.clone()
2091 }
2092
2093 pub fn reset(&mut self, snapshot: Value) -> Value {
2099 if matches!(&self, Value::Dict(d) if d.contains_key("url") && d.contains_key("subscriptions")) {
2103 return crate::pro::ws_client::value_reset(self, snapshot);
2104 }
2105 book_reset(self, snapshot);
2106 self.clone()
2107 }
2108 pub fn reset0(&mut self) -> Value {
2111 book_reset(self, Value::Null);
2112 self.clone()
2113 }
2114
2115 pub fn update(&mut self, snapshot: Value) -> Value {
2118 book_update(self, snapshot);
2119 self.clone()
2120 }
2121
2122 pub fn store(&mut self, price: Value, size: Value) {
2126 if side_kind(self).is_some() {
2127 let delta = Value::List(vec![price, size]);
2128 side_store_array(self, delta);
2129 }
2130 }
2131
2132 pub fn store_to_bids(&mut self, price: Value, size: Value) {
2137 let delta = Value::List(vec![price, size]);
2138 book_store_array_side(self, "bids", delta);
2139 }
2140 pub fn store_to_asks(&mut self, price: Value, size: Value) {
2141 let delta = Value::List(vec![price, size]);
2142 book_store_array_side(self, "asks", delta);
2143 }
2144 pub fn store_array_to_bids(&mut self, delta: Value) {
2145 book_store_array_side(self, "bids", delta);
2146 }
2147 pub fn store_array_to_asks(&mut self, delta: Value) {
2148 book_store_array_side(self, "asks", delta);
2149 }
2150}
2151
2152#[cfg(test)]
2153mod json_int_precision_tests {
2154 use super::Value;
2155
2156 #[test]
2157 fn preserves_large_u64_ids_losslessly() {
2158 let v: serde_json::Value = serde_json::from_str(r#"{"id": 12345678901234567890}"#).unwrap();
2160 let parsed = Value::from_json(&v);
2161 let id = crate::runtime::get_value(&parsed, &Value::Str("id".into()));
2162 assert_eq!(id, Value::Str("12345678901234567890".into()));
2163 }
2164
2165 #[test]
2166 fn small_ints_stay_ints_floats_stay_floats() {
2167 let v: serde_json::Value = serde_json::from_str(r#"{"a": 42, "b": -7, "c": 1.5}"#).unwrap();
2168 let p = Value::from_json(&v);
2169 assert_eq!(crate::runtime::get_value(&p, &Value::Str("a".into())), Value::Int(42));
2170 assert_eq!(crate::runtime::get_value(&p, &Value::Str("b".into())), Value::Int(-7));
2171 assert_eq!(crate::runtime::get_value(&p, &Value::Str("c".into())), Value::Float(1.5));
2172 }
2173}
2174
2175#[cfg(test)]
2176mod value_len_tests {
2177 use super::*;
2178
2179 #[test]
2180 fn len_of_book_cache_handle_reports_backing_store() {
2181 let id = -424242; let handle = book_cache_handle(id);
2185 assert_eq!(handle.len(), 0);
2186 book_cache_push(id, Value::Int(1));
2187 book_cache_push(id, Value::Int(2));
2188 assert_eq!(handle.len(), 2);
2189 }
2190
2191 #[test]
2192 fn len_of_plain_dict_still_counts_keys() {
2193 let mut m = HashMap::new();
2194 m.insert("a".to_string(), Value::Int(1));
2195 m.insert("b".to_string(), Value::Int(2));
2196 assert_eq!(Value::Dict(Arc::new(m)).len(), 2);
2197 }
2198}