lix 0.16.0

Embeddable version control for apps and AI agents.
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
//! Candidate amplification in the hot declared-column index.
//!
//! Not a product module. It measures how many *candidates* a single-value
//! lookup on an indexed column resolves, versus how many rows actually match,
//! after the collection has churned inside one generation. Entries are
//! put-only and a generation spans a branch's lifetime, so the question is
//! whether the wasted candidate resolutions stay a small constant or grow.

use serde_json::json;
use std::time::{Duration, Instant};

use crate::engine::Engine;
use crate::session::SessionContext;
use crate::storage::ProjectedValue;
use crate::storage_adapter::{
    Memory, StorageAdapter, StorageAdapterRead, StorageBeginScanOptions, StoragePrefix,
    StorageReadOptions,
};

fn sizes_from_env(var: &str, default: &[usize]) -> Vec<usize> {
    match std::env::var(var) {
        Ok(raw) => raw
            .split(',')
            .filter(|part| !part.trim().is_empty())
            .map(|part| part.trim().parse::<usize>().expect("size must parse"))
            .collect(),
        Err(_) => default.to_vec(),
    }
}

fn reps_from_env(default: usize) -> usize {
    std::env::var("LIX_INDEX_AGING_REPS")
        .ok()
        .and_then(|raw| raw.parse::<usize>().ok())
        .unwrap_or(default)
}

async fn open_session() -> (Memory, SessionContext<Memory>) {
    let storage = Memory::new();
    Engine::initialize(storage.clone())
        .await
        .expect("engine should initialize");
    let engine = Engine::new(storage.clone())
        .await
        .expect("engine should open");
    let session = engine.open_session().await.expect("session should open");
    (storage, session)
}

/// `(witnesses, entries)` in the hot index plane.
async fn index_record_counts(storage: &Memory) -> (usize, usize) {
    let adapter = StorageAdapter::new(storage.clone());
    let read = adapter
        .begin_read(StorageReadOptions::default())
        .await
        .expect("read the index plane");
    let range = StoragePrefix {
        bytes: bytes::Bytes::new(),
    }
    .to_range()
    .expect("valid empty prefix");
    let mut cursor = read
        .begin_scan(
            crate::hot_state::INDEX_SPACE,
            range,
            StorageBeginScanOptions::default(),
        )
        .await
        .expect("scan the index plane");
    let entries = cursor.collect_all().await.expect("collect index entries");
    let witnesses = entries
        .iter()
        .filter(|entry| match &entry.value {
            ProjectedValue::FullValue(bytes) => !bytes.starts_with(b"["),
            ProjectedValue::KeyOnly => true,
        })
        .count();
    (witnesses, entries.len() - witnesses)
}

fn probe_schemas(parent: &str, child: &str) -> [serde_json::Value; 2] {
    [
        json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": parent,
            "columns": [
                { "name": "id", "type": "text", "nullable": false },
            ],
            "primary_key": ["id"],
        }),
        json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": child,
            "columns": [
                { "name": "id", "type": "text", "nullable": false },
                { "name": "parent_id", "type": "text", "nullable": false },
                { "name": "locale", "type": "text", "nullable": false },
            ],
            "primary_key": ["id"],
            "foreign_keys": [{
                "columns": ["parent_id"],
                "references": { "schema_key": parent, "columns": ["id"] }
            }],
        }),
    ]
}

async fn register(session: &SessionContext<Memory>, schemas: [serde_json::Value; 2]) {
    for schema in schemas {
        session
            .execute(
                "INSERT INTO lix_registered_schema (value) VALUES (CAST($1 AS JSONB))",
                &[crate::Value::Text(schema.to_string())],
            )
            .await
            .expect("schema should register");
    }
}

const CHUNK: usize = 250;

async fn insert_parents(session: &SessionContext<Memory>, table: &str, count: usize) {
    let mut index = 0;
    while index < count {
        let end = (index + CHUNK).min(count);
        let values = (index..end)
            .map(|i| format!("('parent-{i}')"))
            .collect::<Vec<_>>()
            .join(",");
        session
            .execute(&format!("INSERT INTO {table} (id) VALUES {values}"), &[])
            .await
            .expect("parents should insert");
        index = end;
    }
}

