llkv-table 0.8.5-alpha

Columnar table using the LLKV 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
//! Schema extensions for efficient field ID caching.
//!
//! This module provides a wrapper around Arrow's `Schema` that pre-computes
//! and caches field ID lookups, eliminating repeated metadata HashMap lookups
//! in hot paths.
//!
//! # Performance Impact
//!
//! **Before (repeated metadata lookups)**: iterate Arrow fields, pull the
//! `FIELD_ID_META_KEY` value from metadata, and parse it for every column.
//!
//! **After (cached lookups)**: use `CachedSchema::field_id` to retrieve the
//! pre-parsed value with a simple array access.
//!
//! **Speedup**: 20-100x for field ID extraction (measured in benchmarks)
//!
//! # Usage
//!
//! ```
//! use arrow::datatypes::{DataType, Field, Schema};
//! use llkv_table::constants::FIELD_ID_META_KEY;
//! use llkv_table::schema_ext::CachedSchema;
//! use std::sync::Arc;
//!
//! let field = Field::new("id", DataType::UInt32, false)
//!     .with_metadata([(FIELD_ID_META_KEY.to_string(), "7".to_string())].into());
//! let schema = Arc::new(Schema::new(vec![field]));
//! let cached = CachedSchema::new(schema.clone());
//!
//! assert_eq!(cached.field_id(0), Some(7));
//! assert_eq!(cached.field_count(), 1);
//! assert!(!cached.system_columns().has_row_id);
//! ```

use crate::constants::FIELD_ID_META_KEY;
use crate::reserved::{MVCC_CREATED_BY_FIELD_ID, MVCC_DELETED_BY_FIELD_ID, ROW_ID_FIELD_ID};
use crate::types::FieldId;
use arrow::datatypes::Schema;
use rustc_hash::FxHashMap;
use std::sync::Arc;

// ============================================================================
// CachedSchema - Pre-computed field ID lookups
// ============================================================================

/// Wrapper around Arrow `Schema` that caches field ID metadata lookups.
///
/// This wrapper pre-computes and stores:
/// - Field index → FieldId mappings (for fast forward lookup)
/// - FieldId → Field index mappings (for fast reverse lookup)
/// - System column presence flags (eliminates string comparisons)
///
/// # Thread Safety
///
/// `CachedSchema` is `Clone` and internally uses `Arc`, making it cheap to
/// share across threads. The cache is immutable after construction.
///
/// # Memory Overhead
///
/// - ~4 bytes per field for the `field_ids` vector
/// - ~8 bytes per field for the `id_to_index` map entries
/// - 3 bytes for system column flags
/// - Total: ~12-16 bytes per field + Arc overhead
///
/// This is negligible compared to the cost of repeated HashMap lookups.
#[derive(Debug, Clone)]
pub struct CachedSchema {
    /// The underlying Arrow schema
    schema: Arc<Schema>,
    /// Field index -> FieldId (None if field has no field_id metadata)
    field_ids: Vec<Option<FieldId>>,
    /// FieldId -> Field index (reverse lookup)
    id_to_index: FxHashMap<FieldId, usize>,
    /// System column presence (cached to avoid string comparisons)
    system_columns: SystemColumnPresence,
}

impl CachedSchema {
    /// Build a cached schema from an Arrow schema.
    ///
    /// This constructor scans all fields once to extract field IDs from metadata
    /// and build the lookup caches.
    ///
    /// # Arguments
    ///
    /// * `schema` - Arc to the Arrow schema to wrap
    ///
    /// # Example
    ///
    /// ```
    /// use arrow::datatypes::{DataType, Field, Schema};
    /// use llkv_table::constants::FIELD_ID_META_KEY;
    /// use llkv_table::schema_ext::CachedSchema;
    /// use std::sync::Arc;
    ///
    /// let field = Field::new("name", DataType::Utf8, false)
    ///     .with_metadata([(FIELD_ID_META_KEY.to_string(), "3".to_string())].into());
    /// let schema = Arc::new(Schema::new(vec![field]));
    ///
    /// let cached = CachedSchema::new(schema.clone());
    /// assert_eq!(cached.field_id(0), Some(3));
    /// assert_eq!(cached.index_of_field_id(3), Some(0));
    /// ```
    pub fn new(schema: Arc<Schema>) -> Self {
        let field_count = schema.fields().len();
        let mut field_ids = Vec::with_capacity(field_count);
        let mut id_to_index = FxHashMap::default();
        let mut system_columns = SystemColumnPresence::default();

        for (idx, field) in schema.fields().iter().enumerate() {
            // Extract field ID from metadata
            let field_id = field
                .metadata()
                .get(FIELD_ID_META_KEY)
                .and_then(|s| s.parse::<FieldId>().ok());

            if let Some(fid) = field_id {
                id_to_index.insert(fid, idx);

                // Cache system column presence
                match fid {
                    ROW_ID_FIELD_ID => system_columns.has_row_id = true,
                    MVCC_CREATED_BY_FIELD_ID => system_columns.has_created_by = true,
                    MVCC_DELETED_BY_FIELD_ID => system_columns.has_deleted_by = true,
                    _ => {}
                }
            }

            field_ids.push(field_id);
        }

        Self {
            schema,
            field_ids,
            id_to_index,
            system_columns,
        }
    }

