Skip to content

Commit

Permalink
Update review-database to 0.24.0
Browse files Browse the repository at this point in the history
  • Loading branch information
minshao committed Feb 15, 2024
1 parent 57b0e98 commit bfaaf57
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ oinq = { git = "https://github.com/petabi/oinq.git", tag = "0.9.1" }
reqwest = { version = "0.11", default-features = false, features = [
"rustls-tls-native-roots",
] }
review-database = { git = "https://github.com/petabi/review-database.git", tag = "0.23.0" }
review-database = { git = "https://github.com/petabi/review-database.git", tag = "0.24.0" }
roxy = { git = "https://github.com/aicers/roxy.git", tag = "0.2.1" }
rustls = "0.21"
rustls-native-certs = "0.6"
Expand Down
23 changes: 16 additions & 7 deletions src/graphql/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@ impl ClusterMutation {
status: Option<ID>,
) -> Result<ID> {
let db = ctx.data::<Database>()?;
let id_to_i32 = |v: ID| v.as_str().parse().ok();

let status = status.and_then(id_to_i32);
db.update_cluster(
id.as_str().parse()?,
category.and_then(|v| v.as_str().parse().ok()),
qualifier.and_then(|v| v.as_str().parse().ok()),
status.and_then(|v| v.as_str().parse().ok()),
category.and_then(id_to_i32),
qualifier.and_then(id_to_i32),
status,
)
.await?;
Ok(id)
Expand Down Expand Up @@ -196,13 +199,19 @@ impl Cluster {
}

async fn qualifier(&self, ctx: &Context<'_>) -> Result<Qualifier> {
let db = ctx.data::<Database>()?;
Ok(db.load_qualifier(self.qualifier).await?.into())
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.qualifier_map();
Ok(map
.get(u32::try_from(self.qualifier).expect("invalid id"))?
.into())
}

async fn status(&self, ctx: &Context<'_>) -> Result<Status> {
let db = ctx.data::<Database>()?;
Ok(db.load_status(self.status).await?.into())
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.status_map();
Ok(map
.get(u32::try_from(self.status).expect("invalid id"))?
.into())
}
}

Expand Down
43 changes: 29 additions & 14 deletions src/graphql/qualifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use async_graphql::{
types::ID,
Context, Object, Result,
};
use review_database::{self as database, Database};
use database::Store;
use review_database::{self as database};
use std::sync::Arc;
use tokio::sync::RwLock;

#[derive(Default)]
pub(super) struct QualifierQuery;
Expand Down Expand Up @@ -44,18 +47,21 @@ impl QualifierMutation {
#[graphql(guard = "RoleGuard::new(Role::SystemAdministrator)
.or(RoleGuard::new(Role::SecurityAdministrator))")]
async fn add_qualifier(&self, ctx: &Context<'_>, description: String) -> Result<ID> {
let db = ctx.data::<Database>()?;
Ok(ID(db.add_qualifier(&description).await?.to_string()))
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.qualifier_map();
Ok(ID(map.insert(&description)?.to_string()))
}

/// Updates the given qualifier's description.
#[graphql(guard = "RoleGuard::new(Role::SystemAdministrator)
.or(RoleGuard::new(Role::SecurityAdministrator))")]
async fn update_qualifier(&self, ctx: &Context<'_>, id: ID, description: String) -> Result<ID> {
let db = ctx.data::<Database>()?;
db.update_qualifier(id.as_str().parse()?, &description)
.await?;
Ok(id)
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let mut map = db.qualifier_map();
let id: u32 = id.as_str().parse()?;
let old = map.get(id)?;
map.update(id, &old.description, &description)?;
Ok(ID(id.to_string()))
}
}

Expand Down Expand Up @@ -86,8 +92,9 @@ struct QualifierTotalCount;
impl QualifierTotalCount {
/// The total number of edges.
async fn total_count(&self, ctx: &Context<'_>) -> Result<i64> {
let db = ctx.data::<Database>()?;
Ok(db.count_qualifiers().await?)
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.qualifier_map();
Ok(i64::try_from(map.count()?)?)
}
}

