heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
//! Sync server implementation (cloud-side)

use super::{
    auth::{Authorizer, Claims, JwtManager},
    conflicts::{Conflict, ConflictManager, ConflictType},
    Acknowledgment, ConflictResolution, Operation, RowDelta, SyncError, SyncRequest, SyncResponse,
    VectorClock,
};
use chrono::Utc;
use parking_lot::RwLock;
use std::collections::HashMap;
use uuid::Uuid;

/// Versioned change entry for the server's change log
#[derive(Debug, Clone)]
struct VersionedChange {
    /// Server version when this change was recorded
    version: u64,
    /// The row delta
    delta: RowDelta,
}

/// Sync server for cloud instances
pub struct SyncServer {
    current_version: u64,
    vector_clock: VectorClock,
    conflict_manager: ConflictManager,
    jwt_manager: JwtManager,
    authorizer: Authorizer,
    /// In-memory change log: version → changes at that version
    change_log: RwLock<Vec<VersionedChange>>,
    /// Row version tracking: (table, row_id) → (version, vector_clock)
    row_versions: RwLock<HashMap<(String, Vec<u8>), (u64, VectorClock)>>,
}

impl SyncServer {
    /// Create a new sync server
    pub fn new() -> Self {
        Self {
            current_version: 0,
            vector_clock: VectorClock::new(),
            conflict_manager: ConflictManager::new(ConflictResolution::UseServer),
            jwt_manager: JwtManager::from_env_or_default(),
            authorizer: Authorizer::new(),
            change_log: RwLock::new(Vec::new()),
            row_versions: RwLock::new(HashMap::new()),
        }
    }

    /// Create a sync server with custom JWT secret
    pub fn with_jwt_secret(secret: &[u8]) -> Self {
        Self {
            current_version: 0,
            vector_clock: VectorClock::new(),
            conflict_manager: ConflictManager::new(ConflictResolution::UseServer),
            jwt_manager: JwtManager::new(secret),
            authorizer: Authorizer::new(),
            change_log: RwLock::new(Vec::new()),
            row_versions: RwLock::new(HashMap::new()),
        }
    }

    /// Create a sync server with custom JWT manager and authorizer
    pub fn with_auth(jwt_manager: JwtManager, authorizer: Authorizer) -> Self {
        Self {
            current_version: 0,
            vector_clock: VectorClock::new(),
            conflict_manager: ConflictManager::new(ConflictResolution::UseServer),
            jwt_manager,
            authorizer,
            change_log: RwLock::new(Vec::new()),
            row_versions: RwLock::new(HashMap::new()),
        }
    }

    /// Handle sync request from client (with JWT token)
    pub async fn handle_sync_request(
        &mut self,
        request: SyncRequest,
        jwt_token: &str,
    ) -> Result<SyncResponse, SyncError> {
        // 1. Validate client with JWT authentication
        let claims = self.authenticate(&jwt_token).await?;

        // 2. Verify client_id matches JWT claims
        if claims.client_id != request.client_id {
            tracing::warn!(
                "Client ID mismatch: JWT={}, Request={}",
                claims.client_id,
                request.client_id
            );
            return Err(SyncError::Authentication);
        }

        // 3. Get changes since last_sync_version
        let delta = self.get_changes_since(request.last_sync_version).await?;

        // 4. Detect conflicts
        let conflicts = self.detect_conflicts(&request.vector_clock, &delta).await?;

        Ok(SyncResponse {
            server_version: self.current_version,
            delta,
            conflicts,
            continuation_token: None,
            vector_clock: self.vector_clock.clone(),
        })
    }

    /// Handle client deltas (with JWT token)
    pub async fn handle_client_deltas(
        &mut self,
        client_id: Uuid,
        deltas: Vec<RowDelta>,
        jwt_token: &str,
    ) -> Result<Acknowledgment, SyncError> {
        // Validate client with JWT authentication
        let claims = self.authenticate(&jwt_token).await?;

        // Verify client_id matches JWT claims
        if claims.client_id != client_id {
            tracing::warn!(
                "Client ID mismatch in deltas: JWT={}, Request={}",
                claims.client_id,
                client_id
            );
            return Err(SyncError::Authentication);
        }
        // Apply deltas to server database
        let applied_count = deltas.len() as u32;

        // Increment version
        self.current_version += 1;

        Ok(Acknowledgment {
            new_version: self.current_version,
            applied_count,
            failed: vec![],
            vector_clock: self.vector_clock.clone(),
        })
    }

