1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use crate::component::datatype::DataType;
use crate::component::field::Field;
use crate::component::table::Row;
use crate::component::table::Table;
use crate::storage::bytescoder;
use crate::storage::file::File;
use crate::storage::index::Index;
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::io;
use std::path::Path;

#[derive(Debug, Clone)]
pub struct DiskInterface {
    /* definition */
// Ideally, DiskInterface is a stateless struct
}

// structure of `usernames.json`

#[derive(Debug, Serialize, Deserialize)]
pub struct UsernamesJson {
    pub usernames: Vec<UsernameInfo>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UsernameInfo {
    pub name: String,
    pub path: String,
}

// structure of `dbs.json`

#[derive(Debug, Serialize, Deserialize)]
pub struct DbsJson {
    pub dbs: Vec<DbInfo>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DbInfo {
    pub name: String,
    pub path: String,
}

// structure of `tables.json`

#[derive(Debug, Serialize, Deserialize)]
pub struct TablesJson {
    pub tables: Vec<TableMeta>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TableMeta {
    pub name: String,
    pub username: String,
    pub db_name: String,
    pub path_tsv: String,
    pub path_bin: String,
    pub primary_key: Vec<String>,
    pub foreign_key: Vec<String>,
    pub reference_table: Option<String>,
    pub reference_attr: Option<String>,
    pub row_length: u32,
    pub attrs: HashMap<String, Field>,
    pub attrs_order: Vec<String>,
    pub attr_offset_ranges: Vec<Vec<u32>>,
}

#[derive(Debug, PartialEq, Clone)]
pub enum DiskError {
    Io,
    BaseDirExists,
    BaseDirNotExists,
    UsernamesJsonNotExists,
    UsernameExists,
    UsernameNotExists,
    UsernameDirNotExists,
    DbsJsonNotExists,
    DbExists,
    DbNotExists,
    DbDirNotExists,
    TablesJsonNotExists,
    TableExists,
    TableNotExists,
    TableBinNotExists,
    TableTsvNotExists,
    TableIdxFileNotExists,
    JsonParse,
    RangeContainsDeletedRecord,
    RangeExceedLatestRecord,
    RangeAndNumRowsMismatch,
    AttrNotExists,
    BytesError,
    DuplicatedKey,
    IndexKeyNotFound,
}

impl From<io::Error> for DiskError {
    fn from(_err: io::Error) -> DiskError {
        DiskError::Io
    }
}

impl From<serde_json::Error> for DiskError {
    fn from(_err: serde_json::Error) -> DiskError {
        DiskError::JsonParse
    }
}

impl From<bytescoder::BytesCoderError> for DiskError {
    fn from(_err: bytescoder::BytesCoderError) -> DiskError {
        DiskError::BytesError
    }
}

impl fmt::Display for DiskError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            DiskError::Io => write!(f, "No such file or directory."),
            DiskError::BaseDirExists => write!(f, "Base dir already exists and cannot be created again."),
            DiskError::BaseDirNotExists => write!(f, "Base data directory not exists. All data lost."),
            DiskError::UsernamesJsonNotExists => write!(f, "The file `usernames.json` is lost"),
            DiskError::UsernameExists => write!(f, "User name already exists and cannot be created again."),
            DiskError::UsernameNotExists => {
                write!(f, "Specified user name not exists. Please create this username first.")
            }
            DiskError::UsernameDirNotExists => write!(f, "Username exists but corresponding data folder is lost."),
            DiskError::DbsJsonNotExists => write!(f, "The `dbs.json` of the username is lost"),
            DiskError::DbExists => write!(f, "DB already exists and cannot be created again."),
            DiskError::DbNotExists => write!(f, "DB not exists. Please create DB first."),
            DiskError::DbDirNotExists => write!(f, "DB exists but correspoding data folder is lost."),
            DiskError::TablesJsonNotExists => write!(f, "The `tables.json` of the DB is lost."),
            DiskError::TableExists => write!(f, "Table already exists and cannot be created again."),
            DiskError::TableNotExists => write!(f, "Table not exists. Please create table first."),
            DiskError::TableBinNotExists => write!(f, "Table exists but correspoding bin file is lost."),
            DiskError::TableTsvNotExists => write!(f, "Table exists but correspoding tsv file is lost."),
            DiskError::TableIdxFileNotExists => write!(
                f,
                "Index file does not exist. Please build and save it before you can load from it."
            ),
            DiskError::JsonParse => write!(f, "JSON parsing error."),
            DiskError::RangeContainsDeletedRecord => write!(f, "The range of rows to fetch contains deleted records."),
            DiskError::RangeExceedLatestRecord => {
                write!(f, "The range of rows to fetch exceeds the latest record on the table.")
            }
            DiskError::RangeAndNumRowsMismatch => {
                write!(f, "The range of rows does not match number of rows to be modified.")
            }
            DiskError::AttrNotExists => write!(f, "The row does not contain specified attribute."),
            DiskError::BytesError => write!(f, "Error raised from BytesCoder."),
            DiskError::DuplicatedKey => write!(f, "Attempting to insert an duplicated key to index."),
            DiskError::IndexKeyNotFound => {
                write!(f, "Attempting to access or delete a key which does not exist in index.")
            }
        }
    }
}

