tinylfu_cached/cache/command/
mod.rs1use std::hash::Hash;
2use std::time::Duration;
3
4use crate::cache::key_description::KeyDescription;
5use crate::cache::types::{KeyId, Weight};
6
7pub mod acknowledgement;
8pub mod error;
9pub mod command_executor;
10
11pub(crate) enum CommandType<Key, Value>
18 where Key: Hash + Eq + Clone {
19 Put(KeyDescription<Key>, Value),
20 PutWithTTL(KeyDescription<Key>, Value, Duration),
21 Delete(Key),
22 UpdateWeight(KeyId, Weight),
23 Shutdown,
24}
25
26impl<Key, Value> CommandType<Key, Value>
29 where Key: Hash + Eq + Clone {
30 fn description(&self) -> String {
31 match self {
32 CommandType::Put(_, _) => "Put".to_string(),
33 CommandType::PutWithTTL(_, _, _) => "PutWithTTL".to_string(),
34 CommandType::Delete(_) => "Delete".to_string(),
35 CommandType::UpdateWeight(_, _) => "UpdateWeight".to_string(),
36 CommandType::Shutdown => "Shutdown".to_string(),
37 }
38 }
39}
40
41#[derive(Copy, Clone, Debug, Eq, PartialEq)]
53pub enum CommandStatus {
54 Pending,
55 Accepted,
56 Rejected(RejectionReason),
57 ShuttingDown,
58}
59
60#[non_exhaustive]
70#[derive(Copy, Clone, Debug, Eq, PartialEq)]
71pub enum RejectionReason {
72 EnoughSpaceIsNotAvailableAndKeyFailedToEvictOthers,
73 KeyWeightIsGreaterThanCacheWeight,
74 KeyDoesNotExist,
75 KeyAlreadyExists,
76}
77
78#[cfg(test)]
79mod tests {
80 use std::time::Duration;
81
82 use crate::cache::command::CommandType;
83 use crate::cache::key_description::KeyDescription;
84
85 #[test]
86 fn command_description_put() {
87 let put = CommandType::Put(
88 KeyDescription::new(
89 "topic", 1, 2090, 10,
90 ),
91 "microservices");
92
93 assert_eq!("Put", put.description());
94 }
95
96 #[test]
97 fn command_description_put_with_ttl() {
98 let put = CommandType::PutWithTTL(
99 KeyDescription::new(
100 "topic", 1, 2090, 10,
101 ),
102 "microservices",
103 Duration::from_millis(10),
104 );
105
106 assert_eq!("PutWithTTL", put.description());
107 }
108
109 #[test]
110 fn command_description_delete() {
111 let delete: CommandType<&str, &str> = CommandType::Delete("topic");
112
113 assert_eq!("Delete", delete.description());
114 }
115
116 #[test]
117 fn command_description_update_weight() {
118 let update_weight: CommandType<&str, &str> = CommandType::UpdateWeight(10, 200);
119
120 assert_eq!("UpdateWeight", update_weight.description());
121 }
122
123 #[test]
124 fn command_description_shutdown() {
125 let shutdown: CommandType<&str, &str> = CommandType::Shutdown;
126
127 assert_eq!("Shutdown", shutdown.description());
128 }
129}