pinenut-log 0.0.2

An extremely high performance logging system for clients (iOS, Android, Desktop), written in Rust.
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
//! The `Chunk` structure.
//!
//! `Chunk` is a storage unit in Pinenut. In double-buffering system, each side
//! of the buffer maps to an input or output chunk, and the Pinenut log file consists
//! of consecutive chunks. Different compression and encryption algorithms can be
//! selected for log processing between different chunks.
//!
//! # The underlying structure
//!
//! ```plain
//!     ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   n   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐              
//!    ├──── 60 ────┬───────────── n - 60 ──────────────┐              
//!    ▼────────────▼───────────────────────────────────▼              
//! ┌──│   Header   │              Payload              │              
//! │  └────────────┴───────────────────────────────────┘              
//! │  ┌─────────┬───────────┬──────────┬─────────────┬──────────────┬──────────────┐
//! └─▶│  Magic  │  Version  │  Length  │  Writeback  │  Time Range  │  Public Key  │
//!    ▲─────────▲───────────▲──────────▲─────────────▲───┬──────────▲──────────────▲
//!    └─── 4 ───┴──── 2 ────┴─── 4 ────┴───── 1 ─────┴───┼─ 16 ─────┴───── 33 ─────┘
//!                                                       │     ┌─────────┬─────────┐
//!                                                       └────▶│  Start  │   End   │
//!                                                             ▲─────────▲─────────▲
//!                                                             └─── 8 ───┴─── 8 ───┘
//! ```

use std::{
    fmt::{Display, Formatter},
    mem,
    ops::{Deref, DerefMut},
};

use thiserror::Error;

use crate::{encrypt::ecdh::PublicKey, DateTime, Magic, FORMAT_VERSION};

/// Errors that can be occurred during chunk write operations.
#[derive(Error, Clone, Debug)]
pub enum Error {
    /// The chunk has overflowed, the input bytes are too large.
    #[error("chunk overflow")]
    Overflow,
}

/// Represents the `Chunk` structure.
pub(crate) struct Chunk<T>(T);

/// Represents the inclusive time range spanned by a chunk (`start..=end`).
#[repr(C)]
#[derive(Clone, Debug)]
pub(crate) struct TimeRange {
    start: [u8; 8],
    end: [u8; 8],
}

impl TimeRange {
    /// The start datetime of the chunk.
    #[inline]
    pub(crate) fn start(&self) -> DateTime {
        let timestamp = i64::from_le_bytes(self.start);
        // For chunk, time accuracy does not have to be down to nanoseconds.
        DateTime::from_timestamp(timestamp, 0).unwrap_or_default()
    }

    /// The end datetime of the chunk.
    #[inline]
    pub(crate) fn end(&self) -> DateTime {
        let timestamp = i64::from_le_bytes(self.end);
        // For chunk, time accuracy does not have to be down to nanoseconds.
        DateTime::from_timestamp(timestamp, 0).unwrap_or_default()
    }
}

impl Display for TimeRange {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} - {}", self.start(), self.end())
    }
}

/// Represents the header of the chunk.
#[repr(C)]
#[derive(Clone, Debug)]
pub(crate) struct Header {
    magic: [u8; 4],
    version: [u8; 2],
    length: [u8; 4],
    writeback: bool,
    time_range: TimeRange,
    pub_key: PublicKey,
}

impl Header {
    /// Length of a header in bytes. (8 bytes)
    pub(crate) const LEN: usize = mem::size_of::<Self>();

    /// It means: `Feed Cat Chunk`.
    const MAGIC: Magic = Magic::new(0xFEEDCA7C);

    /// Checks the correctness of the chunk.
    #[inline]
    pub(crate) fn validate(&self) -> bool {
        Self::MAGIC == self.magic.into()
    }

    /// The format version of the chunk.
    #[inline]
    pub(crate) fn version(&self) -> u16 {
        u16::from_le_bytes(self.version)
    }

    /// The length of the chunk payload.
    #[inline]
    pub(crate) fn payload_len(&self) -> usize {
        u32::from_le_bytes(self.length) as usize
    }

    /// Represents a chunk to be written back.
    #[inline]
    pub(crate) fn writeback(&self) -> bool {
        self.writeback
    }

