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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use std::{
collections::HashMap,
io::{self, SeekFrom},
ops::Neg,
path::{Path, PathBuf},
sync::{Arc, Weak},
};
use once_cell::sync::Lazy;
use parking_lot::{Mutex, RwLock};
use super::{FileManager, FileOp, ManagedFile, OpenableFile};
use crate::{
error::Error,
io::{File, ManagedFileOpener, OperableFile, PathIds},
ErrorKind,
};
type FileBuffer = Arc<RwLock<Vec<u8>>>;
#[derive(Clone)]
pub struct MemoryFile {
id: Option<u64>,
path: PathBuf,
buffer: FileBuffer,
position: usize,
}
impl std::fmt::Debug for MemoryFile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let buffer = self.buffer.read();
f.debug_struct("MemoryFile")
.field("id", &self.id)
.field("path", &self.path)
.field("buffer", &buffer.len())
.field("position", &self.position)
.finish()
}
}
type OpenBuffers = Arc<Mutex<HashMap<PathBuf, Weak<RwLock<Vec<u8>>>>>>;
static OPEN_BUFFERS: Lazy<OpenBuffers> = Lazy::new(Arc::default);
#[allow(clippy::needless_pass_by_value)]
fn lookup_buffer(
path: impl AsRef<std::path::Path> + Send,
create_if_not_found: bool,
) -> Option<Arc<RwLock<Vec<u8>>>> {
let mut open_buffers = OPEN_BUFFERS.lock();
if let Some(existing_buffer) = open_buffers.get(path.as_ref()).and_then(Weak::upgrade) {
Some(existing_buffer)
} else if create_if_not_found {
let new_buffer = Arc::default();
open_buffers.insert(path.as_ref().to_path_buf(), Arc::downgrade(&new_buffer));
Some(new_buffer)
} else {
None
}
}
impl ManagedFile for MemoryFile {
type Manager = MemoryFileManager;
}
#[allow(clippy::cast_possible_truncation)]
impl super::File for MemoryFile {
fn id(&self) -> Option<u64> {
self.id
}
fn path(&self) -> &Path {
&self.path
}
fn length(&self) -> Result<u64, Error> {
let file_buffer = self.buffer.read();
Ok(file_buffer.len() as u64)
}
fn synchronize(&mut self) -> Result<(), Error> {
Ok(())
}
fn close(self) -> Result<(), Error> {
Ok(())
}
}
pub struct MemoryFileOpener;
impl ManagedFileOpener<MemoryFile> for MemoryFileOpener {
fn open_for_read(
&self,
path: impl AsRef<std::path::Path> + Send,
id: Option<u64>,
) -> Result<MemoryFile, Error> {
let path = path.as_ref();
Ok(MemoryFile {
id,
path: path.to_path_buf(),
buffer: lookup_buffer(path, true).unwrap(),
position: 0,
})
}
fn open_for_append(
&self,
path: impl AsRef<std::path::Path> + Send,
id: Option<u64>,
) -> Result<MemoryFile, Error> {
let path = path.as_ref();
let buffer = lookup_buffer(path, true).unwrap();
let position = {
let buffer = buffer.read();
buffer.len()
};
Ok(MemoryFile {
id,
buffer,
position,
path: path.to_path_buf(),
})
}
}
impl std::io::Seek for MemoryFile {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match pos {
SeekFrom::Start(position) => self.position = usize::try_from(position).unwrap(),
SeekFrom::End(from_end) => {
let buffer = self.buffer.read();
self.position = if from_end.is_positive() {
buffer.len()
} else {
buffer.len() - usize::try_from(from_end.neg()).unwrap()
};
}
SeekFrom::Current(relative) => {
self.position = if relative.is_positive() {
self.position + usize::try_from(relative).unwrap()
} else {
self.position - usize::try_from(relative.neg()).unwrap()
}
}
}
Ok(self.position as u64)
}
}
impl std::io::Read for MemoryFile {
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
let file_buffer = self.buffer.read();
let read_end = self.position as usize + buffer.len();
if read_end > file_buffer.len() {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
ErrorKind::message("read requested more bytes than available"),
));
}
buffer.copy_from_slice(&file_buffer[self.position..read_end]);
self.position = read_end;
Ok(buffer.len())
}
}
impl std::io::Write for MemoryFile {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut file_buffer = self.buffer.write();
file_buffer.extend_from_slice(buf);
self.position += buf.len();
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
#[derive(Debug, Default, Clone)]
pub struct MemoryFileManager {
file_ids: PathIds,
open_files: Arc<Mutex<HashMap<u64, FileBuffer>>>,
}
impl MemoryFileManager {
fn lookup_file(
&self,
path: impl AsRef<Path>,
create_if_needed: bool,
id: Option<u64>,
) -> Result<Option<MemoryFile>, Error> {
let path = path.as_ref();
let id = match id {
Some(id) => id,
None => match self.file_ids.file_id_for_path(path, create_if_needed) {
Some(id) => id,
None => return Ok(None),
},
};
let mut open_files = self.open_files.lock();
if let Some(open_file) = open_files.get(&id) {
Ok(Some(MemoryFile {
id: Some(id),
buffer: open_file.clone(),
path: path.to_path_buf(),
position: 0,
}))
} else if create_if_needed {
let file = MemoryFileOpener.open_for_append(path, Some(id))?;
open_files.insert(id, file.buffer.clone());
Ok(Some(file))
} else {
Ok(None)
}
}
fn forget_file(&self, path: &Path) -> bool {
if let Some(id) = self.file_ids.remove_file_id_for_path(path) {
let mut open_files = self.open_files.lock();
open_files.remove(&id).is_some()
} else {
false
}
}
}
impl FileManager for MemoryFileManager {
type File = MemoryFile;
type FileHandle = OpenMemoryFile;
fn append(&self, path: impl AsRef<Path>) -> Result<Self::FileHandle, Error> {
let path = path.as_ref();
let id = self.file_ids.file_id_for_path(path, true);
self.lookup_file(path, true, id).map(|file| OpenMemoryFile {
file: file.unwrap(),
manager: self.clone(),
})
}
fn read(&self, path: impl AsRef<Path>) -> Result<Self::FileHandle, Error> {
self.append(path)
}
fn file_length(&self, path: impl AsRef<Path>) -> Result<u64, Error> {
let file = self.lookup_file(path, false, None)?.ok_or_else(|| {
ErrorKind::Io(io::Error::new(
io::ErrorKind::NotFound,
ErrorKind::message("not found"),
))
})?;
file.length()
}
fn exists(&self, path: impl AsRef<Path>) -> Result<bool, Error> {
Ok(self.lookup_file(path, false, None)?.is_some())
}
fn delete(&self, path: impl AsRef<Path>) -> Result<bool, Error> {
let path = path.as_ref();
{
let mut open_buffers = OPEN_BUFFERS.lock();
open_buffers.remove(path);
}
Ok(self.forget_file(path))
}
fn delete_directory(&self, path: impl AsRef<Path>) -> Result<(), Error> {
let path = path.as_ref();
let removed_ids = self.file_ids.remove_file_ids_for_path_prefix(path);
let mut open_files = self.open_files.lock();
for id in removed_ids {
open_files.remove(&id);
}
Ok(())
}
fn close_handles<F: FnOnce(u64)>(&self, path: impl AsRef<Path>, publish_callback: F) {
let path = path.as_ref();
self.forget_file(path);
let new_id = self.file_ids.file_id_for_path(path, true).unwrap();
publish_callback(new_id);
}
}
impl ManagedFileOpener<MemoryFile> for MemoryFileManager {
fn open_for_read(
&self,
path: impl AsRef<Path> + Send,
id: Option<u64>,
) -> Result<MemoryFile, Error> {
MemoryFileOpener.open_for_read(path, id)
}
fn open_for_append(
&self,
path: impl AsRef<Path> + Send,
id: Option<u64>,
) -> Result<MemoryFile, Error> {
MemoryFileOpener.open_for_append(path, id)
}
}
#[derive(Debug)]
pub struct OpenMemoryFile {
file: MemoryFile,
manager: MemoryFileManager,
}
impl OpenableFile<MemoryFile> for OpenMemoryFile {
fn id(&self) -> Option<u64> {
self.file.id
}
fn replace_with<C: FnOnce(u64)>(
self,
replacement: MemoryFile,
manager: &MemoryFileManager,
publish_callback: C,
) -> Result<Self, Error> {
let weak_buffer = Arc::downgrade(&replacement.buffer);
drop(self.manager.delete(replacement.path()));
{
let mut open_buffers = OPEN_BUFFERS.lock();
open_buffers.insert(self.file.path.clone(), weak_buffer);
}
manager.close_handles(&self.file.path, publish_callback);
let new_file = manager.append(&self.file.path)?;
{
assert!(Arc::ptr_eq(&new_file.file.buffer, &replacement.buffer));
}
Ok(new_file)
}
fn close(self) -> Result<(), Error> {
drop(self);
Ok(())
}
}
impl OperableFile<MemoryFile> for OpenMemoryFile {
fn execute<Output, Op: FileOp<Output>>(&mut self, operator: Op) -> Output {
operator.execute(&mut self.file)
}
}