rasterizeddb_core 0.0.8

A schemaless, high-performance database written in Rust, designed for speed and scalability.
Documentation
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::{
    arch::x86_64::{_mm_prefetch, _MM_HINT_T0},
    io::{self, Cursor, Read, Seek, SeekFrom}
};

use byteorder::{LittleEndian, ReadBytesExt};

use super::{
    column::Column,
    db_type::DbType,
    row::Row,
    storage_providers::traits::IOOperationsSync,
    support_types::{CursorVector, FileChunk, RowPrefetchResult},
};
use crate::{
    simds::endianess::{read_u32, read_u64, read_u8},
    CHUNK_SIZE, EMPTY_BUFFER, HEADER_SIZE,
};

pub(crate) fn read_row_cursor<'a>(
    first_column_index: u64,
    id: u64,
    length: u32,
    cursor: &mut Cursor<&Vec<u8>>,
) -> io::Result<Row> {
    cursor.seek(SeekFrom::Start(first_column_index)).unwrap();

    let mut columns: Vec<Column> = Vec::default();

    loop {
        let column_type = cursor.read_u8().unwrap();

        let db_type = DbType::from_byte(column_type);

        if db_type == DbType::END {
            break;
        }

        let mut data_buffer = Vec::default();

        if db_type != DbType::STRING {
            let db_size = db_type.get_size();

            let mut preset_buffer = vec![0; db_size as usize];
            cursor.read(&mut preset_buffer).unwrap();
            data_buffer.append(&mut preset_buffer.to_vec());
        } else {
            let str_length = cursor.read_u32::<LittleEndian>().unwrap();
            let mut preset_buffer = vec![0; str_length as usize];
            cursor.read(&mut preset_buffer).unwrap();
            data_buffer.append(&mut preset_buffer.to_vec());
        }

        let column = Column::from_raw_clone(column_type, &data_buffer);

        columns.push(column);
    }

    let mut columns_buffer: Vec<u8> = Vec::default();

    for column in columns {
        columns_buffer.append(&mut &mut column.content.to_vec());
    }

    return Ok(Row {
        id: id,
        length: length,
        columns_data: columns_buffer,
    });
}

pub(crate) async fn read_row_columns(
    io_sync: &mut Box<impl IOOperationsSync>,
    first_column_index: u64,
    id: u64,
    length: u32,
) -> io::Result<Row> {
    let mut columns: Vec<Column> = Vec::default();

    let mut position = first_column_index;

    let mut cursor = io_sync.read_data_to_cursor(&mut position, length).await;

    loop {
        let column_type = cursor.read_u8().unwrap();

        let db_type = DbType::from_byte(column_type);

        if db_type == DbType::END {
            break;
        }

        let mut data_buffer = Vec::default();

        if db_type != DbType::STRING {
            let db_size = db_type.get_size();

            let mut preset_buffer = vec![0; db_size as usize];
            cursor.read_exact(&mut preset_buffer)?;

            data_buffer.append(&mut preset_buffer);
        } else {
            let str_length = cursor.read_u32::<LittleEndian>().unwrap();

            let mut preset_buffer = vec![0; str_length as usize];
            cursor.read_exact(&mut preset_buffer)?;

            data_buffer.append(&mut preset_buffer);
        }

        let column = Column::from_raw_clone(column_type, &data_buffer);

        columns.push(column);
    }

    let mut columns_buffer: Vec<u8> = Vec::default();

    for column in columns {
        columns_buffer.append(&mut &mut column.content.to_vec());
    }

    return Ok(Row {
        id: id,
        length: length,
        columns_data: columns_buffer,
    });
}

