aviladb 0.1.0

AvilaDB - Globally distributed NoSQL database optimized for Brazil and LATAM
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
# AvilaDB - Copilot Instructions

## Project Identity

**AvilaDB** is the **distributed NoSQL database** for the AVL Cloud Platform. It embodies the Arxis philosophy:
- **ARX (Fortress)**: Solid, reliable, ACID-compliant data storage
- **AXIS (Engine)**: High-performance query engine with vector search

**Key Differentiators:**
- πŸ‡§πŸ‡· Optimized for Brazil (5-10ms latency in SΓ£o Paulo/Rio)
- πŸ“¦ 4 MB documents (2x larger than competitors)
- πŸ” Native vector search (HNSW, no external services)
- 🌍 Multi-region writes FREE
- πŸ’° 40-60% cheaper than AWS/Azure for Brazilian workloads

---

## Architecture Principles

### 1. Fortress Philosophy (ARX)
```rust
// βœ… ALWAYS: Guarantee data durability
// βœ… ALWAYS: Use ACID transactions
// βœ… ALWAYS: Encrypt at rest and in transit
// βœ… ALWAYS: Multi-region replication
```

### 2. Engine Philosophy (AXIS)
```rust
// βœ… ALWAYS: Optimize for low latency (target 5-10ms)
// βœ… ALWAYS: Use avila-compress for storage efficiency
// βœ… ALWAYS: Partition-aware query routing
// βœ… ALWAYS: Connection pooling and reuse
```

### 3. Brazil-First Design
```rust
// βœ… ALWAYS: Prioritize Brazilian data centers
// βœ… ALWAYS: Measure latency from SΓ£o Paulo perspective
// βœ… ALWAYS: Price in R$ (Reais), not USD
// βœ… ALWAYS: Portuguese documentation alongside English
```

---

## Code Style & Standards

### Naming Conventions
```rust
// Types: PascalCase
pub struct AvilaClient { }
pub struct Document { }
pub struct VectorIndex { }

// Functions: snake_case
pub async fn insert_document() { }
pub async fn vector_search() { }

// Constants: SCREAMING_SNAKE_CASE
const MAX_DOCUMENT_SIZE_MB: usize = 4;
const MAX_PARTITION_SIZE_GB: usize = 50;
```

### Error Handling
```rust
use thiserror::Error;

#[derive(Error, Debug)]
pub enum AvilaDBError {
    #[error("Document too large: {size} MB (max: 4 MB)")]
    DocumentTooLarge { size: usize },

    #[error("Partition full: {size} GB (max: 50 GB)")]
    PartitionFull { size: usize },

    #[error("Network error: {0}")]
    Network(#[from] std::io::Error),

    #[error("Serialization error: {0}")]
    Serialization(#[from] serde_json::Error),
}

pub type Result<T> = std::result::Result<T, AvilaDBError>;
```

### Async/Await
```rust
// βœ… ALWAYS: Use async for I/O operations
pub async fn insert(&self, doc: Document) -> Result<InsertResult> {
    // Compress before storage
    let compressed = self.compress_document(&doc)?;

    // Write to storage
    self.storage.put(&compressed).await?;

    // Update indexes
    self.update_indexes(&doc).await?;

    Ok(InsertResult { id: doc.id })
}

// βœ… ALWAYS: Provide sync alternatives when possible
pub fn insert_blocking(&self, doc: Document) -> Result<InsertResult> {
    tokio::runtime::Runtime::new()?.block_on(self.insert(doc))
}
```

---

## Performance Guidelines

### 1. Compression (avila-compress)
```rust
use avila_compress::lz4::Lz4Compressor;

// βœ… ALWAYS: Compress documents before storage
pub fn compress_document(&self, doc: &Document) -> Result<Vec<u8>> {
    let json = serde_json::to_vec(doc)?;
    let compressor = Lz4Compressor::new();
    Ok(compressor.compress(&json)?)
}

// βœ… ALWAYS: Decompress on read
pub fn decompress_document(&self, data: &[u8]) -> Result<Document> {
    let compressor = Lz4Compressor::new();
    let json = compressor.decompress(data)?;
    Ok(serde_json::from_slice(&json)?)
}
```

