Skip to content

Commit 150ceab

Browse files
authored
make the directory BufWriter capacity configurable (#52)
1 parent 8f35280 commit 150ceab

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/directory/directory.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use log::Level;
1010

1111
use crate::directory::directory_lock::Lock;
1212
use crate::directory::error::{DeleteError, LockError, OpenReadError, OpenWriteError};
13-
use crate::directory::{FileHandle, FileSlice, WatchCallback, WatchHandle, WritePtr};
13+
use crate::directory::{
14+
FileHandle, FileSlice, TerminatingWrite, WatchCallback, WatchHandle, WritePtr,
15+
};
1416
use crate::index::SegmentMetaInventory;
1517
use crate::IndexMeta;
1618

@@ -143,6 +145,10 @@ pub trait Directory: DirectoryClone + fmt::Debug + Send + Sync + 'static {
143145
/// Returns true if and only if the file exists
144146
fn exists(&self, path: &Path) -> Result<bool, OpenReadError>;
145147

148+
/// Returns a boxed `TerminatingWrite` object, to be passed into `open_write`
149+
/// which wraps it in a `BufWriter`
150+
fn open_write_inner(&self, path: &Path) -> Result<Box<dyn TerminatingWrite>, OpenWriteError>;
151+
146152
/// Opens a writer for the *virtual file* associated with
147153
/// a [`Path`].
148154
///
@@ -169,7 +175,12 @@ pub trait Directory: DirectoryClone + fmt::Debug + Send + Sync + 'static {
169175
/// panic! if `flush` was not called.
170176
///
171177
/// The file may not previously exist.
172-
fn open_write(&self, path: &Path) -> Result<WritePtr, OpenWriteError>;
178+
fn open_write(&self, path: &Path) -> Result<WritePtr, OpenWriteError> {
179+
Ok(io::BufWriter::with_capacity(
180+
self.bufwriter_capacity(),
181+
self.open_write_inner(path)?,
182+
))
183+
}
173184

174185
/// Reads the full content file that has been written using
175186
/// [`Directory::atomic_write()`].
@@ -296,6 +307,10 @@ pub trait Directory: DirectoryClone + fmt::Debug + Send + Sync + 'static {
296307
fn log(&self, message: &str) {
297308
log!(Level::Info, "{message}");
298309
}
310+
311+
fn bufwriter_capacity(&self) -> usize {
312+
8192
313+
}
299314
}
300315

301316
/// DirectoryClone

