Skip to content

Commit 41f4cbd

Browse files
authored
refactor: suggestions (#5)
* refactor: unnest * refactor: don't branch * don't await collection * refactor: string construction
1 parent e877355 commit 41f4cbd

File tree

4 files changed

+28
-49
lines changed

4 files changed

+28
-49
lines changed

src/correlation/mod.rs renamed to src/correlation.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,11 @@ impl Correlation {
5454
.into_iter()
5555
.flat_map(|(_, correlations_bytes)| correlations_bytes)
5656
.filter_map(|correlation| {
57-
if correlation.is_empty() {
58-
None
59-
} else {
60-
match serde_json::from_slice(&correlation) {
61-
Ok(correlation_config) => Some(correlation_config),
62-
Err(e) => {
63-
error!("Unable to load correlation: {e}");
64-
None
65-
}
66-
}
67-
}
57+
serde_json::from_slice(&correlation)
58+
.inspect_err(|e| {
59+
error!("Unable to load correlation: {e}");
60+
})
61+
.ok()
6862
})
6963
.collect();
7064

@@ -107,13 +101,11 @@ impl Correlation {
107101
.find(|c| c.id == correlation_id && c.user_id == user_id)
108102
.cloned();
109103

110-
if let Some(c) = correlation {
111-
Ok(c)
112-
} else {
113-
Err(CorrelationError::AnyhowError(anyhow::Error::msg(format!(
104+
correlation.ok_or_else(|| {
105+
CorrelationError::AnyhowError(anyhow::Error::msg(format!(
114106
"Unable to find correlation with ID- {correlation_id}"
115-
))))
116-
}
107+
)))
108+
})
117109
}
118110

119111
pub async fn update(&self, correlation: &CorrelationConfig) -> Result<(), CorrelationError> {

src/storage/azure_blob.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,8 @@ impl ObjectStorage for BlobStore {
678678
.map(|name| name.as_ref().to_string())
679679
.collect::<Vec<_>>();
680680
for user in users {
681-
let user_dashboard_path = object_store::path::Path::from(format!(
682-
"{}/{}/{}",
683-
USERS_ROOT_DIR, user, "dashboards"
684-
));
681+
let user_dashboard_path =
682+
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/dashboards"));
685683
let dashboards_path = RelativePathBuf::from(&user_dashboard_path);
686684
let dashboard_bytes = self
687685
.get_objects(
@@ -716,10 +714,8 @@ impl ObjectStorage for BlobStore {
716714
.map(|name| name.as_ref().to_string())
717715
.collect::<Vec<_>>();
718716
for user in users {
719-
let user_filters_path = object_store::path::Path::from(format!(
720-
"{}/{}/{}",
721-
USERS_ROOT_DIR, user, "filters"
722-
));
717+
let user_filters_path =
718+
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/filters",));
723719
let resp = self
724720
.client
725721
.list_with_delimiter(Some(&user_filters_path))
@@ -767,10 +763,8 @@ impl ObjectStorage for BlobStore {
767763
.map(|name| name.as_ref().to_string())
768764
.collect::<Vec<_>>();
769765
for user in users {
770-
let user_correlation_path = object_store::path::Path::from(format!(
771-
"{}/{}/{}",
772-
USERS_ROOT_DIR, user, "correlations"
773-
));
766+
let user_correlation_path =
767+
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/correlations"));
774768
let correlations_path = RelativePathBuf::from(&user_correlation_path);
775769
let correlation_bytes = self
776770
.get_objects(

src/storage/localfs.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use async_trait::async_trait;
2727
use bytes::Bytes;
2828
use datafusion::{datasource::listing::ListingTableUrl, execution::runtime_env::RuntimeEnvBuilder};
2929
use fs_extra::file::CopyOptions;
30-
use futures::{stream::FuturesUnordered, TryStreamExt};
30+
use futures::{stream::FuturesUnordered, StreamExt, TryStreamExt};
3131
use relative_path::{RelativePath, RelativePathBuf};
3232
use tokio::fs::{self, DirEntry};
3333
use tokio_stream::wrappers::ReadDirStream;
@@ -430,17 +430,16 @@ impl ObjectStorage for LocalFS {
430430
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError> {
431431
let mut correlations: HashMap<RelativePathBuf, Vec<Bytes>> = HashMap::new();
432432
let users_root_path = self.root.join(USERS_ROOT_DIR);
433-
let directories = ReadDirStream::new(fs::read_dir(&users_root_path).await?);
434-
let users: Vec<DirEntry> = directories.try_collect().await?;
435-
for user in users {
433+
let mut directories = ReadDirStream::new(fs::read_dir(&users_root_path).await?);
434+
while let Some(user) = directories.next().await {
435+
let user = user?;
436436
if !user.path().is_dir() {
437437
continue;
438438
}
439439
let correlations_path = users_root_path.join(user.path()).join("correlations");
440-
let directories = ReadDirStream::new(fs::read_dir(&correlations_path).await?);
441-
let correlations_files: Vec<DirEntry> = directories.try_collect().await?;
442-
for correlation in correlations_files {
443-
let correlation_absolute_path = correlation.path();
440+
let mut files = ReadDirStream::new(fs::read_dir(&correlations_path).await?);
441+
while let Some(correlation) = files.next().await {
442+
let correlation_absolute_path = correlation?.path();
444443
let file = fs::read(correlation_absolute_path.clone()).await?;
445444
let correlation_relative_path = correlation_absolute_path
446445
.strip_prefix(self.root.as_path())

src/storage/s3.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,8 @@ impl ObjectStorage for S3 {
811811
.map(|name| name.as_ref().to_string())
812812
.collect::<Vec<_>>();
813813
for user in users {
814-
let user_dashboard_path = object_store::path::Path::from(format!(
815-
"{}/{}/{}",
816-
USERS_ROOT_DIR, user, "dashboards"
817-
));
814+
let user_dashboard_path =
815+
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/dashboards"));
818816
let dashboards_path = RelativePathBuf::from(&user_dashboard_path);
819817
let dashboard_bytes = self
820818
.get_objects(
@@ -849,10 +847,8 @@ impl ObjectStorage for S3 {
849847
.map(|name| name.as_ref().to_string())
850848
.collect::<Vec<_>>();
851849
for user in users {
852-
let user_filters_path = object_store::path::Path::from(format!(
853-
"{}/{}/{}",
854-
USERS_ROOT_DIR, user, "filters"
855-
));
850+
let user_filters_path =
851+
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/filters",));
856852
let resp = self
857853
.client
858854
.list_with_delimiter(Some(&user_filters_path))
@@ -900,10 +896,8 @@ impl ObjectStorage for S3 {
900896
.map(|name| name.as_ref().to_string())
901897
.collect::<Vec<_>>();
902898
for user in users {
903-
let user_correlation_path = object_store::path::Path::from(format!(
904-
"{}/{}/{}",
905-
USERS_ROOT_DIR, user, "correlations"
906-
));
899+
let user_correlation_path =
900+
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/correlations",));
907901
let correlations_path = RelativePathBuf::from(&user_correlation_path);
908902
let correlation_bytes = self
909903
.get_objects(

0 commit comments

Comments
 (0)