aft-store 0.65.0

On-disk index formats for Agent Foundational Toolkit
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! The content plane's sections in a segment.
//!
//! | Section | Holds |
//! |---|---|
//! | record units | a unit table: each record key in ascending order, located at its surface |
//! | record surfaces | each record's surface part, uncompressed, read in place |
//! | record bodies | each record's body part, compressed when that saves space |
//! | body index | per unit, by ordinal: body offset (u64), stored length (u32), length when unpacked (u32), codec (u8) |
//!
//! Surfaces stay uncompressed because resolving any file reads the surfaces
//! of the files it depends on, often many and often small; a body is read
//! only when its own file is resolved.

use std::collections::BTreeMap;
use std::sync::Arc;

use super::{ContentRecord, DecodeError, RecordKey, TodoMarkers};
use crate::ids::Digest128;
use crate::packed::{self, PackedBuilder};
use crate::segment::{
    Alignment, ErrorKind, KeySpace, KnownSection, Requirement, SectionHandle, SectionId,
    SectionKind, SectionSpec, Segment, SegmentError, SegmentWriter,
};
use crate::table::{UnitLocation, UnitTable, UnitTableBuilder};

const UNITS: SectionKind = SectionKind::new(0x0200);
const SURFACES: SectionKind = SectionKind::new(0x0201);
const BODIES: SectionKind = SectionKind::new(0x0202);
const BODY_INDEX: SectionKind = SectionKind::new(0x0203);
const VERSION: u16 = 1;
const KEY_SPACE: KeySpace = KeySpace::new(0);
const BODY_INDEX_LEN: usize = packed::INDEX_ENTRY_LEN;

/// The content sections this build reads.
pub const KNOWN: [KnownSection; 4] = [
    KnownSection {
        kind: UNITS,
        version: VERSION,
    },
    KnownSection {
        kind: SURFACES,
        version: VERSION,
    },
    KnownSection {
        kind: BODIES,
        version: VERSION,
    },
    KnownSection {
        kind: BODY_INDEX,
        version: VERSION,
    },
];

/// A record's two parts as the sections store them, before the body is
/// compressed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncodedRecord {
    pub surface: Vec<u8>,
    pub body: Vec<u8>,
}

impl crate::heap::HeapBytes for EncodedRecord {
    fn heap_bytes(&self, _seen: &mut crate::heap::Seen) -> usize {
        self.surface.capacity() + self.body.capacity()
    }
}

impl EncodedRecord {
    #[must_use]
    pub fn of(record: &ContentRecord) -> Self {
        let (surface, body) = record.encode();
        Self { surface, body }
    }

    /// The record again.
    ///
    /// # Errors
    ///
    /// When the bytes are not a record this build encodes.
    pub fn decode(&self) -> Result<ContentRecord, DecodeError> {
        ContentRecord::decode(&self.surface, &self.body)
    }

    /// The record's TODO markers, read from its surface part alone.
    ///
    /// # Errors
    ///
    /// When the surface part is not one this build encodes.
    pub fn todos(&self) -> Result<Option<TodoMarkers>, DecodeError> {
        ContentRecord::todos_of(&self.surface)
    }
}

/// Collects records for the content sections of one segment.
#[derive(Debug, Default)]
pub struct ContentSectionsBuilder {
    records: BTreeMap<RecordKey, Arc<EncodedRecord>>,
}

impl crate::heap::HeapBytes for ContentSectionsBuilder {
    fn heap_bytes(&self, seen: &mut crate::heap::Seen) -> usize {
        let entry = std::mem::size_of::<RecordKey>() + std::mem::size_of::<Arc<EncodedRecord>>();
        crate::heap::btree_bytes(self.records.len(), entry)
            + self
                .records
                .values()
                .filter(|record| seen.first(Arc::as_ptr(record)))
                .map(|record| {
                    std::mem::size_of::<EncodedRecord>()
                        + record.surface.capacity()
                        + record.body.capacity()
                })
                .sum::<usize>()
    }
}

