This is an automated email from the ASF dual-hosted git repository.

zyxxoo pushed a commit to branch refactor/rust-rewrite-design
in repository https://gitbox.apache.org/repos/asf/hugegraph.git


The following commit(s) were added to refs/heads/refactor/rust-rewrite-design 
by this push:
     new 68975ec74 refactor(rust): expose testable store digest library
68975ec74 is described below

commit 68975ec74eef9c0a8e89c2f371ee4ba5ad3cd25a
Author: vaughn <[email protected]>
AuthorDate: Sun Sep 13 18:26:33 2026 +0800

    refactor(rust): expose testable store digest library
---
 tools/rust-store-reader/Cargo.toml |  3 +++
 tools/rust-store-reader/src/lib.rs | 43 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/tools/rust-store-reader/Cargo.toml 
b/tools/rust-store-reader/Cargo.toml
index 369889b22..98d052b76 100644
--- a/tools/rust-store-reader/Cargo.toml
+++ b/tools/rust-store-reader/Cargo.toml
@@ -6,6 +6,9 @@
 name = "hugegraph-store-reader"
 version = "0.1.0"
 edition = "2021"
+
+[lib]
+path = "src/lib.rs"
 [dependencies]
 anyhow = "1"
 rocksdb = "0.22"
diff --git a/tools/rust-store-reader/src/lib.rs 
b/tools/rust-store-reader/src/lib.rs
new file mode 100644
index 000000000..99f1eed3e
--- /dev/null
+++ b/tools/rust-store-reader/src/lib.rs
@@ -0,0 +1,43 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license agreements.
+// See the NOTICE file distributed with this work for additional information.
+// The ASF licenses this file to you under the Apache License, Version 2.0.
+// http://www.apache.org/licenses/LICENSE-2.0
+use anyhow::{Context, Result};
+use rocksdb::{IteratorMode, DB};
+use sha2::{Digest, Sha256};
+use std::path::Path;
+
+#[derive(Debug, Eq, PartialEq)]
+pub struct StoreDigest {
+    pub entries: u64,
+    pub sha256: String,
+}
+
+/// Computes a deterministic digest while leaving the database untouched.
+pub fn digest(path: impl AsRef<Path>) -> Result<StoreDigest> {
+    let path = path.as_ref();
+    let db = DB::open_default(path).with_context(|| format!("open RocksDB {}", 
path.display()))?;
+    let mut hasher = Sha256::new();
+    let mut entries = 0;
+    for item in db.iterator(IteratorMode::Start) {
+        let (key, value) = item?;
+        entries += 1;
+        hasher.update((key.len() as u64).to_be_bytes());
+        hasher.update(&key);
+        hasher.update((value.len() as u64).to_be_bytes());
+        hasher.update(&value);
+    }
+    Ok(StoreDigest {
+        entries,
+        sha256: hex::encode(hasher.finalize()),
+    })
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    #[test]
+    fn missing_path_is_error() {
+        assert!(digest("/definitely/missing/hugegraph-db").is_err());
+    }
+}

Reply via email to