-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eccff6e
commit 6e45e71
Showing
9 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::app_state::AppState; | ||
use crate::error::CustomRouteResult; | ||
use crate::identifier::is_valid_table_identifier; | ||
use crate::response::build_response; | ||
use actix_web::http::StatusCode; | ||
use actix_web::{ | ||
post, | ||
web::{self, Path}, | ||
HttpResponse, | ||
}; | ||
use serde_json::json; | ||
|
||
#[post("/v1/table/{name}/count")] | ||
pub async fn handler( | ||
path: Path<String>, | ||
app_state: web::Data<AppState>, | ||
) -> CustomRouteResult<HttpResponse> { | ||
let before = std::time::Instant::now(); | ||
|
||
let table_name = path.into_inner(); | ||
|
||
if table_name.starts_with('_') { | ||
return Ok(build_response( | ||
before.elapsed(), | ||
StatusCode::BAD_REQUEST, | ||
"Invalid table name", | ||
&json!(null), | ||
)); | ||
} | ||
|
||
if !is_valid_table_identifier(&table_name) { | ||
return Ok(build_response( | ||
before.elapsed(), | ||
StatusCode::BAD_REQUEST, | ||
"Invalid table name", | ||
&json!(null), | ||
)); | ||
} | ||
|
||
let tables = app_state.tables.read().await; | ||
|
||
if let Some(table) = tables.get(&table_name) { | ||
let (row_count, cell_count) = { | ||
let table = table.clone(); | ||
|
||
tokio::task::spawn_blocking(move || table.count()) | ||
.await | ||
.expect("should join") | ||
}?; | ||
|
||
let dur = before.elapsed(); | ||
|
||
let micros_total = dur.as_micros(); | ||
|
||
let micros_per_row = if row_count == 0 { | ||
None | ||
} else { | ||
Some(micros_total / row_count as u128) | ||
}; | ||
|
||
Ok(build_response( | ||
dur, | ||
StatusCode::OK, | ||
"Count successful", | ||
&json!({ | ||
"row_count": row_count, | ||
"cell_count": cell_count, | ||
"micros": micros_total, | ||
"micros_per_row": micros_per_row, | ||
}), | ||
)) | ||
} else { | ||
Ok(build_response( | ||
before.elapsed(), | ||
StatusCode::NOT_FOUND, | ||
"Table not found", | ||
&json!(null), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod count; | ||
pub mod create_column_family; | ||
pub mod create_table; | ||
pub mod delete_row; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters