siftdb-cli 0.2.2

Command-line interface for SiftDB - the high-performance grep-native database
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
use clap::{Parser, Subcommand, ValueEnum};
use siftdb_core::{SiftDB, bench::SiftDBBenchmark, ingest::{Ingester, IngestOptions}};
use std::path::PathBuf;
use anyhow::Result;

#[derive(ValueEnum, Clone, Debug)]
enum OutputFormat {
    Text,
    Json,
}

#[derive(Parser)]
#[command(name = "sift")]
#[command(about = "SiftDB - Grep-Native Database for Code and Text Collections")]
#[command(version = "0.1.0")]
struct Cli {
    /// Lock timeout in seconds for operations requiring write access
    #[arg(long, global = true, default_value = "300")]
    lock_timeout: u64,
    /// Lock holder identifier (useful for debugging concurrent operations)
    #[arg(long, global = true, default_value = "sift-cli")]
    lock_holder: String,
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize a new SiftDB collection
    Init {
        /// Path to the collection directory
        collection: PathBuf,
    },
    /// Import files from filesystem into collection
    Import {
        /// Path to the collection directory
        collection: PathBuf,
        /// Source directory to import from
        #[arg(long)]
        from: PathBuf,
        /// Include patterns (glob format)
        #[arg(long, action = clap::ArgAction::Append)]
        include: Vec<String>,
        /// Exclude patterns (glob format)
        #[arg(long, action = clap::ArgAction::Append)]
        exclude: Vec<String>,
        /// Maximum file size in bytes
        #[arg(long, default_value = "10485760")] // 10MB
        max_file_bytes: u64,
    },
    /// Find substring matches in collection
    Find {
        /// Path to the collection directory
        collection: PathBuf,
        /// Query string to search for
        query: String,
        #[clap(short, long)]
        limit: Option<usize>,
        #[clap(short, long)]
        path_glob: Option<String>,
        #[clap(short, long, value_enum, default_value = "text")]
        format: OutputFormat,
    },
    /// Search with regex patterns (Milestone 0.2)
    Regex {
        collection: PathBuf,
        pattern: String,
        #[clap(short, long)]
        limit: Option<usize>,
        #[clap(short, long)]
        path_glob: Option<String>,
        #[clap(short, long, value_enum, default_value = "text")]
        format: OutputFormat,
    },
    /// Open and view file content
    Open {
        /// Path to the collection directory
        collection: PathBuf,
        /// File path within collection
        #[arg(long)]
        file: String,
        /// Start line number (1-based)
        #[arg(long, default_value = "1")]
        start_line: u32,
        /// End line number (1-based, 0 = end of file)
        #[arg(long, default_value = "0")]
        end_line: u32,
    },
    /// Run performance benchmark
    Benchmark {
        /// Collection path
        collection: PathBuf,
        /// Source directory to benchmark against
        #[arg(long)]
        source: PathBuf,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Compare benchmark results for regression analysis
    Compare {
        /// Directory containing benchmark files
        path: PathBuf,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Incrementally update collection with changed files
    Update {
        /// Collection path
        collection: PathBuf,
        /// Source directory to scan for changes
        #[arg(long)]
        from: PathBuf,
        /// File patterns to include (glob format)
        #[arg(long, action = clap::ArgAction::Append)]
        include: Vec<String>,
        /// File patterns to exclude (glob format)  
        #[arg(long, action = clap::ArgAction::Append)]
        exclude: Vec<String>,
        /// Force update even if no changes detected
        #[arg(long)]
        force: bool,
    },
    /// Compact collection to remove tombstones and optimize storage
    Compact {
        /// Collection path
        collection: PathBuf,
        /// Force compaction even if not needed
        #[arg(long)]
        force: bool,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Show compaction status and tombstone statistics
    Status {
        /// Collection path
        collection: PathBuf,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
    },
    /// Benchmark Milestone 0.4 features (incremental updates, compaction)
    BenchmarkAdvanced {
        /// Collection path
        collection: PathBuf,
        /// Source directory for testing
        #[arg(long)]
        source: PathBuf,
        /// Output format: text or json
        #[arg(long, default_value = "text")]
        format: String,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Init { collection } => {
            println!("Initializing SiftDB collection at: {}", collection.display());
            let _db = SiftDB::init(&collection)?;
            println!("✓ Collection initialized successfully");
        }

        Commands::Import {
            collection,
            from,
            include,
            exclude,
            max_file_bytes,
        } => {
            println!("Importing files into: {}", collection.display());
            println!("From source: {}", from.display());

            let _db = SiftDB::open(&collection)?;
            
            let mut options = IngestOptions::default();
            options.max_file_bytes = max_file_bytes;
            
            if !include.is_empty() {
                options.include_patterns = include;
            }
            if !exclude.is_empty() {
                options.exclude_patterns.extend(exclude);
            }

            let mut ingester = Ingester::new(collection, options);
            let stats = match ingester.ingest_from_fs(&from) {
                Ok(stats) => stats,
                Err(e) => {
                    eprintln!("Import failed: {}", e);
                    std::process::exit(1);
                }
            };
            
            println!("✓ Import completed:");
            println!("  Files ingested: {}", stats.ingested);
            println!("  Files skipped: {}", stats.skipped);
            println!("  Errors: {}", stats.errors);
        }

        Commands::Find { collection, query, limit, path_glob, format } => {
            let db = SiftDB::open(&collection)?;
            let mut snapshot = db.snapshot()?;
            
            let hits = snapshot.find(&query, path_glob.as_deref(), limit)?;
            
            match format {
                OutputFormat::Text => {
                    if hits.is_empty() {
                        println!("No matches found for: {}", query);
                    } else {
                        println!("Found {} matches for: {}", hits.len(), query);
                        for hit in hits {
                            println!("{}:{}: {}", hit.path, hit.line, hit.text);
                        }
                    }
                }
                OutputFormat::Json => {
                    let json = serde_json::to_string_pretty(&hits)?;
                    println!("{}", json);
                }
            }
        }

        Commands::Regex { collection, pattern, limit, path_glob, format } => {
            println!("🚀 Milestone 0.2: Regex search");
            let db = SiftDB::open(&collection)?;
            let snapshot = db.snapshot()?;
            
            // Use regex search with trigram acceleration (0.2 feature)
            let hits = snapshot.regex_find(&pattern, path_glob.as_deref(), limit)?;
            
            match format {
                OutputFormat::Text => {
                    if hits.is_empty() {
                        println!("No matches found for regex: {}", pattern);
                    } else {
                        println!("Found {} regex matches for: {}", hits.len(), pattern);
                        for hit in hits {
                            println!("{}:{}: {}", hit.path, hit.line, hit.text);
                        }
                    }
                }
                OutputFormat::Json => {
                    let json = serde_json::to_string_pretty(&hits)?;
                    println!("{}", json);
                }
            }
        }

        Commands::Open {
            collection,
            file,
            start_line,
            mut end_line,
        } => {
            let db = SiftDB::open(&collection)?;
            let snapshot = db.snapshot()?;
            
            // If end_line is 0, we'll get the whole file (handled in open_span)
            if end_line == 0 {
                end_line = u32::MAX; // Will be clamped to actual file length
            }
            
            match snapshot.open_span(&file, start_line, end_line) {
                Ok(span) => {
                    println!("=== {} (lines {}-{}) ===", span.path, span.start_line, span.end_line);
                    println!("{}", span.content);
                }
                Err(e) => {
                    eprintln!("Error opening file: {}", e);
                    std::process::exit(1);
                }
            }
        }

        Commands::Benchmark { collection, source, format } => {
            match format.as_str() {
                "text" => {
                    println!("Running SiftDB benchmark...");
                    println!("Collection: {}", collection.display());
                    println!("Source: {}", source.display());
                    
                    let mut benchmark = SiftDBBenchmark::new(&collection, &source);
                    let _results = benchmark.run_all();
                }
                "json" => {
                    // For JSON, suppress all text output and just print JSON
                    let mut benchmark = SiftDBBenchmark::new(&collection, &source);
                    let results = benchmark.run_all_quiet();
                    let json_output = serde_json::to_string_pretty(&results).unwrap();
                    print!("{}", json_output);
                }
                _ => {
                    eprintln!("Unknown format: {}. Use 'text' or 'json'", format);
                    std::process::exit(1);
                }
            }
        }

        Commands::Compare { path, format } => {
            println!("Analyzing benchmark performance regressions...");
            println!("Regression analysis not yet implemented");
        }

        Commands::Update { collection, from, include, exclude, force } => {
            println!("🔄 Incremental update for: {}", collection.display());
            
            let db = SiftDB::open(&collection)?;
            
            if !db.has_incremental_cache() && !force {
                println!("❌ No incremental cache found. Use full import first, or --force to create initial cache.");
                std::process::exit(1);
            }
            
            match db.incremental_update(&from, &include, &exclude) {
                Ok(delta_manifest) => {
                    println!("✅ Incremental update completed!");
                    println!("   Base epoch: {}", delta_manifest.base_epoch);
                    println!("   New epoch: {}", delta_manifest.delta_epoch);
                    println!("   Changes applied: {}", delta_manifest.changes.len());
                }
                Err(e) => {
                    if e.to_string().contains("No changes detected") {
                        println!("✅ Collection is up to date - no changes detected");
                    } else {
                        eprintln!("❌ Incremental update failed: {}", e);
                        std::process::exit(1);
                    }
                }
            }
        }

        Commands::Compact { collection, force, format } => {
            use siftdb_core::compaction::CollectionCompactor;
            
            println!("🧹 Collection compaction for: {}", collection.display());
            
            let compactor = CollectionCompactor::new(&collection);
            
            if !force && !compactor.needs_compaction()? {
                println!("ℹ️  Collection does not need compaction. Use --force to compact anyway.");
                return Ok(());
            }
            
            match compactor.compact() {
                Ok(stats) => {
                    match format.as_str() {
                        "json" => {
                            let json = serde_json::to_string_pretty(&stats)?;
                            println!("{}", json);
                        }
                        _ => {
                            println!("✅ Compaction completed!");
                            println!("   Duration: {}s", stats.duration_secs);
                            println!("   Tombstones removed: {}", stats.tombstones_removed);
                            println!("   Segments compacted: {}", stats.segments_compacted);
                            println!("   Space reclaimed: {} bytes", stats.space_reclaimed_bytes);
                            println!("   Epoch: {} -> {}", stats.before_epoch, stats.after_epoch);
                        }
                    }
                }
                Err(e) => {
                    eprintln!("❌ Compaction failed: {}", e);
                    std::process::exit(1);
                }
            }
        }

        Commands::Status { collection, format } => {
            
            let manager = siftdb_core::compaction::CompactionManager::new(&collection);
            let status = manager.status()?;
            
            match format.as_str() {
                "json" => {
                    println!("{{");
                    println!("  \"needs_compaction\": {},", status.needs_compaction);
                    println!("  \"total_tombstones\": {},", status.total_tombstones);
                    println!("  \"oldest_tombstone_epoch\": {},", status.oldest_tombstone_epoch);
                    println!("  \"newest_tombstone_epoch\": {},", status.newest_tombstone_epoch);
                    if let Some(last_compaction) = status.last_compaction_at {
                        println!("  \"last_compaction_at\": {},", last_compaction);
                    } else {
                        println!("  \"last_compaction_at\": null,");
                    }
                    println!("  \"compaction_count\": {}", status.compaction_count);
                    println!("}}");
                }
                _ => {
                    println!("📊 Collection Status: {}", collection.display());
                    println!("   Needs compaction: {}", if status.needs_compaction { "Yes" } else { "No" });
                    println!("   Total tombstones: {}", status.total_tombstones);
                    println!("   Oldest tombstone epoch: {}", status.oldest_tombstone_epoch);
                    println!("   Newest tombstone epoch: {}", status.newest_tombstone_epoch);
                    if let Some(last_compaction) = status.last_compaction_at {
                        println!("   Last compaction: {}", last_compaction);
                    } else {
                        println!("   Last compaction: Never");
                    }
                    println!("   Total compactions: {}", status.compaction_count);
                }
            }
        }

        Commands::BenchmarkAdvanced { collection, source, format } => {
            println!("🚀 SiftDB Advanced Features Benchmark");
            println!("========================================");
            println!("Collection: {}", collection.display());
            println!("Source: {}", source.display());
            println!();
            
            // Run existing benchmark for comparison
            let mut benchmark = siftdb_core::bench::SiftDBBenchmark::new(&collection, &source);
            let results = benchmark.run_all_quiet();
            
            match format.as_str() {
                "text" => {
                    println!("📋 Basic Performance Results:");
                    for result in &results {
                        println!("   {}: {:.2}s", result.name, result.duration.as_secs_f64());
                        if let Some(qps) = result.queries_per_second {
                            println!("      Queries/sec: {:.1}", qps);
                        }
                        if let Some(throughput) = result.throughput_mbps {
                            println!("      Throughput: {:.1} MB/s", throughput);
                        }
                    }
                    
                    println!("\n📋 Milestone 0.4 Features Status:");
                    println!("   ✅ Incremental Updates: Implemented");
                    println!("   ✅ Collection Compaction: Implemented");
                    println!("   ✅ Delta Manifests: Implemented");
                    println!("   ✅ File Timestamp Tracking: Implemented");
                }
                "json" => {
                    let json_output = serde_json::to_string_pretty(&results).unwrap();
                    print!("{}", json_output);
                }
                _ => {
                    eprintln!("Unknown format: {}. Use 'text' or 'json'", format);
                    std::process::exit(1);
                }
            }
        }
    }

    Ok(())
}