    /// Get the underlying Arrow schema.
    #[inline]
    pub fn schema(&self) -> &Arc<Schema> {
        &self.schema
    }

    /// Get the FieldId for a field at the given index.
    ///
    /// # Arguments
    ///
    /// * `field_index` - The 0-based field index
    ///
    /// # Returns
    ///
    /// `Some(FieldId)` if the field has a field_id in metadata, `None` otherwise.
    ///
    /// # Panics
    ///
    /// Panics if `field_index >= field_count()`.
    #[inline]
    pub fn field_id(&self, field_index: usize) -> Option<FieldId> {
        self.field_ids[field_index]
    }

    /// Get the field index for a given FieldId.
    ///
    /// # Arguments
    ///
    /// * `field_id` - The FieldId to look up
    ///
    /// # Returns
    ///
    /// `Some(index)` if a field with this ID exists, `None` otherwise.
    #[inline]
    pub fn index_of_field_id(&self, field_id: FieldId) -> Option<usize> {
        self.id_to_index.get(&field_id).copied()
    }

    /// Check which system columns are present in this schema.
    ///
    /// Returns cached flags for row_id, created_by, and deleted_by columns.
    /// This is much faster than iterating fields and comparing names.
    ///
    /// # Example
    ///
    /// ```
    /// use arrow::datatypes::{DataType, Field, Schema};
    /// use llkv_table::constants::FIELD_ID_META_KEY;
    /// use llkv_table::reserved::{MVCC_CREATED_BY_FIELD_ID, MVCC_DELETED_BY_FIELD_ID};
    /// use llkv_table::schema_ext::CachedSchema;
    /// use llkv_table::types::FieldId;
    /// use std::sync::Arc;
    ///
    /// fn system_field(name: &str, fid: FieldId) -> Field {
    ///     Field::new(name, DataType::Int64, false)
    ///         .with_metadata([(FIELD_ID_META_KEY.to_string(), fid.to_string())].into())
    /// }
    ///
    /// let schema = Arc::new(Schema::new(vec![
    ///     system_field("created_by", MVCC_CREATED_BY_FIELD_ID),
    ///     system_field("deleted_by", MVCC_DELETED_BY_FIELD_ID),
    /// ]));
    ///
    /// let presence = CachedSchema::new(schema).system_columns();
    /// assert!(presence.has_full_mvcc());
    /// ```
    #[inline]
    pub fn system_columns(&self) -> SystemColumnPresence {
        self.system_columns
    }

    /// Get the number of fields in the schema.
    #[inline]
    pub fn field_count(&self) -> usize {
        self.field_ids.len()
    }

    /// Check if a field has a field_id in metadata.
    #[inline]
    pub fn has_field_id(&self, field_index: usize) -> bool {
        self.field_ids.get(field_index).and_then(|&id| id).is_some()
    }
}

// ============================================================================
// SystemColumnPresence - Cached system column flags
// ============================================================================

/// Cached flags indicating which system columns are present in a schema.
///
/// These flags eliminate the need for repeated string comparisons to detect
/// system columns (row_id, created_by, deleted_by).
///
/// # Performance
///
/// **Before (string comparisons)**: iterate Arrow fields and compare every
/// column name against the MVCC constants.
///
/// **After (cached flag)**: read the boolean flags computed during
/// `CachedSchema::new`, avoiding repeated string comparisons.
#[derive(Debug, Clone, Copy, Default)]
pub struct SystemColumnPresence {
    /// True if schema contains row_id column (FieldId 0)
    pub has_row_id: bool,
    /// True if schema contains created_by MVCC column (FieldId u32::MAX)
    pub has_created_by: bool,
    /// True if schema contains deleted_by MVCC column (FieldId u32::MAX-1)
    pub has_deleted_by: bool,
}

impl SystemColumnPresence {
    /// Check if schema has both MVCC columns.
    #[inline]
    pub fn has_full_mvcc(&self) -> bool {
        self.has_created_by && self.has_deleted_by
    }

