adk_kernel/stores/
agenda_memory.rs1use std::collections::BTreeMap;
4
5use async_trait::async_trait;
6use tokio::sync::RwLock;
7
8use crate::agenda::{AgendaFilter, AgendaItem, AgendaStateKind, AgendaStore};
9use crate::error::Result;
10
11#[derive(Debug, Default)]
32pub struct InMemoryAgendaStore {
33 items: RwLock<BTreeMap<String, AgendaItem>>,
34}
35
36impl InMemoryAgendaStore {
37 pub fn new() -> Self {
39 Self::default()
40 }
41}
42
43#[async_trait]
44impl AgendaStore for InMemoryAgendaStore {
45 async fn put(&self, item: AgendaItem) -> Result<()> {
46 self.items.write().await.insert(item.id.clone(), item);
47 Ok(())
48 }
49
50 async fn get(&self, id: &str) -> Result<Option<AgendaItem>> {
51 Ok(self.items.read().await.get(id).cloned())
52 }
53
54 async fn list(&self, filter: &AgendaFilter) -> Result<Vec<AgendaItem>> {
55 let items = self.items.read().await;
56 let mut matched: Vec<AgendaItem> =
57 items.values().filter(|item| filter.matches(item)).cloned().collect();
58 matched.sort_by(|a, b| a.created_at.cmp(&b.created_at).then_with(|| a.id.cmp(&b.id)));
59 if let Some(limit) = filter.limit {
60 matched.truncate(limit);
61 }
62 Ok(matched)
63 }
64
65 async fn transition(&self, item: AgendaItem, from: AgendaStateKind) -> Result<bool> {
66 let mut items = self.items.write().await;
67 match items.get_mut(&item.id) {
68 Some(stored) if stored.state.kind() == from => {
69 *stored = item;
70 Ok(true)
71 }
72 Some(_) | None => Ok(false),
73 }
74 }
75
76 async fn delete(&self, id: &str) -> Result<bool> {
77 Ok(self.items.write().await.remove(id).is_some())
78 }
79}