Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
SimranMakhija7 committed May 2, 2024
1 parent da66673 commit 6707ffb
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 56 deletions.
24 changes: 11 additions & 13 deletions src/dto/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ use std::fmt::Debug;

#[derive(Debug, Deserialize, Serialize)]
pub struct NamespaceNotFoundError {
pub message: String,
pub message: String,
}


impl From<NamespaceNotFoundError> for IcebergErrorResponse {
fn from(err: NamespaceNotFoundError) -> Self {
IcebergErrorResponse {
error: ErrorModel {
message: err.message,
r#type: "NamespaceNotFound".to_string(),
code: 404,
stack: None,
},
fn from(err: NamespaceNotFoundError) -> Self {
IcebergErrorResponse {
error: ErrorModel {
message: err.message,
r#type: "NamespaceNotFound".to_string(),
code: 404,
stack: None,
},
}
}
}
}


#[derive(Debug, Deserialize, Serialize)]
pub struct ErrorModel {
pub message: String,
Expand Down Expand Up @@ -58,7 +56,7 @@ pub enum ErrorTypes {
Unauthorized(String),
ServiceUnavailable(String),
ServerError(String),
NamespaceNotFound(String)
NamespaceNotFound(String),
}

impl std::fmt::Display for ErrorTypes {
Expand Down
3 changes: 1 addition & 2 deletions src/dto/rename_request.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize};
use crate::dto::table_data::TableIdent;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct TableRenameRequest {
pub source: TableIdent,
pub destination: TableIdent,
}

10 changes: 4 additions & 6 deletions src/dto/table_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
// use std::collections::HashMap;


#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Table {
pub id: TableIdent,
Expand All @@ -27,7 +26,6 @@ impl TableIdent {
}
}


#[derive(Serialize, Deserialize, Debug, TypedBuilder)]
pub struct TableCreation {
/// The name of the table.
Expand All @@ -39,8 +37,8 @@ pub struct TableCreation {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TableMetadata {
pub table_uuid: String,
// pub file_urls: Option<Vec<String>>,
// pub columns: Option<Vec<ColumnData>>,
// pub properties: Option<HashMap<String, String>>,
pub table_uuid: String,
// pub file_urls: Option<Vec<String>>,
// pub columns: Option<Vec<ColumnData>>,
// pub properties: Option<HashMap<String, String>>,
}
11 changes: 6 additions & 5 deletions src/handlers/namespace_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ pub async fn list_namespaces(
"namespaces": namespaces
});
Ok(Json(json_object))
},
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, format!("Internal server error: {}", e)))
}
Err(e) => Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal server error: {}", e),
)),
}
}

pub async fn create_namespace(
State(repo): State<Arc<NamespaceRepository>>,
new_namespace: Json<NamespaceData>,
) -> Result<Json<NamespaceData>, (StatusCode, String)> {

if repo.namespace_exists(new_namespace.get_name()).unwrap() {
return Err((StatusCode::CONFLICT, format!("namespace already exists")));
}
Expand Down Expand Up @@ -123,8 +125,7 @@ pub async fn set_namespace_properties(
}

// Check if a property key was included in both `removals` and `updates`



repo.set_namespace_properties(
id,
request_body.removals.clone(),
Expand Down
14 changes: 8 additions & 6 deletions src/handlers/table_handler.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::dto::namespace_data::NamespaceIdent;
use crate::dto::rename_request::TableRenameRequest;
use crate::dto::table_data::{TableIdent, TableCreation, Table};
use crate::dto::table_data::{Table, TableCreation, TableIdent};
use crate::repository::table::TableRepository;
use crate::dto::namespace_data::{NamespaceIdent};
use axum::{
extract::{Json, Path, State},
http::StatusCode,
};
use std::io::{ ErrorKind};
use std::io::ErrorKind;
use std::sync::Arc;

pub async fn list_tables(
Expand Down Expand Up @@ -62,7 +62,10 @@ pub async fn load_table(

match repo.load_table(&id, table.clone()) {
Ok(Some(table_data)) => Ok(Json(table_data)),
Ok(None) => Err((StatusCode::NOT_FOUND, format!("Table {} not found", table.clone()))),
Ok(None) => Err((
StatusCode::NOT_FOUND,
format!("Table {} not found", table.clone()),
)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, format!("Error: {}", e))),
}
}
Expand All @@ -82,7 +85,7 @@ pub async fn delete_table(
Err(e) => match e.kind() {
ErrorKind::NotFound => Err((StatusCode::NOT_FOUND, format!("Error: {}", e))),
_ => Err((StatusCode::INTERNAL_SERVER_ERROR, format!("Error: {}", e))),
}
},
}
}

Expand Down Expand Up @@ -117,4 +120,3 @@ pub async fn rename_table(
},
}
}

1 change: 0 additions & 1 deletion src/repository/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod namespace;
pub mod table;