impl ContentSectionsBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a record under `key`. Adding a key twice keeps the first.
    pub fn add(&mut self, key: RecordKey, record: &ContentRecord) {
        self.records
            .entry(key)
            .or_insert_with(|| Arc::new(EncodedRecord::of(record)));
    }

    /// Adds a record already encoded. Adding a key twice keeps the first.
    pub fn add_encoded(&mut self, key: RecordKey, record: Arc<EncodedRecord>) {
        self.records.entry(key).or_insert(record);
    }

    /// The encoded record under `key`, shared.
    #[must_use]
    pub fn get(&self, key: &RecordKey) -> Option<&Arc<EncodedRecord>> {
        self.records.get(key)
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.records.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.records.is_empty()
    }

    /// Keeps only the records `keep` accepts: a build leaves out those
    /// another segment of the family already holds.
    pub fn retain(&mut self, mut keep: impl FnMut(RecordKey) -> bool) {
        self.records.retain(|key, _| keep(*key));
    }

    /// Writes the four sections into `writer`.
    ///
    /// # Errors
    ///
    /// When a write fails.
    pub fn write(self, writer: &mut SegmentWriter) -> Result<(), SegmentError> {
        let mut table = UnitTableBuilder::new(Digest128::LEN);
        let mut surfaces = Vec::new();
        let mut bodies = PackedBuilder::with_capacity(self.records.len());
        for (key, record) in self.records {
            table.push(
                key.digest().as_bytes(),
                UnitLocation {
                    offset: surfaces.len() as u64,
                    len: u32::try_from(record.surface.len()).expect("a surface is under 4 GiB"),
                },
            );
            surfaces.extend_from_slice(&record.surface);
            bodies.push(&record.body);
        }
        let (bodies, index) = bodies.finish();
        let (table, units) = table.finish();
        writer.write_section(spec(UNITS), &table, units)?;
        writer.write_section(spec(SURFACES), &surfaces, units)?;
        writer.write_section(spec(BODIES), &bodies, units)?;
        writer.write_section(spec(BODY_INDEX), &index, units)
    }
}

/// Writes into `writer` the content sections of a segment holding `units`,
/// each `(key, source, ordinal)` the record at `ordinal` of the source
/// segment at `source`, copied as stored: neither part is decoded, so a
/// record of any kind folds. The sections are the bytes a
/// [`ContentSectionsBuilder`] holding the same records writes (see
/// `crate::fold`). `units` must be in strictly ascending key order.
///
/// # Errors
///
/// When a read or a write fails, or a source's sections are malformed.
///
/// # Panics
///
/// When `units` is not in strictly ascending key order or names a source
/// that is not in `sources`.
pub fn write_folded(
    writer: &mut SegmentWriter,
    sources: &[ContentSegment<'_>],
    units: &[(RecordKey, usize, u32)],
) -> Result<(), SegmentError> {
    // Surfaces first, so each unit's place in them is known as the table
    // is built.
    let mut table = UnitTableBuilder::new(Digest128::LEN);
    let mut offset = 0u64;
    writer.begin_section(spec(SURFACES))?;
    for (key, source, ordinal) in units {
        let surface = sources[*source].surface(*ordinal)?;
        table.push(
            key.digest().as_bytes(),
            UnitLocation {
                offset,
                len: u32::try_from(surface.len()).expect("a surface is under 4 GiB"),
            },
        );
        writer.write(&surface)?;
        offset += surface.len() as u64;
    }
    let (table, count) = table.finish();
    writer.end_section(count)?;
    writer.begin_section(spec(BODIES))?;
    let mut bodies = packed::PackedCopy::with_capacity(units.len());
    for (_, source, ordinal) in units {
        let source = &sources[*source];
        let body = packed::read_stored(source.segment, source.bodies, source.index, *ordinal)?;
        bodies.push(writer, &body)?;
    }
    writer.end_section(count)?;
    writer.write_section(spec(BODY_INDEX), &bodies.finish(), count)?;
    writer.write_section(spec(UNITS), &table, count)
}

fn spec(kind: SectionKind) -> SectionSpec {
    SectionSpec {
        id: SectionId::new(kind, KEY_SPACE),
        version: VERSION,
        alignment: Alignment::Word,
        requirement: Requirement::Required,
    }
}

/// The content sections of one open segment.
#[derive(Debug)]
pub struct ContentSegment<'s> {
    segment: &'s Segment,
    units: UnitTable<'s>,
    surfaces: SectionHandle,
    bodies: SectionHandle,
    index: SectionHandle,
}

