#mmap #vec #memory-buffer #file #memmap2 #read-write

memvec

Memory-backed vector, not buffer. Designed for for mmap. Not MemMap, but MemVec!

5 releases

0.1.0 Aug 22, 2022
0.0.5 Jun 18, 2022
0.0.3 May 5, 2022

#1080 in Data structures

Download history 220/week @ 2024-07-22 496/week @ 2024-07-29 462/week @ 2024-08-05 489/week @ 2024-08-12 227/week @ 2024-08-19 372/week @ 2024-08-26 266/week @ 2024-09-02 587/week @ 2024-09-09 400/week @ 2024-09-16 254/week @ 2024-09-23 65/week @ 2024-09-30 127/week @ 2024-10-07 31/week @ 2024-10-14 33/week @ 2024-10-21 34/week @ 2024-10-28 103/week @ 2024-11-04

202 downloads per month

BSD-2-Clause

42KB
932 lines

MemVec

MemVec is a Vec-like interface for continuous memory buffer. Mostly designed to be used with mmap.

VecFile is a simple linear database. For the database, a file is a collection of records. VecFile is the Vec interface of a file of records without read, write, and seek but using mmap.

VecFile + MemVec

use memvec::{MemVec, VecFile};

#[derive(Copy, Clone)]
#[repr(C, packed)]
struct Record {
    time: std::time::Instant,
    event_id: u32,
    _payload: [u8; 50], // we will not use it
}

fn main() {
    let mut path = std::env::temp_dir();
    path.push("vecfile.memvec");

    let vec_file = VecFile::open(&path).expect("file open failed");
    let mut vec =
        unsafe { MemVec::<Record, _>::try_from_memory(vec_file) }.expect("vec file is corrupted");

    if vec.len() == 0 {
        // creating a new file
        println!("creating a new file: {path:?}");
        for i in 0..10 {
            vec.push(Record {
                time: std::time::Instant::now(),
                event_id: '1' as u32 + (i % 3),
                _payload: unsafe { std::mem::zeroed() },
            })
        }
    } else {
        // found an existing file
        for (i, item) in vec.iter().enumerate() {
            println!(
                "idx: {i} time: {time:?} event_id: {event_id}",
                time = item.time,
                event_id = item.event_id
            );
        }
        vec.clear();
        println!("deleted existing file: {path:?}");
    }
}

Dependencies

~170KB