2023-07-18 18:32:38 +00:00
|
|
|
//! Stores a LIVE SELECT query definition on the table
|
2024-07-17 22:44:05 +00:00
|
|
|
use crate::key::category::Categorise;
|
|
|
|
use crate::key::category::Category;
|
2022-06-15 07:41:23 +00:00
|
|
|
use derive::Key;
|
2022-01-13 17:40:20 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-06-20 22:50:26 +00:00
|
|
|
use uuid::Uuid;
|
2022-01-13 17:40:20 +00:00
|
|
|
|
2023-08-19 09:01:37 +00:00
|
|
|
/// Lv is used to track a live query and is cluster independent, i.e. it is tied with a ns/db/tb combo without the cl.
|
|
|
|
/// The live statement includes the node id, so lq can be derived purely from an lv.
|
|
|
|
///
|
|
|
|
/// The value of the lv is the statement.
|
2022-06-15 07:41:23 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)]
|
2024-04-02 20:12:08 +00:00
|
|
|
#[non_exhaustive]
|
2023-07-18 18:32:38 +00:00
|
|
|
pub struct Lq<'a> {
|
2022-03-16 15:33:43 +00:00
|
|
|
__: u8,
|
|
|
|
_a: u8,
|
2023-04-29 16:44:09 +00:00
|
|
|
pub ns: &'a str,
|
2022-03-16 15:33:43 +00:00
|
|
|
_b: u8,
|
2023-04-29 16:44:09 +00:00
|
|
|
pub db: &'a str,
|
2022-03-16 15:33:43 +00:00
|
|
|
_c: u8,
|
2023-04-29 16:44:09 +00:00
|
|
|
pub tb: &'a str,
|
2022-03-16 15:33:43 +00:00
|
|
|
_d: u8,
|
|
|
|
_e: u8,
|
|
|
|
_f: u8,
|
2023-07-05 21:26:13 +00:00
|
|
|
#[serde(with = "uuid::serde::compact")]
|
2023-07-18 18:32:38 +00:00
|
|
|
pub lq: Uuid,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 18:32:38 +00:00
|
|
|
pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, lq: Uuid) -> Lq<'a> {
|
|
|
|
Lq::new(ns, db, tb, lq)
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 07:21:22 +00:00
|
|
|
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
|
2023-07-18 18:32:38 +00:00
|
|
|
let mut k = super::all::new(ns, db, tb).encode().unwrap();
|
2024-08-23 15:34:06 +00:00
|
|
|
k.extend_from_slice(b"!lq\x00");
|
2022-03-18 07:21:22 +00:00
|
|
|
k
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
|
2023-07-18 18:32:38 +00:00
|
|
|
let mut k = super::all::new(ns, db, tb).encode().unwrap();
|
2024-08-23 15:34:06 +00:00
|
|
|
k.extend_from_slice(b"!lq\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00");
|
2022-03-18 07:21:22 +00:00
|
|
|
k
|
|
|
|
}
|
|
|
|
|
2024-07-17 22:44:05 +00:00
|
|
|
impl Categorise for Lq<'_> {
|
|
|
|
fn categorise(&self) -> Category {
|
|
|
|
Category::TableLiveQuery
|
2023-09-27 10:35:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 18:32:38 +00:00
|
|
|
impl<'a> Lq<'a> {
|
|
|
|
pub fn new(ns: &'a str, db: &'a str, tb: &'a str, lq: Uuid) -> Self {
|
2023-04-29 16:44:09 +00:00
|
|
|
Self {
|
2023-05-12 23:40:45 +00:00
|
|
|
__: b'/',
|
|
|
|
_a: b'*',
|
2022-01-13 17:40:20 +00:00
|
|
|
ns,
|
2023-05-12 23:40:45 +00:00
|
|
|
_b: b'*',
|
2022-01-13 17:40:20 +00:00
|
|
|
db,
|
2023-05-12 23:40:45 +00:00
|
|
|
_c: b'*',
|
2022-01-13 17:40:20 +00:00
|
|
|
tb,
|
2023-05-12 23:40:45 +00:00
|
|
|
_d: b'!',
|
|
|
|
_e: b'l',
|
2023-07-18 18:32:38 +00:00
|
|
|
_f: b'q',
|
|
|
|
lq,
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn key() {
|
|
|
|
use super::*;
|
|
|
|
#[rustfmt::skip]
|
2023-07-13 13:44:54 +00:00
|
|
|
let live_query_id = Uuid::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
|
2023-07-18 18:32:38 +00:00
|
|
|
let val = Lq::new("testns", "testdb", "testtb", live_query_id);
|
|
|
|
let enc = Lq::encode(&val).unwrap();
|
2023-07-13 13:44:54 +00:00
|
|
|
assert_eq!(
|
|
|
|
enc,
|
2023-07-18 18:32:38 +00:00
|
|
|
b"/*testns\x00*testdb\x00*testtb\x00!lq\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
|
2023-07-13 13:44:54 +00:00
|
|
|
);
|
|
|
|
|
2023-07-18 18:32:38 +00:00
|
|
|
let dec = Lq::decode(&enc).unwrap();
|
2022-01-13 17:40:20 +00:00
|
|
|
assert_eq!(val, dec);
|
|
|
|
}
|
2023-07-13 13:44:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prefix() {
|
|
|
|
let val = super::prefix("testns", "testdb", "testtb");
|
2023-07-18 18:32:38 +00:00
|
|
|
assert_eq!(val, b"/*testns\x00*testdb\x00*testtb\x00!lq\x00")
|
2023-07-13 13:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suffix() {
|
|
|
|
let val = super::suffix("testns", "testdb", "testtb");
|
2024-08-23 15:34:06 +00:00
|
|
|
assert_eq!(val, b"/*testns\x00*testdb\x00*testtb\x00!lq\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00")
|
2023-07-13 13:44:54 +00:00
|
|
|
}
|
2022-01-13 17:40:20 +00:00
|
|
|
}
|