Skip to content

Commit f986704

Browse files
authored
Merge pull request #1 from trueleo/once_cell
use derive_more instead of manual impl
2 parents c7e2f16 + 80e01ca commit f986704

File tree

3 files changed

+13
-43
lines changed

3 files changed

+13
-43
lines changed

server/src/metadata.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use arrow_schema::Schema;
2020
use once_cell::sync::Lazy;
2121
use std::collections::HashMap;
22-
use std::ops::{Deref, DerefMut};
2322
use std::sync::{Arc, RwLock};
2423

2524
use crate::alerts::Alerts;
@@ -29,25 +28,14 @@ use crate::stats::{Stats, StatsCounter};
2928
use crate::storage::{MergedRecordReader, ObjectStorage, StorageDir};
3029

3130
use self::error::stream_info::{CheckAlertError, LoadError, MetadataError};
31+
use derive_more::{Deref, DerefMut};
3232

3333
// TODO: make return type be of 'static lifetime instead of cloning
3434
// A read-write lock to allow multiple reads while and isolated write
35-
pub static STREAM_INFO: Lazy<StreamType> = Lazy::new(|| StreamType(RwLock::new(HashMap::new())));
35+
pub static STREAM_INFO: Lazy<StreamInfo> = Lazy::new(StreamInfo::default);
3636

37-
#[derive(Debug)]
38-
pub struct StreamType(RwLock<HashMap<String, LogStreamMetadata>>);
39-
40-
impl Deref for StreamType {
41-
type Target = RwLock<HashMap<String, LogStreamMetadata>>;
42-
fn deref(&self) -> &Self::Target {
43-
&self.0
44-
}
45-
}
46-
impl DerefMut for StreamType {
47-
fn deref_mut(&mut self) -> &mut Self::Target {
48-
&mut self.0
49-
}
50-
}
37+
#[derive(Debug, Deref, DerefMut, Default)]
38+
pub struct StreamInfo(RwLock<HashMap<String, LogStreamMetadata>>);
5139

5240
#[derive(Debug)]
5341
pub struct LogStreamMetadata {
@@ -75,7 +63,7 @@ pub const LOCK_EXPECT: &str = "no method in metadata should panic while holding
7563
// 3. When a stream is deleted (remove the entry from the map)
7664
// 4. When first event is sent to stream (update the schema)
7765
// 5. When set alert API is called (update the alert)
78-
impl StreamType {
66+
impl StreamInfo {
7967
pub async fn check_alerts(&self, event: &Event) -> Result<(), CheckAlertError> {
8068
let map = self.read().expect(LOCK_EXPECT);
8169
let meta = map

server/src/storage.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ use crate::utils;
2626
use chrono::{Local, NaiveDateTime, Timelike, Utc};
2727
use datafusion::arrow::error::ArrowError;
2828
use datafusion::parquet::errors::ParquetError;
29+
use derive_more::{Deref, DerefMut};
2930
use once_cell::sync::Lazy;
3031

3132
use std::collections::HashMap;
32-
use std::fmt::{self, Debug, Formatter};
33+
use std::fmt::Debug;
3334
use std::fs::create_dir_all;
34-
use std::ops::{Deref, DerefMut};
3535
use std::path::{Path, PathBuf};
3636
use std::sync::Mutex;
3737

@@ -183,32 +183,13 @@ async fn create_remote_metadata(metadata: &StorageMetadata) -> Result<(), Object
183183
client.put_metadata(metadata).await
184184
}
185185

186-
pub static CACHED_FILES: Lazy<CachedFilesInnerType> =
187-
Lazy::new(|| CachedFilesInnerType(Mutex::new(FileTable::new())));
186+
pub static CACHED_FILES: Lazy<CachedFiles> =
187+
Lazy::new(|| CachedFiles(Mutex::new(FileTable::new())));
188188

189-
pub struct CachedFilesInnerType(Mutex<FileTable<FileLink>>);
189+
#[derive(Debug, Deref, DerefMut)]
190+
pub struct CachedFiles(Mutex<FileTable<FileLink>>);
190191

191-
impl Deref for CachedFilesInnerType {
192-
type Target = Mutex<FileTable<FileLink>>;
193-
194-
fn deref(&self) -> &Self::Target {
195-
&self.0
196-
}
197-
}
198-
199-
impl DerefMut for CachedFilesInnerType {
200-
fn deref_mut(&mut self) -> &mut Self::Target {
201-
&mut self.0
202-
}
203-
}
204-
205-
impl Debug for CachedFilesInnerType {
206-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
207-
f.write_str("CachedFilesInnerType { __private_field: () }")
208-
}
209-
}
210-
211-
impl CachedFilesInnerType {
192+
impl CachedFiles {
212193
pub fn track_parquet(&self) {
213194
let mut table = self.lock().expect("no poisoning");
214195
STREAM_INFO

server/src/storage/file_link.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl Link for FileLink {
7070
}
7171
}
7272

73+
#[derive(Debug)]
7374
pub struct FileTable<L: Link + Default> {
7475
inner: HashMap<PathBuf, L>,
7576
}

0 commit comments

Comments
 (0)