### 2. Batch Operations
```rust
// βœ… PREFER: Batch writes over individual writes
pub async fn insert_batch(&self, docs: Vec<Document>) -> Result<BatchResult> {
    // Compress all documents
    let compressed: Vec<_> = docs.iter()
        .map(|doc| self.compress_document(doc))
        .collect::<Result<_>>()?;

    // Single write transaction
    self.storage.batch_put(compressed).await?;

    Ok(BatchResult { count: docs.len() })
}

// ❌ AVOID: Loop with individual writes
// for doc in docs {
//     self.insert(doc).await?; // Slow!
// }
```

### 3. Query Optimization
```rust
// βœ… ALWAYS: Use partition key in queries
let results = collection
    .query("SELECT * FROM users WHERE userId = @id") // Partition key!
    .param("id", "user123")
    .execute()
    .await?;

// ❌ AVOID: Cross-partition queries without filters
// SELECT * FROM users WHERE email = @email  // Scans all partitions!
```

---

## Storage Layer

### RocksDB Integration
```rust
use rocksdb::{DB, Options, WriteBatch};

pub struct StorageEngine {
    db: DB,
    compression: Lz4Compressor,
}

impl StorageEngine {
    pub fn new(path: &str) -> Result<Self> {
        let mut opts = Options::default();
        opts.create_if_missing(true);
        opts.set_compression_type(rocksdb::DBCompressionType::None); // We use avila-compress!

        let db = DB::open(&opts, path)?;

        Ok(Self {
            db,
            compression: Lz4Compressor::new(),
        })
    }

    pub async fn put(&self, key: &str, value: &[u8]) -> Result<()> {
        // Compress with avila-compress
        let compressed = self.compression.compress(value)?;

        // Store in RocksDB
        self.db.put(key.as_bytes(), &compressed)?;

        Ok(())
    }

    pub async fn get(&self, key: &str) -> Result<Option<Vec<u8>>> {
        match self.db.get(key.as_bytes())? {
            Some(compressed) => {
                // Decompress
                let data = self.compression.decompress(&compressed)?;
                Ok(Some(data))
            }
            None => Ok(None),
        }
    }
}
```

---

## Vector Search

### HNSW Index
```rust
use hnsw::{Hnsw, DistanceMetric};

pub struct VectorIndex {
    index: Hnsw<f32>,
    dimension: usize,
    metric: DistanceMetric,
}

impl VectorIndex {
    pub fn new(dimension: usize, metric: &str) -> Result<Self> {
        let metric = match metric {
            "cosine" => DistanceMetric::Cosine,
            "euclidean" => DistanceMetric::Euclidean,
            "dot" => DistanceMetric::DotProduct,
            _ => return Err(AvilaDBError::InvalidMetric(metric.to_string())),
        };

        let index = Hnsw::new(dimension, 16, 200, metric);

        Ok(Self { index, dimension, metric })
    }

    pub fn add(&mut self, id: usize, vector: &[f32]) -> Result<()> {
        if vector.len() != self.dimension {
            return Err(AvilaDBError::DimensionMismatch {
                expected: self.dimension,
                got: vector.len(),
            });
        }

        self.index.add(id, vector)?;
        Ok(())
    }

    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<(usize, f32)>> {
        let results = self.index.search(query, k)?;
        Ok(results)
    }
}
```

---

## Observability

### Telemetry Integration
```rust
use avx_telemetry::{Telemetry, Span};
use tracing::{info, warn, error, instrument};

#[instrument(skip(self))]
pub async fn insert(&self, doc: Document) -> Result<InsertResult> {
    let _span = Span::current();

    // Track operation
    let start = std::time::Instant::now();

    // Perform insert
    let result = self.insert_internal(doc).await?;

    // Log metrics
    let latency_ms = start.elapsed().as_millis();
    info!(
        latency_ms = latency_ms,
        document_size = result.size_bytes,
        "Document inserted"
    );

    // Warn if high latency
    if latency_ms > 100 {
        warn!(
            latency_ms = latency_ms,
            "High latency detected for insert operation"
        );
    }

    Ok(result)
}
```

### Diagnostics
```rust
pub struct DiagnosticInfo {
    pub latency_ms: u128,
    pub storage_latency_ms: u128,
    pub index_latency_ms: u128,
    pub compression_ratio: f64,
    pub partition_key: String,
}

impl Document {
    pub fn diagnostics(&self) -> DiagnosticInfo {
        DiagnosticInfo {
            latency_ms: self.latency_ms,
            storage_latency_ms: self.storage_latency_ms,
            index_latency_ms: self.index_latency_ms,
            compression_ratio: self.compression_ratio,
            partition_key: self.partition_key.clone(),
        }
    }
}
```

---

## Testing

