iridium-db 0.2.0

A high-performance vector-graph hybrid storage and indexing engine
const std = @import("std");

export fn iridium_zig_noop() i32 {
    return 0;
}

export fn iridium_zig_synthetic_score(node_id: u64) f64 {
    const bucket: f64 = @floatFromInt(node_id % 100);
    return 1.0 - (bucket / 100.0);
}

export fn iridium_zig_synthetic_score_batch(node_ids: [*]const u64, len: usize, out_scores: [*]f64) void {
    var i: usize = 0;
    while (i < len) : (i += 1) {
        const bucket: f64 = @floatFromInt(node_ids[i] % 100);
        out_scores[i] = 1.0 - (bucket / 100.0);
    }
}

export fn iridium_zig_synthetic_score_filter_batch(
    node_ids: [*]const u64,
    len: usize,
    operator_code: u8,
    threshold: f64,
    out_scores: [*]f64,
    out_pass: [*]u8,
) void {
    var i: usize = 0;
    while (i < len) : (i += 1) {
        const bucket: f64 = @floatFromInt(node_ids[i] % 100);
        const score = 1.0 - (bucket / 100.0);
        out_scores[i] = score;

        const pass = switch (operator_code) {
            0 => score > threshold,
            1 => score < threshold,
            2 => @abs(score - threshold) < std.math.floatEps(f64),
            3 => score >= threshold,
            4 => score <= threshold,
            else => false,
        };
        out_pass[i] = if (pass) 1 else 0;
    }
}

export fn iridium_zig_synthetic_rerank_batch(
    node_ids: [*]const u64,
    len: usize,
    operator_code: u8,
    threshold: f64,
    out_scores: [*]f64,
    out_indices: [*]usize,
    out_count: *usize,
) void {
    var pass_count: usize = 0;
    var i: usize = 0;
    while (i < len) : (i += 1) {
        const bucket: f64 = @floatFromInt(node_ids[i] % 100);
        const score = 1.0 - (bucket / 100.0);
        const pass = switch (operator_code) {
            0 => score > threshold,
            1 => score < threshold,
            2 => @abs(score - threshold) < std.math.floatEps(f64),
            3 => score >= threshold,
            4 => score <= threshold,
            else => false,
        };
        if (pass) {
            out_scores[pass_count] = score;
            out_indices[pass_count] = i;
            pass_count += 1;
        }
    }
    out_count.* = pass_count;
}

export fn iridium_zig_cosine_score_batch(
    query: [*]const f32,
    dim: usize,
    embeddings: [*]const f32,
    rows: usize,
    out_scores: [*]f64,
) void {
    if (dim == 0) {
        return;
    }

    var q_norm_sq: f64 = 0.0;
    var d: usize = 0;
    while (d < dim) : (d += 1) {
        const qv: f64 = @floatCast(query[d]);
        q_norm_sq += qv * qv;
    }
    if (q_norm_sq <= std.math.floatEps(f64)) {
        var r0: usize = 0;
        while (r0 < rows) : (r0 += 1) {
            out_scores[r0] = 0.0;
        }
        return;
    }

    var row: usize = 0;
    while (row < rows) : (row += 1) {
        var dot: f64 = 0.0;
        var e_norm_sq: f64 = 0.0;
        const base = row * dim;
        var i: usize = 0;
        while (i < dim) : (i += 1) {
            const qv: f64 = @floatCast(query[i]);
            const ev: f64 = @floatCast(embeddings[base + i]);
            dot += qv * ev;
            e_norm_sq += ev * ev;
        }
        if (e_norm_sq <= std.math.floatEps(f64)) {
            out_scores[row] = 0.0;
        } else {
            out_scores[row] = dot / (@sqrt(q_norm_sq) * @sqrt(e_norm_sq));
        }
    }
}