tito 0.1.43

A flexible database layer with powerful indexing strategies and relationship modeling, supporting multiple backends
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
# Tito

> ⚠️ **WARNING: This project is in early development and NOT PRODUCTION READY.** Use at your own risk. The API may change without notice, and there may be bugs and performance issues. Not recommended for critical applications.

Tito is a powerful, flexible database layer built on top of TiKV, providing robust indexing strategies, relationship modeling, and transaction management capabilities for Rust applications.

## Features

- **Powerful Indexing Strategies**: Define custom indexes with conditional logic for efficient querying
- **Relationship Modeling**: Create and manage embedded relationships between data models
- **Transaction Management**: Full ACID-compliant transaction support
- **Event-Driven Queue**: Built-in persistent event queue with FIFO ordering and partition-based parallelism
- **Async Workers**: Tokio-powered workers for concurrent event processing with per-event-type isolation
- **Type Safety**: Leverages Rust's type system for safety and performance
- **Flexible Query API**: Query data using intuitive builder pattern

## Installation

Add Tito to your project:

```toml
[dependencies]
tito = "0.1.43"
```

## Quick Start

### Setting up a Connection

```rust
// Connect to TiKV with multiple endpoints
let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;

// For production, use multiple PD endpoints for high availability
let tito_db = TiKV::connect(vec![
    "pd1.example.com:2379",
    "pd2.example.com:2379",
    "pd3.example.com:2379"
]).await?;
```

### Creating a Model

```rust
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
struct User {
    id: String,
    name: String,
    email: String,
}

impl TitoModelTrait for User {
    fn relationships(&self) -> Vec<tito::types::TitoRelationshipConfig> {
        vec![] // No relationships for this simple model
    }

    fn references(&self) -> Vec<String> {
        vec![] // No references to other entities
    }

    fn partition_key(&self) -> String {
        self.id.clone() // Partition by user ID
    }

    fn indexes(&self) -> Vec<TitoIndexConfig> {
        vec![TitoIndexConfig {
            condition: true,
            name: "by_email".to_string(),
            fields: vec![TitoIndexField {
                name: "email".to_string(),
                r#type: TitoIndexBlockType::String,
            }],
        }]
    }

    fn table(&self) -> String {
        "users".to_string()
    }

    fn events(&self) -> Vec<TitoEventConfig> {
        vec![] // No events for this model
    }

    fn id(&self) -> String {
        self.id.clone()
    }
}

// Create model with the storage backend  
let user_model = tito_db.clone().model::<User>();
```

### Basic CRUD Operations

```rust
// Create a user
let user_id = DBUuid::new_v4().to_string();
let user = User {
    id: user_id.clone(),
    name: "John Doe".to_string(),
    email: "john@example.com".to_string(),
};

// Create user with transaction
let saved_user = tito_db.transaction(|tx| {
    let user_model = user_model.clone();
    async move {
        user_model.build(user, &tx).await
    }
}).await?;

// Find user by ID
let found_user = user_model.find_by_id(&user_id, vec![]).await?;

// Update user
let updated_user = User {
    id: user_id.clone(),
    name: "John Updated".to_string(),
    email: "john_updated@example.com".to_string(),
};

tito_db.transaction(|tx| {
    let user_model = user_model.clone();
    async move {
        user_model.update(updated_user, &tx).await
    }
}).await?;

// Delete user
tito_db.transaction(|tx| {
    let user_model = user_model.clone();
    async move {
        user_model.delete_by_id(&user_id, &tx).await
    }
}).await?;
```

## Using the Query Builder

Tito provides a powerful query builder pattern for easier and more readable querying.

### Basic Query

```rust
// Find user by email
let mut query = user_model.query_by_index("by_email");
let result = query
    .value("john@example.com")
    .execute()
    .await?;

// Check if we found a user
if let Some(user) = result.items.first() {
    println!("Found user: {}", user.name);
}
```

### Query with Relationships

```rust
// Post model with tag relationships
let mut query = post_model.query_by_index("post-by-author");
let posts = query
    .value("john")              // Author name
    .relationship("tags")       // Include tags relationship
    .limit(Some(10))            // Limit to 10 results
    .execute()
    .await?;

for post in posts.items {
    println!("Post: {} with {} tags", post.title, post.tags.len());
}
```

### Advanced Queries

