@@ -34,7 +34,7 @@ use crate::api::user_context::UserContext;
3434use crate :: api:: errors:: ErrorResponse ;
3535use crate :: database:: dataset_table;
3636
37- use hanami_dataset:: dataset_io:: { DataSetType , init_new_data_set_file, DataSetFileHandle } ;
37+ use hanami_dataset:: dataset_io:: { DataSetType , init_new_data_set_file, DataSetFileWriteHandle_v1_0 , Column } ;
3838
3939use super :: dataset_structs:: DatasetResp ;
4040
@@ -54,8 +54,8 @@ pub struct MnistImage {
5454pub async fn upload_binary ( mut payload : Multipart , path : Path < ( String , String ) > , context : UserContext ) -> Result < CreatedJson < DatasetResp > , ErrorResponse > {
5555 let upload_dir_path = "./uploads" ;
5656 let upload_dir = PathBuf :: from ( & upload_dir_path) ;
57- let dataset_uuid = Uuid :: new_v4 ( ) . to_string ( ) ;
58- let target_filepath: PathBuf = upload_dir. join ( & dataset_uuid) ;
57+ let dataset_uuid = Uuid :: new_v4 ( ) ;
58+ let target_filepath: PathBuf = upload_dir. join ( & dataset_uuid. to_string ( ) ) ;
5959
6060 let ( dataset_type_str, name) = path. into_inner ( ) ;
6161 let dataset_type = dataset_type_str. to_string ( ) ;
@@ -100,7 +100,7 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
100100 } ;
101101
102102 // create file
103- let filepath: PathBuf = upload_dir. join ( filename + & dataset_uuid) ;
103+ let filepath: PathBuf = upload_dir. join ( filename + & dataset_uuid. to_string ( ) ) ;
104104 let mut f = match fs:: File :: create ( & filepath) . await {
105105 Ok ( value) => value,
106106 Err ( e) => {
@@ -134,7 +134,8 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
134134 & filepaths[ 0 ] ,
135135 & filepaths[ 1 ] ,
136136 & target_filepath,
137- & name,
137+ dataset_uuid. clone ( ) ,
138+ name. clone ( ) ,
138139 None )
139140 {
140141 Ok ( images) => images,
@@ -148,7 +149,7 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
148149
149150 // add new dataset to datbase
150151 let file_path_str: String = target_filepath. to_string_lossy ( ) . into ( ) ;
151- match dataset_table:: add_new_dataset ( & dataset_uuid, & name, & file_path_str, & context. user_id ) {
152+ match dataset_table:: add_new_dataset ( & dataset_uuid. to_string ( ) , & name, & file_path_str, & context. user_id ) {
152153 Ok ( _) => { } ,
153154 Err ( _) => {
154155 let msg = format ! ( "Failed to add dataset with ID '{}' to database." , dataset_uuid) ;
@@ -158,7 +159,7 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
158159 } ;
159160
160161 // get new created dataset from database to get addtional information
161- match dataset_table:: get_dataset ( & dataset_uuid) {
162+ match dataset_table:: get_dataset ( & dataset_uuid. to_string ( ) ) {
162163 Ok ( dataset) => {
163164 let resp = DatasetResp {
164165 uuid : dataset. uuid . clone ( ) ,
@@ -184,7 +185,8 @@ pub fn load_mnist_images(
184185 image_path : & PathBuf ,
185186 label_path : & PathBuf ,
186187 target_filepath : & PathBuf ,
187- name : & String ,
188+ uuid : Uuid ,
189+ name : String ,
188190 limit : Option < usize > ,
189191) -> Result < Vec < MnistImage > , Box < dyn Error > > {
190192 let mut img_reader = BufReader :: new ( File :: open ( image_path) ?) ;
@@ -227,66 +229,71 @@ pub fn load_mnist_images(
227229 }
228230
229231 let picture_size: u32 = rows * cols;
230- let mut picture_input = Map :: new ( ) ;
231- picture_input. insert ( "column_start" . to_string ( ) , Value :: Number ( 0 . into ( ) ) ) ;
232- picture_input. insert ( "column_end" . to_string ( ) , Value :: Number ( picture_size. into ( ) ) ) ;
232+ let mut columns: Vec < Column > = Vec :: new ( ) ;
233233
234- let mut output_entry = Map :: new ( ) ;
235- output_entry. insert ( "column_start" . to_string ( ) , Value :: Number ( picture_size. into ( ) ) ) ;
236- output_entry. insert ( "column_end" . to_string ( ) , Value :: Number ( ( picture_size + 10 ) . into ( ) ) ) ;
234+ let pictures = Column {
235+ name : "picture" . to_string ( ) ,
236+ start : 0 ,
237+ end : picture_size,
238+ } ;
239+ let labels = Column {
240+ name : "label" . to_string ( ) ,
241+ start : picture_size,
242+ end : picture_size + 10 ,
243+ } ;
237244
238- let mut description = Map :: new ( ) ;
239- description. insert ( "picture" . to_string ( ) , Value :: Object ( picture_input) ) ;
240- description. insert ( "label" . to_string ( ) , Value :: Object ( output_entry) ) ;
245+ columns. push ( pictures) ;
246+ columns. push ( labels) ;
241247
242- let number_of_columns: u64 = u64:: try_from ( num_images) . unwrap ( ) + 10 ;
243- match init_new_data_set_file (
248+ let mut dataset_handle = match init_new_data_set_file (
244249 & target_filepath,
245- & name,
246- & Value :: Object ( description) ,
247- DataSetType :: Uint8Type ,
248- number_of_columns)
250+ uuid,
251+ name,
252+ "" . to_string ( ) ,
253+ columns,
254+ DataSetType :: Uint8Type )
249255 {
250- Ok ( mut handle) => {
251- let file_path_str: String = target_filepath. to_string_lossy ( ) . into ( ) ;
252-
253- let mut label_data: Vec < u8 > = vec ! [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
254- for ( i, img) in images. iter ( ) . enumerate ( ) {
255- // println!("Image {}: Label = {}", i, img.label);
256- label_data[ usize:: from ( img. label ) ] = 1 ;
257-
258- match handle. target_file . write_all ( & img. pixels ) {
259- Ok ( _) => { } ,
260- Err ( e) => {
261- return Err ( "Image and label count mismatch!" . into ( ) ) ;
262-
263- // return Err(ErrorContainer {
264- // error_type: ErrorType::Error,
265- // msg: format!("Failed to write data-set file '{}' with error: {}.", file_path_str, e),
266- // });
267- }
268- } ;
256+ Ok ( mut handle) => handle,
257+ Err ( e) => {
258+ error ! ( "FAIL {}" , e. msg) ;
259+ return Err ( e. msg . into ( ) ) ;
260+ } ,
261+ } ;
269262
270- match handle. target_file . write_all ( & label_data) {
271- Ok ( _) => { } ,
272- Err ( e) => {
273- return Err ( "Image and label count mismatch!" . into ( ) ) ;
263+ let file_path_str: String = target_filepath. to_string_lossy ( ) . into ( ) ;
274264
275- // return Err(ErrorContainer {
276- // error_type: ErrorType::Error,
277- // msg: format!("Failed to write data-set file '{}' with error: {}.", file_path_str, e),
278- // });
279- }
280- } ;
265+ let mut label_data: Vec < u8 > = vec ! [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
266+ for ( i, img) in images. iter ( ) . enumerate ( ) {
267+ // println!("Image {}: Label = {}", i, img.label);
268+ label_data[ usize:: from ( img. label ) ] = 1 ;
281269
282- label_data[ usize:: from ( img. label ) ] = 0 ;
270+ match dataset_handle. target_file . write_all ( & img. pixels ) {
271+ Ok ( _) => { } ,
272+ Err ( e) => {
273+ return Err ( "Image and label count mismatch!" . into ( ) ) ;
274+
275+ // return Err(ErrorContainer {
276+ // error_type: ErrorType::Error,
277+ // msg: format!("Failed to write data-set file '{}' with error: {}.", file_path_str, e),
278+ // });
283279 }
284- println ! ( "YEAH!" ) ;
285- } ,
286- Err ( e) => {
287- println ! ( "FAIL {}" , e. msg) ;
288- } ,
289- } ;
280+ } ;
281+
282+ match dataset_handle. target_file . write_all ( & label_data) {
283+ Ok ( _) => { } ,
284+ Err ( e) => {
285+ return Err ( "Image and label count mismatch!" . into ( ) ) ;
286+
287+ // return Err(ErrorContainer {
288+ // error_type: ErrorType::Error,
289+ // msg: format!("Failed to write data-set file '{}' with error: {}.", file_path_str, e),
290+ // });
291+ }
292+ } ;
293+
294+ label_data[ usize:: from ( img. label ) ] = 0 ;
295+ }
296+ println ! ( "YEAH!" ) ;
290297
291298 Ok ( images)
292299}
0 commit comments