use magnus::typed_data::Obj; use magnus::value::Fixnum; use magnus::{Integer, RArray, RClass, RHash, RString, Value}; use crate::file_entry::FileEntry; use crate::map::EntryMap; /// A Rust struct wrapped in a Ruby object, providing access to a memory-mapped /// file used to store, update, and read out Prometheus metrics. /// /// - File format: /// - Header: /// - 4 bytes: u32 - total size of metrics in file. /// - 4 bytes: NUL byte padding. /// - Repeating metrics entries: /// - 4 bytes: u32 - entry JSON string size. /// - `N` bytes: UTF-8 encoded JSON string used as entry key. /// - (8 - (4 + `N`) % 8) bytes: 1 to 8 padding space (0x20) bytes to /// reach 8-byte alignment. /// - 8 bytes: f64 - entry value. /// /// All numbers are saved in native-endian format. /// /// Generated via [luismartingarcia/protocol](https://github.com/luismartingarcia/protocol): /// /// /// ``` /// protocol "Used:4,Pad:4,K1 Size:4,K1 Name:4,K1 Value:8,K2 Size:4,K2 Name:4,K2 Value:8" /// /// 0 1 2 3 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | Used | Pad |K1 Size|K1 Name| K1 Value |K2 Size|K2 Name| /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /// | K2 Value | /// +-+-+-+-+-+-+-+ /// ``` #[derive(Debug, Default)] #[magnus::wrap(class = "FastMmapedFileRs", free_immediately, size)] pub struct MmapedFile; impl MmapedFile { /// call-seq: /// new(file) /// /// create a new Mmap object /// /// * file /// /// /// Creates a mapping that's shared with all other processes /// mapping the same area of the file. pub fn new(_klass: RClass, _args: &[Value]) -> magnus::error::Result> { Ok(Obj::wrap(Self)) } /// Initialize a new `FastMmapedFileRs` object. This must be defined in /// order for inheritance to work. pub fn initialize(_rb_self: Obj, _fname: String) -> magnus::error::Result<()> { unimplemented!(); } /// Read the list of files provided from Ruby and convert them to a Prometheus /// metrics String. pub fn to_metrics(file_list: RArray) -> magnus::error::Result { let mut map = EntryMap::new(); map.aggregate_files(file_list)?; let sorted = map.into_sorted()?; FileEntry::entries_to_string(sorted).map_err(|e| e.into()) } /// Document-method: [] /// Document-method: slice /// /// call-seq: [](args) /// /// Element reference - with the following syntax: /// /// self[nth] /// /// retrieve the nth character /// /// self[start..last] /// /// return a substring from start to last /// /// self[start, length] /// /// return a substring of lenght characters from start pub fn slice(_rb_self: Obj, _args: &[Value]) -> magnus::error::Result { unimplemented!(); } /// Document-method: msync /// Document-method: sync /// Document-method: flush /// /// call-seq: msync /// /// flush the file pub fn sync(&self, _args: &[Value]) -> magnus::error::Result<()> { unimplemented!(); } /// Document-method: munmap /// Document-method: unmap /// /// call-seq: munmap /// /// terminate the association pub fn munmap(_rb_self: Obj) -> magnus::error::Result<()> { unimplemented!(); } /// Fetch the `used` header from the `.db` file, the length /// in bytes of the data written to the file. pub fn load_used(&self) -> magnus::error::Result { unimplemented!(); } /// Update the `used` header for the `.db` file, the length /// in bytes of the data written to the file. pub fn save_used(_rb_self: Obj, _used: Fixnum) -> magnus::error::Result { unimplemented!(); } /// Fetch the value associated with a key from the mmap. /// If no entry is present, initialize with the default /// value provided. pub fn fetch_entry( _rb_self: Obj, _positions: RHash, _key: RString, _default_value: f64, ) -> magnus::error::Result { unimplemented!(); } /// Update the value of an existing entry, if present. Otherwise create a new entry /// for the key. pub fn upsert_entry( _rb_self: Obj, _positions: RHash, _key: RString, _value: f64, ) -> magnus::error::Result { unimplemented!(); } }