alopex-cli 0.6.0

Command-line interface for Alopex DB
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
//! StreamingWriter - Streaming output controller
//!
//! Manages streaming output with buffer limits for non-streaming formats.

use std::io::Write;

use crate::error::{CliError, Result};
use crate::models::{Column, Row};
use crate::output::formatter::Formatter;
/// Default buffer limit for non-streaming formats (table).
pub const DEFAULT_BUFFER_LIMIT: usize = 10 * 1024 * 1024;

/// Status returned by write operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteStatus {
    /// Row was written successfully, continue writing.
    Continue,
    /// Limit reached, no more rows will be written.
    LimitReached,
}

/// Streaming writer for output.
///
/// Controls streaming output with the following behaviors:
/// - **Streaming formats** (json, jsonl, csv, tsv): Output rows immediately.
/// - **Non-streaming formats** (table): Buffer rows up to `buffer_limit`.
///
/// # Output Boundary
///
/// - **Output started**: When `output_started == true` (header has been written).
/// - **Buffer overflow**: Returns an error prompting `--output json|csv|tsv` or `--limit`.
pub struct StreamingWriter<W> {
    /// The underlying writer.
    writer: W,
    /// The formatter to use for output.
    formatter: Box<dyn Formatter>,
    /// Column definitions (schema).
    columns: Vec<Column>,
    /// Optional row limit.
    limit: Option<usize>,
    /// Buffer limit for non-streaming formats.
    buffer_limit: usize,
    /// Buffer for non-streaming formats (table).
    buffer: Vec<Row>,
    /// Approximate buffered size in bytes.
    buffer_bytes: usize,
    /// Number of rows written (for limit checking).
    written_count: usize,
    /// Whether header has been output (output started).
    output_started: bool,
    /// Whether quiet mode is enabled (suppress warnings).
    quiet: bool,
}

impl<W: Write> StreamingWriter<W> {
    /// Create a new StreamingWriter.
    ///
    /// # Arguments
    ///
    /// * `writer` - The output writer (e.g., stdout).
    /// * `formatter` - The formatter to use for output.
    /// * `columns` - Column definitions for the output.
    /// * `limit` - Optional row limit.
    pub fn new(
        writer: W,
        formatter: Box<dyn Formatter>,
        columns: Vec<Column>,
        limit: Option<usize>,
    ) -> Self {
        Self {
            writer,
            formatter,
            columns,
            limit,
            buffer_limit: DEFAULT_BUFFER_LIMIT,
            buffer: Vec::new(),
            buffer_bytes: 0,
            written_count: 0,
            output_started: false,
            quiet: false,
        }
    }

    /// Create a new StreamingWriter with a custom buffer limit.
    #[allow(dead_code)]
    pub fn with_buffer_limit(mut self, buffer_limit: usize) -> Self {
        self.buffer_limit = buffer_limit;
        self
    }

    /// Enable quiet mode (suppress warnings).
    pub fn with_quiet(mut self, quiet: bool) -> Self {
        self.quiet = quiet;
        self
    }

    /// Check if quiet mode is enabled.
    ///
    /// When quiet mode is enabled, status-only output (OK messages) should be suppressed.
    pub fn is_quiet(&self) -> bool {
        self.quiet
    }

    /// Prepare the writer for output.
    ///
    /// For streaming formats (json, jsonl, csv, tsv), immediately outputs the header.
    /// For non-streaming formats (table), defers header output.
    ///
    /// # Arguments
    ///
    /// * `row_count_hint` - Optional estimated row count (unused for buffer sizing).
    pub fn prepare(&mut self, row_count_hint: Option<usize>) -> Result<()> {
        let _ = row_count_hint;

        // For streaming formats, output header immediately
        if self.formatter.supports_streaming() {
            self.formatter
                .write_header(&mut self.writer, &self.columns)?;
            self.output_started = true;
        }

        Ok(())
    }