    /// The time range spanned by the chunk.
    #[inline]
    pub(crate) fn time_range(&self) -> &TimeRange {
        &self.time_range
    }

    /// The ECDH public key associated with the chunk.
    #[inline]
    pub(crate) fn pub_key(&self) -> PublicKey {
        self.pub_key
    }

    /// Converts header to bytes representation.
    #[inline]
    pub(crate) fn bytes(self) -> [u8; Self::LEN] {
        // SAFETY: Here the length is guaranteed to be correct.
        unsafe { mem::transmute(self) }
    }
}

impl<T> Chunk<T>
where
    T: Deref<Target = [u8]>,
{
    #[inline]
    pub(crate) fn bind(inner: T) -> Self {
        // Check length and alignment.
        // The alignment of Header is `1`, so memory always conforms to this.
        debug_assert!(inner.len() >= Header::LEN, "the storage is too small");
        Self(inner)
    }

    /// Checks the correctness of the chunk.
    #[inline]
    pub(crate) fn validate(&self) -> bool {
        self.header().validate() && self.header().payload_len() <= self.capacity()
    }

    /// The start datetime of the chunk.
    #[inline]
    pub(crate) fn start_datetime(&self) -> DateTime {
        self.header().time_range().start()
    }

    /// The length of the chunk payload.
    #[inline]
    pub(crate) fn payload_len(&self) -> usize {
        self.header().payload_len()
    }

    /// Checks whether the chunk is almost full.
    pub(crate) fn is_almost_full(&self) -> bool {
        const RATIO: f64 = 0.8;
        self.payload_len() as f64 >= RATIO * self.capacity() as f64
    }

    /// The capacity of the chunk payload.
    #[inline]
    fn capacity(&self) -> usize {
        self.0.len() - Header::LEN
    }

    #[inline]
    fn header(&self) -> &Header {
        // SAFETY: The pointer to the inner is properly aligned for a `Header`. Also, it has
        // been verified at construction to ensure that there are no pointer out-of-bounds
        // issues here.
        unsafe {
            let ptr = self.0.as_ptr() as *const Header;
            &*ptr
        }
    }
}

impl<T> Chunk<T>
where
    T: DerefMut<Target = [u8]>,
{
    /// Initialize the chunk.
    #[inline]
    pub(crate) fn initialize(&mut self, datetime: DateTime, pub_key: PublicKey) {
        let header = self.header_mut();
        header.magic = Header::MAGIC.into();
        header.version = FORMAT_VERSION.to_le_bytes();
        header.length = 0u32.to_le_bytes();
        header.writeback = false;
        header.pub_key = pub_key;

        let datetime = datetime.timestamp().to_le_bytes();
        header.time_range = TimeRange { start: datetime, end: datetime };
    }

    /// Writes bytes to the payload of the chunk.
    #[inline]
    pub(crate) fn write(&mut self, bytes: &[u8]) -> Result<(), Error> {
        let old_len = self.payload_len();
        let new_len = old_len + bytes.len();

        // Checking for overflow
        if new_len > self.capacity() {
            return Err(Error::Overflow);
        }

        let payload = self.payload_mut();
        payload[old_len..new_len].copy_from_slice(bytes);
        self.set_payload_len(new_len);
        Ok(())
    }

    /// Sets the current chunk to be written back.
    #[inline]
    pub(crate) fn set_writeback(&mut self) {
        self.header_mut().writeback = true;
    }

    /// Sets the end datetime of the chunk.
    #[inline]
    pub(crate) fn set_end_datetime(&mut self, datetime: DateTime) {
        self.header_mut().time_range.end = datetime.timestamp().to_le_bytes();
    }

    /// Clears the payload of the chunk.
    #[inline]
    pub(crate) fn clear(&mut self) {
        self.set_payload_len(0);
    }

    #[inline]
    fn set_payload_len(&mut self, len: usize) {
        let len: u32 = len.try_into().expect("len is too large");
        self.header_mut().length = len.to_le_bytes();
    }

    #[inline]
    fn payload_mut(&mut self) -> &mut [u8] {
        &mut self.0[Header::LEN..]
    }

    #[inline]
    fn header_mut(&mut self) -> &mut Header {
        // SAFETY: The pointer to the inner is properly aligned for a `Header`. Also, it has
        // been verified at construction to ensure that there are no pointer out-of-bounds
        // issues here.
        unsafe {
            let ptr = self.0.as_mut_ptr() as *mut Header;
            &mut *ptr
        }
    }
}

