aboutsummaryrefslogtreecommitdiffstats
path: root/lib/storable.ml
blob: 058773cc7c2eb4b00b9aef96ab31694696ed2f99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(** Serialization formats for content-addressed storage.

    These modules define the wire format for persisting relational engine
    objects to storage. Each type has to_bytes/of_bytes for serialization.

    The stored formats capture only the essential data needed for reconstruction:
    - Tuple: relation name and attribute hashes
    - Relation: name, schema, tuple hashes, cardinality
    - Database: name, relation hashes, merkle tree keys, history *)

(** Stored tuple format for serialization *)
module Tuple = struct
  type t = {
    relation : string;
    attributes : (string * Conventions.Hash.t) list;  (* attr_name -> attr_value_hash *)
  }

  let to_bytes (st : t) : bytes =
    Marshal.to_bytes st [Marshal.Closures]

  let of_bytes (b : bytes) : t =
    Marshal.from_bytes b 0
end

(** Stored relation format for serialization *)
module Relation = struct
  type t = {
    name : string;
    schema : Schema.t;
    tree_keys : Conventions.Hash.t list;  (* Merkle tree contents - tuple hashes *)
    cardinality : Conventions.Cardinality.t;
  }

  let to_bytes (sr : t) : bytes =
    Marshal.to_bytes sr [Marshal.Closures]

  let of_bytes (b : bytes) : t =
    Marshal.from_bytes b 0
end

(** Stored database format for serialization *)
module Database = struct
  type t = {
    name : string;
    relations : (string * Conventions.Hash.t) list;  (* name -> relation_hash *)
    tree_keys : Conventions.Hash.t list;  (* Merkle tree contents - relation hashes *)
    history : Conventions.Hash.t list;
    timestamp : float;
  }

  let to_bytes (sd : t) : bytes =
    Marshal.to_bytes sd [Marshal.Closures]

  let of_bytes (b : bytes) : t =
    Marshal.from_bytes b 0
end