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
# ๐Ÿ—„๏ธ AvilaDB

### *The Distributed Fortress. The Data Engine.*

**AVILA** (fortress) + **DB** (database) = **AvilaDB**

> *Where data finds solid ground and engines drive queries*

[![Crates.io](https://img.shields.io/crates/v/aviladb.svg)](https://crates.io/crates/aviladb)
[![Documentation](https://docs.rs/aviladb/badge.svg)](https://docs.rs/aviladb)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Rust](https://img.shields.io/badge/rust-1.70%2B-CE422B.svg)](https://www.rust-lang.org/)
[![AVL Cloud](https://img.shields.io/badge/AVL-Cloud%20Platform-00d4ff)](https://avila.cloud)

**๐Ÿ‡ง๐Ÿ‡ท Latency 5-10ms in Brazil** | **๐ŸŒ Multi-region writes FREE** | **๐Ÿ“ฆ 4 MB documents (2x competitors)**

---

## ๐ŸŽฏ What is AvilaDB?

**AvilaDB** is the distributed NoSQL database for the **AVL Cloud Platform** - built as a **fortress for your data** and an **engine for your queries**.

Like Arxis provides the mathematical citadel, AvilaDB provides the **data citadel**:
- ๐Ÿ›๏ธ **Solid Foundation**: Consistent, durable, ACID-compliant storage
- โš™๏ธ **Query Engine**: High-performance queries with vector search
- ๐Ÿ›ก๏ธ **Protection**: Encryption, backups, multi-region replication
- ๐Ÿš€ **Performance**: 5-10ms latency in Brazil, sub-50ms in LATAM

---

## ๐ŸŒŸ Key Features

### ๐Ÿ‡ง๐Ÿ‡ท **Optimized for Brazil & LATAM**
- **5-10ms latency** in Sรฃo Paulo, Rio, Brasรญlia
- **Sub-50ms** throughout Latin America
- **40-60% cheaper** than AWS DynamoDB or Azure Cosmos DB
- **Native Portuguese** documentation and support

### ๐Ÿ“ฆ **Large Documents**
- **4 MB per document** (vs 400 KB DynamoDB, 2 MB Cosmos DB)
- **50 GB per partition** (vs 10 GB DynamoDB, 20 GB Cosmos DB)
- Hierarchical Partition Keys (HPK) overcome limits

### ๐Ÿ” **Native Vector Search**
- Built-in **HNSW index** for embeddings
- **Semantic search** without external services
- Perfect for AI/Chat/RAG applications
- **Low-cost** compared to Pinecone/Weaviate

### ๐ŸŒ **Multi-region Writes (FREE)**
- Global distribution included
- No extra cost (unlike AWS/Azure)
- Automatic conflict resolution
- Active-active replication

### โšก **High Performance**
- **Compression**: LZ4 via `avila-compress` (>500 MB/s)
- **Throughput Units (TUs)**: Elastic scaling
- **Connection pooling**: Reuse clients
- **Batch operations**: Bulk writes/reads

### ๐Ÿ’ฐ **Pricing (Brazil)**
- **R$ 0,50** per 1M operations
- **R$ 0,20** per GB/month storage
- **FREE** multi-region writes
- **FREE** vector search (built-in)

---

## ๐Ÿ—๏ธ Architecture

AvilaDB follows the **Arxis philosophy** - solid as a fortress, powerful as an engine.

Built on top of world-class libraries from the **[Arxis ecosystem](https://github.com/avilaops/arxis)**:

### Core Dependencies

- ๐Ÿ—œ๏ธ **[avila-compress]https://github.com/avilaops/arxis/tree/main/avila-compress**
  - Native LZ4/Zstd compression (100% Rust, zero external deps)
  - 3x faster than standard implementations (>500 MB/s)
  - Optimized for columnar data and time-series
  - Production-ready โœ…

- ๐Ÿ“Š **[avila-telemetry]https://github.com/avilaops/arxis/tree/main/avila-telemetry**
  - Time series analysis, anomaly detection, forecasting
  - NASA-grade data quality metrics (โ‰ฅ0.95 threshold)
  - Observability and performance monitoring
  - 22 tests passing โœ…

- ๐Ÿงฎ **[avila-math]https://github.com/avilaops/arxis/tree/main/avila-math**
  - Mathematical kernel for vectors and tensors
  - Shared across entire AVL ecosystem
  - High-performance linear algebra
  - 26 tests passing โœ…

- ๐Ÿ“ก **[avx-http]https://github.com/avilaops/arxis/tree/main/avx-http**
  - Native HTTP client/server for AVL Platform
  - Optimized for Brazil and LATAM latency
  - Built on Tokio + Axum

### Architecture Layers

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚        AvilaDB - Data Citadel           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐Ÿ›๏ธ Storage Layer (RocksDB)             โ”‚
โ”‚     - ACID transactions                  โ”‚
โ”‚     - LSM-tree storage                   โ”‚
โ”‚     - avila-compress (LZ4/Zstd)         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  โš™๏ธ Query Engine                        โ”‚
โ”‚     - SQL-like queries                   โ”‚
โ”‚     - Vector search (HNSW + avila-math) โ”‚
โ”‚     - Partition-aware routing           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐Ÿ›ก๏ธ Distribution Layer                  โ”‚
โ”‚     - Raft consensus                     โ”‚
โ”‚     - Multi-region replication          โ”‚
โ”‚     - Automatic failover                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  ๐Ÿš€ API Layer (avx-http + Axum)         โ”‚
โ”‚     - REST + gRPC                        โ”‚
โ”‚     - WebSocket subscriptions           โ”‚
โ”‚     - avila-telemetry observability     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

---

## ๐Ÿ“ฆ Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
aviladb = "0.1"
tokio = { version = "1", features = ["full"] }
```

### Using the CLI

```bash
# Install via curl
curl -sSL https://avila.cloud/install.sh | sh

# Connect to AvilaDB
avila db connect --account my-account

# Query data
avila db query "SELECT * FROM users WHERE active = true"
```

### Local Emulator (Docker)

```bash
docker run -p 8000:8000 avilacloud/aviladb-emulator:latest
```

---

## ๐Ÿš€ Quick Start

### Basic Operations

```rust
use aviladb::{AvilaClient, Document};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to AvilaDB
    let client = AvilaClient::connect("http://localhost:8000").await?;
    let db = client.database("gamedb").await?;
    let players = db.collection("players").await?;

    // Insert document (4 MB limit)
    let player = Document::new()
        .set("userId", "player123")
        .set("username", "CoolGamer")
        .set("level", 42)
        .set("inventory", vec!["sword", "shield"])
        .set("stats", json!({
            "hp": 100,
            "mp": 50,
            "attack": 25
        }));

    let result = players.insert(player).await?;
    println!("Inserted: {}", result.id);

    // Query with filters
    let high_level = players
        .query("SELECT * FROM players WHERE level > @min_level")
        .param("min_level", 40)
        .execute()
        .await?;

    for doc in high_level {
        println!("Player: {}", doc.get("username")?);
    }

    // Update
    players.update()
        .set("level", 43)
        .where_eq("userId", "player123")
        .execute()
        .await?;

    // Delete
    players.delete()
        .where_eq("userId", "player123")
        .execute()
        .await?;

    Ok(())
}
```

### Vector Search (AI/RAG)

```rust
use aviladb::{AvilaClient, VectorIndex};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = AvilaClient::connect("http://localhost:8000").await?;
    let db = client.database("aidb").await?;
    let memories = db.collection("chat_memory").await?;

    // Create vector index (HNSW)
    memories.create_vector_index("embedding", 1536, "cosine").await?;

    // Store with embedding
    let memory = Document::new()
        .set("userId", "user123")
        .set("message", "Tell me about quantum physics")
        .set("embedding", vec![0.1, 0.2, 0.3, /* ... 1536 dims */])
        .set("timestamp", chrono::Utc::now());

    memories.insert(memory).await?;

    // Semantic search
    let query_embedding = vec![0.15, 0.18, 0.29, /* ... */];
    let similar = memories
        .vector_search("embedding", query_embedding)
        .top_k(5)
        .execute()
        .await?;

    for doc in similar {
        println!("Similar: {} (score: {})",
            doc.get("message")?,
            doc.similarity_score()
        );
    }

    Ok(())
}
```

### Hierarchical Partition Keys (HPK)

```rust
use aviladb::{AvilaClient, HierarchicalKey};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = AvilaClient::connect("http://localhost:8000").await?;
    let db = client.database("multitenantdb").await?;

    // Create collection with HPK
    let orders = db.create_collection("orders")
        .partition_key(HierarchicalKey::new()
            .add_level("tenantId")
            .add_level("userId")
            .add_level("orderId")
        )
        .build()
        .await?;

    // Query within tenant
    let tenant_orders = orders
        .query("SELECT * FROM orders WHERE tenantId = @tenant")
        .param("tenant", "acme-corp")
        .execute()
        .await?;

    // Query specific user
    let user_orders = orders
        .query("SELECT * FROM orders WHERE tenantId = @tenant AND userId = @user")
        .param("tenant", "acme-corp")
        .param("user", "user456")
        .execute()
        .await?;

    Ok(())
}
```

---

## ๐Ÿ“Š Use Cases

### ๐ŸŽฎ **Game Development**
- Player profiles and session data
- Leaderboards and rankings
- In-game chat and social features
- Real-time matchmaking queues
- **5-10ms latency** for Brazilian players

### ๐Ÿค– **AI/Chat/RAG Applications**
- Chat history with vector embeddings
- User context and memory
- Semantic search for retrieval
- Multi-user isolation
- **Native vector search** (no extra services)

### ๐Ÿ›’ **E-commerce**
- Product catalogs
- Shopping carts
- Order management
- User profiles and wishlists
- Real-time inventory

### ๐Ÿ“ก **IoT & Scientific**
- Sensor data ingestion
- Device twins and profiles
- Time-series storage
- Telemetry aggregation
- LIGO/LISA data storage

---

## ๐Ÿ†š Comparison with Competitors

| Feature                 | AvilaDB       | AWS DynamoDB        | Azure Cosmos DB |
| ----------------------- | ------------- | ------------------- | --------------- |
| **Max document size**   | **4 MB** โœ…    | 400 KB              | 2 MB            |
| **Partition size**      | **50 GB** โœ…   | 10 GB               | 20 GB           |
| **Multi-region writes** | **FREE** โœ…    | Extra cost          | Extra cost      |
| **Vector search**       | **Native** โœ…  | Requires OpenSearch | Limited         |
| **Brazil latency**      | **5-10ms** โœ…  | 80-120ms            | 40-60ms         |
| **Pricing (1M ops)**    | **R$ 0,50** โœ… | USD 1.25            | USD 0.85        |
| **Storage (GB/month)**  | **R$ 0,20** โœ… | USD 0.25            | USD 0.25        |

**AvilaDB is 40-60% cheaper** for Brazilian workloads! ๐Ÿ‡ง๐Ÿ‡ท

---

## ๐Ÿ› ๏ธ Best Practices

### Data Modeling
```rust
// โœ… GOOD: Embed related data
let user = Document::new()
    .set("userId", "user123")
    .set("profile", json!({
        "name": "Joรฃo Silva",
        "email": "joao@example.com"
    }))
    .set("preferences", json!({
        "theme": "dark",
        "language": "pt-BR"
    }));

// โŒ BAD: Too many separate documents for always-together data
// users -> profiles -> preferences (3 queries instead of 1)
```

### Partition Key Choice
```rust
// โœ… GOOD: High cardinality, even distribution
.partition_key("userId")  // userId, tenantId, deviceId, sessionId

// โŒ BAD: Low cardinality, hot partitions
.partition_key("status")  // "active", "inactive" (only 2 values!)
```

### Diagnostics
```rust
// Log diagnostic info when latency is high
let result = players.query("SELECT * FROM players")
    .execute()
    .await?;

if result.latency_ms() > 100 {
    tracing::warn!(
        "High latency: {} ms\nDiagnostics: {}",
        result.latency_ms(),
        result.diagnostics()
    );
}
```

---

## ๐Ÿงช Development Tools

### CLI
```bash
# Connect to account
avila db connect --account my-account

# List databases
avila db list

# Query interactively
avila db query "SELECT * FROM users LIMIT 10"

# Create collection
avila db create-collection players --partition-key userId

# Import data
avila db import players data.json

# Export data
avila db export players --output backup.json
```

### Emulator
```bash
# Run locally (no cloud costs!)
docker run -p 8000:8000 avilacloud/aviladb-emulator:latest

# Update connection string
export AVILADB_ENDPOINT=http://localhost:8000
```

---

## ๐Ÿ“š Documentation

- **[Official Docs]https://docs.avila.cloud/aviladb** - Complete guide
- **[API Reference]https://docs.rs/aviladb** - Rust API docs
- **[Best Practices]https://docs.avila.cloud/aviladb/best-practices** - Optimization tips
- **[Examples]./examples** - Code samples

---

## ๐Ÿ›๏ธ Philosophy - The Data Citadel

AvilaDB embodies the **Arxis philosophy**:

### ๐Ÿ›๏ธ **ARX - Fortress**
- **Solid storage**: ACID transactions, durability guarantees
- **Protection**: Encryption at rest and in transit
- **Reliability**: Multi-region replication, automatic backups

### โš™๏ธ **AXIS - Engine**
- **Query power**: Fast SQL-like queries, vector search
- **Scalability**: Elastic throughput units, auto-scaling
- **Performance**: Optimized for Brazil and LATAM

### ๐Ÿ‡ง๐Ÿ‡ท **Built for Brazil**
- **Local presence**: Data centers in Sรฃo Paulo, Rio
- **Low latency**: 5-10ms for Brazilian users
- **Fair pricing**: R$ instead of USD, 40-60% cheaper
- **Portuguese-first**: Docs, support, community

---

## ๐Ÿค Contributing

Contributions are welcome! Please:
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/awesome-feature`)
3. Commit your changes (`git commit -m 'Add awesome feature'`)
4. Push to the branch (`git push origin feature/awesome-feature`)
5. Open a Pull Request

---

## ๐Ÿ“ž Support

**Email**: nicolas@avila.inc
**WhatsApp**: +55 17 99781-1471
**GitHub**: https://github.com/avilaops/arxis
**Docs**: https://docs.avila.cloud/aviladb

---

## ๐Ÿ“œ License

Dual-licensed under MIT OR Apache-2.0 - See [LICENSE-MIT](../LICENSE-MIT) and [LICENSE-APACHE](../LICENSE-APACHE) for details.

---

## ๐Ÿ›๏ธ Built by Avila

**AvilaDB** - *The Distributed Fortress*
Part of the **AVL Cloud Platform**

๐Ÿ›๏ธ **Solid as a fortress**
โš™๏ธ **Fast as an engine**
๐Ÿ‡ง๐Ÿ‡ท **Built for Brazil**

Built with โค๏ธ in Rust for the Brazilian and LATAM tech community.