impl<T> Deref for Chunk<T>
where
    T: Deref<Target = [u8]>,
{
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &Self::Target {
        let len = self.payload_len().min(self.capacity()) + Header::LEN;
        &self.0[..len]
    }
}

pub(crate) use reader::{Error as ReadError, Reader};

/// The internal module that implements the chunk reader.
pub(crate) mod reader {
    use std::io;

    use thiserror::Error;

    use crate::{
        chunk::Header,
        common::{BytesBuf, Sink},
        BUFFER_LEN,
    };

    /// Errors that can be occurred during chunk read operations.
    #[derive(Error, Debug)]
    pub(crate) enum Error {
        #[error("invalid chunk")]
        Invalid,
        #[error(transparent)]
        Io(#[from] io::Error),
        #[error("unexpected end of bytes")]
        UnexpectedEnd,
    }

    /// Represents a reader that reads chunks from the underlying [`io::Read`].
    ///
    /// To read chunk, the methods are called in the following order:
    /// * [`Reader::read_header_or_end`]
    /// * [`Reader::read_payload`]
    ///
    /// These methods are called in a loop until either an error occurs or the
    /// underlying reader finishes reading.
    pub(crate) struct Reader<R> {
        inner: R,
        buffer: BytesBuf,
    }

    impl<R> Reader<R>
    where
        R: io::Read + io::Seek,
    {
        /// Construct a new `Reader`.
        #[inline]
        pub(crate) fn new(inner: R) -> Self {
            Self { inner, buffer: BytesBuf::with_capacity(BUFFER_LEN) }
        }

        /// Reads the head of the chunk. If the underlying reader has reached the
        /// end, returns `None`.
        pub(crate) fn read_header_or_reach_to_end(&mut self) -> Result<Option<&Header>, Error> {
            let buffer = self.buffer.as_buffer_mut_slice();
            assert!(buffer.len() >= Header::LEN, "buffer is too small");

            let mut read_len = 0;
            while read_len < Header::LEN {
                match self.inner.read(&mut buffer[read_len..Header::LEN]) {
                    Ok(0) => {
                        return if read_len == 0 { Ok(None) } else { Err(Error::UnexpectedEnd) }
                    }
                    Ok(len) => read_len += len,
                    Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                    Err(e) => return Err(e.into()),
                }
            }

            // SAFETY: Here the length is guaranteed to be correct. The alignment of Header is
            // `1`, so memory always conforms to this.
            let header: &Header = unsafe {
                let ptr = buffer.as_ptr() as *const Header;
                &*ptr
            };

            if header.validate() {
                Ok(Some(header))
            } else {
                Err(Error::Invalid)
            }
        }

        /// Reads the payload of the chunk with payload length.
        pub(crate) fn read_payload<S>(&mut self, len: usize, sink: &mut S) -> Result<(), S::Error>
        where
            S: Sink<Error>,
        {
            let buffer = self.buffer.as_buffer_mut_slice();
            let mut remaining = len;

            while remaining > 0 {
                let capacity = buffer.len().min(remaining);
                match self.inner.read(&mut buffer[..capacity]) {
                    Ok(0) => return Err(Error::UnexpectedEnd.into()),
                    Ok(len) => {
                        remaining -= len;
                        sink.sink(&buffer[..len]).inspect_err(|_| {
                            // Reader needs to ensure that it has pointed to the next chunk to be
                            // read.
                            _ = self.inner.seek(io::SeekFrom::Current(
                                remaining.try_into().expect("payload is too large"),
                            ));
                        })?;
                    }
                    Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                    Err(e) => return Err(Error::Io(e).into()),
                }
            }

            Ok(())
        }

        /// Skips the current payload with payload length.
        #[inline]
        pub(crate) fn skip(&mut self, len: usize) -> Result<(), Error> {
            let len: i64 = len.try_into().expect("chunk is too large");
            self.inner.seek(io::SeekFrom::Current(len))?;
            Ok(())
        }
    }
}