hawdb 0.37.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 crate::store_facade_tests::{
    active_checkpoint_path, read_durable_text, rewrite_checksummed_file,
    CheckpointCodecFixture as Fixture,
};
use crate::{Database, DatabaseConfig, Value};
use hawdb_storage::text::encode_properties;
use std::collections::BTreeMap;

fn assert_inline_checkpoint_statistics(include_statistics: bool) {
    let root = Fixture::new("checkpoint_parse_order", |database| {
        database
            .query("CREATE (:InlineCheckpoint {id: 1})-[:INLINE_LINK]->(:InlineCheckpoint {id: 2})")
            .unwrap();
    });

    let first = encode_properties(&BTreeMap::from([("id".to_string(), Value::Int(1))]));
    let second = encode_properties(&BTreeMap::from([("id".to_string(), Value::Int(2))]));
    let checkpoint = active_checkpoint_path(&root.0);
    rewrite_checksummed_file(
        &checkpoint,
        "canonical_records\ttrue\n",
        &format!("node\t0\t0\t{first}\nnode\t1\t0\t{second}\nrel\t0\t0\t1\t0\t\n"),
        "checkpoint",
    );
    if !include_statistics {
        let text = read_durable_text(&checkpoint, "checkpoint").unwrap();
        let (body, _) = text.rsplit_once("checksum\t").unwrap();
        let without_statistics = body
            .lines()
            .filter(|line| !line.starts_with("stat_"))
            .map(|line| format!("{line}\n"))
            .collect::<String>();
        rewrite_checksummed_file(&checkpoint, body, &without_statistics, "checkpoint");
    }

    for read_only in [true, false] {
        let database = Database::open_with_config(
            &root.0,
            DatabaseConfig {
                read_only,
                recovery_mode: hawdb_storage::config::RecoveryMode::Strict,
                ..Default::default()
            },
        )
        .unwrap();
        let statistics = database.basic_statistics().unwrap();
        assert_eq!(statistics.node_count, 2);
        assert_eq!(statistics.relationship_count, 1);
        assert_eq!(statistics.label_counts.values().copied().sum::<u64>(), 2);
        assert_eq!(statistics.rel_type_counts.values().copied().sum::<u64>(), 1);
    }
}

#[test]
fn inline_checkpoint_statistics_after_records_are_not_counted_twice() {
    assert_inline_checkpoint_statistics(true);
}

#[test]
fn inline_checkpoint_without_statistics_rebuilds_basic_counts() {
    assert_inline_checkpoint_statistics(false);
}