    /// Authenticate client using JWT token
    ///
    /// This method performs:
    /// 1. JWT token validation (signature, expiry, format)
    /// 2. Token expiration checking
    /// 3. Tenant-based authorization
    /// 4. Scope verification
    ///
    /// Returns validated claims on success
    async fn authenticate(&self, token: &str) -> Result<Claims, SyncError> {
        // 1. Parse and validate JWT signature and structure
        let claims = self
            .jwt_manager
            .validate_with_scope(token, "sync:read")
            .map_err(|e| {
                tracing::warn!("JWT validation failed: {:?}", e);
                SyncError::Authentication
            })?;

        // 2. Check expiration (redundant but explicit)
        if claims.is_expired() {
            tracing::warn!("Token expired for user: {}", claims.sub);
            return Err(SyncError::Authentication);
        }

        // 3. Check tenant authorization
        self.authorizer.validate_claims(&claims).map_err(|e| {
            tracing::warn!("Authorization failed for tenant: {}", claims.tenant_id);
            SyncError::Authentication
        })?;

        tracing::debug!(
            "Successfully authenticated client: {} (tenant: {}, user: {})",
            claims.client_id,
            claims.tenant_id,
            claims.sub
        );

        Ok(claims)
    }

    /// Generate JWT token for a client (used during client registration)
    pub fn generate_token(
        &self,
        user_id: String,
        tenant_id: String,
        client_id: Uuid,
    ) -> Result<String, SyncError> {
        self.jwt_manager
            .generate_token(user_id, tenant_id, client_id)
    }

    /// Generate token pair (access + refresh) for a client
    pub fn generate_token_pair(
        &self,
        user_id: String,
        tenant_id: String,
        client_id: Uuid,
    ) -> Result<super::TokenPair, SyncError> {
        let access_token = self
            .jwt_manager
            .generate_token(user_id.clone(), tenant_id.clone(), client_id)?;

        let refresh_token = self
            .jwt_manager
            .generate_refresh_token(user_id, tenant_id, client_id)?;

        Ok(super::TokenPair::new(access_token, refresh_token, 3600))
    }

    /// Refresh an access token using a refresh token
    pub fn refresh_token(&self, refresh_token: &str) -> Result<String, SyncError> {
        self.jwt_manager.refresh_access_token(refresh_token)
    }

    /// Add an allowed tenant to the authorizer
    pub fn add_tenant(&mut self, tenant_id: String) {
        self.authorizer.add_tenant(tenant_id);
    }

    /// Remove an allowed tenant from the authorizer
    pub fn remove_tenant(&mut self, tenant_id: &str) -> bool {
        self.authorizer.remove_tenant(tenant_id)
    }

    /// Query the change log for all changes since a specific version
    ///
    /// Returns all RowDelta entries with version > specified version,
    /// ordered by version ascending.
    async fn get_changes_since(&self, version: u64) -> Result<Vec<RowDelta>, SyncError> {
        let change_log = self.change_log.read();

        // Filter changes that occurred after the specified version
        let deltas: Vec<RowDelta> = change_log
            .iter()
            .filter(|entry| entry.version > version)
            .map(|entry| entry.delta.clone())
            .collect();

        tracing::debug!(
            "Retrieved {} changes since version {} (current: {})",
            deltas.len(),
            version,
            self.current_version
        );

        Ok(deltas)
    }

    /// Detect conflicts between client's vector clock and server's deltas
    ///
    /// Conflict detection uses vector clock comparison:
    /// - If client clock < server clock: no conflict (client needs update)
    /// - If client clock > server clock: no conflict (server needs update)
    /// - If clocks are concurrent: conflict detected
    async fn detect_conflicts(
        &self,
        client_clock: &VectorClock,
        deltas: &[RowDelta],
    ) -> Result<Vec<Conflict>, SyncError> {
        let mut conflicts = Vec::new();
        let row_versions = self.row_versions.read();

        for delta in deltas {
            let key = (delta.table.clone(), delta.row_id.clone());

            // Check if server has a version of this row
            if let Some((server_version, server_clock)) = row_versions.get(&key) {
                // Use vector clock to detect concurrent modifications
                if self.conflict_manager.detect_conflict(client_clock, server_clock) {
                    // Determine conflict type based on operation
                    let conflict_type = match &delta.operation {
                        Operation::Delete => ConflictType::DeleteUpdate,
                        Operation::Insert => ConflictType::UniqueViolation,
                        Operation::Update { .. } => ConflictType::ConcurrentUpdate,
                    };

                    // Retrieve server's version of the data from change log
                    let server_data = self.get_row_data(&delta.table, &delta.row_id);

                    let conflict = Conflict {
                        id: Uuid::new_v4(),
                        table: delta.table.clone(),
                        row_id: delta.row_id.clone(),
                        conflict_type,
                        client_version: delta.data.clone(),
                        server_version: server_data,
                        resolution: self.conflict_manager.strategy().clone(),
                    };

                    tracing::warn!(
                        "Conflict detected: table={}, row_id={:?}, type={:?}",
                        delta.table,
                        delta.row_id,
                        conflict_type
                    );

                    conflicts.push(conflict);
                }
            }
        }

        tracing::debug!(
            "Conflict detection complete: {} conflicts found in {} deltas",
            conflicts.len(),
            deltas.len()
        );

        Ok(conflicts)
    }

