drasi_core/interface/
element_index.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 std::{pin::Pin, sync::Arc};
16
17use async_trait::async_trait;
18use futures::Stream;
19
20use crate::{
21    models::{Element, ElementReference, ElementTimestamp, QueryJoin, TimestampRange},
22    path_solver::match_path::MatchPath,
23};
24
25use super::IndexError;
26
27pub type ElementResult = Result<Arc<Element>, IndexError>;
28pub type ElementStream = Pin<Box<dyn Stream<Item = ElementResult> + Send>>;
29
30#[async_trait]
31pub trait ElementIndex: Send + Sync {
32    async fn get_element(
33        &self,
34        element_ref: &ElementReference,
35    ) -> Result<Option<Arc<Element>>, IndexError>;
36    async fn set_element(
37        &self,
38        element: &Element,
39        slot_affinity: &Vec<usize>,
40    ) -> Result<(), IndexError>;
41    async fn delete_element(&self, element_ref: &ElementReference) -> Result<(), IndexError>;
42    async fn get_slot_element_by_ref(
43        &self,
44        slot: usize,
45        element_ref: &ElementReference,
46    ) -> Result<Option<Arc<Element>>, IndexError>;
47    async fn get_slot_elements_by_inbound(
48        &self,
49        slot: usize,
50        inbound_ref: &ElementReference,
51    ) -> Result<ElementStream, IndexError>;
52    async fn get_slot_elements_by_outbound(
53        &self,
54        slot: usize,
55        outbound_ref: &ElementReference,
56    ) -> Result<ElementStream, IndexError>;
57    async fn clear(&self) -> Result<(), IndexError>;
58
59    async fn set_joins(&self, match_path: &MatchPath, joins: &Vec<Arc<QueryJoin>>);
60}
61
62#[async_trait]
63pub trait ElementArchiveIndex: Send + Sync {
64    async fn get_element_as_at(
65        &self,
66        element_ref: &ElementReference,
67        time: ElementTimestamp,
68    ) -> Result<Option<Arc<Element>>, IndexError>;
69    async fn get_element_versions(
70        &self,
71        element_ref: &ElementReference,
72        range: TimestampRange<ElementTimestamp>,
73    ) -> Result<ElementStream, IndexError>;
74    async fn clear(&self) -> Result<(), IndexError>;
75}