Skip to main content

retia_rocks/bridge/
iter.rs

1/*
2 * Copyright 2022, The Cozo Project Authors.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
5 * If a copy of the MPL was not distributed with this file,
6 * You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9use cxx::UniquePtr;
10
11use crate::bridge::ffi::*;
12
13pub struct IterBuilder {
14    pub(crate) inner: UniquePtr<IterBridge>,
15}
16
17pub struct DbIter {
18    pub(crate) inner: UniquePtr<IterBridge>,
19}
20
21impl IterBuilder {
22    pub fn start(mut self) -> DbIter {
23        self.inner.pin_mut().start();
24        DbIter { inner: self.inner }
25    }
26    pub fn clear_bounds(&mut self) {
27        self.inner.pin_mut().clear_bounds();
28    }
29    pub fn lower_bound(mut self, bound: &[u8]) -> Self {
30        self.inner.pin_mut().set_lower_bound(bound);
31        self
32    }
33    pub fn upper_bound(mut self, bound: &[u8]) -> Self {
34        self.inner.pin_mut().set_upper_bound(bound);
35        self
36    }
37
38    #[inline]
39    pub fn verify_checksums(mut self, val: bool) -> Self {
40        self.inner.pin_mut().verify_checksums(val);
41        self
42    }
43
44    #[inline]
45    pub fn fill_cache(mut self, val: bool) -> Self {
46        self.inner.pin_mut().fill_cache(val);
47        self
48    }
49
50    #[inline]
51    pub fn tailing(mut self, val: bool) -> Self {
52        self.inner.pin_mut().tailing(val);
53        self
54    }
55
56    #[inline]
57    pub fn total_order_seek(mut self, val: bool) -> Self {
58        self.inner.pin_mut().total_order_seek(val);
59        self
60    }
61    #[inline]
62    pub fn auto_prefix_mode(mut self, val: bool) -> Self {
63        self.inner.pin_mut().auto_prefix_mode(val);
64        self
65    }
66    #[inline]
67    pub fn prefix_same_as_start(mut self, val: bool) -> Self {
68        self.inner.pin_mut().prefix_same_as_start(val);
69        self
70    }
71    #[inline]
72    pub fn pin_data(mut self, val: bool) -> Self {
73        self.inner.pin_mut().pin_data(val);
74        self
75    }
76}
77
78impl DbIter {
79    #[inline]
80    pub fn reset(mut self) -> IterBuilder {
81        self.inner.pin_mut().reset();
82        IterBuilder { inner: self.inner }
83    }
84    #[inline]
85    pub fn seek_to_start(&mut self) {
86        self.inner.pin_mut().to_start();
87    }
88    #[inline]
89    pub fn seek_to_end(&mut self) {
90        self.inner.pin_mut().to_end();
91    }
92    #[inline]
93    pub fn seek(&mut self, key: &[u8]) {
94        self.inner.pin_mut().seek(key);
95    }
96    #[inline]
97    pub fn seek_back(&mut self, key: &[u8]) {
98        self.inner.pin_mut().seek_backward(key);
99    }
100    #[inline]
101    pub fn is_valid(&self) -> bool {
102        self.inner.is_valid()
103    }
104    #[inline]
105    pub fn next(&mut self) {
106        self.inner.pin_mut().next();
107    }
108    #[inline]
109    pub fn prev(&mut self) {
110        self.inner.pin_mut().prev();
111    }
112    #[inline]
113    pub fn status(&self) -> RocksDbStatus {
114        let mut status = RocksDbStatus::default();
115        self.inner.status(&mut status);
116        status
117    }
118    #[inline]
119    pub fn key(&self) -> Result<Option<&[u8]>, RocksDbStatus> {
120        if self.is_valid() {
121            Ok(Some(self.inner.key()))
122        } else {
123            let status = self.status();
124            if status.is_ok() {
125                Ok(None)
126            } else {
127                Err(status)
128            }
129        }
130    }
131    #[inline]
132    pub fn val(&self) -> Result<Option<&[u8]>, RocksDbStatus> {
133        if self.is_valid() {
134            Ok(Some(self.inner.val()))
135        } else {
136            let status = self.status();
137            if status.is_ok() {
138                Ok(None)
139            } else {
140                Err(status)
141            }
142        }
143    }
144    #[inline]
145    pub fn pair(&self) -> Result<Option<(&[u8], &[u8])>, RocksDbStatus> {
146        if self.is_valid() {
147            Ok(Some((self.inner.key(), self.inner.val())))
148        } else {
149            let status = self.status();
150            if status.is_ok() {
151                Ok(None)
152            } else {
153                Err(status)
154            }
155        }
156    }
157}