    /// Record a change in the server's change log
    ///
    /// This method should be called when the server applies a change
    /// to track it for future sync operations.
    pub fn record_change(&mut self, delta: RowDelta) {
        let version = self.current_version;

        // Update row version tracking
        {
            let mut row_versions = self.row_versions.write();
            let key = (delta.table.clone(), delta.row_id.clone());
            row_versions.insert(key, (version, delta.vector_clock.clone()));
        }

        // Append to change log
        {
            let mut change_log = self.change_log.write();
            change_log.push(VersionedChange {
                version,
                delta,
            });
        }

        tracing::debug!("Recorded change at version {}", version);
    }

    /// Get the latest data for a specific row from the change log
    fn get_row_data(&self, table: &str, row_id: &[u8]) -> Vec<u8> {
        let change_log = self.change_log.read();

        // Find the most recent change for this row (iterate in reverse)
        for entry in change_log.iter().rev() {
            if entry.delta.table == table && entry.delta.row_id == row_id {
                return entry.delta.data.clone();
            }
        }

        Vec::new() // Row not found in change log
    }

    /// Compact the change log by removing entries older than the specified version
    ///
    /// This helps manage memory usage for long-running servers.
    pub fn compact_change_log(&self, older_than_version: u64) -> usize {
        let mut change_log = self.change_log.write();
        let original_len = change_log.len();

        change_log.retain(|entry| entry.version >= older_than_version);

        let removed = original_len - change_log.len();
        tracing::info!(
            "Compacted change log: removed {} entries older than version {}",
            removed,
            older_than_version
        );

        removed
    }

    /// Get the current server version
    pub fn version(&self) -> u64 {
        self.current_version
    }

    /// Get the number of entries in the change log
    pub fn change_log_size(&self) -> usize {
        self.change_log.read().len()
    }
}

impl Default for SyncServer {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::sync::SyncMode;

    #[tokio::test]
    async fn test_sync_server_creation() {
        let server = SyncServer::new();
        assert_eq!(server.current_version, 0);
    }

    #[tokio::test]
    async fn test_handle_sync_request_with_auth() {
        let mut server = SyncServer::with_jwt_secret(b"test-secret");
        let client_id = Uuid::new_v4();

        // Generate valid token
        let token = server
            .generate_token("user123".to_string(), "tenant456".to_string(), client_id)
            .unwrap();

        let request = SyncRequest {
            client_id,
            last_sync_version: 0,
            changed_tables: vec![],
            pending_changes: 0,
            vector_clock: VectorClock::new(),
            sync_mode: SyncMode::Incremental,
        };

        let response = server.handle_sync_request(request, &token).await.unwrap();
        assert_eq!(response.server_version, 0);
    }

