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
extern crate byteorder;

use crate::component::table::Row;
use crate::storage::bytescoder::BytesCoder;
use crate::storage::diskinterface::{DiskError, DiskInterface, TableMeta};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::fs;
use std::io::{BufReader, Read, Write};
use std::path::Path;

pub struct Index {
    table_meta: TableMeta,
    index_data: Vec<RowPair>,
    num_rows: u32, // row number of the table including deleted
}

/// (row, key_value) pair
#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct RowPair {
    row: u32,
    key_value: Vec<u8>,
}

impl RowPair {
    pub fn new(row: u32, key_value: Vec<u8>) -> Self {
        RowPair { row, key_value }
    }

    pub fn to_bytes(&self) -> Result<Vec<u8>, DiskError> {
        let mut bs: Vec<u8> = vec![];
        bs.write_u32::<BigEndian>(self.row)?;
        bs.extend_from_slice(&self.key_value);

        Ok(bs)
    }
}

#[allow(dead_code)]
impl Index {
    //// construct a new Index
    pub fn new(table_meta: TableMeta) -> Result<Index, DiskError> {
        Ok(Index {
            table_meta,
            index_data: vec![],
            num_rows: 0,
        })
    }

    /// build index from table bin file
    pub fn build_from_bin(&mut self, base_path: &str) -> Result<(), DiskError> {
        // perform storage check toward table level
        DiskInterface::storage_hierarchy_check(
            base_path,
            Some(&self.table_meta.username),
            Some(&self.table_meta.db_name),
            Some(&self.table_meta.name),
        )
        .map_err(|e| e)?;

        // load table bin as chunk of bytes
        let table_bin_path = format!(
            "{}/{}/{}/{}.bin",
            base_path, self.table_meta.username, self.table_meta.db_name, self.table_meta.name
        );
        let table_bin_file = fs::File::open(&table_bin_path)?;
        let mut buffered = BufReader::new(table_bin_file);

        let mut chunk_bytes = vec![];
        buffered.read_to_end(&mut chunk_bytes)?;

        // parse chunk of bytes to vector of rows
        let mut new_index_data: Vec<RowPair> = vec![];
        let mut num_rows: u32 = 0;
        for (row_id, row_bytes) in chunk_bytes.chunks(self.table_meta.row_length as usize).enumerate() {
            // ignore deleted rows
            if row_bytes[0] == 1 as u8 {
                new_index_data.push(RowPair::new(
                    row_id as u32,
                    row_bytes[self.table_meta.attr_offset_ranges[1][0] as usize
                        ..self.table_meta.attr_offset_ranges[1][1] as usize]
                        .to_vec(),
                ));
            }
            num_rows += 1;
        }

        new_index_data.sort_by(|rp1, rp2| rp1.key_value.cmp(&rp2.key_value));

        self.index_data = new_index_data;
        self.num_rows = num_rows;

        Ok(())
    }

    /// save(overwrite) index table into index file
    pub fn save(&self, base_path: &str) -> Result<(), DiskError> {
        // perform storage check toward table level
        DiskInterface::storage_hierarchy_check(
            base_path,
            Some(&self.table_meta.username),
            Some(&self.table_meta.db_name),
            Some(&self.table_meta.name),
        )
        .map_err(|e| e)?;

        // create chunk of bytes to be written
        let mut chunk_bytes = vec![];
        for rp in self.index_data.iter() {
            chunk_bytes.extend_from_slice(&rp.to_bytes()?);
        }

        // write chunk of bytes to index bin
        let index_bin_path = format!(
            "{}/{}/{}/{}_{}.idx",
            base_path,
            self.table_meta.username,
            self.table_meta.db_name,
            self.table_meta.name,
            self.table_meta.primary_key[0]
        );
        let mut index_bin_file = fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(index_bin_path)?;
        index_bin_file.write_all(&chunk_bytes)?;

        Ok(())
    }

