velesdb-server 5.0.0

REST API server for VelesDB vector database
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
//! Relation (graph edge) and TTL handlers for point-bearing collections.
//!
//! These endpoints work on **any** collection type (vector, graph, or metadata)
//! because edges live on the collection's embedded edge store, independently
//! of the payload/vector layer.

use std::sync::Arc;

use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::IntoResponse,
    Json,
};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use velesdb_core::api_types::serde_id;
use velesdb_core::collection::graph::GraphEdge;
use velesdb_core::point::Point;

use crate::types::ErrorResponse;
use crate::AppState;

use super::super::helpers::{
    auto_core_error_response, error_response, get_collection_or_404, run_blocking,
};

use velesdb_core::EXPIRES_AT_KEY;

/// Request body for `POST /collections/{name}/relations`.
#[derive(Debug, Deserialize, ToSchema)]
pub struct RelateRequest {
    /// Source point ID.
    #[serde(deserialize_with = "serde_id::deserialize_id_from_string_or_number")]
    #[cfg_attr(feature = "openapi", schema(schema_with = serde_id::id_input_schema))]
    pub source: u64,
    /// Target point ID.
    #[serde(deserialize_with = "serde_id::deserialize_id_from_string_or_number")]
    #[cfg_attr(feature = "openapi", schema(schema_with = serde_id::id_input_schema))]
    pub target: u64,
    /// Relationship type label (e.g. `"KNOWS"`, `"RELATED_TO"`).
    pub rel_type: String,
    /// Optional edge properties.
    #[serde(default)]
    pub properties: serde_json::Value,
}

/// Response body for `POST /collections/{name}/relations`.
#[derive(Debug, Serialize, ToSchema)]
pub struct RelateResponse {
    /// Allocated edge ID.
    #[serde(serialize_with = "serde_id::serialize_id_as_string")]
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub edge_id: u64,
}

/// A single relation edge in a response.
#[derive(Debug, Serialize, ToSchema)]
pub struct RelationEdge {
    /// Edge ID.
    #[serde(serialize_with = "serde_id::serialize_id_as_string")]
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub id: u64,
    /// Source point ID.
    #[serde(serialize_with = "serde_id::serialize_id_as_string")]
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub source: u64,
    /// Target point ID.
    #[serde(serialize_with = "serde_id::serialize_id_as_string")]
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub target: u64,
    /// Relationship type label.
    pub rel_type: String,
    /// Edge properties (null when empty).
    pub properties: serde_json::Value,
}

/// Response body for `GET /collections/{name}/points/{id}/relations`.
#[derive(Debug, Serialize, ToSchema)]
pub struct RelationsResponse {
    /// Outgoing relation edges.
    pub edges: Vec<RelationEdge>,
    /// Total count.
    pub count: usize,
}

/// Request body for `PATCH /collections/{name}/points/{id}/ttl`.
#[derive(Debug, Deserialize, ToSchema)]
pub struct SetTtlRequest {
    /// Number of seconds from now until this point expires.
    /// A value of `0` expires the point immediately.
    pub ttl_seconds: u64,
}

// ---------------------------------------------------------------------------
// Handler implementations
// ---------------------------------------------------------------------------

/// The `properties` coercion of [`relate_points`]: an object passes through,
/// `null` means none, anything else is the caller's error — split out so the
/// handler reads as its three phases (resolve, coerce, insert).
#[allow(clippy::result_large_err)]
fn relation_properties(
    value: &serde_json::Value,
) -> Result<std::collections::HashMap<String, serde_json::Value>, axum::response::Response> {
    match value {
        serde_json::Value::Object(map) => {
            Ok(map.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
        }
        serde_json::Value::Null => Ok(std::collections::HashMap::new()),
        _ => Err(error_response(
            StatusCode::BAD_REQUEST,
            "properties must be an object or null".to_string(),
        )),
    }
}

/// Create a relation edge between two points in a collection.
///
/// Works on vector, graph, and metadata collections alike.
/// The edge ID is auto-assigned; the response body carries the allocated value.
#[utoipa::path(
    post,
    path = "/collections/{name}/relations",
    params(("name" = String, Path, description = "Collection name")),
    request_body = RelateRequest,
    responses(
        (status = 201, description = "Relation created", body = RelateResponse),
        (status = 400, description = "Invalid request", body = ErrorResponse),
        (status = 404, description = "Collection not found, or source/target point has no stored payload (VELES-022 NodeNotFound)", body = ErrorResponse),
        (status = 500, description = "Internal server error", body = ErrorResponse)
    ),
    tag = "graph"
)]
pub async fn relate_points(
    Path(name): Path<String>,
    State(state): State<Arc<AppState>>,
    Json(req): Json<RelateRequest>,
) -> axum::response::Response {
    let coll = match get_collection_or_404(&state, &name) {
        Ok(c) => c,
        Err(r) => return r,
    };

    let properties = match relation_properties(&req.properties) {
        Ok(map) => map,
        Err(resp) => return resp,
    };

    // Edge allocation + insertion take write locks and persist — run them on
    // the blocking pool.
    match run_blocking(move || insert_edge_with_retry(&coll, &req, properties)).await {
        Ok(Ok(edge_id)) => (StatusCode::CREATED, Json(RelateResponse { edge_id })).into_response(),
        Ok(Err(r)) | Err(r) => r,
    }
}

