From ce238b46e7b71d15aadbcb2d779f9afa9d2c5848 Mon Sep 17 00:00:00 2001 From: Rouven Hi Date: Sat, 1 Oct 2022 11:57:02 +0200 Subject: [PATCH] feat: rename cache --- src/integration_test_resources_api.rs | 4 ++-- src/{in_memory_cache.rs => kv_store.rs} | 14 +++++++------- src/main.rs | 4 ++-- src/resource_endpoint.rs | 4 ++-- src/resource_processor.rs | 12 +++--------- src/resource_reader.rs | 16 ++++++++-------- src/resource_reader_test.rs | 5 +++-- 7 files changed, 27 insertions(+), 32 deletions(-) rename src/{in_memory_cache.rs => kv_store.rs} (90%) diff --git a/src/integration_test_resources_api.rs b/src/integration_test_resources_api.rs index 4654b23..9955891 100644 --- a/src/integration_test_resources_api.rs +++ b/src/integration_test_resources_api.rs @@ -13,7 +13,7 @@ use rand::Rng; use crate::geo_location::GeoLocation; use crate::resource_reader::{RemoteResource, ResourceReader}; -use crate::{in_memory_cache, resource_endpoint, resource_processor, resource_reader, scheduler}; +use crate::{kv_store, resource_endpoint, resource_processor, resource_reader, scheduler}; const TEST_JPEG_EXIF_URL: &str = "https://raw.githubusercontent.com/ianare/exif-samples/master/jpg/gps/DSCN0010.jpg"; @@ -277,7 +277,7 @@ fn build_app( > { scheduler::init(); scheduler::fetch_resources(resource_reader.clone(), kv_writer_mutex.clone()); - let geo_location_cache = in_memory_cache::new(); + let geo_location_cache = kv_store::new(); App::new() .app_data(web::Data::new(kv_reader)) .app_data(web::Data::new(resource_reader)) diff --git a/src/in_memory_cache.rs b/src/kv_store.rs similarity index 90% rename from src/in_memory_cache.rs rename to src/kv_store.rs index 1a14e72..d51c6b6 100644 --- a/src/in_memory_cache.rs +++ b/src/kv_store.rs @@ -2,24 +2,24 @@ use std::sync::{Arc, Mutex}; use evmap::{ReadHandle, WriteHandle}; -/// Holds the cache reader and writer +/// Provides an in memory kev value reader and writer /// Writer is stored as an arc mutex for protected shared writing #[derive(Clone)] -pub struct InMemoryCache { +pub struct KvStore { kv_reader: ReadHandle, kv_writer: Arc>>, } /// Initializes a thread safe in memory cache -pub fn new() -> InMemoryCache { +pub fn new() -> KvStore { let (kv_reader, kv_writer) = evmap::new::(); - InMemoryCache { + KvStore { kv_reader, kv_writer: Arc::new(Mutex::new(kv_writer)), } } -impl InMemoryCache { +impl KvStore { /// Checks if the cache contains the given key /// Returns true if the key is present, false otherwise /// # Arguments @@ -27,8 +27,8 @@ impl InMemoryCache { /// /// # Example /// ``` - /// use in_memory_cache::InMemoryCache; - /// let mut in_memory_cache = InMemoryCache::init(); + /// use cache::Cache; + /// let mut cache = Cache::init(); /// let key = "key".to_string(); /// in_memory_cache.insert(key.clone(), "value".to_string()); /// assert_eq!(in_memory_cache.contains_key(key.as_str()), true); diff --git a/src/main.rs b/src/main.rs index d32d382..909e671 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ mod config_endpoint; mod exif_reader; mod geo_location; mod image_processor; -mod in_memory_cache; +mod kv_store; mod resource_endpoint; mod resource_processor; mod resource_reader; @@ -52,7 +52,7 @@ async fn main() -> std::io::Result<()> { scheduler::fetch_resources(resource_reader.clone(), kv_writer_mutex.clone()); // Initialize geo location cache - let geo_location_cache = in_memory_cache::new(); + let geo_location_cache = kv_store::new(); // Run the actual web server and hold the main thread here println!("Launching webserver 🚀"); diff --git a/src/resource_endpoint.rs b/src/resource_endpoint.rs index bd6a7ea..6ff7101 100644 --- a/src/resource_endpoint.rs +++ b/src/resource_endpoint.rs @@ -3,7 +3,7 @@ use actix_web::web; use actix_web::HttpResponse; use evmap::ReadHandle; -use crate::in_memory_cache::InMemoryCache; +use crate::kv_store::KvStore; use crate::resource_reader::{RemoteResource, ResourceReader}; use crate::{image_processor, resource_processor}; @@ -122,7 +122,7 @@ pub async fn get_resource_metadata_by_id( pub async fn get_resource_metadata_description_by_id( resources_id: web::Path, kv_reader: web::Data>, - geo_location_cache: web::Data, + geo_location_cache: web::Data, ) -> HttpResponse { let resource = kv_reader .get_one(resources_id.as_str()) diff --git a/src/resource_processor.rs b/src/resource_processor.rs index d9b60ec..003544d 100644 --- a/src/resource_processor.rs +++ b/src/resource_processor.rs @@ -6,7 +6,7 @@ use rand::Rng; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use crate::geo_location; -use crate::in_memory_cache::InMemoryCache; +use crate::kv_store::KvStore; use crate::resource_reader::RemoteResource; pub fn md5(string: &str) -> String { @@ -47,10 +47,7 @@ pub fn get_all(kv_reader: &ReadHandle) -> Vec { /// Builds the display value for the specified resource /// The display value contains the date and location of a resource -pub async fn build_display_value( - resource: RemoteResource, - geo_location_cache: &InMemoryCache, -) -> String { +pub async fn build_display_value(resource: RemoteResource, geo_location_cache: &KvStore) -> String { let mut display_value: String = String::new(); // Append taken date @@ -79,10 +76,7 @@ pub async fn build_display_value( /// Returns the city name for the specified resource /// The city name is taken from the cache, if available /// If not, the city name is taken from the geo location service -async fn get_city_name( - resource: RemoteResource, - geo_location_cache: &InMemoryCache, -) -> Option { +async fn get_city_name(resource: RemoteResource, geo_location_cache: &KvStore) -> Option { let resource_location = resource.location?; let resource_location_string = resource_location.to_string(); diff --git a/src/resource_reader.rs b/src/resource_reader.rs index 632429b..8c07b3f 100644 --- a/src/resource_reader.rs +++ b/src/resource_reader.rs @@ -11,9 +11,9 @@ use rayon::iter::IntoParallelRefIterator; use rayon::iter::ParallelIterator; use serde::{Deserialize, Serialize}; -use crate::{exif_reader, resource_processor}; use crate::geo_location::GeoLocation; use crate::image_processor::ImageOrientation; +use crate::{exif_reader, resource_processor}; /// A resource reader that reads available resources from the filesystem #[derive(Clone)] @@ -29,9 +29,7 @@ impl ResourceReader { /// Returns the resource file data pub fn read_resource_data(&self, resource: &RemoteResource) -> Vec { match resource.resource_type { - RemoteResourceType::Local => { - fs::read(resource.path.clone()).unwrap() - } + RemoteResourceType::Local => fs::read(resource.path.clone()).unwrap(), RemoteResourceType::Samba => { // TODO: implement me vec![] @@ -41,16 +39,18 @@ impl ResourceReader { /// Returns all available resources from the filesystem pub fn list_all_resources(&self) -> Vec { - let local_resources: Vec = self.local_resource_paths + let local_resources: Vec = self + .local_resource_paths .par_iter() .inspect(|x| println!(" ## local ## {x}")) // TODO: remove me .flat_map(|path| read_all_local_files_recursive(&PathBuf::from(path))) .map(|resource| exif_reader::fill_exif_data(&resource)) .collect(); - let samba_resources: Vec = self.samba_resource_paths + let samba_resources: Vec = self + .samba_resource_paths .par_iter() - .inspect(|x| println!(" ## remote ## {x}"))// TODO: remove me + .inspect(|x| println!(" ## remote ## {x}")) // TODO: remove me .flat_map(|path| read_all_samba_files_recursive(&PathBuf::from(path))) .map(|resource| exif_reader::fill_exif_data(&resource)) .collect(); @@ -150,7 +150,7 @@ pub fn read_all_local_files_recursive(folder_path: &PathBuf) -> Vec Vec { +pub fn read_all_samba_files_recursive(_folder_path: &PathBuf) -> Vec { // TODO: implement me vec![] } diff --git a/src/resource_reader_test.rs b/src/resource_reader_test.rs index cd9f4bc..c7c00e0 100644 --- a/src/resource_reader_test.rs +++ b/src/resource_reader_test.rs @@ -66,8 +66,9 @@ fn read_jpg_with_exif_image_resource() { create_test_image(&base_test_dir, "", test_image_name, TEST_JPEG_EXIF_URL); // WHEN reading resources from a folder - let resources_read = - exif_reader::fill_exif_data(&resource_reader::read_all_local_files_recursive(&base_test_dir)[0]); + let resources_read = exif_reader::fill_exif_data( + &resource_reader::read_all_local_files_recursive(&base_test_dir)[0], + ); // THEN the resource metadata should be correct assert_eq!(