kronicler 0.1.2

Automatic performance capture and analysis for production applications in Python using a custom columnar database 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
405
406
use super::bufferpool::Bufferpool;
use super::constants::DATA_DIRECTORY;
use super::filewriter::{build_binary_writer, Writer};
use super::row::FieldType;
use log::info;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, RwLock};

/// Used to safe the state of the Column struct
#[derive(Serialize, Deserialize, Debug)]
pub struct ColumnMetadata {
    // Which column it is
    pub column_index: usize,
    pub current_index: usize,
    pub name: String,
    pub field_type: FieldType,
}

/// Implement column specific traits
impl ColumnMetadata {
    fn new(name: String, column_index: usize, field_type: FieldType) -> Self {
        ColumnMetadata {
            column_index,
            current_index: 0,
            name,
            field_type,
        }
    }
}

pub struct Column {
    pub metadata: ColumnMetadata,
    bufferpool: Arc<RwLock<Bufferpool>>,
}

/// Implement common traits from Metadata
/// TODO: How do I use ./metadata.rs as a trait and then have the return time for `load` be the
/// correct type? Right now, I will just have load and save be their own functions
impl Column {
    pub fn metadata_exists(column_index: usize) -> bool {
        let filepath = format!("{}/column-{}.data", DATA_DIRECTORY, column_index);

        Path::new(&filepath).exists()
    }

    pub fn save(&self) {
        let writer: Writer<ColumnMetadata> = build_binary_writer();
        let filepath = format!(
            "{}/column-{}.data",
            DATA_DIRECTORY, self.metadata.column_index
        );
        info!(
            "Saving Column {} to {}",
            self.metadata.column_index, filepath
        );
        writer.write_file(filepath.as_str(), &self.metadata);
    }

    pub fn load(column_index: usize) -> ColumnMetadata {
        let writer: Writer<ColumnMetadata> = build_binary_writer();
        let filepath = format!("{}/column-{}.data", DATA_DIRECTORY, column_index);

        info!("Loading Column {} to {}", column_index, filepath);
        writer.read_file(filepath.as_str())
    }
}

impl Column {
    pub fn insert(&mut self, value: &FieldType) {
        let i = self.metadata.current_index;

        let mut bp = self.bufferpool.write().expect("Could write.");
        // Index is auto-incremented
        bp.insert(i, self.metadata.column_index, value);

        self.metadata.current_index += 1;
    }

    pub fn fetch(&mut self, index: usize) -> Option<FieldType> {
        info!("Fetching {}", index);
        let field_type_size = self.metadata.field_type.get_size();

        let bufferpool = self.bufferpool.write();

        match bufferpool {
            Ok(mut bp) => return bp.fetch(index, self.metadata.column_index, field_type_size),
            Err(e) => {
                info!("{}", e);
                return None;
            }
        }
    }