```rust
// Find active users by role
let mut query = user_model.query_by_index("by_role_and_status");
let active_admins = query
    .value("admin")             // First index field (role)
    .value("active")            // Second index field (status)
    .limit(Some(20))            // Limit results
    .execute()
    .await?;

// Use cursor for pagination
if let Some(cursor) = active_admins.cursor {
    // Get next page using the same query with a cursor
    let mut next_page_query = user_model.query_by_index("by_role_and_status");
    let next_page = next_page_query
        .value("admin")
        .value("active")
        .cursor(Some(cursor))   // Pass the cursor
        .limit(Some(20))
        .execute()
        .await?;
}
```

### Reverse Order Query

```rust
// Get latest posts in reverse chronological order
let mut query = post_model.query_by_index("post-by-created");
let latest_posts = query
    .value("2023")              // Index by year
    .execute_reverse()          // Execute in reverse order
    .await?;
```

### Transaction-Specific Queries

```rust
tito_db.transaction(|tx| {
    let user_model = user_model.clone();
    async move {
        // Query within transaction context
        let mut query = user_model.query_by_index("by_email");
        let user = query
            .value("john@example.com")
            .execute_tx(&tx)        // Execute within transaction
            .await?;

        // Update user in same transaction
        if let Some(mut user) = user.items.first().cloned() {
            user.name = "John Smith".to_string();
            user_model.update(user, &tx).await?;
        }

        Ok::<_, TitoError>(())
    }
}).await?;
```

## Advanced Features

### Conditional Indexing

Tito lets you conditionally index data based on business rules:

```rust
TitoIndexConfig {
    condition: self.status == "active", // Only index active items
    name: "by_active_status".to_string(),
    fields: vec![TitoIndexField {
        name: "status".to_string(),
        r#type: TitoIndexBlockType::String,
    }],
}
```

### Relationship Modeling

Define relationships between models and fetch related data efficiently:

```rust
// Post model with references to multiple tags
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
struct Post {
    id: String,
    title: String,
    content: String,
    author: String,
    tag_ids: Vec<String>, // Reference to related tag IDs
    #[serde(default)]
    tags: Vec<Tag>,       // Will be populated when relationship is fetched
}

impl TitoModelTrait for Post {
    fn relationships(&self) -> Vec<TitoRelationshipConfig> {
        // Define the relationship between posts and tags
        vec![TitoRelationshipConfig {
            source_field_name: "tag_ids".to_string(),
            destination_field_name: "tags".to_string(),
            model: "tag".to_string(),
        }]
    }

    fn references(&self) -> Vec<String> {
        self.tag_ids.clone() // References to tag IDs
    }

    fn partition_key(&self) -> String {
        self.id.clone()
    }

    // ... other implementation details (indexes, table, events, id)
}

// Fetch a post with related tags using query builder
let mut query = post_model.query_by_index("post-by-id");
let post = query
    .value(post_id)
    .relationship("tags")
    .execute()
    .await?;
```

### Composite Indexes

Create and query using composite indexes:

```rust
// Define composite index
TitoIndexConfig {
    condition: true,
    name: "by_category_and_date".to_string(),
    fields: vec![
        TitoIndexField {
            name: "category".to_string(),
            r#type: TitoIndexBlockType::String,
        },
        TitoIndexField {
            name: "published_at".to_string(),
            r#type: TitoIndexBlockType::Number,
        },
    ],
}

// Query using composite index
let mut query = article_model.query_by_index("by_category_and_date");
let articles = query
    .value("technology")         // category value
    .value("20230101")           // published_at value as number
    .limit(Some(5))
    .execute()
    .await?;
```

## Event Queue System

Tito includes a built-in event-driven queue system for asynchronous processing with FIFO ordering and partition-based parallelism.

### Defining Events

Define events in your model by implementing the `events()` method:

```rust
impl TitoModelTrait for User {
    fn events(&self) -> Vec<TitoEventConfig> {
        vec![TitoEventConfig {
            name: "user".to_string(), // Event type name
        }]
    }

    // Other trait methods...
}
```

### Creating Events

Events are automatically created when you perform operations with event options:

```rust
use tito::{TitoOptions, TitoOperation};

// Create a user and generate an INSERT event
tito_db.transaction(|tx| {
    let user_model = user_model.clone();
    async move {
        user_model
            .build_with_options(
                user,
                TitoOptions::with_events(TitoOperation::Insert),
                &tx,
            )
            .await?;
        Ok::<_, TitoError>(())
    }
}).await?;

// Update a user and generate an UPDATE event
tito_db.transaction(|tx| {
    let user_model = user_model.clone();
    async move {
        user_model
            .update_with_options(
                user,
                TitoOptions::with_events(TitoOperation::Update),
                &tx,
            )
            .await?;
        Ok::<_, TitoError>(())
    }
}).await?;
```