    /// Write a row to the output.
    ///
    /// For streaming formats, the row is output immediately.
    /// For non-streaming formats, the row is buffered.
    ///
    /// # Returns
    ///
    /// * `WriteStatus::Continue` - Row was written, continue writing.
    /// * `WriteStatus::LimitReached` - Row limit reached, stop writing.
    ///
    /// # Note
    ///
    /// The row is taken by ownership to avoid unnecessary cloning when buffering.
    pub fn write_row(&mut self, row: Row) -> Result<WriteStatus> {
        // Check limit
        if let Some(limit) = self.limit {
            if self.written_count >= limit {
                return Ok(WriteStatus::LimitReached);
            }
        }

        if self.formatter.supports_streaming() {
            // Streaming format: output immediately
            self.formatter.write_row(&mut self.writer, &row)?;
            self.written_count += 1;
        } else {
            // Non-streaming format: buffer the row
            let row_bytes = estimate_row_bytes(&row);
            self.buffer_bytes = self.buffer_bytes.saturating_add(row_bytes);
            if self.buffer_bytes > self.buffer_limit {
                return Err(CliError::InvalidArgument(
                    "Buffer limit exceeded (~10MB). \
                     Use --output json|csv|tsv or --limit to reduce results."
                        .into(),
                ));
            }
            self.buffer.push(row);
            self.written_count += 1;
        }

        Ok(WriteStatus::Continue)
    }

    /// Finish output, flushing any buffered rows and writing the footer.
    pub fn finish(&mut self) -> Result<()> {
        // For non-streaming formats, output header if not yet done
        if !self.output_started {
            self.formatter
                .write_header(&mut self.writer, &self.columns)?;
            self.output_started = true;

            // Flush any buffered rows
            for row in self.buffer.drain(..) {
                self.formatter.write_row(&mut self.writer, &row)?;
            }
        }

        // Write footer
        self.formatter.write_footer(&mut self.writer)?;

        Ok(())
    }

    /// Returns the number of rows written.
    #[allow(dead_code)]
    pub fn written_count(&self) -> usize {
        self.written_count
    }

    /// Returns whether output has started.
    #[allow(dead_code)]
    pub fn output_started(&self) -> bool {
        self.output_started
    }

    /// Returns approximate buffered bytes.
    #[allow(dead_code)]
    pub fn buffered_bytes(&self) -> usize {
        self.buffer_bytes
    }
}

fn estimate_row_bytes(row: &Row) -> usize {
    row.columns
        .iter()
        .map(estimate_value_bytes)
        .sum::<usize>()
        .saturating_add(row.columns.len() * 8)
}

