Repository navigation
Expand file tree
/
Copy pathboolean.expanded.rs
More file actions
425 lines (425 loc) · 14.5 KB
/
Copy pathboolean.expanded.rs
File metadata and controls
425 lines (425 loc) · 14.5 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use serde::Deserialize as _;
#[pco_store::store(group_by = [database_id], float_round = 2)]
pub struct QueryStat {
pub database_id: i64,
pub toplevel: bool,
pub calls: i64,
}
#[doc = concat!(
" Generated by pco_store to store and load compressed versions of [",
stringify!(QueryStat), "]"
)]
pub struct CompressedQueryStats {
filter: Option<Filter>,
database_id: i64,
toplevel: Vec<u8>,
calls: Vec<u8>,
}
impl CompressedQueryStats {
pub fn new(rows: &Vec<QueryStat>) -> anyhow::Result<Self> {
Ok(Self {
database_id: rows[0].database_id.clone(),
toplevel: ::pco::standalone::simple_compress(
&rows.iter().map(|r| r.toplevel as u16).collect::<Vec<_>>(),
&::pco::ChunkConfig::default(),
)?,
calls: ::pco::standalone::simple_compress(
&rows.iter().map(|r| r.calls).collect::<Vec<_>>(),
&::pco::ChunkConfig::default(),
)?,
filter: None,
})
}
/// Loads data for the specified filters.
pub async fn load(
db: &impl ::std::ops::Deref<Target = deadpool_postgres::ClientWrapper>,
mut filter: Filter,
fields: impl TryInto<Fields>,
) -> anyhow::Result<Vec<CompressedQueryStats>> {
let mut fields = fields
.try_into()
.map_err(|_| anyhow::Error::msg("unknown field"))?;
fields.merge_filter(&filter);
if filter.database_id.is_empty() {
return Err(anyhow::Error::msg("database_id".to_string() + " is required"));
}
let sql = "SELECT ".to_string() + fields.select().as_str() + " FROM "
+ "query_stats" + " WHERE " + "database_id = ANY($1)";
let mut results = Vec::new();
for row in db
.query(&db.prepare_cached(&sql).await?, &[&filter.database_id])
.await?
{
results.push(fields.load_from_row(row, Some(filter.clone()))?);
}
Ok(results)
}
/// Deletes data for the specified filters, returning it to the caller.
///
/// Note that all rows are returned from [decompress][Self::decompress] even if post-decompress filters would normally apply.
pub async fn delete(
db: &impl ::std::ops::Deref<Target = deadpool_postgres::ClientWrapper>,
mut filter: Filter,
fields: impl TryInto<Fields>,
) -> anyhow::Result<Vec<CompressedQueryStats>> {
let mut fields = fields
.try_into()
.map_err(|_| anyhow::Error::msg("unknown field"))?;
fields.merge_filter(&filter);
if filter.database_id.is_empty() {
return Err(anyhow::Error::msg("database_id".to_string() + " is required"));
}
let sql = "DELETE FROM ".to_string() + "query_stats" + " WHERE "
+ "database_id = ANY($1)" + " RETURNING " + fields.select().as_str();
let mut results = Vec::new();
for row in db
.query(&db.prepare_cached(&sql).await?, &[&filter.database_id])
.await?
{
results.push(fields.load_from_row(row, None)?);
}
Ok(results)
}
/// Decompresses a group of data points.
pub fn decompress(self) -> anyhow::Result<Vec<QueryStat>> {
let mut results = Vec::new();
let toplevel: Vec<u16> = if self.toplevel.is_empty() {
Vec::new()
} else {
::pco::standalone::simple_decompress(&self.toplevel)?
};
let calls: Vec<i64> = if self.calls.is_empty() {
Vec::new()
} else {
::pco::standalone::simple_decompress(&self.calls)?
};
let len = [toplevel.len(), calls.len()].into_iter().max().unwrap_or(0);
for index in 0..len {
let row = QueryStat {
database_id: self.database_id.clone(),
toplevel: toplevel.get(index).cloned().unwrap_or_default() == 1,
calls: calls.get(index).cloned().unwrap_or_default(),
};
if self.filter.as_ref().map(|f| f.matches(&row)) != Some(false) {
results.push(row);
}
}
Ok(results)
}
/// Writes the data to disk.
pub async fn store(
db: &impl ::std::ops::Deref<Target = deadpool_postgres::ClientWrapper>,
rows: Vec<QueryStat>,
) -> anyhow::Result<()> {
if rows.is_empty() {
return Ok(());
}
let mut grouped_rows: ahash::AHashMap<_, Vec<QueryStat>> = ahash::AHashMap::new();
for row in rows {
grouped_rows.entry((row.database_id.clone(),)).or_default().push(row);
}
let sql = "COPY query_stats (database_id, toplevel, calls) FROM STDIN BINARY";
let types = &[
tokio_postgres::types::Type::INT8,
tokio_postgres::types::Type::BYTEA,
tokio_postgres::types::Type::BYTEA,
];
let stmt = db.copy_in(&db.prepare_cached(&sql).await?).await?;
let writer = tokio_postgres::binary_copy::BinaryCopyInWriter::new(stmt, types);
futures::pin_mut!(writer);
for rows in grouped_rows.into_values() {
let row = Self::new(&rows)?;
writer
.as_mut()
.write(&[&rows[0].database_id, &row.toplevel, &row.calls])
.await?;
}
writer.finish().await?;
Ok(())
}
/// Writes the data to disk, with the provided grouping closure applied.
///
/// This can be used to improve the compression ratio and reduce read IO, for example
/// by compacting real-time data into a single row per hour / day / week.
pub async fn store_grouped<F, R>(
db: &impl ::std::ops::Deref<Target = deadpool_postgres::ClientWrapper>,
rows: Vec<QueryStat>,
grouping: F,
) -> anyhow::Result<()>
where
F: Fn(&QueryStat) -> R,
R: Eq + std::hash::Hash,
{
if rows.is_empty() {
return Ok(());
}
let mut grouped_rows: ahash::AHashMap<_, Vec<QueryStat>> = ahash::AHashMap::new();
for row in rows {
grouped_rows
.entry((row.database_id.clone(), grouping(&row)))
.or_default()
.push(row);
}
let sql = "COPY query_stats (database_id, toplevel, calls) FROM STDIN BINARY";
let types = &[
tokio_postgres::types::Type::INT8,
tokio_postgres::types::Type::BYTEA,
tokio_postgres::types::Type::BYTEA,
];
let stmt = db.copy_in(&db.prepare_cached(&sql).await?).await?;
let writer = tokio_postgres::binary_copy::BinaryCopyInWriter::new(stmt, types);
futures::pin_mut!(writer);
for rows in grouped_rows.into_values() {
let row = Self::new(&rows)?;
writer
.as_mut()
.write(&[&rows[0].database_id, &row.toplevel, &row.calls])
.await?;
}
writer.finish().await?;
Ok(())
}
}
#[serde_with::serde_as]
#[derive(Debug, Default, serde::Deserialize, Clone, PartialEq)]
#[serde(deny_unknown_fields)]
#[doc = concat!(
" Generated by pco_store to specify filters when loading [", stringify!(QueryStat),
"]"
)]
pub struct Filter {
#[serde(default)]
#[serde_as(deserialize_as = "serde_with::DefaultOnNull<serde_with::OneOrMany<_>>")]
pub database_id: Vec<i64>,
#[serde(default)]
#[serde_as(deserialize_as = "serde_with::DefaultOnNull<serde_with::OneOrMany<_>>")]
pub toplevel: Vec<bool>,
#[serde(default)]
#[serde_as(deserialize_as = "serde_with::DefaultOnNull<serde_with::OneOrMany<_>>")]
pub calls: Vec<i64>,
}
impl Filter {
/// Builds new filter with the required fields defined by `group_by` and `timestamp`
pub fn new(database_id: &[i64]) -> Self {
Self {
database_id: database_id.into(),
..Self::default()
}
}
fn matches(&self, row: &QueryStat) -> bool {
(self.database_id.is_empty() || self.database_id.contains(&row.database_id))
&& (self.toplevel.is_empty() || self.toplevel.contains(&row.toplevel))
&& (self.calls.is_empty() || self.calls.contains(&row.calls))
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[doc = concat!(
" Generated by pco_store to choose which fields to decompress when loading [",
stringify!(QueryStat), "]"
)]
pub struct Fields {
database_id: bool,
toplevel: bool,
calls: bool,
}
impl Fields {
pub fn new(fields: &[&str]) -> anyhow::Result<Self> {
fields.try_into().map_err(|e| anyhow::Error::msg(e))
}
pub fn required() -> Self {
Self {
database_id: true,
toplevel: false,
calls: false,
}
}
fn merge_filter(&mut self, filter: &Filter) {
(!filter.toplevel.is_empty()).then(|| self.toplevel = true);
(!filter.calls.is_empty()).then(|| self.calls = true);
}
fn select(&self) -> String {
let mut fields = Vec::new();
self.database_id.then(|| fields.push("database_id"));
self.toplevel.then(|| fields.push("toplevel"));
self.calls.then(|| fields.push("calls"));
fields.join(", ")
}
fn load_from_row(
&self,
row: tokio_postgres::Row,
filter: Option<Filter>,
) -> anyhow::Result<CompressedQueryStats> {
let mut index = 0;
Ok(CompressedQueryStats {
filter,
database_id: if self.database_id {
let v = row.get(index);
index += 1;
v
} else {
Default::default()
},
toplevel: if self.toplevel {
let v = row.get(index);
index += 1;
v
} else {
Default::default()
},
calls: if self.calls {
let v = row.get(index);
index += 1;
v
} else {
Default::default()
},
})
}
}
impl Default for Fields {
fn default() -> Self {
Self {
database_id: true,
toplevel: true,
calls: true,
}
}
}
impl TryFrom<&[&str]> for Fields {
type Error = &'static str;
fn try_from(input: &[&str]) -> Result<Self, Self::Error> {
let mut fields = Self::required();
for s in input {
match *s {
"database_id" => fields.database_id = true,
"toplevel" => fields.toplevel = true,
"calls" => fields.calls = true,
_ => return Err("unknown field"),
}
}
Ok(fields)
}
}
impl<const N: usize> TryFrom<&[&str; N]> for Fields {
type Error = &'static str;
fn try_from(input: &[&str; N]) -> Result<Self, Self::Error> {
Self::try_from(&input[..])
}
}
impl TryFrom<Vec<String>> for Fields {
type Error = &'static str;
fn try_from(input: Vec<String>) -> Result<Self, Self::Error> {
let input: Vec<_> = input.iter().map(|s| s.as_str()).collect();
Self::try_from(input.as_slice())
}
}
impl From<()> for Fields {
fn from(_: ()) -> Self {
Self::default()
}
}
impl<'de> serde::Deserialize<'de> for Fields {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_any(FieldsVisitor)
}
}
struct FieldsVisitor;
impl<'de> serde::de::Visitor<'de> for FieldsVisitor {
type Value = Fields;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("an array of strings matching the struct fields")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut fields = Vec::new();
while let Some(field) = seq.next_element()? {
fields.push(field);
}
Fields::try_from(fields).map_err(serde::de::Error::custom)
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Fields::default())
}
}
fn serde_compress<T>(items: Vec<T>) -> anyhow::Result<Vec<u8>>
where
T: serde::Serialize,
{
use std::io::Write;
let mut output = Vec::new();
let mut encoder = zstd::stream::write::Encoder::new(&mut output, 3)?;
for item in items {
rmp_serde::encode::write(&mut encoder, &item)?;
}
encoder.finish()?;
Ok(output)
}
fn serde_decompress<T>(input: &[u8]) -> impl Iterator<Item = anyhow::Result<T>> + '_
where
T: for<'de> serde::Deserialize<'de> + 'static,
{
let decoder = match zstd::stream::read::Decoder::new(input) {
Ok(d) => d,
Err(e) => {
return Box::new(std::iter::once(Err(e.into())))
as Box<dyn Iterator<Item = _>>;
}
};
let buffered = std::io::BufReader::with_capacity(128 * 1024, decoder);
let mut de = rmp_serde::decode::Deserializer::new(buffered);
Box::new(
std::iter::from_fn(move || match serde::Deserialize::deserialize(&mut de) {
Ok(item) => Some(Ok(item)),
Err(
rmp_serde::decode::Error::InvalidMarkerRead(ref e),
) if e.kind() == std::io::ErrorKind::UnexpectedEof => None,
Err(e) => Some(Err(e.into())),
}),
)
}
fn pco_compress_nested<T>(nested_values: Vec<Vec<T>>) -> anyhow::Result<Vec<u8>>
where
T: ::pco::data_types::Number,
{
let mut lengths = Vec::new();
let mut values = Vec::new();
for vals in nested_values {
lengths.push(vals.len() as u64);
values.extend(vals);
}
let length_bytes = ::pco::standalone::simple_compress(
&lengths,
&::pco::ChunkConfig::default(),
)?;
let value_bytes = ::pco::standalone::simple_compress(
&values,
&::pco::ChunkConfig::default(),
)?;
let (length_bytes, value_bytes) = (
serde_bytes::Bytes::new(&length_bytes),
serde_bytes::Bytes::new(&value_bytes),
);
Ok(rmp_serde::to_vec(&(length_bytes, value_bytes))?)
}
fn pco_decompress_nested<T>(bytes: Vec<u8>) -> anyhow::Result<Vec<Vec<T>>>
where
T: ::pco::data_types::Number,
{
let (length_bytes, value_bytes): (Vec<u8>, Vec<u8>) = rmp_serde::from_slice(&bytes)?;
let lengths = ::pco::standalone::simple_decompress::<u64>(&length_bytes)?;
let values = ::pco::standalone::simple_decompress::<T>(&value_bytes)?;
let mut values = values.into_iter();
let mut nested_values = Vec::with_capacity(lengths.len());
for length in lengths {
nested_values.push(values.by_ref().take(length as usize).collect::<Vec<T>>());
}
Ok(nested_values)
}