1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Query time-to-live (`packages/zql/src/query/ttl.ts`).
use crate::json::{Value, number};
/// How long a query stays cached after its last use.
///
/// `Forever` and negative durations never expire, `None` expires
/// immediately. Durations are clamped to 10 minutes when sent to the server.
#[derive(Clone, Debug, PartialEq)]
pub enum Ttl {
/// Expires as soon as nothing uses the query.
None,
/// Never expires.
Forever,
/// A duration in milliseconds.
Milliseconds(f64),
/// A duration string as JS accepts it: a number followed by `s`, `m`,
/// `h`, `d`, or `y` (for example `"5m"`).
String(String),
}
/// The TTL a query gets when it doesn't name one: five minutes.
pub const DEFAULT_TTL_MILLISECONDS: f64 = 5.0 * 60.0 * 1000.0;
/// The longest TTL the server keeps a query for: ten minutes.
pub const MAX_TTL_MILLISECONDS: f64 = 10.0 * 60.0 * 1000.0;
fn multiplier(unit: char) -> Option<f64> {
Some(match unit {
's' => 1000.0,
'm' => 60.0 * 1000.0,
'h' => 60.0 * 60.0 * 1000.0,
'd' => 24.0 * 60.0 * 60.0 * 1000.0,
'y' => 365.0 * 24.0 * 60.0 * 60.0 * 1000.0,
_ => return None,
})
}
impl Ttl {
/// The default TTL as a string.
pub const DEFAULT: &str = "5m";
/// The TTL JS's `preload` uses when it isn't given one: the query
/// stops being synced as soon as its preload is cleaned up.
/// [`Ttl::default`] is the default of views and `run`.
pub const DEFAULT_PRELOAD: Ttl = Ttl::None;
/// `n` seconds.
pub fn seconds(n: f64) -> Ttl {
Ttl::String(number::format(n) + "s")
}
/// `n` minutes.
pub fn minutes(n: f64) -> Ttl {
Ttl::String(number::format(n) + "m")
}
/// `n` hours.
pub fn hours(n: f64) -> Ttl {
Ttl::String(number::format(n) + "h")
}
/// `n` days.
pub fn days(n: f64) -> Ttl {
Ttl::String(number::format(n) + "d")
}
/// A TTL as JS holds it: a string or a number.
pub fn from_json(v: &Value) -> Option<Ttl> {
match v {
Value::Number(n) => Some(Ttl::Milliseconds(*n)),
Value::String(s) => Some(Ttl::from(s.as_str())),
_ => None,
}
}
/// The TTL as JS holds it.
pub fn to_json(&self) -> Value {
match self {
Ttl::None => "none".into(),
Ttl::Forever => "forever".into(),
Ttl::Milliseconds(n) => Value::Number(*n),
Ttl::String(s) => s.as_str().into(),
}
}
/// `parseTTL`: milliseconds, with `-1` for never expiring.
pub fn parsed(&self) -> f64 {
match self {
Ttl::None => 0.0,
Ttl::Forever => -1.0,
Ttl::Milliseconds(n) if n.is_nan() => 0.0,
Ttl::Milliseconds(n) if !n.is_finite() || *n < 0.0 => -1.0,
Ttl::Milliseconds(n) => *n,
Ttl::String(s) if s == "none" => 0.0,
Ttl::String(s) if s == "forever" => -1.0,
Ttl::String(s) => {
let Some(unit) = s.chars().last() else {
return f64::NAN;
};
match multiplier(unit) {
Some(m) => number::parse(&s[..s.len() - unit.len_utf8()]) * m,
None => f64::NAN,
}
}
}
}
/// `compareTTL`: never-expiring sorts last.
pub fn compare(a: &Ttl, b: &Ttl) -> f64 {
let (ap, bp) = (a.parsed(), b.parsed());
match (ap == -1.0, bp == -1.0) {
(true, false) => 1.0,
(false, true) => -1.0,
_ => ap - bp,
}
}
/// `normalizeTTL`: the shortest equivalent spelling of a number of
/// milliseconds; strings are returned as is.
pub fn normalized(&self) -> Ttl {
let Ttl::Milliseconds(ttl) = *self else {
return self.clone();
};
if ttl < 0.0 {
return Ttl::Forever;
}
if ttl == 0.0 {
return Ttl::None;
}
let plain = number::format(ttl);
let mut shortest = plain.clone();
for unit in ['y', 'd', 'h', 'm', 's'] {
let candidate =
number::format(ttl / multiplier(unit).expect("unit")) + &unit.to_string();
if candidate.encode_utf16().count() < shortest.encode_utf16().count() {
shortest = candidate;
}
}
if shortest.len() < plain.len() {
Ttl::String(shortest)
} else {
self.clone()
}
}
/// `clampTTL`: milliseconds, at most 10 minutes; never-expiring becomes
/// 10 minutes.
pub fn clamped(&self) -> f64 {
let p = self.parsed();
if p == -1.0 || p > MAX_TTL_MILLISECONDS {
MAX_TTL_MILLISECONDS
} else {
p
}
}
}
impl From<&str> for Ttl {
fn from(s: &str) -> Ttl {
match s {
"none" => Ttl::None,
"forever" => Ttl::Forever,
s => Ttl::String(s.to_owned()),
}
}
}
impl Default for Ttl {
fn default() -> Ttl {
Ttl::String(Ttl::DEFAULT.to_owned())
}
}