src/directory/managed_directory.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::directory::error::{DeleteError, LockError, OpenReadError, OpenWriteEr
1212
use crate::directory::footer::{Footer, FooterProxy, FOOTER_LEN};
1313
use crate::directory::{
1414
DirectoryLock, DirectoryPanicHandler, FileHandle, FileSlice, GarbageCollectionResult, Lock,
15-
WatchCallback, WatchHandle, WritePtr, MANAGED_LOCK, META_LOCK,
15+
TerminatingWrite, WatchCallback, WatchHandle, MANAGED_LOCK, META_LOCK,
1616
};
1717
use crate::error::DataCorruption;
1818
use crate::index::SegmentMetaInventory;
@@ -310,16 +310,19 @@ impl Directory for ManagedDirectory {
310310
Ok(reader)
311311
}
312312

313-
fn open_write(&self, path: &Path) -> result::Result<WritePtr, OpenWriteError> {
313+
fn open_write_inner(
314+
&self,
315+
path: &Path,
316+
) -> result::Result<Box<dyn TerminatingWrite>, OpenWriteError> {
314317
self.register_file_as_managed(path)
315318
.map_err(|io_error| OpenWriteError::wrap_io_error(io_error, path.to_path_buf()))?;
316-
Ok(io::BufWriter::new(Box::new(FooterProxy::new(
319+
Ok(Box::new(FooterProxy::new(
317320
self.directory
318321
.open_write(path)?
319322
.into_inner()
320323
.map_err(|_| ())
321324
.expect("buffer should be empty"),
322-
))))
325+
)))
323326
}
324327

325328
fn atomic_write(&self, path: &Path, data: &[u8]) -> io::Result<()> {
@@ -380,6 +383,10 @@ impl Directory for ManagedDirectory {
380383
fn log(&self, message: &str) {
381384
self.directory.log(message);
382385
}
386+
387+
fn bufwriter_capacity(&self) -> usize {
388+
self.directory.bufwriter_capacity()
389+
}
383390
}
384391

385392
impl Clone for ManagedDirectory {

src/directory/mmap_directory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::fmt;
33
use std::fs::{self, File, OpenOptions};
4-
use std::io::{self, BufWriter, Read, Write};
4+
use std::io::{self, Read, Write};
55
use std::ops::Deref;
66
use std::path::{Path, PathBuf};
77
use std::sync::{Arc, RwLock, Weak};
@@ -21,7 +21,7 @@ use crate::directory::error::{
2121
use crate::directory::file_watcher::FileWatcher;
2222
use crate::directory::{
2323
AntiCallToken, Directory, DirectoryLock, FileHandle, Lock, OwnedBytes, TerminatingWrite,
24-
WatchCallback, WatchHandle, WritePtr,
24+
WatchCallback, WatchHandle,
2525
};
2626

2727
pub type ArcBytes = Arc<dyn Deref<Target = [u8]> + Send + Sync + 'static>;
@@ -413,7 +413,7 @@ impl Directory for MmapDirectory {
413413
.map_err(|io_err| OpenReadError::wrap_io_error(io_err, path.to_path_buf()))
414414
}
415415

416-
fn open_write(&self, path: &Path) -> Result<WritePtr, OpenWriteError> {
416+
fn open_write_inner(&self, path: &Path) -> Result<Box<dyn TerminatingWrite>, OpenWriteError> {
417417
debug!("Open Write {:?}", path);
418418
let full_path = self.resolve_path(path);
419419

@@ -443,7 +443,7 @@ impl Directory for MmapDirectory {
443443
// sync_directory() is called.
444444

445445
let writer = SafeFileWriter::new(file);
446-
Ok(BufWriter::new(Box::new(writer)))
446+
Ok(Box::new(writer))
447447
}
448448

449449
fn atomic_read(&self, path: &Path) -> Result<Vec<u8>, OpenReadError> {

src/directory/ram_directory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::HashMap;
2-
use std::io::{self, BufWriter, Cursor, Write};
2+
use std::io::{self, Cursor, Write};
33
use std::path::{Path, PathBuf};
44
use std::sync::{Arc, RwLock};
55
use std::{fmt, result};
@@ -11,7 +11,7 @@ use crate::core::META_FILEPATH;
1111
use crate::directory::error::{DeleteError, OpenReadError, OpenWriteError};
1212
use crate::directory::{
1313
AntiCallToken, Directory, FileSlice, TerminatingWrite, WatchCallback, WatchCallbackList,
14-
WatchHandle, WritePtr,
14+
WatchHandle,
1515
};
1616

1717
/// Writer associated with the [`RamDirectory`].
@@ -197,7 +197,7 @@ impl Directory for RamDirectory {
197197
.exists(path))
198198
}
199199

200-
fn open_write(&self, path: &Path) -> Result<WritePtr, OpenWriteError> {
200+
fn open_write_inner(&self, path: &Path) -> Result<Box<dyn TerminatingWrite>, OpenWriteError> {
201201
let mut fs = self.fs.write().unwrap();
202202
let path_buf = PathBuf::from(path);
203203
let vec_writer = VecWriter::new(path_buf.clone(), self.clone());
@@ -206,7 +206,7 @@ impl Directory for RamDirectory {
206206
if exists {
207207
Err(OpenWriteError::FileAlreadyExists(path_buf))
208208
} else {
209-
Ok(BufWriter::new(Box::new(vec_writer)))
209+
Ok(Box::new(vec_writer))
210210
}
211211
}
212212

0 commit comments

Comments
 (0)