brk_vec/variants/
indexed.rs1use std::{
2 cmp::Ordering,
3 fmt::Debug,
4 path::{Path, PathBuf},
5 time::Duration,
6};
7
8use arc_swap::ArcSwap;
9use brk_core::Height;
10
11use crate::{
12 AnyCollectableVec, AnyIterableVec, AnyVec, BoxedVecIterator, CollectableVec, Compressed, Error,
13 GenericStoredVec, Mmap, Result, StoredIndex, StoredType, StoredVec, Value, Version,
14};
15
16use super::StoredVecIterator;
17
18#[derive(Debug, Clone)]
19pub struct IndexedVec<I, T> {
20 height: Option<Height>,
21 inner: StoredVec<I, T>,
22}
23
24impl<I, T> IndexedVec<I, T>
25where
26 I: StoredIndex,
27 T: StoredType,
28{
29 pub fn forced_import(path: &Path, version: Version, compressed: Compressed) -> Result<Self> {
30 Ok(Self {
31 height: Height::try_from(Self::path_height_(path).as_path()).ok(),
32 inner: StoredVec::forced_import(path, version, compressed)?,
33 })
34 }
35
36 #[inline]
37 pub fn get_or_read(&self, index: I, mmap: &Mmap) -> Result<Option<Value<T>>> {
38 self.inner.get_or_read(index, mmap)
39 }
40
41 #[inline]
42 pub fn push_if_needed(&mut self, index: I, value: T) -> Result<()> {
43 let len = self.inner.len();
44 match len.cmp(&index.to_usize()?) {
45 Ordering::Greater => {
46 Ok(())
49 }
50 Ordering::Equal => {
51 self.inner.push(value);
52 Ok(())
53 }
54 Ordering::Less => {
55 dbg!(index, value, len, self.path_height());
56 Err(Error::IndexTooHigh)
57 }
58 }
59 }
60
61 pub fn truncate_if_needed(&mut self, index: I, height: Height) -> Result<()> {
62 if self.height.is_none_or(|self_height| self_height != height) {
63 height.write(&self.path_height())?;
64 }
65 self.inner.truncate_if_needed(index)?;
66 Ok(())
67 }
68
69 pub fn flush(&mut self, height: Height) -> Result<()> {
70 height.write(&self.path_height())?;
71 self.inner.flush()
72 }
73
74 pub fn mmap(&self) -> &ArcSwap<Mmap> {
75 self.inner.mmap()
76 }
77
78 #[inline]
79 pub fn hasnt(&self, index: I) -> Result<bool> {
80 self.inner.has(index).map(|b| !b)
81 }
82
83 pub fn height(&self) -> brk_core::Result<Height> {
84 Height::try_from(self.path_height().as_path())
85 }
86 fn path_height(&self) -> PathBuf {
87 Self::path_height_(self.inner.path())
88 }
89 fn path_height_(path: &Path) -> PathBuf {
90 path.join("height")
91 }
92}
93
94impl<I, T> AnyVec for IndexedVec<I, T>
95where
96 I: StoredIndex,
97 T: StoredType,
98{
99 #[inline]
100 fn version(&self) -> Version {
101 self.inner.version()
102 }
103
104 #[inline]
105 fn name(&self) -> String {
106 self.inner.name()
107 }
108
109 #[inline]
110 fn len(&self) -> usize {
111 self.inner.len()
112 }
113
114 #[inline]
115 fn modified_time(&self) -> Result<Duration> {
116 self.inner.modified_time()
117 }
118
119 #[inline]
120 fn index_type_to_string(&self) -> &str {
121 I::to_string()
122 }
123}
124
125pub trait AnyIndexedVec: AnyVec {
126 fn height(&self) -> brk_core::Result<Height>;
127 fn flush(&mut self, height: Height) -> Result<()>;
128}
129
130impl<I, T> AnyIndexedVec for IndexedVec<I, T>
131where
132 I: StoredIndex,
133 T: StoredType,
134{
135 fn height(&self) -> brk_core::Result<Height> {
136 self.height()
137 }
138
139 fn flush(&mut self, height: Height) -> Result<()> {
140 self.flush(height)
141 }
142}
143
144impl<'a, I, T> IntoIterator for &'a IndexedVec<I, T>
145where
146 I: StoredIndex,
147 T: StoredType,
148{
149 type Item = (I, Value<'a, T>);
150 type IntoIter = StoredVecIterator<'a, I, T>;
151
152 fn into_iter(self) -> Self::IntoIter {
153 self.inner.into_iter()
154 }
155}
156
157impl<I, T> AnyIterableVec<I, T> for IndexedVec<I, T>
158where
159 I: StoredIndex,
160 T: StoredType,
161{
162 fn boxed_iter<'a>(&'a self) -> BoxedVecIterator<'a, I, T>
163 where
164 T: 'a,
165 {
166 Box::new(self.into_iter())
167 }
168}
169
170impl<I, T> AnyCollectableVec for IndexedVec<I, T>
171where
172 I: StoredIndex,
173 T: StoredType,
174{
175 fn collect_range_serde_json(
176 &self,
177 from: Option<i64>,
178 to: Option<i64>,
179 ) -> Result<Vec<serde_json::Value>> {
180 CollectableVec::collect_range_serde_json(self, from, to)
181 }
182}