fn estimate_value_bytes(value: &crate::models::Value) -> usize {
    match value {
        crate::models::Value::Null => 4,
        crate::models::Value::Bool(_) => 1,
        crate::models::Value::Int(_) => 8,
        crate::models::Value::Float(_) => 8,
        crate::models::Value::Text(text) => text.len(),
        crate::models::Value::Bytes(bytes) => bytes.len(),
        crate::models::Value::Vector(values) => values.len() * 4,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::CliError;
    use crate::models::{DataType, Value};
    use crate::output::csv::CsvFormatter;
    use crate::output::json::JsonFormatter;
    use crate::output::jsonl::JsonlFormatter;
    use crate::output::table::TableFormatter;

    fn test_columns() -> Vec<Column> {
        vec![
            Column::new("id", DataType::Int),
            Column::new("name", DataType::Text),
        ]
    }

    fn test_row(id: i64, name: &str) -> Row {
        Row::new(vec![Value::Int(id), Value::Text(name.to_string())])
    }

    #[test]
    fn test_streaming_format_immediate_output() {
        let mut output = Vec::new();
        let formatter = Box::new(JsonlFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(&mut output, formatter, columns, None);

        writer.prepare(None).unwrap();
        assert!(writer.output_started());

        let status = writer.write_row(test_row(1, "Alice")).unwrap();
        assert_eq!(status, WriteStatus::Continue);
        assert_eq!(writer.written_count(), 1);

        writer.finish().unwrap();

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("\"id\":1"));
        assert!(result.contains("\"name\":\"Alice\""));
    }

    #[test]
    fn test_non_streaming_format_buffered_output() {
        let mut output = Vec::new();
        let formatter = Box::new(TableFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(&mut output, formatter, columns, None);

        writer.prepare(None).unwrap();
        assert!(!writer.output_started()); // Header not output yet

        let status = writer.write_row(test_row(1, "Alice")).unwrap();
        assert_eq!(status, WriteStatus::Continue);
        assert!(!writer.output_started()); // Still buffering

        writer.finish().unwrap();
        assert!(writer.output_started()); // Now output started

        let result = String::from_utf8(output).unwrap();
        assert!(result.contains("id"));
        assert!(result.contains("Alice"));
    }

    #[test]
    fn test_limit_enforcement() {
        let mut output = Vec::new();
        let formatter = Box::new(CsvFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(&mut output, formatter, columns, Some(2));

        writer.prepare(None).unwrap();

        assert_eq!(
            writer.write_row(test_row(1, "Alice")).unwrap(),
            WriteStatus::Continue
        );
        assert_eq!(
            writer.write_row(test_row(2, "Bob")).unwrap(),
            WriteStatus::Continue
        );
        assert_eq!(
            writer.write_row(test_row(3, "Charlie")).unwrap(),
            WriteStatus::LimitReached
        );

        assert_eq!(writer.written_count(), 2);

        writer.finish().unwrap();
    }

    #[test]
    fn test_buffer_overflow_errors() {
        let mut output = Vec::new();
        let formatter = Box::new(TableFormatter::new());
        let columns = test_columns();

        let mut writer =
            StreamingWriter::new(&mut output, formatter, columns, None).with_buffer_limit(40); // Very small buffer

        writer.prepare(None).unwrap();
        assert!(!writer.output_started());

        // Add rows to buffer
        assert_eq!(
            writer.write_row(test_row(1, "Alice")).unwrap(),
            WriteStatus::Continue
        );
        let err = writer.write_row(test_row(2, "Bob")).unwrap_err();
        assert!(matches!(err, CliError::InvalidArgument(_)));
    }

    #[test]
    fn test_empty_output() {
        let mut output = Vec::new();
        let formatter = Box::new(JsonFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(&mut output, formatter, columns, None);

        writer.prepare(None).unwrap();
        writer.finish().unwrap();

        let result = String::from_utf8(output).unwrap();
        // JSON array format: should be valid empty array
        assert!(result.contains('['));
        assert!(result.contains(']'));
    }

    #[test]
    fn test_csv_streaming() {
        let mut output = Vec::new();
        let formatter = Box::new(CsvFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(&mut output, formatter, columns, None);

        writer.prepare(None).unwrap();
        assert!(writer.output_started()); // CSV is streaming

        writer.write_row(test_row(1, "Alice")).unwrap();
        writer.write_row(test_row(2, "Bob")).unwrap();
        writer.finish().unwrap();

        let result = String::from_utf8(output).unwrap();
        assert_eq!(result, "id,name\n1,Alice\n2,Bob\n");
    }

    #[test]
    fn test_written_count() {
        let mut output = Vec::new();
        let formatter = Box::new(CsvFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(&mut output, formatter, columns, None);

        writer.prepare(None).unwrap();

        assert_eq!(writer.written_count(), 0);
        writer.write_row(test_row(1, "Alice")).unwrap();
        assert_eq!(writer.written_count(), 1);
        writer.write_row(test_row(2, "Bob")).unwrap();
        assert_eq!(writer.written_count(), 2);

        writer.finish().unwrap();
    }

    #[test]
    fn test_table_with_small_data() {
        let mut output = Vec::new();
        let formatter = Box::new(TableFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(&mut output, formatter, columns, None);

        writer.prepare(None).unwrap();
        assert!(!writer.output_started()); // Table is non-streaming

        writer.write_row(test_row(1, "Alice")).unwrap();
        writer.write_row(test_row(2, "Bob")).unwrap();
        writer.finish().unwrap();

        let result = String::from_utf8(output).unwrap();
        // Table output should contain the data
        assert!(result.contains("id"));
        assert!(result.contains("name"));
        assert!(result.contains("Alice"));
        assert!(result.contains("Bob"));
    }

    #[test]
    fn test_streaming_large_row_count_does_not_buffer() {
        let formatter = Box::new(JsonlFormatter::new());
        let columns = test_columns();

        let mut writer = StreamingWriter::new(std::io::sink(), formatter, columns, None);

        writer.prepare(None).unwrap();
        for i in 0..12_000 {
            writer.write_row(test_row(i, "row")).unwrap();
        }

        assert_eq!(writer.written_count(), 12_000);
        assert!(writer.buffer.is_empty());

        writer.finish().unwrap();
    }
}