grumpydb 2.0.0

A disk-based object storage engine with B+Tree indexing and page-based storage
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! # TaskMan — A simple task manager powered by GrumpyDB
//!
//! This is a **fully documented example application** that demonstrates how to
//! use GrumpyDB as a storage engine for a real application.
//!
//! ## Purpose
//!
//! TaskMan is a CLI task manager that stores tasks in a GrumpyDB database.
//! It showcases:
//! - Opening/creating a database
//! - CRUD operations (Create, Read, Update, Delete)
//! - Range scans and filtering
//! - Data model conversion (Rust structs ↔ GrumpyDB Values)
//! - Error handling patterns
//!
//! ## How to run
//!
//! ```bash
//! # Add a task
//! cargo run --example taskman -- add "Buy groceries" --desc "Milk, bread, eggs" --tags shopping,food
//!
//! # List all tasks
//! cargo run --example taskman -- list
//!
//! # Mark a task as done (use the short ID shown in `list`)
//! cargo run --example taskman -- done <id>
//!
//! # Show task details
//! cargo run --example taskman -- show <id>
//!
//! # Delete a task
//! cargo run --example taskman -- delete <id>
//!
//! # Show statistics
//! cargo run --example taskman -- stats
//! ```
//!
//! ## Architecture
//!
//! ```text
//! main.rs     → CLI parsing + command dispatch
//! task.rs        → Task struct + Value conversions
//! store.rs       → TaskStore wrapper around GrumpyDb
//! concurrent.rs  → SharedDb wrapper for multi-threaded access (Phase 7b)
//! ```

mod concurrent;
mod store;
mod task;

use std::env;
use std::path::PathBuf;

use store::TaskStore;
use task::Task;

// ─────────────────────────────────────────────────────────────────────────────
// Database location: tasks are stored in a `.taskman` directory
// in the current working directory. This is where GrumpyDB creates
// its data.db and index.db files.
// ─────────────────────────────────────────────────────────────────────────────
fn db_path() -> PathBuf {
    PathBuf::from(".taskman")
}