1 change: 0 additions & 1 deletion src/repository/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,3 @@ impl NamespaceRepository {
fn current_time() -> String {
"current_time".to_string()
}

68 changes: 47 additions & 21 deletions src/repository/table.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::database::database::Database;
use crate::dto::namespace_data::{NamespaceData, NamespaceIdent};
use crate::dto::rename_request::TableRenameRequest;
use crate::dto::table_data::{TableIdent, TableCreation, Table, TableMetadata};
use crate::dto::namespace_data::{NamespaceIdent, NamespaceData};
use crate::dto::table_data::{Table, TableCreation, TableIdent, TableMetadata};
use std::io::{Error, ErrorKind};
use std::sync::{Arc, Mutex};
use uuid::Uuid;
Expand All @@ -15,9 +15,12 @@ impl TableRepository {
Self { database }
}

pub fn list_all_tables(&self, namespace: &NamespaceIdent) -> Result<Option<Vec<TableIdent>>, Error> {
pub fn list_all_tables(
&self,
namespace: &NamespaceIdent,
) -> Result<Option<Vec<TableIdent>>, Error> {
let db = self.database.lock().unwrap();
let _ : NamespaceData = match db.get("NamespaceData", namespace)? {
let _: NamespaceData = match db.get("NamespaceData", namespace)? {
Some(data) => data,
None => {
return Err(std::io::Error::new(
Expand All @@ -29,9 +32,13 @@ impl TableRepository {
db.get::<NamespaceIdent, Vec<TableIdent>>("TableNamespaceMap", namespace)
}

pub fn create_table(&self, namespace: &NamespaceIdent, table_creation: &TableCreation) -> Result<(), Error> {
pub fn create_table(
&self,
namespace: &NamespaceIdent,
table_creation: &TableCreation,
) -> Result<(), Error> {
let db = self.database.lock().unwrap();
let _ : NamespaceData = match db.get("NamespaceData", namespace)? {
let _: NamespaceData = match db.get("NamespaceData", namespace)? {
Some(data) => data,
None => {
return Err(std::io::Error::new(
Expand All @@ -42,12 +49,10 @@ impl TableRepository {
};

let table_id = TableIdent::new(namespace.clone(), table_creation.name.clone());
let table_uuid = Uuid::new_v4().to_string();

let table_metadata = TableMetadata{
table_uuid
};

let table_uuid = Uuid::new_v4().to_string();

let table_metadata = TableMetadata { table_uuid };

let mut tables = db
.get::<NamespaceIdent, Vec<TableIdent>>("TableNamespaceMap", namespace)
.unwrap()
Expand All @@ -56,17 +61,27 @@ impl TableRepository {
if tables.contains(&table_id) {
return Err(std::io::Error::new(
ErrorKind::AlreadyExists,
format!("Table {} already exists in namespace {}", table_creation.name, namespace.clone().0.join("\u{1F}")),
))
format!(
"Table {} already exists in namespace {}",
table_creation.name,
namespace.clone().0.join("\u{1F}")
),
));
}

db.insert("TableData", &table_id, &Table{id: table_id.clone(), metadata: table_metadata})?;
db.insert(
"TableData",
&table_id,
&Table {
id: table_id.clone(),
metadata: table_metadata,
},
)?;
tables.push(table_id.clone());
let r_val = db.insert("TableNamespaceMap", namespace, &tables);
r_val
}


pub fn load_table(
&self,
namespace: &NamespaceIdent,
Expand All @@ -82,7 +97,7 @@ impl TableRepository {
let db = self.database.lock().unwrap();
let table_id = TableIdent::new(namespace.clone(), table_name.clone());

let _ : Table = match db.get::<TableIdent, Table>("TableData", &table_id)? {
let _: Table = match db.get::<TableIdent, Table>("TableData", &table_id)? {
Some(data) => data,
None => {
return Err(std::io::Error::new(
Expand All @@ -101,7 +116,11 @@ impl TableRepository {
db.insert("TableNamespaceMap", namespace, &tables)
}

pub fn table_exists(&self, namespace: &NamespaceIdent, table_name: String) -> Result<bool, Error> {
pub fn table_exists(
&self,
namespace: &NamespaceIdent,
table_name: String,
) -> Result<bool, Error> {
let table = self.load_table(namespace, table_name)?;
Ok(table.is_some())
}
Expand All @@ -116,14 +135,21 @@ impl TableRepository {
.ok_or_else(|| Error::new(ErrorKind::NotFound, "Source table not found"))?;

if self.table_exists(&destination.namespace, destination.name.clone())? {
return Err(Error::new(ErrorKind::AlreadyExists, "Destination table already exists"));
return Err(Error::new(
ErrorKind::AlreadyExists,
"Destination table already exists",
));
}

let mut new_table = table.clone();
new_table.id = destination.clone();

self.create_table(&destination.namespace.clone(), &TableCreation{name: destination.name.clone()})?;
self.create_table(
&destination.namespace.clone(),
&TableCreation {
name: destination.name.clone(),
},
)?;
self.drop_table(&namespace, source.name.clone())
}
}

2 changes: 1 addition & 1 deletion src/structs/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod error;
pub mod error;

0 comments on commit 6707ffb

Please sign in to comment.