aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hashing.ml
diff options
context:
space:
mode:
authorMarcos Magueta <maguetamarcos@gmail.com>2026-04-03 16:17:38 -0300
committerMarcos Magueta <maguetamarcos@gmail.com>2026-04-03 16:17:38 -0300
commit63ae3c930de89af6f85229a467caa3f53cb844a2 (patch)
treecda4d513672f9fefa09c3a1dff69d8443890e224 /lib/hashing.ml
parentc5bc3a4992b4968780c286a084ad7c02b2b58f1e (diff)
downloadSakura-63ae3c930de89af6f85229a467caa3f53cb844a2.tar.gz
Sakura-63ae3c930de89af6f85229a467caa3f53cb844a2.zip
format
Diffstat (limited to '')
-rw-r--r--lib/hashing.ml37
1 files changed, 20 insertions, 17 deletions
diff --git a/lib/hashing.ml b/lib/hashing.ml
index 4709ded..9475e46 100644
--- a/lib/hashing.ml
+++ b/lib/hashing.ml
@@ -1,8 +1,8 @@
(** Hash computation for tuples and relations.
This module provides content-addressable hashing for the relational engine.
- All hashes are deterministic - the same content always produces the same hash.
- This enables:
+ All hashes are deterministic - the same content always produces the same
+ hash. This enables:
- Deduplication of tuples
- Merkle tree construction for relations and databases
- Time-travel queries (load any historical state by hash) *)
@@ -16,38 +16,41 @@ let bytes_to_hex bytes =
let hi = c lsr 4 in
let lo = c land 0xf in
Bytes.set hex (i * 2) (Char.chr (if hi < 10 then hi + 48 else hi + 87));
- Bytes.set hex (i * 2 + 1) (Char.chr (if lo < 10 then lo + 48 else lo + 87));
+ Bytes.set hex
+ ((i * 2) + 1)
+ (Char.chr (if lo < 10 then lo + 48 else lo + 87))
done;
Bytes.to_string hex
-(** Compute hash for a tuple.
- The hash is computed from:
+(** Compute hash for a tuple. The hash is computed from:
- The relation name
- - All attribute names and their marshalled values (sorted by name for determinism) *)
+ - All attribute names and their marshalled values (sorted by name for
+ determinism) *)
let hash_tuple (tuple : Tuple.materialized) : Conventions.Hash.t =
let sorted_attrs =
Tuple.AttributeMap.bindings tuple.attributes
|> List.sort (fun (a, _) (b, _) -> String.compare a b)
in
let content =
- List.fold_left (fun acc (name, attr) ->
- (* Marshal the value and hex-encode to avoid null bytes *)
- let value_bytes = Marshal.to_bytes attr.Attribute.value [Marshal.Closures] in
- let value_hex = bytes_to_hex value_bytes in
- acc ^ name ^ ":" ^ value_hex ^ ";"
- ) (tuple.relation ^ "|") sorted_attrs
+ List.fold_left
+ (fun acc (name, attr) ->
+ (* Marshal the value and hex-encode to avoid null bytes *)
+ let value_bytes =
+ Marshal.to_bytes attr.Attribute.value [ Marshal.Closures ]
+ in
+ let value_hex = bytes_to_hex value_bytes in
+ acc ^ name ^ ":" ^ value_hex ^ ";")
+ (tuple.relation ^ "|") sorted_attrs
in
Conventions.Hash.hash_text content
-(** Compute hash for a relation.
- The hash is computed from:
+(** Compute hash for a relation. The hash is computed from:
- The relation name
- The schema (attribute names and types)
- The merkle tree root hash (representing all tuples) *)
let hash_relation ~name ~(schema : Schema.t) ~tree : Conventions.Hash.t =
let schema_str = Schema.to_string schema in
- let tree_hash = match Merkle.root_hash tree with
- | Some h -> h
- | None -> "empty"
+ let tree_hash =
+ match Merkle.root_hash tree with Some h -> h | None -> "empty"
in
Conventions.Hash.hash_text (name ^ "|" ^ schema_str ^ "|" ^ tree_hash)