/// Inserts `count` children; child `i` points at `parent_of(i)`.
async fn insert_children(
    session: &SessionContext<Memory>,
    table: &str,
    count: usize,
    parent_of: impl Fn(usize) -> usize,
) {
    let mut index = 0;
    while index < count {
        let end = (index + CHUNK).min(count);
        let values = (index..end)
            .map(|i| {
                let parent = parent_of(i);
                format!("('child-{i}', 'parent-{parent}', 'en')")
            })
            .collect::<Vec<_>>()
            .join(",");
        session
            .execute(
                &format!(r#"INSERT INTO {table} (id, "parent_id", locale) VALUES {values}"#),
                &[],
            )
            .await
            .expect("children should insert");
        index = end;
    }
}

/// `id IN (...)` in chunks over `child-1 .. child-{count-1}`.
async fn mutate_children_except_first(
    session: &SessionContext<Memory>,
    statement: impl Fn(&str) -> String,
    count: usize,
) {
    let mut index = 1;
    while index < count {
        let end = (index + CHUNK).min(count);
        let ids = (index..end)
            .map(|i| format!("'child-{i}'"))
            .collect::<Vec<_>>()
            .join(",");
        session
            .execute(&statement(&ids), &[])
            .await
            .expect("bulk mutation should run");
        index = end;
    }
}

async fn timed_lookup(
    session: &SessionContext<Memory>,
    sql: &str,
    expect_rows: usize,
    reps: usize,
) -> Duration {
    let mut samples = Vec::new();
    for _ in 0..reps {
        let start = Instant::now();
        let rows = session.execute(sql, &[]).await.expect("lookup should run");
        let elapsed = start.elapsed();
        assert_eq!(
            rows.len(),
            expect_rows,
            "lookup returned the wrong row count"
        );
        samples.push(elapsed);
    }
    samples.sort();
    samples[samples.len() / 2]
}

/// READ PATH. One live match at `parent-0` in every arm; only the number of
/// stale candidates in that bucket differs.
///
/// * `aged`   — N children created at `parent-0`, then N-1 deleted. Live
///   collection is 1 row. Candidates at `parent-0` are N.
/// * `fresh`  — 1 child created at `parent-0`. Live collection is 1 row,
///   candidates 1. This is the control: identical answer, identical live
///   state, no stale candidates.
/// * `moved`  — N children created at `parent-0`, then N-1 repointed at
///   `parent-1`. Live collection is N rows, candidates at `parent-0` are N.
///   Its `locale` lookup is the unindexed comparator: a full scan of the same
///   N live rows.
#[tokio::test]
#[ignore = "measurement probe, not a gate"]
async fn hot_index_read_path_candidate_amplification() {
    let sizes = sizes_from_env("LIX_INDEX_AGING_SIZES", &[100, 1_000, 10_000]);
    let reps = reps_from_env(5);
    println!("read path | arm,n,index_entries,live_rows,answer_rows,median_us,scan_comparator_us");
    for n in sizes {
        // ---- aged arm: N candidates, 1 live row ----
        {
            let (storage, session) = open_session().await;
            register(&session, probe_schemas("agedp", "agedc")).await;
            insert_parents(&session, "agedp", 2).await;
            insert_children(&session, "agedc", n, |_| 0).await;
            mutate_children_except_first(
                &session,
                |ids| format!("DELETE FROM agedc WHERE id IN ({ids})"),
                n,
            )
            .await;
            let (_, entries) = index_record_counts(&storage).await;
            let median = timed_lookup(
                &session,
                r#"SELECT id FROM agedc WHERE "parent_id" = 'parent-0'"#,
                1,
                reps,
            )
            .await;
            // Unindexed comparator on the same churned collection: equality
            // on a column the schema does not declare keeps the ordinary
            // scan, so whatever the 9,999 tombstones cost shows up here and
            // not in the indexed number alone.
            let scan = timed_lookup(
                &session,
                "SELECT id FROM agedc WHERE locale = 'nomatch'",
                0,
                reps,
            )
            .await;
            println!(
                "aged,{n},{entries},1,1,{},{}",
                median.as_micros(),
                scan.as_micros()
            );
        }
        // ---- fresh control: 1 candidate, 1 live row ----
        {
            let (storage, session) = open_session().await;
            register(&session, probe_schemas("freshp", "freshc")).await;
            insert_parents(&session, "freshp", 2).await;
            insert_children(&session, "freshc", 1, |_| 0).await;
            let (_, entries) = index_record_counts(&storage).await;
            let median = timed_lookup(
                &session,
                r#"SELECT id FROM freshc WHERE "parent_id" = 'parent-0'"#,
                1,
                reps,
            )
            .await;
            println!("fresh,{n},{entries},1,1,{},-", median.as_micros());
        }
        // ---- moved arm: N candidates, 1 live match, N live rows ----
        {
            let (storage, session) = open_session().await;
            register(&session, probe_schemas("movedp", "movedc")).await;
            insert_parents(&session, "movedp", 2).await;
            insert_children(&session, "movedc", n, |_| 0).await;
            mutate_children_except_first(
                &session,
                |ids| format!(r#"UPDATE movedc SET "parent_id" = 'parent-1' WHERE id IN ({ids})"#),
                n,
            )
            .await;
            let (_, entries) = index_record_counts(&storage).await;
            let median = timed_lookup(
                &session,
                r#"SELECT id FROM movedc WHERE "parent_id" = 'parent-0'"#,
                1,
                reps,
            )
            .await;
            // Unindexed comparator: same collection, equality on a column the
            // schema does not declare, so the engine keeps its ordinary scan.
            let scan = timed_lookup(
                &session,
                r#"SELECT id FROM movedc WHERE locale = 'nomatch'"#,
                0,
                reps,
            )
            .await;
            println!(
                "moved,{n},{entries},{n},1,{},{}",
                median.as_micros(),
                scan.as_micros()
            );
        }
    }
}

fn unique_probe_schema(key: &str) -> serde_json::Value {
    json!({
        "$schema": "https://lix.dev/schema-v1.json",
        "key": key,
        "columns": [
            { "name": "id", "type": "text", "nullable": false },
            { "name": "slug", "type": "text", "nullable": false },
            { "name": "tag", "type": "text", "nullable": false },
        ],
        "primary_key": ["id"],
        "unique": [["slug"]],
    })
}

/// A composite unique group: `declared_column_probe` declines composite
/// groups, so this collection keeps the scan route on the write path.
fn composite_unique_schema(key: &str) -> serde_json::Value {
    json!({
        "$schema": "https://lix.dev/schema-v1.json",
        "key": key,
        "columns": [
            { "name": "id", "type": "text", "nullable": false },
            { "name": "slug", "type": "text", "nullable": false },
            { "name": "tag", "type": "text", "nullable": false },
        ],
        "primary_key": ["id"],
        "unique": [["slug", "tag"]],
    })
}

/// WRITE PATH. `validate_committed_unique_constraints` probes the same plane
/// for single-column unique groups, so every insert re-resolves whatever
/// candidates have accumulated under the value being inserted.
///
/// The loop recreates one logical slot: insert a fresh row holding
/// `slug='dup'`, delete it, repeat. Each iteration leaves one more permanent
/// candidate in the `dup` bucket while the live collection stays at one row.
/// Insert latency is sampled at the requested checkpoints.
#[tokio::test]
#[ignore = "measurement probe, not a gate"]
async fn hot_index_write_path_unique_probe_amplification() {
    write_path_churn("uniqprobe", unique_probe_schema("uniqprobe")).await;
}

/// Control for the write-path probe. The unique group is composite, so
/// `declared_column_probe` declines and the same churn runs on the collection
/// scan route. Whatever the accumulated tombstones cost the scan appears here;
/// the difference between the two runs is what the index plane adds.
#[tokio::test]
#[ignore = "measurement probe, not a gate"]
async fn hot_index_write_path_composite_unique_control() {
    write_path_churn("compprobe", composite_unique_schema("compprobe")).await;
}

async fn write_path_churn(table: &str, schema: serde_json::Value) {
    let checkpoints = sizes_from_env("LIX_INDEX_AGING_WRITE_CHECKPOINTS", &[10, 100, 500, 1_000]);
    let max = *checkpoints.iter().max().expect("at least one checkpoint");
    let (storage, session) = open_session().await;
    session
        .execute(
            "INSERT INTO lix_registered_schema (value) VALUES (CAST($1 AS JSONB))",
            &[crate::Value::Text(schema.to_string())],
        )
        .await
        .expect("schema should register");

    println!(
        "write path {table} | accumulated_candidates,index_entries,live_rows,median_insert_us"
    );
    let reps = reps_from_env(5);
    let mut next = 0usize;
    let mut samples: Vec<Duration> = Vec::new();
    for iteration in 0..=max {
        let sampling = next < checkpoints.len() && iteration + reps > checkpoints[next];
        let start = Instant::now();
        session
            .execute(
                &format!("INSERT INTO {table} (id, slug, tag) VALUES ($1, 'dup', 't')"),
                &[crate::Value::Text(format!("row-{iteration}"))],
            )
            .await
            .expect("insert should succeed");
        if sampling {
            samples.push(start.elapsed());
        }
        if next < checkpoints.len() && iteration == checkpoints[next] {
            let (_, entries) = index_record_counts(&storage).await;
            samples.sort();
            let median = samples[samples.len() / 2];
            println!("{iteration},{entries},1,{}", median.as_micros());
            samples.clear();
            next += 1;
        }
        session
            .execute(
                &format!("DELETE FROM {table} WHERE id = $1"),
                &[crate::Value::Text(format!("row-{iteration}"))],
            )
            .await
            .expect("delete should succeed");
    }
}