impl<'s> ContentSegment<'s> {
    /// The segment's content sections, or `None` when it has none.
    ///
    /// # Errors
    ///
    /// When the segment has only some of them, or they are malformed.
    pub fn open(segment: &'s Segment) -> Result<Option<Self>, SegmentError> {
        let handles = [UNITS, SURFACES, BODIES, BODY_INDEX]
            .map(|kind| segment.section(SectionId::new(kind, KEY_SPACE)));
        let [Some(units), Some(surfaces), Some(bodies), Some(index)] = handles else {
            if handles.iter().any(Option::is_some) {
                return Err(malformed(
                    segment,
                    "a segment has only some content sections",
                ));
            }
            return Ok(None);
        };
        let units = UnitTable::open(segment, units)?;
        if segment.info(index).len != u64::from(units.len()) * BODY_INDEX_LEN as u64 {
            return Err(malformed(
                segment,
                "the body index does not match the unit table",
            ));
        }
        Ok(Some(Self {
            segment,
            units,
            surfaces,
            bodies,
            index,
        }))
    }

    #[must_use]
    pub fn len(&self) -> u32 {
        self.units.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.units.is_empty()
    }

    /// The ordinal of the record with `key`.
    ///
    /// # Errors
    ///
    /// When a read fails.
    pub fn find(&self, key: RecordKey) -> Result<Option<u32>, SegmentError> {
        Ok(self
            .units
            .get(key.digest().as_bytes())?
            .map(|(ordinal, _)| ordinal))
    }

    /// The record key at `ordinal`.
    ///
    /// # Errors
    ///
    /// When a read fails.
    pub fn key(&self, ordinal: u32) -> Result<RecordKey, SegmentError> {
        let (key, _) = self.units.entry(ordinal)?;
        let bytes = <[u8; Digest128::LEN]>::try_from(key.as_slice())
            .map_err(|_| malformed(self.segment, "a record key has the wrong width"))?;
        Ok(RecordKey::from_digest(Digest128::from_bytes(bytes)))
    }

    /// The surface bytes of the record at `ordinal`.
    ///
    /// # Errors
    ///
    /// When a read fails or a block fails its checksum.
    pub fn surface(&self, ordinal: u32) -> Result<Vec<u8>, SegmentError> {
        let (_, location) = self.units.entry(ordinal)?;
        let end = location
            .offset
            .checked_add(u64::from(location.len))
            .ok_or_else(|| malformed(self.segment, "a surface's range overflows"))?;
        self.segment.read_range(self.surfaces, location.offset..end)
    }

    /// The body bytes of the record at `ordinal`, unpacked.
    ///
    /// # Errors
    ///
    /// When a read fails, a block fails its checksum, or the stored body
    /// does not unpack to its recorded length.
    pub fn body(&self, ordinal: u32) -> Result<Vec<u8>, SegmentError> {
        packed::read(self.segment, self.bodies, self.index, ordinal)
    }

    /// The whole record at `ordinal`.
    ///
    /// # Errors
    ///
    /// As [`surface`](Self::surface) and [`body`](Self::body), and when the
    /// parts do not decode.
    pub fn record(&self, ordinal: u32) -> Result<ContentRecord, ContentReadError> {
        let surface = self.surface(ordinal)?;
        let body = self.body(ordinal)?;
        Ok(ContentRecord::decode(&surface, &body)?)
    }