### Setting Up Workers

Create workers to process events by event type:

```rust
use tito::queue::{TitoQueue, run_worker};
use tito::types::PartitionConfig;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use tokio::sync::broadcast;

// Create queue
let queue = Arc::new(TitoQueue {
    engine: tito_db.clone(),
});

// Worker configuration
let is_leader = Arc::new(AtomicBool::new(true));
let partition_config = PartitionConfig {
    start: 0,
    end: 1024, // Process all 1024 partitions
};

// Create shutdown channel
let (shutdown_tx, shutdown_rx) = broadcast::channel(1);

// Define event handler
let handler = move |event: tito::TitoEvent| {
    Box::pin(async move {
        println!("Processing: {} - {}", event.action, event.entity);
        // Your event processing logic here
        Ok::<_, TitoError>(())
    }) as BoxFuture<'static, Result<(), TitoError>>
};

// Start worker for "user" events
let worker_handle = run_worker(
    queue.clone(),
    String::from("user"), // Event type to process
    handler,
    partition_config,
    is_leader.clone(),
    5, // Concurrency level
    shutdown_rx,
)
.await;

// Gracefully shutdown when done
let _ = shutdown_tx.send(());
let _ = worker_handle.await;
```

### Key Concepts

- **Event Types**: Each model can define multiple event types. Workers process one event type at a time, enabling independent scaling and failure isolation.
- **FIFO Ordering**: Events are processed oldest-first within each partition, ensuring correct ordering.
- **Partitions**: The system uses 1024 fixed partitions for parallel processing. Partitioning is determined by the model's `partition_key()` method.
- **Checkpoints**: Each event type maintains independent checkpoints per partition, tracking processing progress.
- **Leader Election**: Use the `is_leader` flag to ensure only one worker processes events in distributed setups.
- **Graceful Shutdown**: Workers respect shutdown signals for clean termination.

### Multiple Workers

You can run multiple workers for different event types:

```rust
// Worker for user events
let user_worker = run_worker(
    queue.clone(),
    String::from("user"),
    user_handler,
    partition_config.clone(),
    is_leader.clone(),
    5,
    shutdown_rx.resubscribe(),
).await;

// Worker for order events
let order_worker = run_worker(
    queue.clone(),
    String::from("order"),
    order_handler,
    partition_config.clone(),
    is_leader.clone(),
    10, // Different concurrency
    shutdown_rx.resubscribe(),
).await;
```

Each event type acts as an independent queue (similar to Kafka topics), allowing you to:
- Scale workers independently based on load
- Isolate failures to specific event types
- Configure different concurrency levels per event type

## Running the Examples

Tito comes with comprehensive examples demonstrating all major features. To run them, you'll need a TiKV server running locally.

### Setting up TiKV

The quickest way to get started is using Docker:

```bash
# Start a local TiKV cluster
docker run -d --name tikv \
  -p 2379:2379 \
  pingcap/pd:latest \
  --name "pd" \
  --data-dir "/data/pd" \
  --client-urls "http://0.0.0.0:2379"

docker run -d --name tikv \
  -p 20160:20160 \
  --link pd \
  pingcap/tikv:latest \
  --pd "pd:2379" \
  --data-dir "/data/tikv"
```

Or use TiUP for a production-like setup:
```bash
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
tiup playground --mode tikv-slim
```

### Example 1: Basic CRUD (`crud.rs`)

Demonstrates fundamental database operations:

```bash
cargo run --example crud
```

**What it does:**
- Connects to TiKV
- Creates a user with email indexing
- Finds the user by ID
- Updates user information
- Deletes the user

**Expected output:**
```
Created user: User { id: "...", name: "John Doe", email: "john@example.com" }
Found user: User { id: "...", name: "John Doe", email: "john@example.com" }
User updated successfully
User deleted successfully
```

### Example 2: Relationships and Queries (`blog.rs`)

Shows how to work with related data using the powerful relationship system:

```bash
cargo run --example blog
```

**What it does:**
- Creates tags (Technology, Travel, Rust, Databases)
- Creates blog posts with multiple tags
- Demonstrates relationship hydration
- Queries posts by tag
- Queries posts by author

