1use crate::object_store::{DirType, PosixAttributes, Timestamp};
6use anyhow::Error;
7use async_trait::async_trait;
8use std::future::Future;
9use std::sync::Arc;
10use storage_device::buffer::{BufferFuture, BufferRef, MutableBufferRef};
11use storage_units::BlockSize;
12
13pub const INVALID_OBJECT_ID: u64 = 0;
16
17pub trait ObjectHandle: Send + Sync + 'static {
20 fn object_id(&self) -> u64;
23
24 fn block_size(&self) -> BlockSize;
27
28 fn allocate_buffer(&self, size: usize) -> BufferFuture<'_>;
30
31 fn set_trace(&self, _v: bool) {}
33}
34
35#[derive(Clone, Debug, PartialEq)]
36pub struct ObjectProperties {
37 pub refs: u64,
39 pub allocated_size: u64,
41 pub data_attribute_size: u64,
44 pub creation_time: Timestamp,
46 pub modification_time: Timestamp,
48 pub access_time: Timestamp,
50 pub change_time: Timestamp,
52 pub sub_dirs: u64,
54 pub posix_attributes: Option<PosixAttributes>,
56 pub dir_type: DirType,
58}
59
60#[async_trait]
61pub trait ReadObjectHandle: ObjectHandle {
62 async fn read(&self, offset: u64, buf: MutableBufferRef<'_>) -> Result<usize, Error>;
65
66 fn get_size(&self) -> u64;
68}
69
70pub trait WriteObjectHandle: ObjectHandle {
71 fn write_or_append(
76 &self,
77 offset: Option<u64>,
78 buf: BufferRef<'_>,
79 ) -> impl Future<Output = Result<u64, Error>> + Send;
80
81 fn truncate(&self, size: u64) -> impl Future<Output = Result<(), Error>> + Send;
85
86 fn flush(&self) -> impl Future<Output = Result<(), Error>> + Send;
88}
89
90pub trait WriteBytes: Sized {
92 fn block_size(&self) -> BlockSize;
93
94 fn write_bytes(&mut self, buf: &[u8]) -> impl Future<Output = Result<(), Error>> + Send;
97
98 fn complete(self) -> impl Future<Output = Result<u64, Error>> + Send;
100
101 fn skip(&mut self, amount: u64) -> impl Future<Output = Result<(), Error>> + Send;
104}
105
106impl LayerObject for dyn ReadObjectHandle + '_ {}
107
108#[async_trait]
110pub trait LayerObject: ReadObjectHandle {
111 fn as_slice(&self) -> Option<&[u8]> {
114 None
115 }
116
117 fn has_io_error(&self) -> bool {
119 false
120 }
121
122 fn purge_cached_data(&self) {}
124
125 async fn close(&self) {}
128}
129
130impl<T: ObjectHandle + ?Sized> ObjectHandle for Arc<T> {
131 fn object_id(&self) -> u64 {
132 (**self).object_id()
133 }
134
135 fn block_size(&self) -> BlockSize {
136 (**self).block_size()
137 }
138
139 fn allocate_buffer(&self, size: usize) -> BufferFuture<'_> {
140 (**self).allocate_buffer(size)
141 }
142
143 fn set_trace(&self, v: bool) {
144 (**self).set_trace(v)
145 }
146}
147
148#[async_trait]
149impl<T: ReadObjectHandle + ?Sized> ReadObjectHandle for Arc<T> {
150 async fn read(&self, offset: u64, buf: MutableBufferRef<'_>) -> Result<usize, Error> {
151 (**self).read(offset, buf).await
152 }
153
154 fn get_size(&self) -> u64 {
155 (**self).get_size()
156 }
157}
158
159#[async_trait]
160impl<T: LayerObject + ?Sized> LayerObject for Arc<T> {
161 fn as_slice(&self) -> Option<&[u8]> {
162 (**self).as_slice()
163 }
164
165 fn has_io_error(&self) -> bool {
166 (**self).has_io_error()
167 }
168
169 fn purge_cached_data(&self) {
170 (**self).purge_cached_data()
171 }
172
173 async fn close(&self) {
174 (**self).close().await
175 }
176}
177
178impl<T: ObjectHandle + ?Sized> ObjectHandle for Box<T> {
179 fn object_id(&self) -> u64 {
180 (**self).object_id()
181 }
182
183 fn block_size(&self) -> BlockSize {
184 (**self).block_size()
185 }
186
187 fn allocate_buffer(&self, size: usize) -> BufferFuture<'_> {
188 (**self).allocate_buffer(size)
189 }
190
191 fn set_trace(&self, v: bool) {
192 (**self).set_trace(v)
193 }
194}
195
196#[async_trait]
197impl<T: ReadObjectHandle + ?Sized> ReadObjectHandle for Box<T> {
198 async fn read(&self, offset: u64, buf: MutableBufferRef<'_>) -> Result<usize, Error> {
199 (**self).read(offset, buf).await
200 }
201
202 fn get_size(&self) -> u64 {
203 (**self).get_size()
204 }
205}
206
207#[async_trait]
208impl<T: LayerObject + ?Sized> LayerObject for Box<T> {
209 fn as_slice(&self) -> Option<&[u8]> {
210 (**self).as_slice()
211 }
212
213 fn has_io_error(&self) -> bool {
214 (**self).has_io_error()
215 }
216
217 fn purge_cached_data(&self) {
218 (**self).purge_cached_data()
219 }
220
221 async fn close(&self) {
222 (**self).close().await
223 }
224}