Skip to content

Commit

Permalink
feat: Open file with custom filename cache (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
AzHicham authored Apr 11, 2024
1 parent 2185b7b commit c7d0353
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
5 changes: 3 additions & 2 deletions cpp/philipsslide.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ std::unique_ptr<Facade> PhilipsEngine::facade(std::string const& input) const {
// File properties
Facade::Facade(ISyntaxFacade& facade) : _facade(facade) {}

void Facade::open(rust::Str url, rust::Str container) const {
void Facade::open(rust::Str url, rust::Str container, rust::Str cache_filename) const {
std::string _url(url);
std::string _container(container);
_facade.open(_url, _container, std::ios::in | std::ios::binary, "");
std::string _cache_filename(cache_filename);
_facade.open(_url, _container, std::ios::in | std::ios::binary, _cache_filename);
}

void Facade::close() const { _facade.close(); }
Expand Down
2 changes: 1 addition & 1 deletion cpp/philipsslide.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Facade {
public:
Facade(ISyntaxFacade& facade);

void open(rust::Str url, rust::Str container) const;
void open(rust::Str url, rust::Str container, rust::Str cache_filename) const;
void close() const;
size_t numImages() const;
std::string const& iSyntaxFileVersion() const;
Expand Down
2 changes: 1 addition & 1 deletion src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) mod ffi {
fn facade(self: &PhilipsEngine, input: &CxxString) -> Result<UniquePtr<Facade>>;

// Facade properties
fn open(self: &Facade, url: &str, container: &str) -> Result<()>;
fn open(self: &Facade, url: &str, container: &str, cache_filename: &str) -> Result<()>;
fn close(self: &Facade) -> Result<()>;
fn numImages(self: &Facade) -> Result<usize>;
fn iSyntaxFileVersion(self: &Facade) -> Result<&CxxString>;
Expand Down
11 changes: 8 additions & 3 deletions src/facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ impl<'a> Drop for Facade<'a> {
/// The facade allow file manipulation & file information retrieval
/// NOTE: Philips Engine and all internal objects are not thread safe
impl<'a> Facade<'a> {
/// Open an ISyntax file through a facade
pub(crate) fn open<P: AsRef<Path>>(
/// Open an ISyntax file through a facade and specify a cache file
/// if the container allows it
pub(crate) fn open_with_cache_file<P: AsRef<Path>, R: AsRef<Path>>(
&self,
filename: P,
container: &ContainerName,
cache_filename: R,
) -> Result<()> {
let filename = filename.as_ref().display().to_string();
Ok(self.inner.open(&filename, container.as_str())?)
let cache_filename = cache_filename.as_ref().display().to_string();
Ok(self
.inner
.open(&filename, container.as_str(), &cache_filename)?)
}

// close close hold by the facade
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ pub struct View<'a> {
_lifetime: PhantomData<&'a ()>, // Note: Represent Image Lifetime
}

#[derive(Debug, Clone)]
pub enum ImageType {
WSI,
MacroImage,
LabelImage,
}

#[derive(Debug, Clone)]
pub enum ContainerName {
Default,
Ficom,
Expand Down
15 changes: 14 additions & 1 deletion src/pixel_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ impl PhilipsEngine {
/// This facade is a handle to a file
/// May fail if the fail cannot be opened
pub fn facade<P: AsRef<Path>>(&self, filename: P, container: &ContainerName) -> Result<Facade> {
self.facade_with_cache_file(filename, container, "")
}

/// Create a new instance of Facade
/// A Facade is a reference to a Philips Engine internal object
/// This facade is a handle to a file
/// May fail if the fail cannot be opened
pub fn facade_with_cache_file<P: AsRef<Path>, R: AsRef<Path>>(
&self,
filename: P,
container: &ContainerName,
cache_filename: R,
) -> Result<Facade> {
let facade_id = rand::thread_rng().gen::<u64>().to_string();
let_cxx_string!(facade_id = facade_id);
let facade = Facade {
inner: self.inner.facade(&facade_id)?,
_lifetime: Default::default(),
};
facade.open(filename, container)?;
facade.open_with_cache_file(filename, container, cache_filename)?;
Ok(facade)
}

Expand Down
22 changes: 21 additions & 1 deletion tests/test_facade.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod fixture;

use fixture::{missing_file, sample, sample_i2syntax, unsupported_file};
use std::path::Path;
use std::path::{Path, PathBuf};

use philips_isyntax_rs::{ContainerName, PhilipsEngine};
use rstest::rstest;
Expand Down Expand Up @@ -157,3 +157,23 @@ fn test_multiple_file(#[case] filename: &Path) {
assert_eq!(facade2.isyntax_file_version().unwrap(), "100.5");
assert_eq!(facade2.num_images().unwrap(), 3);
}

#[rstest]
#[case(sample())]
fn test_facade_with_cache_file(#[case] filename: &Path) {
let engine = PhilipsEngine::new();

let cache_file = PathBuf::from("/tmp/sample-cache-file.fic");
assert!(!cache_file.exists());

let facade = engine
.facade_with_cache_file(
filename,
&ContainerName::CachingFicom,
"/tmp/sample-cache-file.fic",
)
.expect("Cannot open file");
assert_eq!(facade.isyntax_file_version().unwrap(), "100.5");

assert!(cache_file.exists());
}

1 comment on commit c7d0353

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

philips-isyntax-rs Benchmark

Benchmark suite Current: c7d0353 Previous: a068200 Ratio
philips_i2syntax_read_region_256_lvl_0 1333203 ns/iter (± 152428) 1324901 ns/iter (± 193732) 1.01
philips_i2syntax_read_region_256_lvl_1 1130790 ns/iter (± 35438) 1130232 ns/iter (± 42253) 1.00
philips_i2syntax_read_region_512_lvl_0 1537315 ns/iter (± 169786) 1519983 ns/iter (± 56156) 1.01
philips_i2syntax_read_region_512_lvl_1 7275806 ns/iter (± 228918) 7300408 ns/iter (± 187292) 1.00
philips_read_region_256_lvl_0 487003 ns/iter (± 32812) 486078 ns/iter (± 41911) 1.00
philips_read_region_256_lvl_1 1219479 ns/iter (± 83153) 1223099 ns/iter (± 43039) 1.00
philips_read_region_512_lvl_0 3495476 ns/iter (± 252105) 3510218 ns/iter (± 145256) 1.00
philips_read_region_512_lvl_1 2717976 ns/iter (± 89702) 2699333 ns/iter (± 65245) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.