Skip to content

Commit 92fba76

Browse files
author
Devdutt Shenoi
committed
refactor: with less code
1 parent 6340a8e commit 92fba76

File tree

1 file changed

+61
-60
lines changed

1 file changed

+61
-60
lines changed

src/handlers/http/logstream.rs

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,15 @@ pub async fn get_retention(stream_name: Path<String>) -> Result<impl Responder,
165165
// For query mode, if the stream not found in memory map,
166166
//check if it exists in the storage
167167
//create stream and schema from storage
168-
if PARSEABLE.options.mode == Mode::Query {
169-
match PARSEABLE
170-
.create_stream_and_schema_from_storage(&stream_name)
171-
.await
172-
{
173-
Ok(true) => {}
174-
Ok(false) | Err(_) => return Err(StreamNotFound(stream_name.clone()).into()),
175-
}
168+
if PARSEABLE.options.mode == Mode::Query
169+
&& matches!(
170+
PARSEABLE
171+
.create_stream_and_schema_from_storage(&stream_name)
172+
.await,
173+
Ok(false) | Err(_)
174+
)
175+
{
176+
return Err(StreamNotFound(stream_name.clone()).into());
176177
}
177178

178179
let retention = PARSEABLE
@@ -191,17 +192,18 @@ pub async fn put_retention(
191192
// For query mode, if the stream not found in memory map,
192193
//check if it exists in the storage
193194
//create stream and schema from storage
194-
if PARSEABLE.options.mode == Mode::Query {
195-
match PARSEABLE
196-
.create_stream_and_schema_from_storage(&stream_name)
197-
.await
198-
{
199-
Ok(true) => {}
200-
Ok(false) | Err(_) => return Err(StreamNotFound(stream_name.clone()).into()),
201-
}
195+
if PARSEABLE.options.mode == Mode::Query
196+
&& matches!(
197+
PARSEABLE
198+
.create_stream_and_schema_from_storage(&stream_name)
199+
.await,
200+
Ok(false) | Err(_)
201+
)
202+
{
203+
return Err(StreamNotFound(stream_name.clone()).into());
202204
}
203-
let stream = PARSEABLE.get_stream(&stream_name)?;
204205

206+
let stream = PARSEABLE.get_stream(&stream_name)?;
205207
let retention: Retention = match serde_json::from_value(json) {
206208
Ok(retention) => retention,
207209
Err(err) => return Err(StreamError::InvalidRetentionConfig(err)),
@@ -231,15 +233,14 @@ pub async fn get_stats(
231233
// For query mode, if the stream not found in memory map,
232234
//check if it exists in the storage
233235
//create stream and schema from storage
234-
if PARSEABLE.options.mode == Mode::Query {
235-
match PARSEABLE
236-
.create_stream_and_schema_from_storage(&stream_name)
237-
.await
238-
{
239-
Ok(true) => {}
240-
Ok(false) | Err(_) => return Err(StreamNotFound(stream_name.clone()).into()),
241-
}
242-
} else {
236+
if PARSEABLE.options.mode != Mode::Query
237+
|| matches!(
238+
PARSEABLE
239+
.create_stream_and_schema_from_storage(&stream_name)
240+
.await,
241+
Ok(false) | Err(_)
242+
)
243+
{
243244
return Err(StreamNotFound(stream_name).into());
244245
}
245246
}
@@ -325,15 +326,14 @@ pub async fn get_stats(
325326
pub async fn get_stream_info(stream_name: Path<String>) -> Result<impl Responder, StreamError> {
326327
let stream_name = stream_name.into_inner();
327328
if !PARSEABLE.streams.contains(&stream_name) {
328-
if PARSEABLE.options.mode == Mode::Query {
329-
match PARSEABLE
330-
.create_stream_and_schema_from_storage(&stream_name)
331-
.await
332-
{
333-
Ok(true) => {}
334-
Ok(false) | Err(_) => return Err(StreamNotFound(stream_name.clone()).into()),
335-
}
336-
} else {
329+
if PARSEABLE.options.mode != Mode::Query
330+
|| matches!(
331+
PARSEABLE
332+
.create_stream_and_schema_from_storage(&stream_name)
333+
.await,
334+
Ok(false) | Err(_)
335+
)
336+
{
337337
return Err(StreamNotFound(stream_name).into());
338338
}
339339
}
@@ -385,14 +385,15 @@ pub async fn put_stream_hot_tier(
385385
// For query mode, if the stream not found in memory map,
386386
//check if it exists in the storage
387387
//create stream and schema from storage
388-
if PARSEABLE.options.mode == Mode::Query {
389-
match PARSEABLE
390-
.create_stream_and_schema_from_storage(&stream_name)
391-
.await
392-
{
393-
Ok(true) => {}
394-
Ok(false) | Err(_) => return Err(StreamNotFound(stream_name.clone()).into()),
395-
}
388+
if PARSEABLE.options.mode == Mode::Query
389+
&& matches!(
390+
PARSEABLE
391+
.create_stream_and_schema_from_storage(&stream_name)
392+
.await,
393+
Ok(false) | Err(_)
394+
)
395+
{
396+
return Err(StreamNotFound(stream_name.clone()).into());
396397
}
397398

398399
let stream = PARSEABLE.get_stream(&stream_name)?;
@@ -439,15 +440,14 @@ pub async fn get_stream_hot_tier(stream_name: Path<String>) -> Result<impl Respo
439440
// For query mode, if the stream not found in memory map,
440441
//check if it exists in the storage
441442
//create stream and schema from storage
442-
if PARSEABLE.options.mode == Mode::Query {
443-
match PARSEABLE
444-
.create_stream_and_schema_from_storage(&stream_name)
445-
.await
446-
{
447-
Ok(true) => {}
448-
Ok(false) | Err(_) => return Err(StreamNotFound(stream_name.clone()).into()),
449-
}
450-
} else {
443+
if PARSEABLE.options.mode != Mode::Query
444+
|| matches!(
445+
PARSEABLE
446+
.create_stream_and_schema_from_storage(&stream_name)
447+
.await,
448+
Ok(false) | Err(_)
449+
)
450+
{
451451
return Err(StreamNotFound(stream_name).into());
452452
}
453453
}
@@ -468,14 +468,15 @@ pub async fn delete_stream_hot_tier(
468468
// For query mode, if the stream not found in memory map,
469469
//check if it exists in the storage
470470
//create stream and schema from storage
471-
if PARSEABLE.options.mode == Mode::Query {
472-
match PARSEABLE
473-
.create_stream_and_schema_from_storage(&stream_name)
474-
.await
475-
{
476-
Ok(true) => {}
477-
Ok(false) | Err(_) => return Err(StreamNotFound(stream_name.clone()).into()),
478-
}
471+
if PARSEABLE.options.mode == Mode::Query
472+
&& matches!(
473+
PARSEABLE
474+
.create_stream_and_schema_from_storage(&stream_name)
475+
.await,
476+
Ok(false) | Err(_)
477+
)
478+
{
479+
return Err(StreamNotFound(stream_name.clone()).into());
479480
}
480481

481482
if PARSEABLE.get_stream(&stream_name)?.get_stream_type() == StreamType::Internal {

0 commit comments

Comments
 (0)