1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use bytes::{Bytes, BytesMut};
use num::ToPrimitive;
use std::os::unix::fs::FileExt;
use std::path::Path;
use std::{io, marker::PhantomData, path::PathBuf};
use tokio::task::JoinError;
use tokio::{
    fs::{File, OpenOptions},
    io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
};

use crate::io_types::{
    AppendInfo, AsyncAppend, AsyncBufRead, AsyncClose, AsyncFlush, AsyncRemove, AsyncTruncate,
    FallibleEntity, IntegerConversionError, ReadBytes, SizedEntity, UnreadError, UnwrittenError,
};
use crate::AppendLocation;

pub struct RandomRead;

pub struct Seek;

pub struct TokioFile<K, const FLUSH_ON_APPEND: bool = false> {
    inner: File,
    backing_file_path: PathBuf,

    size: u64,

    _phantom_data: PhantomData<K>,
}

impl<K, const FA: bool> TokioFile<K, FA> {
    pub async fn new<P: AsRef<Path>>(path: P) -> Result<Self, TokioFileError> {
        let backing_file_path = path.as_ref().to_path_buf();

        let inner = OpenOptions::new()
            .read(true)
            .write(true)
            .append(true)
            .create(true)
            .open(path.as_ref())
            .await
            .map_err(TokioFileError::IoError)?;

        let initial_size = inner
            .metadata()
            .await
            .map_err(TokioFileError::IoError)?
            .len();

        Ok(Self {
            inner,
            backing_file_path,
            size: initial_size,
            _phantom_data: PhantomData,
        })
    }
}

impl<K, const FA: bool> SizedEntity for TokioFile<K, FA> {
    type Position = u64;

    type Size = u64;

    fn size(&self) -> Self::Size {
        self.size
    }
}

pub enum TokioFileError {
    IoError(io::Error),
    JoinError(JoinError),

    IntoStdFileConversionFailed,

    IntegerConversionError,
}

impl From<IntegerConversionError> for TokioFileError {
    fn from(_: IntegerConversionError) -> Self {
        Self::IntegerConversionError
    }
}

impl<K, const FA: bool> FallibleEntity for TokioFile<K, FA> {
    type Error = TokioFileError;
}

impl<K, const FA: bool> AsyncTruncate for TokioFile<K, FA> {
    async fn truncate(&mut self, position: Self::Position) -> Result<(), Self::Error> {
        self.inner.flush().await.map_err(Self::Error::IoError)?;

        self.inner
            .set_len(position)
            .await
            .map_err(Self::Error::IoError)?;

        self.size = position;

        Ok(())
    }
}

impl<K, const FLUSH_ON_APPEND: bool> AsyncAppend for TokioFile<K, FLUSH_ON_APPEND> {
    async fn append(
        &mut self,
        bytes: Bytes,
    ) -> Result<AppendInfo<Self::Position, Self::Size>, UnwrittenError<Self::Error>> {
        let write_position = self.size();

        let write_len = self
            .inner
            .write(&bytes)
            .await
            .map_err(|err| UnwrittenError {
                unwritten: bytes.clone(),
                err: TokioFileError::IoError(err),
            })?
            .to_u64()
            .ok_or_else(|| UnwrittenError {
                unwritten: bytes.clone(),
                err: Self::Error::IntegerConversionError,
            })?;

        if FLUSH_ON_APPEND {
            self.inner.flush().await.map_err(|err| UnwrittenError {
                unwritten: bytes.clone(),
                err: TokioFileError::IoError(err),
            })?;
        }

        self.size += write_len;

        Ok(AppendInfo {
            bytes,
            location: AppendLocation {
                write_position,
                write_len,
            },
        })
    }
}

impl AsyncBufRead for TokioFile<Seek, true> {
    async fn read_at_buf(
        &mut self,
        position: Self::Position,
        mut buffer: BytesMut,
    ) -> Result<ReadBytes<BytesMut, Self::Size>, UnreadError<Self::Error>> {
        self.inner
            .seek(io::SeekFrom::Start(position))
            .await
            .map_err(|err| UnreadError {
                unread: buffer.clone(),
                err: Self::Error::IoError(err),
            })?;

        let read_len = self
            .inner
            .read(&mut buffer)
            .await
            .map_err(|err| UnreadError {
                unread: buffer.clone(),
                err: Self::Error::IoError(err),
            })?
            .to_u64()
            .ok_or_else(|| UnreadError {
                unread: buffer.clone(),
                err: Self::Error::IntegerConversionError,
            })?;

        self.inner
            .seek(io::SeekFrom::Start(self.size))
            .await
            .map_err(|err| UnreadError {
                unread: buffer.clone(),
                err: Self::Error::IoError(err),
            })?;

        Ok(ReadBytes {
            read_bytes: buffer,
            read_len,
        })
    }
}

#[cfg(target_family = "unix")]
impl AsyncBufRead for TokioFile<RandomRead, true> {
    async fn read_at_buf(
        &mut self,
        position: Self::Position,
        buffer: BytesMut,
    ) -> Result<ReadBytes<BytesMut, Self::Size>, UnreadError<Self::Error>> {
        let reader = self
            .inner
            .try_clone()
            .await
            .map_err(|err| UnreadError {
                unread: buffer.clone(),
                err: Self::Error::IoError(err),
            })?
            .try_into_std()
            .map_err(|_| UnreadError {
                unread: buffer.clone(),
                err: Self::Error::IntoStdFileConversionFailed,
            })?;

        let read_len_usize = buffer.len();

        let read_len = read_len_usize.to_u64().ok_or_else(|| UnreadError {
            unread: buffer.clone(),
            err: Self::Error::IntegerConversionError,
        })?;

        let mut read_buffer = buffer;

        let read_buffer = tokio::task::spawn_blocking(move || {
            let x = reader.read_exact_at(&mut read_buffer, position);

            match x {
                Ok(_) => Ok(read_buffer),
                Err(err) => Err((err, read_buffer)),
            }
        })
        .await;

        match read_buffer {
            Ok(Ok(read_buffer)) => Ok(ReadBytes {
                read_bytes: read_buffer,
                read_len,
            }),
            Ok(Err((err, unread))) => Err(UnreadError {
                unread,
                err: Self::Error::IoError(err),
            }),
            Err(err) => Err(UnreadError {
                unread: Bytes::from(vec![0u8; read_len_usize]).into(),
                err: Self::Error::JoinError(err),
            }),
        }
    }
}

impl<K, const FA: bool> AsyncFlush for TokioFile<K, FA> {
    async fn flush(&mut self) -> Result<(), Self::Error> {
        self.inner.flush().await.map_err(Self::Error::IoError)
    }
}

impl<K, const FA: bool> AsyncRemove for TokioFile<K, FA> {
    async fn remove(self) -> Result<(), Self::Error> {
        tokio::fs::remove_file(self.backing_file_path)
            .await
            .map_err(Self::Error::IoError)
    }
}

impl<K, const FA: bool> AsyncClose for TokioFile<K, FA> {
    async fn close(mut self) -> Result<(), Self::Error> {
        self.flush().await?;

        drop(self.inner);

        Ok(())
    }
}