shoal_core/shared/
queries.rs1use rkyv::{Archive, Deserialize, Serialize};
4use uuid::Uuid;
5
6use super::traits::ShoalDatabase;
7
8use super::traits::ShoalTable;
9use crate::server::ring::Ring;
10use crate::server::shard::ShardInfo;
11
12#[derive(Debug, Archive, Serialize, Deserialize)]
14pub struct Queries<S: ShoalDatabase> {
15 pub id: Uuid,
17 pub queries: Vec<S::QueryKinds>,
19}
20
21impl<S: ShoalDatabase> Queries<S> {
22 #[must_use]
28 pub fn add<Q: Into<S::QueryKinds>>(mut self, query: Q) -> Self {
29 self.queries.push(query.into());
31 self
32 }
33}
34
35impl<S: ShoalDatabase> Default for Queries<S> {
36 fn default() -> Self {
37 Queries {
38 id: Uuid::new_v4(),
39 queries: Vec::default(),
40 }
41 }
42}
43
44#[derive(Debug, Archive, Serialize, Deserialize, Clone)]
46pub enum Query<T: ShoalTable + std::fmt::Debug> {
47 Insert { key: u64, row: T },
49 Get(Get<T>),
51 Delete { key: u64, sort_key: T::Sort },
53 Update(Update<T>),
55}
56
57impl<T: ShoalTable + std::fmt::Debug> Query<T> {
58 pub fn find_shard<'a>(&self, ring: &'a Ring, tmp: &mut Vec<&'a ShardInfo>) {
60 match self {
62 Query::Insert { key, .. } | Query::Delete { key, .. } => {
63 tmp.push(ring.find_shard(*key))
64 }
65 Query::Get(get) => {
66 for key in &get.partition_keys {
67 tmp.push(ring.find_shard(*key))
68 }
69 }
70 Query::Update(update) => tmp.push(ring.find_shard(update.partition_key)),
71 }
72 }
73}
74
75pub struct TaggedQuery<R: ShoalTable> {
77 pub id: Uuid,
79 pub index: usize,
81 pub query: Query<R>,
83}
84
85impl<R: ShoalTable> TaggedQuery<R> {
86 pub fn new(id: Uuid, index: usize, query: Query<R>) -> Self {
94 Self { id, index, query }
95 }
96}
97
98#[derive(Debug, Archive, Serialize, Deserialize, Clone)]
100pub struct Get<R: ShoalTable> {
102 pub partition_keys: Vec<u64>,
104 pub sort_keys: Vec<R::Sort>,
106 pub filters: Option<R::Filters>,
108 pub limit: Option<usize>,
110}
111
112#[derive(Debug, Archive, Serialize, Deserialize, Clone)]
114pub struct Update<T: ShoalTable> {
115 pub partition_key: u64,
117 pub sort_key: T::Sort,
119 pub update: T::Update,
121}
122
123