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
//! Branch operation handlers
//!
//! Implements HTTP request handlers for branch CRUD operations.

use axum::{
    extract::{Path, State},
    http::StatusCode,
    Json,
};
use tracing::{info, warn, error};

use crate::api::{
    models::{
        ApiError,
        CreateBranchRequest,
        BranchResponse,
        BranchListResponse,
        MergeBranchRequest,
        MergeBranchResponse,
    },
    server::AppState,
};
use crate::storage::BranchOptions;

/// List all branches
///
/// GET /v1/branches
///
/// Returns a list of all active branches in the database.
///
/// # Response
///
/// - 200 OK: Returns BranchListResponse with all branches
/// - 500 Internal Server Error: Database error
/// - 501 Not Implemented: Branching not enabled
pub async fn list_branches(
    State(state): State<AppState>,
) -> Result<Json<BranchListResponse>, ApiError> {
    info!("Listing all branches");

    // Access the branch manager from storage
    let branch_manager = state.db.storage.branch_manager()
        .ok_or_else(|| ApiError::new(
            axum::http::StatusCode::NOT_IMPLEMENTED,
            "BranchingNotEnabled",
            "Database branching is not enabled for this instance"
        ))?;

    let branches = branch_manager.list_branches()
        .map_err(|e| {
            error!("Failed to list branches: {}", e);
            ApiError::from(e)
        })?;

    let response = BranchListResponse {
        total: branches.len(),
        branches: branches.into_iter().map(BranchResponse::from).collect(),
    };

    info!("Found {} branches", response.total);

    Ok(Json(response))
}

/// Create a new branch
///
/// POST /v1/branches
///
/// Creates a new database branch with the specified configuration.
///
/// # Request Body
///
/// - name: Branch name (required)
/// - parent: Parent branch name (optional, defaults to "main")
/// - snapshot_id: Snapshot to branch from (optional, defaults to current)
/// - options: Branch options (optional)
///
/// # Response
///
/// - 201 Created: Returns BranchResponse with created branch details
/// - 400 Bad Request: Invalid request
/// - 409 Conflict: Branch already exists
/// - 500 Internal Server Error: Database error
/// - 501 Not Implemented: Branching not enabled
pub async fn create_branch(
    State(state): State<AppState>,
    Json(request): Json<CreateBranchRequest>,
) -> Result<(StatusCode, Json<BranchResponse>), ApiError> {
    info!("Creating branch: {}", request.name);

    // Validate branch name
    if request.name.is_empty() {
        warn!("Invalid branch name: empty");
        return Err(ApiError::bad_request("Branch name cannot be empty"));
    }

    // Access the branch manager from storage
    let branch_manager = state.db.storage.branch_manager()
        .ok_or_else(|| ApiError::new(
            axum::http::StatusCode::NOT_IMPLEMENTED,
            "BranchingNotEnabled",
            "Database branching is not enabled for this instance"
        ))?;

    // Get current snapshot ID or use provided one
    let snapshot_id = request.snapshot_id.unwrap_or_else(|| {
        branch_manager.current_timestamp()
    });

    // Convert options
    let options = request.options
        .map(BranchOptions::from)
        .unwrap_or_default();

    // Create the branch
    let branch_id = branch_manager.create_branch(
        &request.name,
        request.parent.as_deref(),
        snapshot_id,
        options,
    ).map_err(|e| {
        error!("Failed to create branch '{}': {}", request.name, e);
        ApiError::from(e)
    })?;

    info!("Branch '{}' created with ID: {}", request.name, branch_id);

    // Get the created branch metadata
    let metadata = branch_manager.get_branch_by_name(&request.name)
        .map_err(|e| {
            error!("Failed to retrieve created branch: {}", e);
            ApiError::from(e)
        })?;

    Ok((StatusCode::CREATED, Json(BranchResponse::from(metadata))))
}