pub(crate) async fn delete_row_file(
    first_column_index: u64,
    io_sync: &mut Box<impl IOOperationsSync>,
) -> io::Result<()> {
    // LEN (4)
    let mut position = first_column_index.clone() - 4;

    let columns_length = io_sync.read_data(&mut position, 4).await;
    let columns_length = Cursor::new(columns_length)
        .read_u32::<LittleEndian>()
        .unwrap();

    //ID (8) LEN (4) COLUMNS (columns_length) START (1) END (1)
    let empty_buffer = vec![0; (columns_length + 4 + 8 + 1 + 1) as usize];

    // Write the empty buffer to overwrite the data
    //ID (8) LEN (4) START (1)
    io_sync.write_data_seek(
        SeekFrom::Start(first_column_index - 4 - 8 - 1),
        &empty_buffer,
    );
    io_sync.verify_data_and_sync(first_column_index - 4 - 8 - 1, &empty_buffer);

    return Ok(());
}

pub(crate) async fn skip_empty_spaces_file(
    io_sync: &mut Box<impl IOOperationsSync>,
    file_position: &mut u64,
    file_length: u64,
) -> u64 {
    if file_length == *file_position || file_length < *file_position {
        return *file_position;
    }

    let mut check_next_buffer = io_sync.read_data(file_position, 8).await;

    if check_next_buffer == EMPTY_BUFFER {
        loop {
            io_sync
                .read_data_into_buffer(file_position, &mut check_next_buffer)
                .await;

            if file_length == *file_position || file_length < *file_position {
                return *file_position;
            }

            if check_next_buffer != EMPTY_BUFFER {
                break;
            }
        }

        let index_of_row_start = check_next_buffer
            .iter()
            .position(|x| *x == DbType::START.to_byte())
            .unwrap();

        let move_back = -8 as i64 + index_of_row_start as i64;

        *file_position = ((*file_position) as i64 + move_back) as u64;

        return *file_position;
    } else {
        let index_of_row_start = check_next_buffer
            .iter()
            .position(|x| *x == DbType::START.to_byte());

        if let Some(index_of_row_start) = index_of_row_start {
            let deduct = (index_of_row_start as i64 - 8) * -1;

            *file_position = ((*file_position) as i64 - deduct) as u64;
        } else {
            panic!(
                "FILE -> 254 DbType::START not found: {:?}",
                check_next_buffer
            );
        }
    }

    return *file_position;
}

pub(crate) fn skip_empty_spaces_cursor(
    position: &mut u64,
    cursor_vector: &mut CursorVector,
    cursor_length: u32,
) -> io::Result<()> {
    if cursor_length <= *position as u32 + 8 {
        return Ok(());
    }

    let check_next_buffer =
        &cursor_vector.vector.as_slice()[*position as usize..*position as usize + 8];

    let check_next_buffer_ptr = check_next_buffer.as_ptr();

    #[cfg(target_arch = "x86_64")]
    {
        unsafe { _mm_prefetch::<_MM_HINT_T0>(check_next_buffer_ptr as *const i8) };
    }

    *position += 8;

    if check_next_buffer == EMPTY_BUFFER {
        loop {
            let check_next_buffer =
                &cursor_vector.vector.as_slice()[*position as usize..*position as usize + 8];

            let check_next_buffer_ptr = check_next_buffer.as_ptr();

            #[cfg(target_arch = "x86_64")]
            {
                unsafe { _mm_prefetch::<_MM_HINT_T0>(check_next_buffer_ptr as *const i8) };
            }

            *position += 8;

            let cursor_position = *position;

            if cursor_length == cursor_position as u32 || cursor_length < cursor_position as u32 {
                return Ok(());
            }

            if !(check_next_buffer == EMPTY_BUFFER) {
                break;
            }
        }

        let index_of_row_start = check_next_buffer
            .iter()
            .position(|x| *x == DbType::START.to_byte())
            .unwrap();

        let move_back = -8 as i64 + index_of_row_start as i64;

        *position -= (move_back * -1) as u64;
    } else {
        let index_of_row_start = check_next_buffer
            .iter()
            .position(|x| *x == DbType::START.to_byte());

        if let Some(index_of_row_start) = index_of_row_start {
            let deduct = (index_of_row_start as i64 - 8) * -1;

            *position -= deduct as u64;
        } else {
            panic!(
                "CURSOR -> 254 DbType::START not found: VECTOR {:?}|POSITION {}",
                check_next_buffer, position
            );
        }
    }

    return Ok(());
}