    /// Load index from storage
    pub fn load(&mut self, base_path: &str) -> Result<(), DiskError> {
        // perform storage check toward table level
        DiskInterface::storage_hierarchy_check(
            base_path,
            Some(&self.table_meta.username),
            Some(&self.table_meta.db_name),
            Some(&self.table_meta.name),
        )
        .map_err(|e| e)?;

        let index_bin_path = format!(
            "{}/{}/{}/{}_{}.idx",
            base_path,
            self.table_meta.username,
            self.table_meta.db_name,
            self.table_meta.name,
            self.table_meta.primary_key[0]
        );
        if !Path::new(&index_bin_path).exists() {
            return Err(DiskError::TableIdxFileNotExists);
        }
        let index_bin_file = fs::File::open(&index_bin_path)?;
        let mut buffered = BufReader::new(index_bin_file);

        let mut chunk_bytes = vec![];
        buffered.read_to_end(&mut chunk_bytes)?;

        // parse chunk of bytes to vector of rows
        let mut index_data: Vec<RowPair> = vec![];
        for rp_bytes in chunk_bytes
            .chunks((self.table_meta.attr_offset_ranges[1][1] - self.table_meta.attr_offset_ranges[1][0] + 4) as usize)
        {
            index_data.push(RowPair::new(
                (&rp_bytes[..4]).read_u32::<BigEndian>()?,
                rp_bytes[4..].to_vec(),
            ));
        }

        self.index_data = index_data;
        self.num_rows = DiskInterface::get_num_rows(
            &self.table_meta.username,
            &self.table_meta.db_name,
            &self.table_meta.name,
            Some(base_path),
        )?;

        Ok(())
    }

    /// insert a row-key pair into the index
    pub fn insert(&mut self, row: &Row) -> Result<(), DiskError> {
        let new_row_pair = RowPair::new(
            self.num_rows.clone(),
            BytesCoder::attr_to_bytes(
                &self.table_meta.attrs[&self.table_meta.primary_key[0]].datatype,
                row.data
                    .get(&self.table_meta.primary_key[0])
                    .ok_or_else(|| DiskError::AttrNotExists)?,
            )?,
        );
        match self
            .index_data
            .binary_search_by(|rp| rp.key_value.cmp(&new_row_pair.key_value))
        {
            Ok(_pos) => return Err(DiskError::DuplicatedKey),
            Err(pos) => {
                self.index_data.insert(pos, new_row_pair);
                self.num_rows += 1;
            }
        }

        Ok(())
    }