#[allow(dead_code)]
impl DiskInterface {
    pub fn create_file_base(file_base_path: Option<&str>) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::create_file_base(base_path)?)
    }

    pub fn create_username(username: &str, file_base_path: Option<&str>) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::create_username(username, base_path)?)
    }

    pub fn get_usernames(file_base_path: Option<&str>) -> Result<Vec<String>, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::get_usernames(base_path)?)
    }

    pub fn remove_username(username: &str, file_base_path: Option<&str>) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::remove_username(username, base_path)?)
    }

    pub fn create_db(username: &str, db_name: &str, file_base_path: Option<&str>) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::create_db(username, db_name, base_path)?)
    }

    pub fn get_dbs(username: &str, file_base_path: Option<&str>) -> Result<Vec<String>, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::get_dbs(username, base_path)?)
    }

    pub fn remove_db(username: &str, db_name: &str, file_base_path: Option<&str>) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::remove_db(username, db_name, base_path)?)
    }

    pub fn create_table(
        username: &str,
        db_name: &str,
        table: &Table,
        file_base_path: Option<&str>,
    ) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::create_table(username, db_name, table, base_path)?)
    }

    pub fn get_tables(username: &str, db_name: &str, file_base_path: Option<&str>) -> Result<Vec<String>, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::get_tables(username, db_name, base_path)?)
    }

    pub fn load_tables_meta(
        username: &str,
        db_name: &str,
        file_base_path: Option<&str>,
    ) -> Result<Vec<TableMeta>, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::load_tables_meta(username, db_name, base_path)?)
    }

    pub fn load_table_meta(
        username: &str,
        db_name: &str,
        table_name: &str,
        file_base_path: Option<&str>,
    ) -> Result<TableMeta, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::load_table_meta(username, db_name, table_name, base_path)?)
    }

    pub fn drop_table(
        username: &str,
        db_name: &str,
        table_name: &str,
        file_base_path: Option<&str>,
    ) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::drop_table(username, db_name, table_name, base_path)?)
    }

    pub fn append_rows(
        username: &str,
        db_name: &str,
        table_name: &str,
        rows: &Vec<Row>,
        file_base_path: Option<&str>,
    ) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::append_rows(username, db_name, table_name, rows, base_path)?)
    }

    pub fn fetch_rows(
        username: &str,
        db_name: &str,
        table_name: &str,
        row_range: &Vec<u32>,
        file_base_path: Option<&str>,
    ) -> Result<Vec<Row>, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::fetch_rows(username, db_name, table_name, row_range, base_path)?)
    }

    pub fn delete_rows(
        username: &str,
        db_name: &str,
        table_name: &str,
        row_range: &Vec<u32>,
        file_base_path: Option<&str>,
    ) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::delete_rows(username, db_name, table_name, row_range, base_path)?)
    }

    pub fn modify_rows(
        username: &str,
        db_name: &str,
        table_name: &str,
        row_range: &Vec<u32>,
        new_rows: &Vec<Row>,
        file_base_path: Option<&str>,
    ) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::modify_rows(
            username, db_name, table_name, row_range, new_rows, base_path,
        )?)
    }

    pub fn get_num_rows(
        username: &str,
        db_name: &str,
        table_name: &str,
        file_base_path: Option<&str>,
    ) -> Result<u32, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        Ok(File::get_num_rows(username, db_name, table_name, base_path)?)
    }

    pub fn storage_hierarchy_check(
        base_path: &str,
        username: Option<&str>,
        db_name: Option<&str>,
        table_name: Option<&str>,
    ) -> Result<(), DiskError> {
        // check if base directory exists
        if !Path::new(base_path).exists() {
            return Err(DiskError::BaseDirNotExists);
        }

        // check if `usernames.json` exists
        let usernames_json_path = format!("{}/{}", base_path, "usernames.json");
        if !Path::new(&usernames_json_path).exists() {
            return Err(DiskError::UsernamesJsonNotExists);
        }

        // base level check passed
        if username == None {
            return Ok(());
        }

        // check if username exists
        let usernames_file = fs::File::open(&usernames_json_path)?;
        let usernames_json: UsernamesJson = serde_json::from_reader(usernames_file)?;
        if !usernames_json
            .usernames
            .iter()
            .map(|username_info| username_info.name.clone())
            .collect::<Vec<String>>()
            .contains(&username.unwrap().to_string())
        {
            return Err(DiskError::UsernameNotExists);
        }

        // check if username directory exists
        let username_path = format!("{}/{}", base_path, username.unwrap());
        if !Path::new(&username_path).exists() {
            return Err(DiskError::UsernameDirNotExists);
        }

        // check if `dbs.json` exists
        let dbs_json_path = format!("{}/{}", username_path, "dbs.json");
        if !Path::new(&dbs_json_path).exists() {
            return Err(DiskError::DbsJsonNotExists);
        }

        // username level check passed
        if db_name == None {
            return Ok(());
        }

        // check if db exists
        let dbs_file = fs::File::open(&dbs_json_path)?;
        let dbs_json: DbsJson = serde_json::from_reader(dbs_file)?;
        if !dbs_json
            .dbs
            .iter()
            .map(|db_info| db_info.name.clone())
            .collect::<Vec<String>>()
            .contains(&db_name.unwrap().to_string())
        {
            return Err(DiskError::DbNotExists);
        }

        // check if db directory exists
        let db_path = format!("{}/{}", username_path, db_name.unwrap());
        if !Path::new(&db_path).exists() {
            return Err(DiskError::DbDirNotExists);
        }

        // check if `tables.json` exists
        let tables_json_path = format!("{}/{}", db_path, "tables.json");
        if !Path::new(&tables_json_path).exists() {
            return Err(DiskError::TablesJsonNotExists);
        }

        // db level check passed
        if table_name == None {
            return Ok(());
        }

        // check if table exists
        let tables_file = fs::File::open(&tables_json_path)?;
        let tables_json: TablesJson = serde_json::from_reader(tables_file)?;
        if !tables_json
            .tables
            .iter()
            .map(|table_meta| table_meta.name.clone())
            .collect::<Vec<String>>()
            .contains(&table_name.unwrap().to_string())
        {
            return Err(DiskError::TableNotExists);
        }

        // check if table bin exists
        let table_bin_path = format!("{}/{}.bin", db_path, table_name.unwrap());
        if !Path::new(&table_bin_path).exists() {
            return Err(DiskError::TableBinNotExists);
        }

        if dotenv!("ENABLE_TSV") == "true" {
            // check if table tsv exists
            let table_tsv_path = format!("{}/{}.tsv", db_path, table_name.unwrap());
            if !Path::new(&table_tsv_path).exists() {
                return Err(DiskError::TableTsvNotExists);
            }
        }

        Ok(())
    }

    pub fn get_datatype_size(datatype: &DataType) -> u32 {
        match datatype {
            DataType::Char(length) => length.clone() as u32,
            DataType::Double => 8,
            DataType::Float => 4,
            DataType::Int => 4,
            DataType::Varchar(length) => length.clone() as u32,
            DataType::Url => 256,
        }
    }

    pub fn build_index_from_table_bin(
        username: &str,
        db_name: &str,
        table_name: &str,
        file_base_path: Option<&str>,
    ) -> Result<Index, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        let table_meta = DiskInterface::load_table_meta(username, db_name, table_name, Some(base_path))?;
        let mut index = Index::new(table_meta)?;
        index.build_from_bin(base_path)?;

        Ok(index)
    }

    pub fn load_index(
        username: &str,
        db_name: &str,
        table_name: &str,
        file_base_path: Option<&str>,
    ) -> Result<Index, DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        let table_meta = DiskInterface::load_table_meta(username, db_name, table_name, Some(base_path))?;
        let mut index = Index::new(table_meta)?;
        index.load(base_path)?;

        Ok(index)
    }

    pub fn save_index(index: &Index, file_base_path: Option<&str>) -> Result<(), DiskError> {
        let base_path = file_base_path.unwrap_or(dotenv!("FILE_BASE_PATH"));
        index.save(base_path)?;

        Ok(())
    }
}

// #[cfg(test)]
// mod tests {
//     use super::*;
// }