### Unit Tests
```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_insert_and_query() {
        let client = AvilaClient::connect("http://localhost:8000").await.unwrap();
        let db = client.database("testdb").await.unwrap();
        let collection = db.collection("users").await.unwrap();

        // Insert
        let doc = Document::new()
            .set("userId", "test123")
            .set("name", "Test User");

        let result = collection.insert(doc).await.unwrap();
        assert!(!result.id.is_empty());

        // Query
        let docs = collection
            .query("SELECT * FROM users WHERE userId = @id")
            .param("id", "test123")
            .execute()
            .await
            .unwrap();

        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].get::<String>("name").unwrap(), "Test User");
    }

    #[test]
    fn test_document_size_limit() {
        let mut doc = Document::new();

        // 4 MB limit
        let large_data = vec![0u8; 5 * 1024 * 1024]; // 5 MB
        doc.set("data", large_data);

        let result = doc.validate();
        assert!(matches!(result, Err(AvilaDBError::DocumentTooLarge { .. })));
    }
}
```

### Integration Tests
```rust
#[tokio::test]
async fn test_vector_search_e2e() {
    let client = AvilaClient::connect("http://localhost:8000").await.unwrap();
    let db = client.database("vectordb").await.unwrap();
    let collection = db.collection("embeddings").await.unwrap();

    // Create index
    collection.create_vector_index("embedding", 3, "cosine").await.unwrap();

    // Insert vectors
    let docs = vec![
        Document::new()
            .set("text", "hello world")
            .set("embedding", vec![1.0, 0.0, 0.0]),
        Document::new()
            .set("text", "hello rust")
            .set("embedding", vec![0.9, 0.1, 0.0]),
    ];

    collection.insert_batch(docs).await.unwrap();

    // Search
    let query = vec![1.0, 0.0, 0.0];
    let results = collection
        .vector_search("embedding", query)
        .top_k(2)
        .execute()
        .await
        .unwrap();

    assert_eq!(results.len(), 2);
    assert_eq!(results[0].get::<String>("text").unwrap(), "hello world");
}
```

---

## Documentation

### Public API Documentation
```rust
/// AvilaDB client for connecting to the database.
///
/// # Examples
///
/// ```rust
/// use aviladb::AvilaClient;
///
/// #[tokio::main]
/// async fn main() {
///     let client = AvilaClient::connect("http://localhost:8000").await.unwrap();
///     let db = client.database("mydb").await.unwrap();
/// }
/// ```
pub struct AvilaClient { }

/// Inserts a document into the collection.
///
/// Documents are limited to 4 MB. They are automatically compressed
/// using `avila-compress` before storage.
///
/// # Errors
///
/// Returns `AvilaDBError::DocumentTooLarge` if the document exceeds 4 MB.
///
/// # Examples
///
/// ```rust
/// let doc = Document::new()
///     .set("userId", "user123")
///     .set("name", "JoΓ£o Silva");
///
/// let result = collection.insert(doc).await?;
/// println!("Inserted: {}", result.id);
/// ```
pub async fn insert(&self, doc: Document) -> Result<InsertResult> { }
```

---

## Best Practices Summary

### βœ… ALWAYS
- Use `avila-compress` for document compression
- Measure latency and log diagnostics when > 100ms
- Use partition keys in queries to avoid cross-partition scans
- Batch operations instead of loops with individual operations
- Encrypt data at rest and in transit
- Use `avx-telemetry` for observability

### ❌ NEVER
- Store uncompressed documents > 1 MB
- Make cross-partition queries without filters
- Store sensitive data without encryption
- Skip error handling in async code
- Hardcode credentials or endpoints

### πŸ‡§πŸ‡· Brazil-Specific
- Always test latency from SΓ£o Paulo (primary region)
- Provide Portuguese documentation
- Price in R$ (Reais)
- Consider local holidays for maintenance windows

---

## Related Crates

- **avila-compress**: Native LZ4 compression (used for storage)
- **avx-telemetry**: Observability and tracing
- **avx-gateway**: API gateway (routes requests to AvilaDB)
- **avx-api-core**: Shared types and utilities

---

## Contact & Support

**Project Lead**: Nicolas Ávila
**Email**: nicolas@avila.inc
**WhatsApp**: +55 17 99781-1471
**GitHub**: https://github.com/avilaops/arxis

---

**AvilaDB** - *The Distributed Fortress*
πŸ›οΈ Solid as a fortress | βš™οΈ Fast as an engine | πŸ‡§πŸ‡· Built for Brazil