    /// delete a row-key pair from the index
    pub fn delete(&mut self, row: &Row) -> Result<(), DiskError> {
        let key_val = BytesCoder::attr_to_bytes(
            &self.table_meta.attrs[&self.table_meta.primary_key[0]].datatype,
            row.data
                .get(&self.table_meta.primary_key[0])
                .ok_or_else(|| DiskError::AttrNotExists)?,
        )?;
        match self.index_data.binary_search_by(|rp| rp.key_value.cmp(&key_val)) {
            Ok(pos) => self.index_data.remove(pos),
            Err(_pos) => return Err(DiskError::IndexKeyNotFound),
        };

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::component::datatype::DataType;
    use crate::component::field;
    use crate::component::field::Field;
    use crate::component::table::Table;
    use std::fs;
    use std::path::Path;

    #[test]
    pub fn test_build_save_load_insert_delete() {
        let file_base_path = "data9";
        if Path::new(file_base_path).exists() {
            fs::remove_dir_all(file_base_path).unwrap();
        }

        DiskInterface::create_file_base(Some(file_base_path)).unwrap();
        DiskInterface::create_username("crazyguy", Some(file_base_path)).unwrap();
        DiskInterface::create_db("crazyguy", "BookerDB", Some(file_base_path)).unwrap();

        let mut aff_table = Table::new("Affiliates");
        aff_table.fields.insert(
            "AffID".to_string(),
            Field::new_all("AffID", DataType::Int, true, None, field::Checker::None, false),
        );
        aff_table.fields.insert(
            "AffName".to_string(),
            Field::new_all(
                "AffName",
                DataType::Varchar(40),
                true,
                None,
                field::Checker::None,
                false,
            ),
        );
        aff_table.fields.insert(
            "AffEmail".to_string(),
            Field::new_all(
                "AffEmail",
                DataType::Varchar(50),
                true,
                None,
                field::Checker::None,
                false,
            ),
        );
        aff_table.fields.insert(
            "AffPhoneNum".to_string(),
            Field::new_all(
                "AffPhoneNum",
                DataType::Varchar(20),
                false,
                Some("+886900000000".to_string()),
                field::Checker::None,
                false,
            ),
        );
        aff_table.primary_key.push("AffID".to_string());

        DiskInterface::create_table("crazyguy", "BookerDB", &aff_table, Some(file_base_path)).unwrap();

        let data = vec![
            ("AffID", "2"),
            ("AffName", "Tom"),
            ("AffEmail", "tom@foo.com"),
            ("AffPhoneNum", "+886900000001"),
        ];
        aff_table.insert_row(data).unwrap();

        let data = vec![
            ("AffID", "7"),
            ("AffName", "Ben"),
            ("AffEmail", "ben@foo.com"),
            ("AffPhoneNum", "+886900000002"),
        ];
        aff_table.insert_row(data).unwrap();

        // to be deleted
        let data = vec![
            ("AffID", "6"),
            ("AffName", "Leo"),
            ("AffEmail", "leo@dee.com"),
            ("AffPhoneNum", "+886900000003"),
        ];
        aff_table.insert_row(data).unwrap();

        let data = vec![
            ("AffID", "1"),
            ("AffName", "John"),
            ("AffEmail", "john@dee.com"),
            ("AffPhoneNum", "+886900000004"),
        ];
        aff_table.insert_row(data).unwrap();

        // to be deleted
        let data = vec![
            ("AffID", "4"),
            ("AffName", "Ray"),
            ("AffEmail", "ray@dee.com"),
            ("AffPhoneNum", "+886900000005"),
        ];
        aff_table.insert_row(data).unwrap();

        // to be deleted
        let data = vec![
            ("AffID", "5"),
            ("AffName", "Bryn"),
            ("AffEmail", "bryn@dee.com"),
            ("AffPhoneNum", "+886900000006"),
        ];
        aff_table.insert_row(data).unwrap();

        let data = vec![
            ("AffID", "8"),
            ("AffName", "Eric"),
            ("AffEmail", "eric@doo.com"),
            ("AffPhoneNum", "+886900000007"),
        ];
        aff_table.insert_row(data).unwrap();

        let data = vec![
            ("AffID", "3"),
            ("AffName", "Vinc"),
            ("AffEmail", "vinc@doo.com"),
            ("AffPhoneNum", "+886900000008"),
        ];
        aff_table.insert_row(data).unwrap();

        DiskInterface::append_rows(
            "crazyguy",
            "BookerDB",
            "Affiliates",
            &aff_table.rows[..].iter().cloned().collect(),
            Some(file_base_path),
        )
        .unwrap();

        DiskInterface::delete_rows("crazyguy", "BookerDB", "Affiliates", &vec![2, 3], Some(file_base_path)).unwrap();
        DiskInterface::delete_rows("crazyguy", "BookerDB", "Affiliates", &vec![4, 6], Some(file_base_path)).unwrap();

        let index =
            DiskInterface::build_index_from_table_bin("crazyguy", "BookerDB", "Affiliates", Some(file_base_path))
                .unwrap();

        assert_eq!(index.index_data.len(), 5);
        assert_eq!(index.num_rows, 8);

        for i in 1..index.index_data.len() {
            assert!(index.index_data[i - 1].key_value < index.index_data[i].key_value);
        }

        let index_data = index.index_data.to_vec();

        DiskInterface::save_index(&index, Some(file_base_path)).unwrap();
        let mut index = DiskInterface::load_index("crazyguy", "BookerDB", "Affiliates", Some(file_base_path)).unwrap();

        assert_eq!(index_data, index.index_data);

        let data = vec![
            ("AffID", "5"),
            ("AffName", "Allie"),
            ("AffEmail", "allie@doo.com"),
            ("AffPhoneNum", "+886900000005"),
        ];
        aff_table.insert_row(data).unwrap();

        index.insert(&aff_table.rows[aff_table.rows.len() - 1]).unwrap();

        assert_eq!(index.index_data.len(), 6);
        for i in 1..index.index_data.len() {
            assert!(index.index_data[i - 1].key_value < index.index_data[i].key_value);
        }
        assert_eq!(index.num_rows, 9);

        assert_eq!(
            index.insert(&aff_table.rows[aff_table.rows.len() - 1]).unwrap_err(),
            DiskError::DuplicatedKey,
        );

        index.delete(&aff_table.rows[aff_table.rows.len() - 2]).unwrap();

        assert_eq!(index.index_data.len(), 5);
        for i in 1..index.index_data.len() {
            assert!(index.index_data[i - 1].key_value < index.index_data[i].key_value);
        }
        assert_eq!(index.num_rows, 9);

        assert_eq!(
            index.delete(&aff_table.rows[aff_table.rows.len() - 2]).unwrap_err(),
            DiskError::IndexKeyNotFound,
        );

        let index_data = index.index_data.to_vec();

        DiskInterface::save_index(&index, Some(file_base_path)).unwrap();
        let index = DiskInterface::load_index("crazyguy", "BookerDB", "Affiliates", Some(file_base_path)).unwrap();

        assert_eq!(index_data, index.index_data);
    }
}