pub(crate) async fn row_prefetching(
    io_sync: &mut Box<impl IOOperationsSync>,
    file_position: &mut u64,
    file_length: u64,
) -> io::Result<Option<RowPrefetchResult>> {
    if *file_position < file_length {
        *file_position = skip_empty_spaces_file(io_sync, file_position, file_length).await;

        let mut cursor = io_sync.read_data_to_cursor(file_position, 1 + 8 + 4).await;

        let start_now_byte = cursor.read_u8();

        if start_now_byte.is_err() {
            return Ok(None);
        }

        let start_row = DbType::from_byte(start_now_byte.unwrap());

        if start_row != DbType::START {
            panic!("Start row signal not present.");
        }

        let found_id = cursor.read_u64::<LittleEndian>().unwrap();

        let length = cursor.read_u32::<LittleEndian>().unwrap();

        return Ok(Some(RowPrefetchResult {
            found_id: found_id,
            length: length,
        }));
    } else {
        return Ok(None);
    }
}

#[inline(always)]
pub fn row_prefetching_cursor(
    position: &mut u64,
    cursor_vector: &mut CursorVector,
    chunk: &FileChunk,
) -> io::Result<Option<RowPrefetchResult>> {
    skip_empty_spaces_cursor(position, cursor_vector, chunk.chunk_size).unwrap();

    //let mut header_bytes: [u8; 13] = [0; 13];

    let slice = cursor_vector.vector.as_slice();

    if chunk.chunk_size <= *position as u32 + 8 {
        return Ok(None);
    }

    let header_bytes = &slice[*position as usize..*position as usize + 13];

    #[allow(static_mut_refs)]
    unsafe {
        let start_bytes = [header_bytes[0]];

        let found_id_bytes = [
            header_bytes[1],
            header_bytes[2],
            header_bytes[3],
            header_bytes[4],
            header_bytes[5],
            header_bytes[6],
            header_bytes[7],
            header_bytes[8],
        ];

        let length_bytes = [
            header_bytes[9],
            header_bytes[10],
            header_bytes[11],
            header_bytes[12],
        ];

        let start_now_ptr = start_bytes.as_ptr();

        #[cfg(target_arch = "x86_64")]
        {
            _mm_prefetch::<_MM_HINT_T0>(start_now_ptr as *const i8);
        }

        let start_signal = read_u8(start_now_ptr);

        let start_signal_type = DbType::from_byte(start_signal);

        if start_signal_type != DbType::START {
            panic!("Start row signal not present.");
        }

        let found_id_ptr = found_id_bytes.as_ptr();

        #[cfg(target_arch = "x86_64")]
        {
            _mm_prefetch::<_MM_HINT_T0>(found_id_ptr as *const i8);
        }

        let found_id = read_u64(found_id_ptr);

        let length_ptr = length_bytes.as_ptr();

        #[cfg(target_arch = "x86_64")]
        {
            _mm_prefetch::<_MM_HINT_T0>(length_ptr as *const i8);
        }

        let length = read_u32(length_ptr);

        *position += 13;

        return Ok(Some(RowPrefetchResult {
            found_id: found_id,
            length: length,
        }));
    }
}

pub(crate) fn add_in_memory_index(
    current_chunk_size: &mut u32,
    current_id: u64,
    file_position: u64,
    in_memory_index: &mut Option<Vec<FileChunk>>,
) -> bool {
    if *current_chunk_size > CHUNK_SIZE {
        if let Some(file_chunks_indexes) = in_memory_index.as_mut() {
            let entry = FileChunk {
                current_file_position: file_position - *current_chunk_size as u64,
                chunk_size: *current_chunk_size,
                next_row_id: current_id,
            };
            file_chunks_indexes.push(entry);
        } else {
            let mut chunks_vec: Vec<FileChunk> = Vec::default();
            let first_entry = FileChunk {
                current_file_position: HEADER_SIZE as u64,
                chunk_size: *current_chunk_size,
                next_row_id: current_id,
            };

            chunks_vec.push(first_entry);
            *in_memory_index = Some(chunks_vec);
        }

        *current_chunk_size = 0;

        true
    } else {
        false
    }
}