**Expected output:**
```
Created tags:
- Technology: <uuid>
- Travel: <uuid>
...

Created posts:
1. Introduction to TiKV (by Alice)
2. Best cities to visit in Europe (by Bob)
3. Using Rust with TiKV (by Alice)

Post with tags:
Title: Introduction to TiKV
Tags:
- Technology
- Databases

Technology posts:
- Using Rust with TiKV (by Alice)
  Tags: Technology, Rust, Databases
...
```

**Key concepts demonstrated:**
- One-to-many relationships (Post → Tags)
- Relationship hydration with `.relationship("tags")`
- Composite indexes for efficient querying
- Query builder pattern

### Example 3: Event Queue System (`queue_fifo.rs`)

Demonstrates the event-driven queue with FIFO processing:

```bash
cargo run --example queue_fifo
```

**What it does:**
- Creates 5 users with events enabled
- Starts a worker to process events
- Shows FIFO ordering (oldest events first)
- Demonstrates partition-based parallelism
- Graceful shutdown

**Expected output:**
```
🚀 Testing FIFO Queue with Checkpoints

📝 Creating 5 users to generate events...
✓ Created: User 1 (user1@example.com)
✓ Created: User 2 (user2@example.com)
...

📊 Setting up queue worker...
⚙️  Worker started! Processing events in FIFO order...

  [1] Processing event: INSERT - table:user:xxx (sequence: 00001...)
  [2] Processing event: INSERT - table:user:xxx (sequence: 00001...)
  ...

✅ Complete! Processed 5 events in FIFO order
   (Oldest events processed first!)
```

**Key concepts demonstrated:**
- Event generation with `TitoOptions::with_events()`
- Event worker setup with `run_worker()`
- Event type isolation ("user" events)
- FIFO ordering within partitions
- Checkpoint-based progress tracking
- Graceful shutdown handling

### How the Queue System Works

The event queue is perfect for:
- **Async processing**: Offload heavy work from request handlers
- **Event sourcing**: Track all changes to your data
- **Multi-tenant processing**: Partition by tenant ID for isolation
- **Saga patterns**: Coordinate distributed transactions

**Architecture:**

```
Model Change (INSERT/UPDATE/DELETE)
Event Generated (if events() enabled)
Stored in TiKV: event:{event_type}:{partition}:{sequence}
Worker polls: queue:{event_type}:PENDING:{partition}:{sequence}
Process Event (your handler logic)
Mark Complete: queue:{event_type}:COMPLETED:{partition}:{sequence}
Update Checkpoint: queue_checkpoint:{event_type}:{partition}
```

**Practical Example - Email Notifications:**

```rust
// 1. Define event in your User model
impl TitoModelTrait for User {
    fn events(&self) -> Vec<TitoEventConfig> {
        vec![TitoEventConfig {
            name: "user".to_string(),
        }]
    }

    fn partition_key(&self) -> String {
        // Partition by user_id for ordered processing per user
        self.id.clone()
    }
}

// 2. Create user with event
tito_db.transaction(|tx| {
    let user_model = user_model.clone();
    async move {
        user_model.build_with_options(
            new_user,
            TitoOptions::with_events(TitoOperation::Insert),
            &tx,
        ).await?;
        Ok::<_, TitoError>(())
    }
}).await?;

// 3. Worker processes event asynchronously
let handler = move |event: TitoEvent| {
    Box::pin(async move {
        if event.action == "INSERT" {
            // Send welcome email
            send_welcome_email(&event.entity_id()).await?;
        }
        Ok::<_, TitoError>(())
    }) as BoxFuture<'static, Result<(), TitoError>>
};

let worker = run_worker(
    queue.clone(),
    String::from("user"), // Only process "user" events
    handler,
    partition_config,
    is_leader,
    5, // Process 5 events concurrently
    shutdown_rx,
).await;
```

**Scaling Pattern:**

```rust
// Server 1: Partitions 0-511
let partition_config = PartitionConfig { start: 0, end: 512 };

// Server 2: Partitions 512-1023
let partition_config = PartitionConfig { start: 512, end: 1024 };

// Both process same event type, different partitions
// = Horizontal scalability + load balancing
```

## Requirements

- Rust 2021 edition or later
- TiKV server running (local or remote)

## License

This project is licensed under the Apache License, Version 2.0.