blob: 865c757be6fd482a15419dbcd039e5ea87564750 (
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
57
58
59
60
|
(** Structural delta between two Database.t states.
Computes what changed from ancestor → target at the relation level. *)
type relation_diff =
| RelationAdded of Relation.t
| RelationRemoved of string
| RelationModified of {
name : string;
added_tuples : Conventions.Hash.t list;
removed_tuples : Conventions.Hash.t list;
schema_changed : bool;
}
type t = {
ancestor : Conventions.Hash.t;
left : Conventions.Hash.t;
right : Conventions.Hash.t;
relation_diffs : relation_diff list;
}
module StringSet = Set.Make(String)
let relation_keys rel =
match rel.Relation.tree with
| None -> StringSet.empty
| Some tree -> StringSet.of_list (Merkle.keys tree)
(** Compute relation-level diffs from ancestor → target. *)
let diff ~ancestor ~(target : Database.t) =
let ancestor_names =
Database.get_relation_names ancestor |> StringSet.of_list
in
let target_names =
Database.get_relation_names target |> StringSet.of_list
in
let all_names = StringSet.union ancestor_names target_names in
StringSet.fold (fun name acc ->
let in_ancestor = StringSet.mem name ancestor_names in
let in_target = StringSet.mem name target_names in
match in_ancestor, in_target with
| false, true ->
let rel = Option.get (Database.get_relation target name) in
RelationAdded rel :: acc
| true, false ->
RelationRemoved name :: acc
| true, true ->
let anc_rel = Option.get (Database.get_relation ancestor name) in
let tgt_rel = Option.get (Database.get_relation target name) in
(* Compare hashes — if identical, no diff *)
if anc_rel.Relation.hash = tgt_rel.Relation.hash then acc
else begin
let anc_keys = relation_keys anc_rel in
let tgt_keys = relation_keys tgt_rel in
let added_tuples = StringSet.diff tgt_keys anc_keys |> StringSet.elements in
let removed_tuples = StringSet.diff anc_keys tgt_keys |> StringSet.elements in
let schema_changed = anc_rel.Relation.schema <> tgt_rel.Relation.schema in
RelationModified { name; added_tuples; removed_tuples; schema_changed } :: acc
end
| false, false -> acc (* impossible *)
) all_names []
|