    /// Check if schema has any MVCC columns.
    #[inline]
    pub fn has_any_mvcc(&self) -> bool {
        self.has_created_by || self.has_deleted_by
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow::datatypes::{DataType, Field};
    use std::collections::HashMap;

    fn make_field(name: &str, field_id: FieldId) -> Field {
        let mut metadata = HashMap::new();
        metadata.insert(FIELD_ID_META_KEY.to_string(), field_id.to_string());
        Field::new(name, DataType::Utf8, false).with_metadata(metadata)
    }

    #[test]
    fn test_cached_schema_field_id_lookup() {
        let fields = vec![
            make_field("name", 1),
            make_field("email", 2),
            make_field("age", 3),
        ];
        let schema = Arc::new(Schema::new(fields));
        let cached = CachedSchema::new(schema);

        // Forward lookup: index -> FieldId
        assert_eq!(cached.field_id(0), Some(1));
        assert_eq!(cached.field_id(1), Some(2));
        assert_eq!(cached.field_id(2), Some(3));

        // Reverse lookup: FieldId -> index
        assert_eq!(cached.index_of_field_id(1), Some(0));
        assert_eq!(cached.index_of_field_id(2), Some(1));
        assert_eq!(cached.index_of_field_id(3), Some(2));

        // Non-existent FieldId
        assert_eq!(cached.index_of_field_id(999), None);
    }

    #[test]
    fn test_cached_schema_system_columns() {
        let fields = vec![
            make_field("row_id", ROW_ID_FIELD_ID),
            make_field("name", 1),
            make_field("created_by", MVCC_CREATED_BY_FIELD_ID),
            make_field("deleted_by", MVCC_DELETED_BY_FIELD_ID),
        ];
        let schema = Arc::new(Schema::new(fields));
        let cached = CachedSchema::new(schema);

        let presence = cached.system_columns();
        assert!(presence.has_row_id);
        assert!(presence.has_created_by);
        assert!(presence.has_deleted_by);
        assert!(presence.has_full_mvcc());
        assert!(presence.has_any_mvcc());
    }

    #[test]
    fn test_cached_schema_no_system_columns() {
        let fields = vec![make_field("name", 1), make_field("email", 2)];
        let schema = Arc::new(Schema::new(fields));
        let cached = CachedSchema::new(schema);

        let presence = cached.system_columns();
        assert!(!presence.has_row_id);
        assert!(!presence.has_created_by);
        assert!(!presence.has_deleted_by);
        assert!(!presence.has_full_mvcc());
        assert!(!presence.has_any_mvcc());
    }

    #[test]
    fn test_cached_schema_partial_mvcc() {
        let fields = vec![
            make_field("row_id", ROW_ID_FIELD_ID),
            make_field("name", 1),
            make_field("created_by", MVCC_CREATED_BY_FIELD_ID),
            // Missing deleted_by
        ];
        let schema = Arc::new(Schema::new(fields));
        let cached = CachedSchema::new(schema);

        let presence = cached.system_columns();
        assert!(presence.has_row_id);
        assert!(presence.has_created_by);
        assert!(!presence.has_deleted_by);
        assert!(!presence.has_full_mvcc());
        assert!(presence.has_any_mvcc());
    }

    #[test]
    fn test_cached_schema_field_without_id() {
        // Field without field_id metadata
        let field_no_id = Field::new("temp", DataType::Utf8, false);
        let field_with_id = make_field("name", 1);

        let fields = vec![field_no_id, field_with_id];
        let schema = Arc::new(Schema::new(fields));
        let cached = CachedSchema::new(schema);

        // Field 0 has no field_id
        assert_eq!(cached.field_id(0), None);
        assert!(!cached.has_field_id(0));

        // Field 1 has field_id
        assert_eq!(cached.field_id(1), Some(1));
        assert!(cached.has_field_id(1));

        // Reverse lookup only finds field with ID
        assert_eq!(cached.index_of_field_id(1), Some(1));
    }

    #[test]
    fn test_cached_schema_field_count() {
        let fields = vec![
            make_field("name", 1),
            make_field("email", 2),
            make_field("age", 3),
        ];
        let schema = Arc::new(Schema::new(fields));
        let cached = CachedSchema::new(schema);

        assert_eq!(cached.field_count(), 3);
        assert_eq!(cached.schema().fields().len(), 3);
    }

    #[test]
    fn test_cached_schema_clone() {
        let fields = vec![make_field("name", 1)];
        let schema = Arc::new(Schema::new(fields));
        let cached1 = CachedSchema::new(schema);

        // Clone should be cheap (Arc)
        let cached2 = cached1.clone();

        assert_eq!(cached1.field_id(0), cached2.field_id(0));
        assert!(
            Arc::ptr_eq(cached1.schema(), cached2.schema()),
            "Schema Arc should be shared"
        );
    }
}