/// Maximum collision retries before giving up on edge-ID allocation.
const MAX_EDGE_RETRIES: u32 = 1_000;

/// Assigns a collision-free edge ID and inserts the edge, retrying on `EdgeExists`.
///
/// Returns an `INTERNAL_SERVER_ERROR` response when [`MAX_EDGE_RETRIES`]
/// consecutive IDs are all taken — this indicates a corrupted ID-space seed and
/// should never occur in practice.
#[allow(clippy::result_large_err)]
fn insert_edge_with_retry(
    coll: &velesdb_core::collection::AnyCollection,
    req: &RelateRequest,
    properties: std::collections::HashMap<String, serde_json::Value>,
) -> Result<u64, axum::response::Response> {
    let mut next_id = coll.max_edge_id().map_or(1, |m| m.saturating_add(1));
    for _ in 0..MAX_EDGE_RETRIES {
        if coll.edge_exists(next_id) {
            next_id = next_id.saturating_add(1);
            continue;
        }
        let edge = match GraphEdge::new(next_id, req.source, req.target, &req.rel_type) {
            Ok(e) => e.with_properties(properties.clone()),
            Err(e) => {
                return Err(error_response(
                    StatusCode::BAD_REQUEST,
                    format!("invalid edge: {e}"),
                ))
            }
        };
        match coll.add_edge(edge) {
            Ok(()) => return Ok(next_id),
            Err(velesdb_core::Error::EdgeExists(_)) => {
                next_id = next_id.saturating_add(1);
            }
            // Route through the shared error→status mapping (e.g.
            // NodeNotFound -> 404, not a generic 500) instead of collapsing
            // every non-EdgeExists error into Internal Server Error.
            Err(e) => return Err(auto_core_error_response(&e)),
        }
    }
    Err(error_response(
        StatusCode::INTERNAL_SERVER_ERROR,
        "edge id allocation exhausted after too many retries".to_string(),
    ))
}

/// Remove a relation edge by ID.
#[utoipa::path(
    delete,
    path = "/collections/{name}/relations/{edge_id}",
    params(
        ("name" = String, Path, description = "Collection name"),
        ("edge_id" = String, Path, description = "Edge ID to remove (u64 as a string; precision-safe above 2^53-1)", pattern = "^[0-9]+$")
    ),
    responses(
        (status = 204, description = "Relation removed"),
        (status = 404, description = "Collection or edge not found", body = ErrorResponse),
        (status = 500, description = "Internal server error", body = ErrorResponse)
    ),
    tag = "graph"
)]
pub async fn unrelate_points(
    Path((name, edge_id)): Path<(String, u64)>,
    State(state): State<Arc<AppState>>,
) -> axum::response::Response {
    let coll = match get_collection_or_404(&state, &name) {
        Ok(c) => c,
        Err(r) => return r,
    };

    // Edge removal takes write locks and persists — run it on the blocking pool.
    let removed = match run_blocking(move || coll.remove_edge(edge_id)).await {
        Ok(removed) => removed,
        Err(resp) => return resp,
    };
    if removed {
        StatusCode::NO_CONTENT.into_response()
    } else {
        let err = velesdb_core::Error::EdgeNotFound(edge_id);
        (
            StatusCode::NOT_FOUND,
            Json(ErrorResponse {
                error: format!("{err} in collection '{name}'"),
                code: Some(err.code().to_string()),
            }),
        )
            .into_response()
    }
}

