hawdb 0.4.0

An embedded Rust graph database for local data-plane workloads
Documentation
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

#[test]
fn knowledge_retrieval_expands_graph_context_by_ordered_adjacency() {
    let mut db = Database::new();
    db.query("CREATE (:Memory {id: 'root', title: 'Ordered retrieval context'})")
        .unwrap();
    let lower_neighbor_id = {
        let branch_runtime = db.runtime.get_mut().unwrap();
        branch_runtime.store.create_node(
            &mut branch_runtime.catalog,
            "Entity",
            BTreeMap::from([
                (
                    "id".to_string(),
                    Value::String("lower-neighbor".to_string()),
                ),
                (
                    "name".to_string(),
                    Value::String("Lower neighbor".to_string()),
                ),
            ]),
        )
    }
    .unwrap();
    let higher_neighbor_id = {
        let branch_runtime = db.runtime.get_mut().unwrap();
        branch_runtime.store.create_node(
            &mut branch_runtime.catalog,
            "Entity",
            BTreeMap::from([
                (
                    "id".to_string(),
                    Value::String("higher-neighbor".to_string()),
                ),
                (
                    "name".to_string(),
                    Value::String("Higher neighbor".to_string()),
                ),
            ]),
        )
    }
    .unwrap();
    {
        let branch_runtime = db.runtime.get_mut().unwrap();
        branch_runtime.store.create_relationship(
            &mut branch_runtime.catalog,
            NodeId(0),
            higher_neighbor_id,
            "MENTIONS",
            BTreeMap::new(),
        )
    }
    .unwrap();
    {
        let branch_runtime = db.runtime.get_mut().unwrap();
        branch_runtime.store.create_relationship(
            &mut branch_runtime.catalog,
            NodeId(0),
            lower_neighbor_id,
            "RELATES_TO",
            BTreeMap::new(),
        )
    }
    .unwrap();

    let mut search_index = SearchIndex::in_memory();
    db.rebuild_search_projection(&mut search_index, SearchRebuildOptions::default())
        .unwrap();

    let output = db
        .retrieve_knowledge(
            &search_index,
            &KnowledgeRetrievalRequest {
                query_text: "ordered retrieval".to_string(),
                query_embedding: None,
                mode: SearchMode::Text,
                limit: 1,
                offset: 0,
                rank_window: None,
                search_fusion_weights: SearchFusionWeights::default(),
                metadata_filters: BTreeMap::new(),
                candidate_limit: None,
                candidate_scoring: KnowledgeCandidateScoringPolicy::Max,
                graph_seed_limit: 0,
                graph_context_limit: 1,
                graph_context_max_hops: 1,
            },
        )
        .unwrap();

    assert_eq!(output.graph_context_paths.len(), 1);
    assert_eq!(
        output
            .diagnostics
            .graph_context_input_candidate_set
            .cardinality,
        1
    );
    assert_eq!(
        output.diagnostics.graph_context_candidate_set.cardinality,
        1
    );
    assert_eq!(
        output.diagnostics.graph_context_candidate_set.id_space,
        "canonical_graph_relationship_id"
    );
    assert_eq!(
        output.graph_context_paths[0].target_external_id.as_deref(),
        Some("lower-neighbor")
    );
    assert_eq!(
        output.graph_context_paths[0].relationship_type.as_str(),
        "RELATES_TO"
    );
    assert!(output
        .fanout_reasons
        .iter()
        .any(|reason| reason.contains("graph_context_limit 1")));
}

#[test]
fn knowledge_retrieval_reports_dense_graph_context_without_truncation() {
    let mut db = Database::new();
    db.query("CREATE (:Memory {id: 'root', title: 'Dense retrieval root'})")
        .unwrap();
    for index in 0..DENSE_ADJACENCY_DEGREE_THRESHOLD {
        let target = {
            let branch_runtime = db.runtime.get_mut().unwrap();
            branch_runtime.store.create_node(
                &mut branch_runtime.catalog,
                "Entity",
                BTreeMap::from([
                    ("id".to_string(), Value::String(format!("entity-{index}"))),
                    ("name".to_string(), Value::String(format!("Entity {index}"))),
                ]),
            )
        }
        .unwrap();
        {
            let branch_runtime = db.runtime.get_mut().unwrap();
            branch_runtime.store.create_relationship(
                &mut branch_runtime.catalog,
                NodeId(0),
                target,
                "MENTIONS",
                BTreeMap::new(),
            )
        }
        .unwrap();
    }

    let mut search_index = SearchIndex::in_memory();
    db.rebuild_search_projection(&mut search_index, SearchRebuildOptions::default())
        .unwrap();

    let output = db
        .retrieve_knowledge(
            &search_index,
            &KnowledgeRetrievalRequest {
                query_text: "dense retrieval".to_string(),
                query_embedding: None,
                mode: SearchMode::Text,
                limit: 1,
                offset: 0,
                rank_window: None,
                search_fusion_weights: SearchFusionWeights::default(),
                metadata_filters: BTreeMap::new(),
                candidate_limit: None,
                candidate_scoring: KnowledgeCandidateScoringPolicy::Max,
                graph_seed_limit: 0,
                graph_context_limit: DENSE_ADJACENCY_DEGREE_THRESHOLD,
                graph_context_max_hops: 1,
            },
        )
        .unwrap();

    assert_eq!(
        output.graph_context_paths.len(),
        DENSE_ADJACENCY_DEGREE_THRESHOLD
    );
    assert_eq!(output.diagnostics.fanout_reason_count, 1);
    assert!(!output.diagnostics.graph_context_truncated);
    assert!(output
        .diagnostics
        .graph_context_truncation_reasons
        .is_empty());
    assert!(output.fanout_reasons[0]
        .contains("graph_context dense_adjacency MENTIONS outgoing node 0 degree"));
}