/// Get branch details
///
/// GET /v1/branches/:name
///
/// Retrieves detailed information about a specific branch.
///
/// # Path Parameters
///
/// - name: Branch name
///
/// # Response
///
/// - 200 OK: Returns BranchResponse with branch details
/// - 404 Not Found: Branch does not exist
/// - 500 Internal Server Error: Database error
/// - 501 Not Implemented: Branching not enabled
pub async fn get_branch(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> Result<Json<BranchResponse>, ApiError> {
    info!("Getting branch: {}", name);

    // Access the branch manager from storage
    let branch_manager = state.db.storage.branch_manager()
        .ok_or_else(|| ApiError::new(
            axum::http::StatusCode::NOT_IMPLEMENTED,
            "BranchingNotEnabled",
            "Database branching is not enabled for this instance"
        ))?;

    let metadata = branch_manager.get_branch_by_name(&name)
        .map_err(|e| {
            warn!("Branch '{}' not found: {}", name, e);
            ApiError::from(e)
        })?;

    info!("Found branch '{}'", name);

    Ok(Json(BranchResponse::from(metadata)))
}

/// Delete a branch
///
/// DELETE /v1/branches/:name
///
/// Deletes a branch. The branch must not have any child branches.
/// Cannot delete the main branch.
///
/// # Path Parameters
///
/// - name: Branch name
///
/// # Query Parameters
///
/// - if_exists: If true, don't error if branch doesn't exist (optional)
///
/// # Response
///
/// - 204 No Content: Branch deleted successfully
/// - 400 Bad Request: Cannot delete main branch or branch has children
/// - 404 Not Found: Branch does not exist (unless if_exists=true)
/// - 500 Internal Server Error: Database error
/// - 501 Not Implemented: Branching not enabled
pub async fn delete_branch(
    State(state): State<AppState>,
    Path(name): Path<String>,
) -> Result<StatusCode, ApiError> {
    info!("Deleting branch: {}", name);

    // Access the branch manager from storage
    let branch_manager = state.db.storage.branch_manager()
        .ok_or_else(|| ApiError::new(
            axum::http::StatusCode::NOT_IMPLEMENTED,
            "BranchingNotEnabled",
            "Database branching is not enabled for this instance"
        ))?;

    // Drop the branch (if_exists = false for strict error handling)
    branch_manager.drop_branch(&name, false)
        .map_err(|e| {
            error!("Failed to delete branch '{}': {}", name, e);
            ApiError::from(e)
        })?;

    info!("Branch '{}' deleted successfully", name);

    Ok(StatusCode::NO_CONTENT)
}

/// Merge branches
///
/// POST /v1/branches/:name/merge
///
/// Merges the source branch (specified in path) into the target branch
/// (specified in request body).
///
/// # Path Parameters
///
/// - name: Source branch name to merge from
///
/// # Request Body
///
/// - target: Target branch name to merge into
/// - strategy: Merge strategy (auto, manual, theirs, ours)
///
/// # Response
///
/// - 200 OK: Merge completed, returns MergeBranchResponse
/// - 409 Conflict: Merge conflicts detected (if strategy is manual)
/// - 422 Unprocessable Entity: Branches are not in valid state for merge
/// - 404 Not Found: Source or target branch not found
/// - 500 Internal Server Error: Database error
/// - 501 Not Implemented: Branching not enabled
pub async fn merge_branch(
    State(state): State<AppState>,
    Path(source_name): Path<String>,
    Json(request): Json<MergeBranchRequest>,
) -> Result<Json<MergeBranchResponse>, ApiError> {
    info!("Merging branch '{}' into '{}'", source_name, request.target);

    // Access the branch manager from storage
    let branch_manager = state.db.storage.branch_manager()
        .ok_or_else(|| ApiError::new(
            axum::http::StatusCode::NOT_IMPLEMENTED,
            "BranchingNotEnabled",
            "Database branching is not enabled for this instance"
        ))?;

    // Perform the merge
    let merge_result = branch_manager.merge_branch(
        &source_name,
        &request.target,
        request.strategy.into(),
    ).map_err(|e| {
        error!("Failed to merge '{}' into '{}': {}", source_name, request.target, e);
        ApiError::from(e)
    })?;

    let message = if merge_result.completed {
        if merge_result.conflicts.is_empty() {
            format!(
                "Successfully merged {} keys from '{}' into '{}' with no conflicts",
                merge_result.merged_keys,
                source_name,
                request.target
            )
        } else {
            format!(
                "Successfully merged {} keys from '{}' into '{}' with {} conflicts resolved",
                merge_result.merged_keys,
                source_name,
                request.target,
                merge_result.conflicts.len()
            )
        }
    } else {
        format!(
            "Merge from '{}' into '{}' failed due to {} conflicts (manual strategy requires conflict resolution)",
            source_name,
            request.target,
            merge_result.conflicts.len()
        )
    };

    info!("{}", message);

    Ok(Json(MergeBranchResponse {
        merge_timestamp: merge_result.merge_timestamp,
        merged_keys: merge_result.merged_keys,
        conflicts: merge_result.conflicts.into_iter()
            .map(|c| c.into())
            .collect(),
        completed: merge_result.completed,
        message,
    }))
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::EmbeddedDatabase;
    use crate::api::models::branch::MergeStrategyDto;
    use std::sync::Arc;

    fn create_test_state() -> AppState {
        let db = Arc::new(EmbeddedDatabase::new_in_memory().unwrap());
        let query_registry = Arc::new(crate::compute::QueryRegistry::new());
        AppState { db, query_registry, auth_bridge: None, oauth_registry: None, change_notifier: None }
    }

    #[tokio::test]
    async fn test_list_branches() {
        let state = create_test_state();

        let result = list_branches(State(state)).await;
        assert!(result.is_ok());

        let response = result.unwrap();
        // Should have at least the main branch
        assert!(response.0.total >= 1);
    }

    #[tokio::test]
    async fn test_create_branch() {
        let state = create_test_state();

        let request = CreateBranchRequest {
            name: "test-branch".to_string(),
            parent: Some("main".to_string()),
            snapshot_id: None,
            options: None,
        };

        let result = create_branch(State(state), Json(request)).await;
        assert!(result.is_ok());

        let (status, response) = result.unwrap();
        assert_eq!(status, StatusCode::CREATED);
        assert_eq!(response.0.name, "test-branch");
    }

    #[tokio::test]
    async fn test_create_branch_empty_name() {
        let state = create_test_state();

        let request = CreateBranchRequest {
            name: "".to_string(),
            parent: None,
            snapshot_id: None,
            options: None,
        };

        let result = create_branch(State(state), Json(request)).await;
        assert!(result.is_err());

        let err = result.unwrap_err();
        assert_eq!(err.status, StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn test_get_branch() {
        let state = create_test_state();

        // Get the main branch
        let result = get_branch(State(state), Path("main".to_string())).await;
        assert!(result.is_ok());

        let response = result.unwrap();
        assert_eq!(response.0.name, "main");
    }

    #[tokio::test]
    async fn test_get_branch_not_found() {
        let state = create_test_state();

        let result = get_branch(State(state), Path("nonexistent".to_string())).await;
        assert!(result.is_err());

        let err = result.unwrap_err();
        assert_eq!(err.status, StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_delete_branch() {
        let state = create_test_state();

        // Create a branch first
        let request = CreateBranchRequest {
            name: "temp-branch".to_string(),
            parent: Some("main".to_string()),
            snapshot_id: None,
            options: None,
        };
        create_branch(State(state.clone()), Json(request)).await.unwrap();

        // Delete it
        let result = delete_branch(State(state), Path("temp-branch".to_string())).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), StatusCode::NO_CONTENT);
    }

    #[tokio::test]
    async fn test_delete_main_branch() {
        let state = create_test_state();

        let result = delete_branch(State(state), Path("main".to_string())).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_merge_branch() {
        let state = create_test_state();

        // Create a branch to merge
        let create_req = CreateBranchRequest {
            name: "feature".to_string(),
            parent: Some("main".to_string()),
            snapshot_id: None,
            options: None,
        };
        create_branch(State(state.clone()), Json(create_req)).await.unwrap();

        // Merge it back
        let merge_req = MergeBranchRequest {
            target: "main".to_string(),
            strategy: MergeStrategyDto::Auto,
        };

        let result = merge_branch(State(state), Path("feature".to_string()), Json(merge_req)).await;
        assert!(result.is_ok());

        let response = result.unwrap();
        assert!(response.0.completed);
    }
}