fn main() {
    let args: Vec<String> = env::args().collect();

    // The first argument after the binary name is the subcommand.
    if args.len() < 2 {
        print_help();
        return;
    }

    let command = args[1].as_str();

    // Dispatch to the appropriate handler.
    // Each handler opens the database, performs its operation, and closes it.
    let result = match command {
        "add" => cmd_add(&args[2..]),
        "list" => cmd_list(&args[2..]),
        "done" => cmd_set_status(&args[2..], true),
        "undone" => cmd_set_status(&args[2..], false),
        "show" => cmd_show(&args[2..]),
        "delete" => cmd_delete(&args[2..]),
        "stats" => cmd_stats(),
        "export" => cmd_export(&args[2..]),
        "import" => cmd_import(&args[2..]),
        "flush" => cmd_flush(),
        "compact" => cmd_compact(),
        "count" => cmd_count(),
        "bench" => cmd_bench(&args[2..]),
        "serve" => cmd_serve(&args[2..]),
        "generate" => cmd_generate(&args[2..]),
        "search" => cmd_search(&args[2..]),
        "help" | "--help" | "-h" => {
            print_help();
            Ok(())
        }
        _ => {
            eprintln!("Unknown command: {command}");
            print_help();
            Err("unknown command".to_string())
        }
    };

    if let Err(e) = result {
        eprintln!("Error: {e}");
        std::process::exit(1);
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: add
//
// Usage: taskman add "Task title" [--desc "Description"] [--tags tag1,tag2]
//
// Demonstrates: GrumpyDb::insert()
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_add(args: &[String]) -> Result<(), String> {
    if args.is_empty() {
        return Err("Usage: taskman add \"Task title\" [--desc \"...\"] [--tags t1,t2]".into());
    }

    let title = &args[0];
    let mut description: Option<&str> = None;
    let mut tags: Vec<&str> = Vec::new();

    // Simple argument parsing without external dependencies.
    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--desc" | "-d" => {
                i += 1;
                if i < args.len() {
                    description = Some(&args[i]);
                }
            }
            "--tags" | "-t" => {
                i += 1;
                if i < args.len() {
                    tags = args[i].split(',').collect();
                }
            }
            _ => {}
        }
        i += 1;
    }

    // Create the task with a generated UUID and current timestamp.
    let task = Task::new(title, description, tags);

    // Open the database, insert the task, close it.
    // GrumpyDb::open() creates the directory and files if they don't exist.
    let mut store = TaskStore::open(&db_path())?;
    let id = store.add_task(task)?;
    store.close()?;

    println!("Created task {}", &id.to_string()[..8]);
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: list
//
// Usage: taskman list [--done | --pending]
//
// Demonstrates: GrumpyDb::scan(..) + filtering
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_list(args: &[String]) -> Result<(), String> {
    let mut store = TaskStore::open(&db_path())?;

    let tasks = if args.first().map(|s| s.as_str()) == Some("--done") {
        store.list_by_status(true)?
    } else if args.first().map(|s| s.as_str()) == Some("--pending") {
        store.list_by_status(false)?
    } else {
        store.list_all_tasks()?
    };

    if tasks.is_empty() {
        println!("No tasks found. Add one with: taskman add \"My task\"");
    } else {
        println!("Tasks ({} total):", tasks.len());
        println!("{}", "-".repeat(60));
        for task in &tasks {
            // Task implements Display, which formats it nicely.
            println!("  {task}");
        }
    }

    store.close()?;
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: done / undone
//
// Usage: taskman done <id>
//        taskman undone <id>
//
// Demonstrates: read-modify-write pattern (get → modify → update)
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_set_status(args: &[String], done: bool) -> Result<(), String> {
    if args.is_empty() {
        return Err("Usage: taskman done <task-id>".into());
    }

    let id = parse_task_id(&args[0])?;
    let mut store = TaskStore::open(&db_path())?;

    // This demonstrates the read-modify-write pattern:
    // 1. Read the task (get)
    // 2. Modify the `done` field
    // 3. Write it back (update = full replacement)
    store.set_task_done(&id, done)?;
    store.close()?;

    let status = if done { "done" } else { "pending" };
    println!("Task {} marked as {status}", &args[0]);
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: show
//
// Usage: taskman show <id>
//
// Demonstrates: GrumpyDb::get() with full detail display
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_show(args: &[String]) -> Result<(), String> {
    if args.is_empty() {
        return Err("Usage: taskman show <task-id>".into());
    }

    let id = parse_task_id(&args[0])?;
    let mut store = TaskStore::open(&db_path())?;

    // db.get() returns None if the key doesn't exist — not an error.
    match store.get_task(&id)? {
        Some(task) => {
            println!("Task Details");
            println!("{}", "=".repeat(40));
            println!("  ID:          {}", task.id);
            println!("  Title:       {}", task.title);
            println!(
                "  Description: {}",
                task.description.as_deref().unwrap_or("(none)")
            );
            println!(
                "  Status:      {}",
                if task.done { "Done" } else { "Pending" }
            );
            println!("  Created:     {}", format_timestamp(task.created_at));
            if !task.tags.is_empty() {
                println!("  Tags:        {}", task.tags.join(", "));
            }
        }
        None => {
            println!("Task {} not found", &args[0]);
        }
    }

    store.close()?;
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: delete
//
// Usage: taskman delete <id>
//
// Demonstrates: GrumpyDb::delete()
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_delete(args: &[String]) -> Result<(), String> {
    if args.is_empty() {
        return Err("Usage: taskman delete <task-id>".into());
    }

    let id = parse_task_id(&args[0])?;
    let mut store = TaskStore::open(&db_path())?;

    // db.delete() removes the document and its index entry.
    // Returns KeyNotFound if the key doesn't exist.
    store.delete_task(&id)?;
    store.close()?;

    println!("Task {} deleted", &args[0]);
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: stats
//
// Usage: taskman stats
//
// Demonstrates: scan(..) + aggregation
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_stats() -> Result<(), String> {
    let mut store = TaskStore::open(&db_path())?;

    // stats() internally calls scan(..) to get all tasks, then counts them.
    // This is a full table scan — O(n) — but simple and correct for small datasets.
    let (total, done, pending) = store.stats()?;
    store.close()?;

    println!("Task Statistics");
    println!("{}", "=".repeat(30));
    println!("  Total:   {total}");
    println!("  Done:    {done}");
    println!("  Pending: {pending}");
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: compact
//
// Usage: taskman compact
//
// Demonstrates: GrumpyDb::compact()
// Defragments data pages and rebuilds the B+Tree index to reclaim space.
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_compact() -> Result<(), String> {
    let mut store = TaskStore::open(&db_path())?;

    let start = std::time::Instant::now();
    let result = store.compact()?;
    let elapsed = start.elapsed();

    store.close()?;

    println!("Compaction complete in {elapsed:.2?}");
    println!("  Documents preserved: {}", result.documents);
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: count
//
// Usage: taskman count
//
// Demonstrates: GrumpyDb::document_count()
// Returns the number of documents via B+Tree metadata (O(1), no scan needed).
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_count() -> Result<(), String> {
    let store = TaskStore::open(&db_path())?;
    let count = store.document_count();
    store.close()?;
    println!("{count} documents");
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: bench
//
// Usage: taskman bench [--writers N] [--readers N] [--count N]
//
// Demonstrates: SharedDb concurrent access from multiple threads.
// Uses a temporary database to avoid polluting the main task store.
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_bench(args: &[String]) -> Result<(), String> {
    let mut writers = 2;
    let mut readers = 4;
    let mut count = 1000;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--writers" | "-w" => {
                i += 1;
                if i < args.len() {
                    writers = args[i].parse().unwrap_or(2);
                }
            }
            "--readers" | "-r" => {
                i += 1;
                if i < args.len() {
                    readers = args[i].parse().unwrap_or(4);
                }
            }
            "--count" | "-n" => {
                i += 1;
                if i < args.len() {
                    count = args[i].parse().unwrap_or(1000);
                }
            }
            _ => {}
        }
        i += 1;
    }

    // Use a temporary directory for benchmarks (don't pollute the real task store).
    let bench_dir = std::env::temp_dir().join("taskman_bench");
    let _ = std::fs::remove_dir_all(&bench_dir);
    let result = concurrent::run_bench(&bench_dir, writers, readers, count);
    let _ = std::fs::remove_dir_all(&bench_dir);
    result
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: serve
//
// Usage: taskman serve [--port PORT]
//
// Demonstrates: SharedDb shared across client connection threads.
// Each TCP client gets its own thread with a cloned SharedDb handle.
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_serve(args: &[String]) -> Result<(), String> {
    let mut port = "8080";
    let mut i = 0;
    while i < args.len() {
        if args[i] == "--port" || args[i] == "-p" {
            i += 1;
            if i < args.len() {
                port = &args[i];
            }
        }
        i += 1;
    }
    let addr = format!("127.0.0.1:{port}");
    concurrent::run_server(&db_path(), &addr)
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: generate
//
// Usage: taskman generate --count N
//
// Demonstrates: bulk insert performance.
// Generates N synthetic tasks with predictable data for benchmarking.
// Shows ops/sec throughput — useful for measuring buffer pool impact.
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_generate(args: &[String]) -> Result<(), String> {
    let mut count = 1000usize;
    let mut i = 0;
    while i < args.len() {
        if args[i] == "--count" || args[i] == "-n" {
            i += 1;
            if i < args.len() {
                count = args[i].parse().unwrap_or(1000);
            }
        }
        i += 1;
    }

    let mut store = TaskStore::open(&db_path())?;

    // Measure insert throughput.
    // Each insert: encode document → store in slotted page → index in B+Tree → WAL commit.
    // With a buffer pool, repeated page access is served from cache → fewer disk reads.
    let start = std::time::Instant::now();
    let tags_pool = [
        "work",
        "personal",
        "urgent",
        "low-priority",
        "meeting",
        "errand",
    ];

    for i in 0..count {
        let tag_idx = i % tags_pool.len();
        let task = Task::new(
            format!("Generated task #{i}"),
            Some(&format!("Auto-generated task for benchmarking (batch {i})")),
            vec![tags_pool[tag_idx]],
        );
        store
            .add_task(task)
            .map_err(|e| format!("Insert {i} failed: {e}"))?;

        // Progress indicator every 1000 tasks
        if (i + 1) % 1000 == 0 {
            print!("\r  Generated {}/{count}...", i + 1);
            let _ = std::io::Write::flush(&mut std::io::stdout());
        }
    }

    let elapsed = start.elapsed();
    let ops_sec = count as f64 / elapsed.as_secs_f64();

    // Show buffer pool stats — demonstrates how the LRU cache reduces disk I/O.
    // With a buffer pool, the current data page stays cached between inserts,
    // so only the first insert (or a new page allocation) triggers a disk read.
    let (reads, writes, cached, capacity) = store.pool_stats();

    store.close()?;

    println!("\r  Generated {count} tasks in {elapsed:.2?} ({ops_sec:.0} ops/sec)");
    println!("  Buffer pool: {reads} reads, {writes} writes, {cached}/{capacity} pages cached");
    println!("  Use 'taskman search --tag urgent' to test scan+filter performance.");
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: search
//
// Usage: taskman search --tag <tag>
//
// Demonstrates: scan(..) + filter performance.
// Scans ALL documents (O(n)) and filters by tag.
// With a buffer pool, the B+Tree traversal and page reads hit the cache
// on repeated scans → significantly faster than hitting disk every time.
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_search(args: &[String]) -> Result<(), String> {
    let mut tag_filter: Option<&str> = None;
    let mut i = 0;
    while i < args.len() {
        if args[i] == "--tag" || args[i] == "-t" {
            i += 1;
            if i < args.len() {
                tag_filter = Some(&args[i]);
            }
        }
        i += 1;
    }

    let Some(tag) = tag_filter else {
        return Err("Usage: taskman search --tag <tag>".into());
    };

    let mut store = TaskStore::open(&db_path())?;

    // Measure scan + filter time.
    // This is a full table scan — O(n) — because GrumpyDB is a key-value store.
    // The buffer pool helps by caching B+Tree pages and data pages during the scan.
    let start = std::time::Instant::now();
    let all_tasks = store.list_all_tasks()?;
    let scan_time = start.elapsed();

    let start_filter = std::time::Instant::now();
    let matching: Vec<&Task> = all_tasks
        .iter()
        .filter(|t| t.tags.iter().any(|t_tag| t_tag == tag))
        .collect();
    let filter_time = start_filter.elapsed();

    // Buffer pool stats — shows how many pages were read from disk vs cache.
    // On repeated scans, most page reads come from cache → faster scans.
    let (reads, writes, cached, capacity) = store.pool_stats();

    store.close()?;

    println!("Search results for tag '{tag}':");
    println!("{}", "-".repeat(50));
    if matching.is_empty() {
        println!("  No tasks found with tag '{tag}'");
    } else {
        for task in &matching {
            println!("  {task}");
        }
    }
    println!();
    println!("Performance:");
    println!(
        "  Scanned:  {} documents in {scan_time:.2?}",
        all_tasks.len()
    );
    println!(
        "  Filtered: {} matches in {filter_time:.2?}",
        matching.len()
    );
    println!("  Total:    {:.2?}", scan_time + filter_time);
    println!("  Buffer pool: {reads} reads, {writes} writes, {cached}/{capacity} cached");
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: export
//
// Usage: taskman export [file]
//
// Demonstrates: scan(..) for full data export
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_export(args: &[String]) -> Result<(), String> {
    let mut store = TaskStore::open(&db_path())?;
    let data = store.export_tasks()?;
    store.close()?;

    if let Some(file_path) = args.first() {
        std::fs::write(file_path, &data).map_err(|e| format!("Failed to write file: {e}"))?;
        println!("Exported to {file_path}");
    } else {
        print!("{data}");
    }
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: import
//
// Usage: taskman import <file>
//
// Demonstrates: batch insert with crash safety
//
// Each task is inserted as a separate WAL transaction. If the process crashes
// mid-import, already-committed tasks are safe. The partially-written last
// task is rolled back automatically by WAL recovery on next open.
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_import(args: &[String]) -> Result<(), String> {
    if args.is_empty() {
        return Err("Usage: taskman import <file>".into());
    }

    let data =
        std::fs::read_to_string(&args[0]).map_err(|e| format!("Failed to read file: {e}"))?;

    let mut store = TaskStore::open(&db_path())?;
    let count = store.import_tasks(&data)?;
    store.close()?;

    println!("Imported {count} tasks");
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// COMMAND: flush
//
// Usage: taskman flush
//
// Demonstrates: explicit flush + WAL checkpoint.
// After this, all data is guaranteed durable on disk, and the WAL is truncated.
// ─────────────────────────────────────────────────────────────────────────────
fn cmd_flush() -> Result<(), String> {
    let mut store = TaskStore::open(&db_path())?;
    store.flush()?;
    store.close()?;
    println!("Database flushed and WAL checkpointed");
    Ok(())
}

// ─────────────────────────────────────────────────────────────────────────────
// HELPERS
// ─────────────────────────────────────────────────────────────────────────────

/// Parses a task ID from a string. Accepts both full UUIDs and short (8-char) prefixes.
///
/// For short prefixes, we scan the database to find a matching task.
/// This is a convenience feature — full UUIDs always work.
fn parse_task_id(input: &str) -> Result<uuid::Uuid, String> {
    // Try parsing as a full UUID first.
    if let Ok(id) = uuid::Uuid::parse_str(input) {
        return Ok(id);
    }

    // If it's a short prefix (e.g., "a3b4c5d6"), scan for a match.
    if input.len() >= 4 {
        let mut store = TaskStore::open(&db_path())?;
        let tasks = store.list_all_tasks()?;
        store.close()?;

        let matches: Vec<&Task> = tasks
            .iter()
            .filter(|t| t.id.to_string().starts_with(input))
            .collect();

        match matches.len() {
            0 => return Err(format!("No task found matching '{input}'")),
            1 => return Ok(matches[0].id),
            n => return Err(format!("{n} tasks match '{input}' — use more characters")),
        }
    }

    Err(format!("Invalid task ID: '{input}'"))
}

/// Formats a Unix timestamp as a human-readable date string.
fn format_timestamp(ts: i64) -> String {
    // Simple formatting without external crate.
    // In production, use `chrono` or `time` for proper formatting.
    let secs = ts as u64;
    let days = secs / 86400;
    let years = 1970 + days / 365; // approximate
    let remaining_days = days % 365;
    let months = remaining_days / 30 + 1;
    let day = remaining_days % 30 + 1;
    format!("{years:04}-{months:02}-{day:02}")
}

/// Prints the help message with usage examples.
fn print_help() {
    println!(
        r#"TaskMan — A task manager powered by GrumpyDB

USAGE:
    cargo run --example taskman -- <COMMAND> [OPTIONS]

COMMANDS:
    add <title> [--desc "..."] [--tags t1,t2]   Add a new task
    list [--done | --pending]                    List tasks
    show <id>                                    Show task details
    done <id>                                    Mark task as done
    undone <id>                                  Mark task as pending
    delete <id>                                  Delete a task
    stats                                        Show task statistics
    export [file]                                Export tasks (to stdout or file)
    import <file>                                Import tasks from file
    flush                                        Flush data + WAL checkpoint
    compact                                      Defragment data + rebuild index
    count                                        Document count (O(1), no scan)
    generate [--count N]                         Generate N synthetic tasks (with perf stats)
    search --tag <tag>                           Search tasks by tag (with perf stats)
    bench [--writers N] [--readers N] [--count N] Concurrent benchmark
    serve [--port PORT]                          Start TCP server (multi-client)
    help                                         Show this help

EXAMPLES:
    cargo run --example taskman -- add "Buy groceries" --tags shopping
    cargo run --example taskman -- list
    cargo run --example taskman -- done a3b4c5d6
    cargo run --example taskman -- generate --count 5000
    cargo run --example taskman -- search --tag urgent
    cargo run --example taskman -- bench --writers 4 --count 5000
    cargo run --example taskman -- flush
    cargo run --example taskman -- stats

DATA:
    Tasks are stored in .taskman/ in the current directory.
    Files: data.db (documents), index.db (B+Tree index), wal.log (Write-Ahead Log)

PERFORMANCE:
    GrumpyDB uses a buffer pool (LRU page cache, 256 frames = 2 MiB) to reduce
    disk I/O. The 'generate' and 'search' commands show buffer pool stats:
    - reads:  disk reads (cache misses)
    - writes: disk writes (dirty page flushes)
    - cached: pages currently in the pool
    Use 'generate --count 50000' then 'search --tag urgent' to see the cache in action.

CRASH SAFETY:
    Every write is protected by the Write-Ahead Log (WAL).
    If the process crashes, committed data is recovered automatically on next open.
    Use 'flush' to force a checkpoint and truncate the WAL.
"#
    );
}