mariadb-cli 0.1.1

One-shot agent-native command line client for MariaDB servers
Documentation
//! Proof 5, second half: a long command file does not follow its own volume.
//!
//! The first half of the proof, a million rows read as a flow, is measured by
//! counting what the flow holds. A cut cannot be measured that way without
//! trusting a counter somebody has to remember to update, so this measures the
//! heap itself: every allocation the process makes while the file is being cut
//! is counted, and the count is read at the moments that matter.
//!
//! This file carries one test on purpose. The counter is global to the process,
//! so a second test running beside it would be measuring both.

use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};

/// Bytes handed out and not yet given back.
static LIVE: AtomicUsize = AtomicUsize::new(0);

/// The system allocator, counting what it lends.
struct Counting;

// SAFETY: every call is forwarded to the system allocator unchanged, and the
// only addition is an atomic counter, which allocates nothing itself.
unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let pointer = unsafe { System.alloc(layout) };
        if !pointer.is_null() {
            LIVE.fetch_add(layout.size(), Ordering::Relaxed);
        }
        pointer
    }

    unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
        LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
        unsafe { System.dealloc(pointer, layout) };
    }

    unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        let moved = unsafe { System.realloc(pointer, layout, new_size) };
        if !moved.is_null() {
            LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
            LIVE.fetch_add(new_size, Ordering::Relaxed);
        }
        moved
    }
}

#[global_allocator]
static ALLOCATOR: Counting = Counting;

/// Bytes the process is holding right now.
fn live() -> usize {
    LIVE.load(Ordering::Relaxed)
}

/// A file of many statements, each one small, so that what grows is the count.
fn file_of(statements: usize) -> String {
    let mut file = String::new();
    for index in 0..statements {
        use std::fmt::Write as _;
        // Written straight into the file rather than through a second string
        // built only to be copied in and dropped on every turn of the loop.
        let _ = writeln!(file, "SELECT {index};");
    }
    file
}

/// FR-028. Every statement of the file is handed over in order and carries its
/// own position, from the first to the twenty thousandth, which is what a
/// result reported per command with its position rests on.
///
/// A long command file costs its longest statement, and never its whole self.
///
/// Two things are measured, because either one alone would pass a cut that is
/// still wrong.
///
/// The first is that what the walk holds *besides the statement in the
/// caller's hand* is the same at the twenty thousandth statement as at the
/// first. Asking whether the heap is simply equal at both ends would be the
/// wrong question and would fail on a correct walk: the statements are not all
/// the same length, so holding one of them is not always the same number of
/// bytes. Taking the statement's own length out is what leaves the property
/// the proof is about, and it is exact rather than approximate.
///
/// The second is that the walk holds far less than the same statements
/// gathered. Without it a cut that chopped the file up before handing the
/// first statement over would pass: such a cut is also perfectly flat, at the
/// cost of the whole file.
#[test]
fn a_long_command_file_is_cut_as_a_flow_and_never_held_whole() {
    let statements = 20_000;
    let file = file_of(statements);

    let before = live();
    let mut walk = mariadb_cli::exec::script::split(&file, ";").expect("the file is readable");

    let first = walk.next().expect("a first statement");
    // What the walk holds that is not the statement being looked at.
    let besides_the_first = live() - before - first.sql.len();
    assert_eq!(first.index, 1);

    let mut seen = 1;
    let mut widest = first.sql.len();
    let mut last = first;
    for piece in &mut walk {
        seen += 1;
        // The statement handed over before this one is dropped by the
        // assignment, so exactly one statement is alive when the heap is read.
        last = piece;
        widest = widest.max(last.sql.len());
        assert_eq!(
            live() - before - last.sql.len(),
            besides_the_first,
            "the cut grew by statement {}",
            last.index
        );
    }
    drop(walk);

    assert_eq!(seen, statements, "every statement is handed over");
    assert_eq!(last.index, statements);
    assert_eq!(last.sql, format!("SELECT {}", statements - 1));

    // The peak is the file's blanked twin plus the longest single statement,
    // which is what "costs its longest statement" means in bytes.
    let peak = besides_the_first + widest;
    assert!(
        peak < file.len() + 1024,
        "the walk peaked at {peak} bytes over a {} byte file, so it is holding more than \
         the file's blanked twin and one statement",
        file.len()
    );

    // The same statements gathered, which is what the cut used to do and what
    // this walk must cost far less than.
    let before_gathering = live();
    let gathered: Vec<_> = mariadb_cli::exec::script::split(&file, ";")
        .expect("the file is readable")
        .collect();
    let holding_them_all = live() - before_gathering;
    assert_eq!(gathered.len(), statements);
    drop(gathered);

    assert!(
        peak * 2 < holding_them_all,
        "the walk holds {peak} bytes and the same statements gathered hold {holding_them_all}; \
         a walk that costs what gathering costs is not a walk"
    );
}