v-storage 0.2.0-heed

Storage layer for the veda platform
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
# v-storage

[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Rust](https://img.shields.io/badge/rust-1.70+-orange.svg)](https://www.rust-lang.org/)
[![Build Status](https://img.shields.io/badge/build-passing-green.svg)](https://github.com/your-org/v-storage)

A flexible storage abstraction library for the Veda platform. Provides unified interface for different storage backends including memory, LMDB, Tarantool, and remote storage.

## ๐Ÿš€ Features

- **Multiple Storage Backends**: Memory, LMDB, Tarantool (TTStorage), Remote storage
- **Unified API**: Common `Storage` trait for all backends
- **Three Architecture Patterns**: Dynamic dispatch, generic containers, enum dispatch
- **Individual Support**: Native support for Veda platform Individual objects
- **Zero-cost Abstractions**: Compile-time optimization options with minimal overhead
- **Factory Patterns**: Builder, Provider, and Config patterns for easy construction
- **Error Handling**: Comprehensive error types and result handling
- **Backward Compatibility**: Support for legacy API methods

## ๐Ÿ“ฆ Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
v-storage = "0.1.0"

# Optional features
v-storage = { version = "0.1.0", features = ["tt_2", "tokio_0_2"] }
```

### Available Features

- `tt_2` - Tarantool 2.x support
- `tt_3` - Tarantool 3.x support  
- `tokio_0_2` - Tokio 0.2 runtime support
- `tokio_1` - Tokio 1.x runtime support

## ๐Ÿƒ Quick Start

```rust
use v_storage::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create memory storage
    let storage = VStorage::builder()
        .memory()
        .build()?;
    let mut storage = VStorage::new(storage);

    // Store Individual with semantic predicates
    storage.put_value(StorageId::Individuals, "user:1", r#"{
        "@": "user:1",
        "rdf:type": [{"type": "Uri", "data": "foaf:Person"}],
        "rdfs:label": [{"type": "String", "data": "John Smith - Software Engineer"}],
        "foaf:name": [{"type": "String", "data": "John"}],
        "foaf:familyName": [{"type": "String", "data": "Smith"}],
        "foaf:age": [{"type": "Integer", "data": 30}],
        "foaf:mbox": [{"type": "String", "data": "john.smith@example.com"}],
        "veda:hasPosition": [{"type": "Uri", "data": "position:software_engineer"}],
        "org:memberOf": [{"type": "Uri", "data": "org:development_team"}]
    }"#)?;
    
    // Retrieve Individual
    if let StorageResult::Ok(data) = storage.get_value(StorageId::Individuals, "user:1") {
        println!("User Individual: {} bytes", data.len());
    }

    Ok(())
}
```

## ๐Ÿ—๏ธ Architecture

### Storage Types

| Type | Description | Dispatch | Use Case |
|------|-------------|----------|----------|
| **VStorage** | Dynamic dispatch with trait objects | Runtime | Maximum flexibility, runtime type selection |
| **VStorageGeneric** | Compile-time typed containers | Static | Type safety, known storage type |
| **VStorageEnum** | Static dispatch through enum | Static | Applications preferring enum dispatch |

### Storage Backends

- **Memory Storage** - In-memory HashMap-based storage
- **LMDB Storage** - Lightning Memory-Mapped Database  
- **Tarantool Storage** - In-memory NoSQL database
- **Remote Storage** - Network-based storage client

### StorageId Types

- **Individuals** - Main Individual objects for Veda platform
- **Tickets** - System tickets and tasks
- **Az** - Authorization and permission data

## ๐Ÿ“– Usage Examples

### Basic Operations

```rust
use v_storage::*;

// Create storage using Builder pattern
let storage = VStorage::builder()
    .memory()
    .build()?;
let mut storage = VStorage::new(storage);

// Basic operations with semantic Individual data
storage.put_value(StorageId::Individuals, "person:example", r#"{
    "@": "person:example",
    "rdf:type": [{"type": "Uri", "data": "foaf:Person"}],
    "rdfs:label": [{"type": "String", "data": "Example Person"}],
    "foaf:name": [{"type": "String", "data": "Example"}]
}"#)?;
storage.get_value(StorageId::Individuals, "person:example")?;
storage.remove_value(StorageId::Individuals, "person:example")?;
storage.count(StorageId::Individuals)?;
```

### Factory Patterns

```rust
// Builder Pattern
let storage = VStorage::builder()
    .lmdb("/path/to/db", StorageMode::ReadWrite, None)
    .build()?;

// Provider Pattern  
let storage = StorageProvider::memory();

// Config Pattern
let config = StorageConfig::Memory;
let storage = VStorage::from_config(config)?;
```

### Working with Individual Objects

```rust
use v_individual_model::onto::individual::Individual;

let mut individual = Individual::default();
let result = storage.get_individual(
    StorageId::Individuals, 
    "person:john", 
    &mut individual
);

match result {
    v_result_code::ResultCode::Ok => {
        println!("Individual loaded: {}", individual.get_id());
    },
    v_result_code::ResultCode::NotFound => {
        println!("Individual not found");
    },
    _ => println!("Error loading individual"),
}
```

### Static Dispatch Usage

```rust
// Use VStorageEnum for static dispatch
let mut storage = VStorageEnum::memory();

// Batch operations with semantic Individual data
for i in 0..1000 {
    let individual_data = format!(r#"{{
        "@": "person:{}",
        "rdf:type": [{{"type": "Uri", "data": "foaf:Person"}}],
        "rdfs:label": [{{"type": "String", "data": "Person {}"}}],
        "foaf:name": [{{"type": "String", "data": "Person{}"}}],
        "veda:index": [{{"type": "Integer", "data": {}}}]
    }}"#, i, i, i, i);
    
    storage.put_value(
        StorageId::Individuals, 
        &format!("person:{}", i), 
        &individual_data
    )?;
}
```

## ๐ŸŽฏ Design Patterns

### Strategy Pattern
Unified `Storage` interface allows switching between different storage implementations:

```rust
fn process_data(storage: &mut dyn Storage) {
    // Works with any storage implementation
    storage.put_value(StorageId::Individuals, "person:demo", r#"{
        "@": "person:demo",
        "rdf:type": [{"type": "Uri", "data": "foaf:Person"}],
        "rdfs:label": [{"type": "String", "data": "Demo Person"}]
    }"#);
}
```

### Builder Pattern
Fluent API for configuring storage:

```rust
let storage = VStorage::builder()
    .memory()
    .build()?;
```

### Abstract Factory
Multiple ways to create storage instances:

```rust
// Through provider
let storage1 = StorageProvider::memory();

// Through config
let storage2 = VStorage::from_config(StorageConfig::Memory)?;

// Through builder
let storage3 = VStorage::builder().memory().build()?;
```

## ๐Ÿ“Š Architecture Comparison

Theoretical dispatch overhead (in practice, database I/O dominates):

| Storage Type | Dispatch | Memory | Flexibility |
|--------------|----------|--------|-------------|
| VStorageEnum | Static (enum match) | Stack | Fixed set of types |
| VStorageGeneric | Static (monomorphization) | Direct | Compile-time known |
| VStorage | Dynamic (vtable) | Heap | Runtime selection |

*Note: In real applications, storage backend (LMDB, Tarantool, network) dominates timing.*

## ๐Ÿ”ง Configuration

### Memory Storage
```rust
let storage = VStorage::builder()
    .memory()
    .build()?;
```

### LMDB Storage
```rust
let storage = VStorage::builder()
    .lmdb("/path/to/database", StorageMode::ReadWrite, Some(1000))
    .build()?;
```

### Tarantool Storage
```rust
// Requires tt_2 or tt_3 feature
let storage = VStorage::builder()
    .tarantool("127.0.0.1:3301", "username", "password")
    .build()?;
```

### Remote Storage
```rust
let storage = VStorage::builder()
    .remote("127.0.0.1:8080")
    .build()?;
```

## ๐Ÿ“š Examples

The [`examples/`](examples/) directory contains comprehensive examples:

- **[basic_usage.rs]examples/basic_usage.rs** - Basic operations and API usage
- **[storage_types.rs]examples/storage_types.rs** - Comparison of different storage types
- **[factory_patterns.rs]examples/factory_patterns.rs** - Various construction patterns
- **[individual_operations.rs]examples/individual_operations.rs** - Working with Individual objects

Run examples:
```bash
cargo run --example basic_usage
cargo run --example storage_types
cargo run --example factory_patterns
cargo run --example individual_operations
```

## ๐Ÿ› ๏ธ Development

### Building

```bash
# Basic build
cargo build

# With all features
cargo build --features "tt_2 tokio_0_2"

# Release build
cargo build --release
```

### Testing

```bash
# Run tests
cargo test

# Run tests with features
cargo test --features "tt_2 tokio_0_2"

# Run integration tests
cargo test --test integration_tests
```

### Documentation

```bash
# Generate documentation
cargo doc --open

# Generate with examples
cargo doc --examples --open
```

## ๐Ÿ” Error Handling

The library uses comprehensive error types:

```rust
match storage.get_value(StorageId::Individuals, "key") {
    StorageResult::Ok(value) => println!("Value: {}", value),
    StorageResult::NotFound => println!("Key not found"),
    StorageResult::Error(e) => println!("Error: {}", e),
}
```

## ๐Ÿงช Testing

```bash
# Unit tests
cargo test

# Integration tests  
cargo test --test integration_tests

# Example tests
cargo test --examples

# All tests with release optimization
cargo test --release
```

## ๐Ÿ“‹ Requirements

- **Rust**: 1.70 or higher
- **Operating System**: Linux, macOS, Windows
- **Dependencies**: See [Cargo.toml]Cargo.toml

### Optional Requirements

- **LMDB**: System LMDB libraries for LMDBStorage
- **Tarantool**: Running Tarantool instance for TTStorage
- **Network**: Connectivity for RemoteStorage

## ๐Ÿค Contributing

We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.

### Development Setup

1. Clone the repository
2. Install Rust 1.70+
3. Run `cargo build`
4. Run `cargo test`
5. Check examples with `cargo run --example basic_usage`

### Code Style

- Use `cargo fmt` for formatting
- Use `cargo clippy` for linting
- Add tests for new features
- Update documentation

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ”— Related Projects

- **[Veda Platform]https://github.com/semantic-machines/veda** - Semantic data management platform
- **[v-individual-model]https://crates.io/crates/v-individual-model** - Individual object model
- **[LMDB]https://symas.com/lmdb/** - Lightning Memory-Mapped Database
- **[Tarantool]https://www.tarantool.io/** - In-memory database and application server

## ๐Ÿ’ก FAQ

**Q: Which storage type should I choose?**
A: Use `VStorageEnum` for applications preferring static dispatch, `VStorageGeneric` for type safety with known storage types, and `VStorage` for maximum flexibility.

**Q: Can I switch storage backends at runtime?**
A: Yes, with `VStorage` dynamic dispatch. Generic types require compile-time selection.

**Q: Is the library thread-safe?**
A: Storage instances are not thread-safe by default. Use appropriate synchronization for concurrent access.

**Q: How do I handle network failures with RemoteStorage?**
A: The library returns `StorageResult::Error` for network issues. Implement retry logic in your application.

---

**Built with โค๏ธ for the Veda platform**