    /// The TODO markers of the record at `ordinal`, read from its surface
    /// part alone (see [`ContentRecord::todos_of`]).
    ///
    /// # Errors
    ///
    /// As [`surface`](Self::surface), and when the markers do not decode.
    pub fn todos(&self, ordinal: u32) -> Result<Option<TodoMarkers>, ContentReadError> {
        Ok(ContentRecord::todos_of(&self.surface(ordinal)?)?)
    }
}

/// Why a stored record could not be read.
#[derive(Debug, thiserror::Error)]
pub enum ContentReadError {
    #[error(transparent)]
    Segment(#[from] SegmentError),
    #[error(transparent)]
    Decode(#[from] DecodeError),
}

fn malformed(segment: &Segment, reason: &'static str) -> SegmentError {
    SegmentError::new(segment.path(), ErrorKind::Malformed(reason))
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;
    use crate::cache::BlockCache;
    use crate::content::{Body, RecordBody, RefKind, Reference, Surface, TodoMarker, TodoMarkers};
    use crate::ids::{ContentKey, FamilyId, SegmentId, WriterIncarnation};
    use crate::segment::{BLOCK_LEN, SegmentMeta};

    fn record(index: u32) -> ContentRecord {
        let reference = Reference {
            ordinal: index,
            kind: RefKind::Call,
            caller_symbol: Some(format!("caller_{index}")),
            short_name: Some("target".into()),
            full_ref: Some(format!("module::target_{}", index % 7)),
            module_path: None,
            line: index,
            byte_start: 10,
            byte_end: 20,
            path_override: None,
            local_name: None,
            requested_name: None,
            namespace_alias: None,
            wildcard: false,
            import_kind: None,
        };
        ContentRecord {
            language: "rust".into(),
            extractor_version: "v1".into(),
            data: RecordBody::Parsed {
                surface: Box::new(Surface {
                    exported_symbols: vec![format!("item_{index}")],
                    ..Surface::default()
                }),
                // Bodies of all sizes: some compress, some do not.
                body: Box::new(Body {
                    references: vec![reference; index as usize % 40],
                    ..Body::default()
                }),
            },
            // Every third record holds TODO markers, read back on their own
            // by `todos`.
            todos: (index % 3 == 0).then(|| TodoMarkers {
                language: "rust".into(),
                items: Some(vec![TodoMarker {
                    line: index,
                    marker: "TODO".into(),
                    author: None,
                    text: format!("item {index}"),
                }]),
            }),
        }
    }

    #[test]
    fn records_are_found_by_key_and_read_back_whole() {
        let dir = tempfile::tempdir().unwrap();
        let family = FamilyId::from_digest(Digest128::of(b"family"));
        let path = dir.path().join("1.seg");
        let mut writer = SegmentWriter::create(
            &path,
            SegmentMeta {
                family,
                segment: SegmentId::new(1),
                incarnation: WriterIncarnation::new(1),
                created_unix_ms: 0,
            },
        )
        .unwrap();
        let key = |index: u32| {
            RecordKey::new(
                ContentKey::of(format!("content {index}").as_bytes()),
                "rust",
                "v1",
            )
        };
        let mut builder = ContentSectionsBuilder::new();
        for index in 0..300 {
            builder.add(key(index), &record(index));
        }
        builder.write(&mut writer).unwrap();
        writer.finish().unwrap();
        let segment = Segment::open(
            &path,
            family,
            &KNOWN,
            Arc::new(BlockCache::new(8 * BLOCK_LEN)),
        )
        .unwrap();
        let content = ContentSegment::open(&segment).unwrap().unwrap();
        assert_eq!(content.len(), 300);
        for index in 0..300 {
            let ordinal = content.find(key(index)).unwrap().unwrap();
            assert_eq!(content.key(ordinal).unwrap(), key(index));
            assert_eq!(content.record(ordinal).unwrap(), record(index));
            assert_eq!(content.todos(ordinal).unwrap(), record(index).todos);
        }
        assert_eq!(
            content
                .find(RecordKey::new(ContentKey::of(b"content 1"), "rust", "v2"))
                .unwrap(),
            None,
            "another extractor version is another record"
        );
    }
}