Expand All @@ -98,18 +105,26 @@ async fn load(
first: Option<usize>,
last: Option<usize>,
) -> Result<Connection<String, Qualifier, QualifierTotalCount, EmptyFields>> {
let after = slicing::decode_cursor(&after)?;
let before = slicing::decode_cursor(&before)?;
let after = slicing::decode_cursor(&after)?.map(|(id, description)| {
let id = u32::try_from(id).expect("id out of range");
review_database::Qualifier { id, description }
});
let before = slicing::decode_cursor(&before)?.map(|(id, description)| {
let id = u32::try_from(id).expect("id out of range");
review_database::Qualifier { id, description }
});
let is_first = first.is_some();
let limit = slicing::limit(first, last)?;
let db = ctx.data::<Database>()?;
let rows = db.load_qualifiers(&after, &before, is_first, limit).await?;
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.qualifier_map();

let rows = map.get_range(after, before, is_first, limit + 1)?;
let (rows, has_previous, has_next) = slicing::page_info(is_first, limit, rows);
let mut connection =
Connection::with_additional_fields(has_previous, has_next, QualifierTotalCount);
connection.edges.extend(rows.into_iter().map(|row| {
let cursor = slicing::encode_cursor(row.id, &row.description);
let cursor =
slicing::encode_cursor(i32::try_from(row.id).expect("invalid id"), &row.description);
Edge::new(cursor, row.into())
}));
Ok(connection)
Expand Down
43 changes: 30 additions & 13 deletions src/graphql/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use async_graphql::{
types::ID,
Context, Object, Result,
};
use review_database::{self as database, Database};
use database::Store;
use review_database::{self as database};
use std::sync::Arc;
use tokio::sync::RwLock;

#[derive(Default)]
pub(super) struct StatusQuery;
Expand Down Expand Up @@ -44,17 +47,21 @@ impl StatusMutation {
#[graphql(guard = "RoleGuard::new(Role::SystemAdministrator)
.or(RoleGuard::new(Role::SecurityAdministrator))")]
async fn add_status(&self, ctx: &Context<'_>, description: String) -> Result<ID> {
let db = ctx.data::<Database>()?;
Ok(ID(db.add_status(&description).await?.to_string()))
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.status_map();
Ok(ID(map.insert(&description)?.to_string()))
}

/// Updates the given status's description.
#[graphql(guard = "RoleGuard::new(Role::SystemAdministrator)
.or(RoleGuard::new(Role::SecurityAdministrator))")]
async fn update_status(&self, ctx: &Context<'_>, id: ID, description: String) -> Result<ID> {
let db = ctx.data::<Database>()?;
db.update_status(id.as_str().parse()?, &description).await?;
Ok(id)
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let mut map = db.status_map();
let id: u32 = id.as_str().parse()?;
let old = map.get(id)?;
map.update(id, &old.description, &description)?;
Ok(ID(id.to_string()))
}
}

Expand Down Expand Up @@ -85,8 +92,9 @@ struct StatusTotalCount;
impl StatusTotalCount {
/// The total number of edges.
async fn total_count(&self, ctx: &Context<'_>) -> Result<i64> {
let db = ctx.data::<Database>()?;
Ok(db.count_statuses().await?)
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.status_map();
Ok(i64::try_from(map.count()?)?)
}
}

Expand All @@ -97,18 +105,27 @@ async fn load(
first: Option<usize>,
last: Option<usize>,
) -> Result<Connection<String, Status, StatusTotalCount, EmptyFields>> {
let after = slicing::decode_cursor(&after)?;
let before = slicing::decode_cursor(&before)?;
let after = slicing::decode_cursor(&after)?.map(|(id, description)| {
let id = u32::try_from(id).expect("id out of range");
review_database::Status { id, description }
});
let before = slicing::decode_cursor(&before)?.map(|(id, description)| {
let id = u32::try_from(id).expect("id out of range");
review_database::Status { id, description }
});
let is_first = first.is_some();
let limit = slicing::limit(first, last)?;
let db = ctx.data::<Database>()?;
let rows = db.load_statuses(&after, &before, is_first, limit).await?;
let db = ctx.data::<Arc<RwLock<Store>>>()?.read().await;
let map = db.status_map();

let rows = map.get_range(after, before, is_first, limit + 1)?;

let (rows, has_previous, has_next) = slicing::page_info(is_first, limit, rows);
let mut connection =
Connection::with_additional_fields(has_previous, has_next, StatusTotalCount);
connection.edges.extend(rows.into_iter().map(|row| {
let cursor = slicing::encode_cursor(row.id, &row.description);
let cursor =
slicing::encode_cursor(i32::try_from(row.id).expect("invalid id"), &row.description);
Edge::new(cursor, row.into())
}));
Ok(connection)
Expand Down
14 changes: 10 additions & 4 deletions src/graphql/tidb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ impl TidbMutation {
async fn insert_tidb(&self, ctx: &Context<'_>, dbfile: String) -> Result<TidbOutput> {
let tidb = database::Tidb::new(&dbfile)?;
let store = super::get_store(ctx).await?;
let (name, version) = tidb.insert(&store)?;
Ok(TidbOutput { name, version })
tidb.insert(&store)?;
Ok(TidbOutput {
name: tidb.name,
version: tidb.version,
})
}

/// Removes Tidb, returning the name and version of database that removed
Expand Down Expand Up @@ -92,8 +95,11 @@ impl TidbMutation {
) -> Result<TidbOutput> {
let tidb = database::Tidb::new(&new)?;
let store = super::get_store(ctx).await?;
let (name, version) = tidb.update(&store, &name)?;
Ok(TidbOutput { name, version })
tidb.update(&store, &name)?;
Ok(TidbOutput {
name: tidb.name,
version: tidb.version,
})
}
}

Expand Down

0 comments on commit bfaaf57

Please sign in to comment.