    #[tokio::test]
    async fn test_handle_sync_request_invalid_token() {
        let mut server = SyncServer::with_jwt_secret(b"test-secret");

        let request = SyncRequest {
            client_id: Uuid::new_v4(),
            last_sync_version: 0,
            changed_tables: vec![],
            pending_changes: 0,
            vector_clock: VectorClock::new(),
            sync_mode: SyncMode::Incremental,
        };

        // Invalid token should fail
        let result = server.handle_sync_request(request, "invalid.token.here").await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SyncError::Authentication));
    }

    #[tokio::test]
    async fn test_handle_sync_request_mismatched_client_id() {
        let mut server = SyncServer::with_jwt_secret(b"test-secret");
        let client_id = Uuid::new_v4();
        let different_client_id = Uuid::new_v4();

        // Generate token for one client
        let token = server
            .generate_token("user123".to_string(), "tenant456".to_string(), client_id)
            .unwrap();

        // Try to use it with a different client_id
        let request = SyncRequest {
            client_id: different_client_id,
            last_sync_version: 0,
            changed_tables: vec![],
            pending_changes: 0,
            vector_clock: VectorClock::new(),
            sync_mode: SyncMode::Incremental,
        };

        let result = server.handle_sync_request(request, &token).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SyncError::Authentication));
    }

    #[tokio::test]
    async fn test_handle_client_deltas_with_auth() {
        let mut server = SyncServer::with_jwt_secret(b"test-secret");
        let client_id = Uuid::new_v4();

        // Generate valid token
        let token = server
            .generate_token("user123".to_string(), "tenant456".to_string(), client_id)
            .unwrap();

        let deltas = vec![];
        let ack = server
            .handle_client_deltas(client_id, deltas, &token)
            .await
            .unwrap();

        assert_eq!(ack.applied_count, 0);
        assert_eq!(ack.new_version, 1);
    }

    #[tokio::test]
    async fn test_tenant_authorization() {
        let jwt_manager = super::super::JwtManager::new(b"test-secret");
        let mut authorizer = super::super::Authorizer::new();
        authorizer.add_tenant("allowed-tenant".to_string());

        let mut server = SyncServer::with_auth(jwt_manager, authorizer);
        let client_id = Uuid::new_v4();

        // Token for allowed tenant - should succeed
        let token = server
            .generate_token(
                "user123".to_string(),
                "allowed-tenant".to_string(),
                client_id,
            )
            .unwrap();

        let request = SyncRequest {
            client_id,
            last_sync_version: 0,
            changed_tables: vec![],
            pending_changes: 0,
            vector_clock: VectorClock::new(),
            sync_mode: SyncMode::Incremental,
        };

        let result = server.handle_sync_request(request, &token).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_tenant_authorization_denied() {
        let jwt_manager = super::super::JwtManager::new(b"test-secret");
        let mut authorizer = super::super::Authorizer::new();
        authorizer.add_tenant("allowed-tenant".to_string());

        let mut server = SyncServer::with_auth(jwt_manager, authorizer);
        let client_id = Uuid::new_v4();

        // Token for disallowed tenant - should fail
        let token = server
            .generate_token(
                "user123".to_string(),
                "forbidden-tenant".to_string(),
                client_id,
            )
            .unwrap();

        let request = SyncRequest {
            client_id,
            last_sync_version: 0,
            changed_tables: vec![],
            pending_changes: 0,
            vector_clock: VectorClock::new(),
            sync_mode: SyncMode::Incremental,
        };

        let result = server.handle_sync_request(request, &token).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SyncError::Authentication));
    }

    #[tokio::test]
    async fn test_token_generation() {
        let server = SyncServer::with_jwt_secret(b"test-secret");
        let client_id = Uuid::new_v4();

        let token = server
            .generate_token("user123".to_string(), "tenant456".to_string(), client_id)
            .unwrap();

        assert!(!token.is_empty());

        // Validate the token can be decoded
        let claims = server.authenticate(&token).await.unwrap();
        assert_eq!(claims.sub, "user123");
        assert_eq!(claims.tenant_id, "tenant456");
        assert_eq!(claims.client_id, client_id);
    }

    #[tokio::test]
    async fn test_token_pair_generation() {
        let server = SyncServer::with_jwt_secret(b"test-secret");
        let client_id = Uuid::new_v4();

        let token_pair = server
            .generate_token_pair("user123".to_string(), "tenant456".to_string(), client_id)
            .unwrap();

        assert!(!token_pair.access_token.is_empty());
        assert!(!token_pair.refresh_token.is_empty());
        assert_eq!(token_pair.token_type, "Bearer");

        // Validate access token
        let claims = server.authenticate(&token_pair.access_token).await.unwrap();
        assert_eq!(claims.sub, "user123");
    }

    #[tokio::test]
    async fn test_refresh_token_flow() {
        let server = SyncServer::with_jwt_secret(b"test-secret");
        let client_id = Uuid::new_v4();

        // Generate token pair
        let token_pair = server
            .generate_token_pair("user123".to_string(), "tenant456".to_string(), client_id)
            .unwrap();

        // Use refresh token to get new access token
        let new_access_token = server.refresh_token(&token_pair.refresh_token).unwrap();

        // Validate new access token
        let claims = server.authenticate(&new_access_token).await.unwrap();
        assert_eq!(claims.sub, "user123");
        assert_eq!(claims.tenant_id, "tenant456");
    }

    #[tokio::test]
    async fn test_tenant_management() {
        let mut server = SyncServer::with_jwt_secret(b"test-secret");

        // Add tenants
        server.add_tenant("tenant1".to_string());
        server.add_tenant("tenant2".to_string());

        // Remove tenant
        assert!(server.remove_tenant("tenant1"));
        assert!(!server.remove_tenant("nonexistent"));
    }
}