drasi_core/interface/
future_queue.rs

1// Copyright 2024 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use async_trait::async_trait;
16
17use crate::models::{ElementReference, ElementTimestamp};
18
19use super::IndexError;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum PushType {
23    Always,
24    IfNotExists,
25    Overwrite,
26}
27
28#[derive(Debug, Clone, Hash, PartialEq, Eq)]
29pub struct FutureElementRef {
30    pub element_ref: ElementReference,
31    pub original_time: ElementTimestamp,
32    pub due_time: ElementTimestamp,
33    pub group_signature: u64,
34}
35
36#[async_trait]
37pub trait FutureQueue: Send + Sync {
38    async fn push(
39        &self,
40        push_type: PushType,
41        position_in_query: usize,
42        group_signature: u64,
43        element_ref: &ElementReference,
44        original_time: ElementTimestamp,
45        due_time: ElementTimestamp,
46    ) -> Result<bool, IndexError>;
47
48    async fn remove(
49        &self,
50        position_in_query: usize,
51        group_signature: u64,
52    ) -> Result<(), IndexError>;
53
54    async fn pop(&self) -> Result<Option<FutureElementRef>, IndexError>;
55
56    async fn peek_due_time(&self) -> Result<Option<ElementTimestamp>, IndexError>;
57
58    async fn clear(&self) -> Result<(), IndexError>;
59}
60
61#[async_trait]
62pub trait FutureQueueConsumer: Send + Sync {
63    async fn on_due(
64        &self,
65        future_ref: &FutureElementRef,
66    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
67    async fn on_error(
68        &self,
69        future_ref: &FutureElementRef,
70        error: Box<dyn std::error::Error + Send + Sync>,
71    );
72    fn now(&self) -> u64;
73}