-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback backend for all unsupported platforms
- Loading branch information
Showing
3 changed files
with
50 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.