    pub fn new(
        name: String,
        column_index: usize,
        bufferpool: Arc<RwLock<Bufferpool>>,
        field_type: FieldType,
    ) -> Self {
        {
            // let mut bp = bufferpool.write().expect("Should write.");
            // bp.create_column(column_index);
        }

        // Use existing metadata if it's around
        if Column::metadata_exists(column_index) {
            return Column {
                metadata: Column::load(column_index),
                bufferpool,
            };
        }

        Column {
            metadata: ColumnMetadata::new(name, column_index, field_type),
            bufferpool,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::row::create_function_name;
    use std::fs;

    fn cleanup_test_file(column_index: usize) {
        let filepath = format!("{}/column-{}.data", DATA_DIRECTORY, column_index);
        let _ = fs::remove_file(filepath);
    }

    #[test]
    fn column_metadata_new() {
        let field_type = FieldType::Epoch(0);
        let metadata = ColumnMetadata::new("test_column".to_string(), 5, field_type.clone());

        assert_eq!(metadata.column_index, 5);
        assert_eq!(metadata.current_index, 0);
        assert_eq!(metadata.name, "test_column");
        assert_eq!(metadata.field_type, field_type);
    }

    #[test]
    fn column_metadata_with_name_field() {
        let field_type = FieldType::Name(create_function_name("function"));
        let metadata = ColumnMetadata::new("name_column".to_string(), 1, field_type.clone());

        assert_eq!(metadata.column_index, 1);
        assert_eq!(metadata.name, "name_column");
        assert_eq!(metadata.field_type, field_type);
    }

    #[test]
    fn column_new_creates_fresh_column() {
        let column_index = 999; // Use high number to avoid conflicts
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let field_type = FieldType::Epoch(0);

        let column = Column::new(
            "test".to_string(),
            column_index,
            bufferpool,
            field_type.clone(),
        );

        assert_eq!(column.metadata.column_index, column_index);
        assert_eq!(column.metadata.current_index, 0);
        assert_eq!(column.metadata.name, "test");
        assert_eq!(column.metadata.field_type, field_type);

        cleanup_test_file(column_index);
    }

    #[test]
    fn column_insert_increments_index() {
        let column_index = 1000;
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let mut column = Column::new(
            "counter".to_string(),
            column_index,
            bufferpool,
            FieldType::Epoch(0),
        );

        assert_eq!(column.metadata.current_index, 0);

        column.insert(&FieldType::Epoch(100));
        assert_eq!(column.metadata.current_index, 1);

        column.insert(&FieldType::Epoch(200));
        assert_eq!(column.metadata.current_index, 2);

        column.insert(&FieldType::Epoch(300));
        assert_eq!(column.metadata.current_index, 3);

        cleanup_test_file(column_index);
    }

    #[test]
    fn column_insert_and_fetch() {
        let column_index = 1001;
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let mut column = Column::new(
            "data".to_string(),
            column_index,
            bufferpool,
            FieldType::Epoch(0),
        );

        let value1 = FieldType::Epoch(42);
        let value2 = FieldType::Epoch(1337);

        column.insert(&value1);
        column.insert(&value2);

        let fetched1 = column.fetch(0);
        let fetched2 = column.fetch(1);

        assert_eq!(fetched1, Some(value1));
        assert_eq!(fetched2, Some(value2));

        cleanup_test_file(column_index);
    }

    #[test]
    fn column_insert_name_field() {
        let column_index = 1002;
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let field_type = FieldType::Name(create_function_name("default"));
        let mut column = Column::new(
            "names".to_string(),
            column_index,
            bufferpool,
            field_type.clone(),
        );

        let name1 = FieldType::Name(create_function_name("function_a"));
        let name2 = FieldType::Name(create_function_name("function_b"));

        column.insert(&name1);
        column.insert(&name2);

        assert_eq!(column.metadata.current_index, 2);

        let fetched1 = column.fetch(0);
        let fetched2 = column.fetch(1);

        assert_eq!(fetched1, Some(name1));
        assert_eq!(fetched2, Some(name2));

        cleanup_test_file(column_index);
    }

    #[test]
    #[should_panic]
    fn column_fetch_nonexistent_index() {
        let column_index = 1003;
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let mut column = Column::new(
            "sparse".to_string(),
            column_index,
            bufferpool,
            FieldType::Epoch(0),
        );

        column.insert(&FieldType::Epoch(100));

        // TODO: This would actually return non, instead of returning 0
        // Try to fetch an index that doesn't exist
        let result = column.fetch(0);
        assert_eq!(result, None);

        cleanup_test_file(column_index);
    }

    #[test]
    fn column_save_and_load() {
        let column_index = 1004;
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let field_type = FieldType::Epoch(0);
        let mut column = Column::new(
            "persistent".to_string(),
            column_index,
            Arc::clone(&bufferpool),
            field_type.clone(),
        );

        // Insert some data
        column.insert(&FieldType::Epoch(111));
        column.insert(&FieldType::Epoch(222));
        column.insert(&FieldType::Epoch(333));

        // Save the column
        column.save();

        // Verify metadata file exists
        assert!(Column::metadata_exists(column_index));

        // Load the metadata
        let loaded_metadata = Column::load(column_index);

        assert_eq!(loaded_metadata.column_index, column_index);
        assert_eq!(loaded_metadata.current_index, 3);
        assert_eq!(loaded_metadata.name, "persistent");
        assert_eq!(loaded_metadata.field_type, field_type);

        cleanup_test_file(column_index);
    }

    #[test]
    fn column_new_loads_existing_metadata() {
        let column_index = 1005;
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let field_type = FieldType::Epoch(0);

        // Create and save a column
        let mut column1 = Column::new(
            "original".to_string(),
            column_index,
            Arc::clone(&bufferpool),
            field_type.clone(),
        );
        column1.insert(&FieldType::Epoch(999));
        column1.save();

        // Create a new column with same index - should load existing metadata
        let column2 = Column::new(
            "should_be_ignored".to_string(),
            column_index,
            Arc::clone(&bufferpool),
            field_type.clone(),
        );

        assert_eq!(column2.metadata.name, "original");
        assert_eq!(column2.metadata.current_index, 1);

        cleanup_test_file(column_index);
    }

    #[test]
    fn column_metadata_exists_false_for_new_column() {
        let column_index = 9999;
        cleanup_test_file(column_index);

        assert!(!Column::metadata_exists(column_index));
    }

    #[test]
    fn column_multiple_inserts_sequential() {
        let column_index = 1006;
        cleanup_test_file(column_index);

        let bufferpool = Arc::new(RwLock::new(Bufferpool::new(column_index + 1)));
        let mut column = Column::new(
            "sequence".to_string(),
            column_index,
            bufferpool,
            FieldType::Epoch(0),
        );

        // Insert 10 sequential values
        for i in 0..10 {
            column.insert(&FieldType::Epoch(i * 10));
        }

        assert_eq!(column.metadata.current_index, 10);

        // Verify all values
        for i in 0..10 {
            let fetched = column.fetch(i as usize);
            assert_eq!(fetched, Some(FieldType::Epoch(i * 10)));
        }

        cleanup_test_file(column_index);
    }

    #[test]
    fn column_field_type_size_epoch() {
        let field_type = FieldType::Epoch(100);
        let metadata = ColumnMetadata::new("test".to_string(), 0, field_type);

        assert_eq!(metadata.field_type.get_size(), 16);
    }

    #[test]
    fn column_field_type_size_name() {
        let field_type = FieldType::Name(create_function_name("test"));
        let metadata = ColumnMetadata::new("test".to_string(), 0, field_type);

        assert_eq!(metadata.field_type.get_size(), 64);
    }
}