Skip to content

Commit

Permalink
feat: rename cache
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Oct 1, 2022
1 parent 44dd657 commit ce238b4
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/integration_test_resources_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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))
Expand Down
14 changes: 7 additions & 7 deletions src/in_memory_cache.rs → src/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ 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<String, String>,
kv_writer: Arc<Mutex<WriteHandle<String, String>>>,
}

/// Initializes a thread safe in memory cache
pub fn new() -> InMemoryCache {
pub fn new() -> KvStore {
let (kv_reader, kv_writer) = evmap::new::<String, String>();
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
/// * `key` - The key to check
///
/// # 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);
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 🚀");
Expand Down
4 changes: 2 additions & 2 deletions src/resource_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<String>,
kv_reader: web::Data<ReadHandle<String, String>>,
geo_location_cache: web::Data<InMemoryCache>,
geo_location_cache: web::Data<KvStore>,
) -> HttpResponse {
let resource = kv_reader
.get_one(resources_id.as_str())
Expand Down
12 changes: 3 additions & 9 deletions src/resource_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -47,10 +47,7 @@ pub fn get_all(kv_reader: &ReadHandle<String, String>) -> Vec<String> {

/// 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
Expand Down Expand Up @@ -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<String> {
async fn get_city_name(resource: RemoteResource, geo_location_cache: &KvStore) -> Option<String> {
let resource_location = resource.location?;
let resource_location_string = resource_location.to_string();

Expand Down
16 changes: 8 additions & 8 deletions src/resource_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -29,9 +29,7 @@ impl ResourceReader {
/// Returns the resource file data
pub fn read_resource_data(&self, resource: &RemoteResource) -> Vec<u8> {
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![]
Expand All @@ -41,16 +39,18 @@ impl ResourceReader {

/// Returns all available resources from the filesystem
pub fn list_all_resources(&self) -> Vec<RemoteResource> {
let local_resources: Vec<RemoteResource> = self.local_resource_paths
let local_resources: Vec<RemoteResource> = 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<RemoteResource> = self.samba_resource_paths
let samba_resources: Vec<RemoteResource> = 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();
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn read_all_local_files_recursive(folder_path: &PathBuf) -> Vec<RemoteResour

/// Reads all files of a samba folder and returns all found resources
/// The folder is recursively searched
pub fn read_all_samba_files_recursive(folder_path: &PathBuf) -> Vec<RemoteResource> {
pub fn read_all_samba_files_recursive(_folder_path: &PathBuf) -> Vec<RemoteResource> {
// TODO: implement me
vec![]
}
Expand Down
5 changes: 3 additions & 2 deletions src/resource_reader_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit ce238b4

Please sign in to comment.