pub(crate) fn add_last_in_memory_index(
    file_position: u64,
    file_length: u64,
    in_memory_index: &mut Option<Vec<FileChunk>>,
) {
    if file_position <= file_length {
        if let Some(file_chunks_indexes) = in_memory_index.as_mut() {
            let entry = FileChunk {
                current_file_position: file_position,
                chunk_size: file_length as u32 - file_position as u32,
                next_row_id: 0,
            };
            file_chunks_indexes.push(entry);
        } else {
            let mut chunks_vec: Vec<FileChunk> = Vec::default();

            let first_entry = FileChunk {
                current_file_position: HEADER_SIZE as u64,
                chunk_size: file_length as u32 - file_position as u32,
                next_row_id: 0,
            };

            chunks_vec.push(first_entry);
            *in_memory_index = Some(chunks_vec);
        }
    }
}

pub(crate) async fn indexed_row_fetching_file(
    io_sync: &mut Box<impl IOOperationsSync>,
    position: &mut u64,
    length: u32,
) -> io::Result<Row> {
    let mut cursor = io_sync
        .read_data_to_cursor(position, (length + 1 + 1 + 8 + 4) as u32)
        .await;

    let start_now_byte = cursor.read_u8();

    let start_row = DbType::from_byte(start_now_byte.unwrap());

    if start_row != DbType::START {
        panic!("Start row signal not present.");
    }

    let found_id = cursor.read_u64::<LittleEndian>().unwrap();

    let length = cursor.read_u32::<LittleEndian>().unwrap();

    let mut columns: Vec<Column> = Vec::default();

    loop {
        let column_type = cursor.read_u8().unwrap();

        let db_type = DbType::from_byte(column_type);

        if db_type == DbType::END {
            break;
        }

        let mut data_buffer = Vec::default();

        if db_type != DbType::STRING {
            let db_size = db_type.get_size();

            let mut preset_buffer = vec![0; db_size as usize];
            cursor.read(&mut preset_buffer).unwrap();
            data_buffer.append(&mut preset_buffer.to_vec());
        } else {
            let str_length = cursor.read_u32::<LittleEndian>().unwrap();
            let mut preset_buffer = vec![0; str_length as usize];
            cursor.read(&mut preset_buffer).unwrap();
            data_buffer.append(&mut preset_buffer.to_vec());
        }

        let column = Column::from_raw_clone(column_type, &data_buffer);

        columns.push(column);
    }

    let mut columns_buffer: Vec<u8> = Vec::default();

    for column in columns {
        columns_buffer.append(&mut &mut column.content.to_vec());
    }

    return Ok(Row {
        id: found_id,
        length: length,
        columns_data: columns_buffer,
    });
}

pub(crate) fn columns_cursor_to_row(
    mut columns_cursor: Cursor<Vec<u8>>,
    id: u64,
    length: u32,
) -> io::Result<Row> {
    let mut columns: Vec<Column> = Vec::default();

    columns_cursor.set_position(0);

    loop {
        let column_type = columns_cursor.read_u8().unwrap();

        let db_type = DbType::from_byte(column_type);

        if db_type == DbType::END {
            break;
        }

        let mut data_buffer = Vec::default();

        if db_type != DbType::STRING {
            let db_size = db_type.get_size();

            let mut preset_buffer = vec![0; db_size as usize];
            columns_cursor.read(&mut preset_buffer).unwrap();
            data_buffer.append(&mut preset_buffer.to_vec());
        } else {
            let str_length = columns_cursor.read_u32::<LittleEndian>().unwrap();
            let mut preset_buffer = vec![0; str_length as usize];
            columns_cursor.read(&mut preset_buffer).unwrap();
            data_buffer.append(&mut preset_buffer.to_vec());
        }

        let column = Column::from_raw_clone(column_type, &data_buffer);

        columns.push(column);
    }

    let mut columns_buffer: Vec<u8> = Vec::default();

    for column in columns {
        columns_buffer.append(&mut column.content.to_vec());
    }

    return Ok(Row {
        id: id,
        length: length,
        columns_data: columns_buffer,
    });
}