/// List outgoing relation edges for a point.
#[utoipa::path(
    get,
    path = "/collections/{name}/points/{id}/relations",
    params(
        ("name" = String, Path, description = "Collection name"),
        ("id" = String, Path, description = "Point ID (u64 as a string; precision-safe above 2^53-1)", pattern = "^[0-9]+$")
    ),
    responses(
        (status = 200, description = "Outgoing relations", body = RelationsResponse),
        (status = 404, description = "Collection not found", body = ErrorResponse),
        (status = 500, description = "Internal server error", body = ErrorResponse)
    ),
    tag = "graph"
)]
pub async fn get_point_relations(
    Path((name, id)): Path<(String, u64)>,
    State(state): State<Arc<AppState>>,
) -> axum::response::Response {
    let coll = match get_collection_or_404(&state, &name) {
        Ok(c) => c,
        Err(r) => return r,
    };

    // Edge listing takes edge-store shard locks — run it on the blocking pool.
    let raw_edges = match run_blocking(move || coll.get_outgoing_edges(id)).await {
        Ok(edges) => edges,
        Err(resp) => return resp,
    };
    let edges: Vec<RelationEdge> = raw_edges
        .into_iter()
        .map(|e| RelationEdge {
            id: e.id(),
            source: e.source(),
            target: e.target(),
            rel_type: e.label().to_string(),
            properties: serde_json::to_value(e.properties()).unwrap_or_default(),
        })
        .collect();

    let count = edges.len();
    Json(RelationsResponse { edges, count }).into_response()
}

/// Set (or refresh) the durable TTL of a point.
///
/// Persists `_veles_expires_at` in the point's payload so the expiry
/// survives a restart. A `ttl_seconds` of `0` expires the point immediately.
/// Expired points are excluded from all read surfaces (search/get/scroll/query);
/// refreshing an expired point returns 404; storage is reclaimed lazily.
#[utoipa::path(
    patch,
    path = "/collections/{name}/points/{id}/ttl",
    params(
        ("name" = String, Path, description = "Collection name"),
        ("id" = String, Path, description = "Point ID (u64 as a string; precision-safe above 2^53-1)", pattern = "^[0-9]+$")
    ),
    request_body = SetTtlRequest,
    responses(
        (status = 204, description = "TTL set successfully"),
        (status = 400, description = "Non-object payload", body = ErrorResponse),
        (status = 404, description = "Collection or point not found", body = ErrorResponse),
        (status = 500, description = "Internal server error", body = ErrorResponse)
    ),
    tag = "points"
)]
pub async fn set_point_ttl(
    Path((name, id)): Path<(String, u64)>,
    State(state): State<Arc<AppState>>,
    Json(req): Json<SetTtlRequest>,
) -> axum::response::Response {
    let coll = match get_collection_or_404(&state, &name) {
        Ok(c) => c,
        Err(r) => return r,
    };

    // The read-modify-write (get + upsert with fsync) is synchronous core
    // code — run it on the blocking pool.
    run_blocking(move || set_point_ttl_sync(&coll, id, req.ttl_seconds, &name))
        .await
        .unwrap_or_else(|resp| resp)
}

/// Synchronous body of [`set_point_ttl`]: reads the point, stamps the durable
/// expiry, and upserts it back. Runs on the blocking pool.
fn set_point_ttl_sync(
    coll: &velesdb_core::AnyCollection,
    id: u64,
    ttl_seconds: u64,
    name: &str,
) -> axum::response::Response {
    let point = match coll.get(&[id]).into_iter().flatten().next() {
        Some(p) => p,
        None => {
            let err = velesdb_core::Error::PointNotFound(id);
            return (
                StatusCode::NOT_FOUND,
                Json(ErrorResponse {
                    error: format!("{err} in collection '{name}'"),
                    code: Some(err.code().to_string()),
                }),
            )
                .into_response();
        }
    };

    let expires_at = now_secs().saturating_add(ttl_seconds);
    let updated = match stamp_ttl(point, id, expires_at, name) {
        Ok(p) => p,
        Err(r) => return r,
    };

    match coll.upsert(vec![updated]) {
        Ok(()) => StatusCode::NO_CONTENT.into_response(),
        Err(e) => error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("failed to set TTL: {e}"),
        ),
    }
}

/// Injects `_veles_expires_at` into a point's payload and returns an updated
/// [`Point`] ready for upsert. Returns an error response when the payload is
/// not a JSON object.
// axum::response::Response is intentionally large; this is the standard handler error type.
#[allow(clippy::result_large_err)]
fn stamp_ttl(
    point: Point,
    id: u64,
    expires_at: u64,
    collection: &str,
) -> Result<Point, axum::response::Response> {
    let mut payload = point
        .payload
        .unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::new()));

    let Some(obj) = payload.as_object_mut() else {
        return Err(error_response(
            StatusCode::BAD_REQUEST,
            format!("point {id} in '{collection}' has a non-object payload"),
        ));
    };

    obj.insert(
        EXPIRES_AT_KEY.to_string(),
        serde_json::Value::from(expires_at),
    );

    Ok(Point {
        id,
        vector: point.vector,
        payload: Some(payload),
        sparse_vectors: point.sparse_vectors,
    })
}

fn now_secs() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}