Skip to content

Commit

Permalink
Add fallback backend for all unsupported platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Nov 5, 2023
1 parent 7da7eea commit 4ba570a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
48 changes: 48 additions & 0 deletions src/tree_store/page_store/file_backend/fallback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::{DatabaseError, Result, StorageBackend};
use std::fs::File;
use std::io;
use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::Mutex;

/// Stores a database as a file on-disk.
#[derive(Debug)]
pub struct FileBackend {
file: Mutex<File>,
}

impl FileBackend {
/// Creates a new backend which stores data to the given file.
pub fn new(file: File) -> Result<Self, DatabaseError> {
Self {
file: Mutex::new(file),
}
}
}

impl StorageBackend for FileBackend {
fn len(&self) -> Result<u64, io::Error> {
Ok(self.file.lock().unwrap().metadata()?.len())
}

fn read(&self, offset: u64, len: usize) -> Result<Vec<u8>, io::Error> {
let mut result = vec![0; len];
let file = self.file.lock().unwrap();
file.seek(SeekFrom::Start(offset))?;
file.read_exact(&mut result)?;
Ok(result)
}

fn set_len(&self, len: u64) -> Result<(), io::Error> {
self.file.lock().unwrap().set_len(len)
}

fn sync_data(&self, _eventual: bool) -> Result<(), io::Error> {
self.file.lock().unwrap().sync_data()
}

fn write(&self, offset: u64, data: &[u8]) -> Result<(), io::Error> {
let file = self.file.lock().unwrap();
file.seek(SeekFrom::Start(offset))?;
file.write_all(data)
}
}
4 changes: 2 additions & 2 deletions src/tree_store/page_store/file_backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ mod windows;
pub use windows::FileBackend;

#[cfg(not(any(windows, unix, target_os = "wasi")))]
mod unsupported;
mod fallback;
#[cfg(not(any(windows, unix, target_os = "wasi")))]
pub use unsupported::FileBackend;
pub use fallback::FileBackend;
36 changes: 0 additions & 36 deletions src/tree_store/page_store/file_backend/unsupported.rs

This file was deleted.

0 comments on commit 4ba570a

Please sign in to comment.