|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use depot::{ |
| 3 | + inspect::{self, CatalogQuery, RawScanQuery, RowsQuery, SampleQuery}, |
| 4 | + types::{BucketId, DatabaseBranchId}, |
| 5 | +}; |
| 6 | +use rivet_api_builder::ApiCtx; |
| 7 | +use serde::Deserialize; |
| 8 | +use uuid::Uuid; |
| 9 | + |
| 10 | +#[derive(Debug, Deserialize)] |
| 11 | +pub struct BucketPath { |
| 12 | + pub bucket_id: String, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Deserialize)] |
| 16 | +pub struct DatabasePath { |
| 17 | + pub bucket_id: String, |
| 18 | + pub database_id: String, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Debug, Deserialize)] |
| 22 | +pub struct BranchPath { |
| 23 | + pub branch_id: String, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Deserialize)] |
| 27 | +pub struct BranchRowsPath { |
| 28 | + pub branch_id: String, |
| 29 | + pub family: String, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, Deserialize)] |
| 33 | +pub struct PageTracePath { |
| 34 | + pub branch_id: String, |
| 35 | + pub pgno: u32, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Deserialize)] |
| 39 | +pub struct RawKeyPath { |
| 40 | + pub key: String, |
| 41 | +} |
| 42 | + |
| 43 | +pub async fn summary(ctx: ApiCtx, _path: (), _query: ()) -> Result<inspect::InspectResponse> { |
| 44 | + let udb = ctx.pools().udb()?; |
| 45 | + inspect::summary(&udb, ctx.pools().node_id()).await |
| 46 | +} |
| 47 | + |
| 48 | +pub async fn catalog( |
| 49 | + ctx: ApiCtx, |
| 50 | + _path: (), |
| 51 | + query: CatalogQuery, |
| 52 | +) -> Result<inspect::CatalogResponse> { |
| 53 | + let udb = ctx.pools().udb()?; |
| 54 | + inspect::catalog(&udb, ctx.pools().node_id(), query).await |
| 55 | +} |
| 56 | + |
| 57 | +pub async fn bucket( |
| 58 | + ctx: ApiCtx, |
| 59 | + path: BucketPath, |
| 60 | + query: SampleQuery, |
| 61 | +) -> Result<inspect::InspectResponse> { |
| 62 | + let bucket_id = parse_bucket_id(&path.bucket_id)?; |
| 63 | + let udb = ctx.pools().udb()?; |
| 64 | + inspect::bucket(&udb, ctx.pools().node_id(), bucket_id, query).await |
| 65 | +} |
| 66 | + |
| 67 | +pub async fn database( |
| 68 | + ctx: ApiCtx, |
| 69 | + path: DatabasePath, |
| 70 | + query: SampleQuery, |
| 71 | +) -> Result<inspect::InspectResponse> { |
| 72 | + let bucket_id = parse_bucket_id(&path.bucket_id)?; |
| 73 | + let udb = ctx.pools().udb()?; |
| 74 | + inspect::database( |
| 75 | + &udb, |
| 76 | + ctx.pools().node_id(), |
| 77 | + bucket_id, |
| 78 | + path.database_id, |
| 79 | + query, |
| 80 | + ) |
| 81 | + .await |
| 82 | +} |
| 83 | + |
| 84 | +pub async fn branch( |
| 85 | + ctx: ApiCtx, |
| 86 | + path: BranchPath, |
| 87 | + query: SampleQuery, |
| 88 | +) -> Result<inspect::InspectResponse> { |
| 89 | + let branch_id = parse_database_branch_id(&path.branch_id)?; |
| 90 | + let udb = ctx.pools().udb()?; |
| 91 | + inspect::branch(&udb, ctx.pools().node_id(), branch_id, query).await |
| 92 | +} |
| 93 | + |
| 94 | +pub async fn page_trace( |
| 95 | + ctx: ApiCtx, |
| 96 | + path: PageTracePath, |
| 97 | + _query: (), |
| 98 | +) -> Result<inspect::InspectResponse> { |
| 99 | + let branch_id = parse_database_branch_id(&path.branch_id)?; |
| 100 | + let udb = ctx.pools().udb()?; |
| 101 | + inspect::page_trace( |
| 102 | + &udb, |
| 103 | + ctx.pools().node_id(), |
| 104 | + branch_id, |
| 105 | + path.pgno, |
| 106 | + ) |
| 107 | + .await |
| 108 | +} |
| 109 | + |
| 110 | +pub async fn branch_rows( |
| 111 | + ctx: ApiCtx, |
| 112 | + path: BranchRowsPath, |
| 113 | + query: RowsQuery, |
| 114 | +) -> Result<inspect::PaginatedRowsResponse> { |
| 115 | + let branch_id = parse_database_branch_id(&path.branch_id)?; |
| 116 | + let family = inspect::RowFamily::parse(&path.family)?; |
| 117 | + let udb = ctx.pools().udb()?; |
| 118 | + inspect::branch_rows( |
| 119 | + &udb, |
| 120 | + ctx.pools().node_id(), |
| 121 | + branch_id, |
| 122 | + family, |
| 123 | + query, |
| 124 | + ) |
| 125 | + .await |
| 126 | +} |
| 127 | + |
| 128 | +pub async fn raw_key( |
| 129 | + ctx: ApiCtx, |
| 130 | + path: RawKeyPath, |
| 131 | + _query: (), |
| 132 | +) -> Result<inspect::InspectResponse> { |
| 133 | + let key = inspect::decode_path_key(&path.key)?; |
| 134 | + let udb = ctx.pools().udb()?; |
| 135 | + inspect::raw_key(&udb, ctx.pools().node_id(), key).await |
| 136 | +} |
| 137 | + |
| 138 | +pub async fn raw_scan( |
| 139 | + ctx: ApiCtx, |
| 140 | + _path: (), |
| 141 | + query: RawScanQuery, |
| 142 | +) -> Result<inspect::PaginatedRowsResponse> { |
| 143 | + let udb = ctx.pools().udb()?; |
| 144 | + inspect::raw_scan(&udb, ctx.pools().node_id(), query).await |
| 145 | +} |
| 146 | + |
| 147 | +pub async fn decode_key( |
| 148 | + ctx: ApiCtx, |
| 149 | + path: RawKeyPath, |
| 150 | + _query: (), |
| 151 | +) -> Result<inspect::InspectResponse> { |
| 152 | + let key = inspect::decode_path_key(&path.key)?; |
| 153 | + inspect::decode_key_response(ctx.pools().node_id(), key) |
| 154 | +} |
| 155 | + |
| 156 | +fn parse_bucket_id(value: &str) -> Result<BucketId> { |
| 157 | + Ok(BucketId::from_uuid( |
| 158 | + Uuid::parse_str(value).context("parse Depot bucket id")?, |
| 159 | + )) |
| 160 | +} |
| 161 | + |
| 162 | +fn parse_database_branch_id(value: &str) -> Result<DatabaseBranchId> { |
| 163 | + Ok(DatabaseBranchId::from_uuid( |
| 164 | + Uuid::parse_str(value).context("parse Depot database branch id")?, |
| 165 | + )) |
| 166 | +} |
0 commit comments