aboutsummaryrefslogtreecommitdiffstats
path: root/lib/relation.ml
blob: d27f2e2ca4c6a7741471dc7f909156e333727c58 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
module Protocol = struct
  open Protocol_conv_xml
  open Sexplib0.Sexp_conv

  type tuple = string list
  [@@deriving sexp, protocol ~driver:(module Xml_light)]

  type relation = {
    attribute_name : string;
    attribute_type : string;
    tuples : tuple;
  }
  [@@deriving sexp, protocol ~driver:(module Xml_light)]

  type facts = relation list
  [@@deriving sexp, protocol ~driver:(module Xml_light)]
end

(*
let test_write_and_retrieve() =
  let open Disk in
  let stream: Executor.history = [{state = Executor._EMPTY_SHA_HASH_; files = Executor.StringMap.empty}] in
  let locations: Executor.locations = Executor.StringMap.empty in
  let open Extensions.Result in
  let command_write1: Command.t =
    {kind = Command.WRITE; timestamp = 10.0; hash = ""; content = "Daisy"; filename = "user/first-name"} in
  let+ ((stream, locations), Command.ComputedHash handle) = Command.commit_and_perform stream locations command_write1 in
  let command_write2: Command.t =
    {kind = Command.WRITE; timestamp = 10.0; hash = ""; content = "Blossom"; filename = "user/last-name"} in
  let command_write3: Command.t =
    {kind = Command.WRITE; timestamp = 10.0; hash = ""; content = "Minnie"; filename = "user/first-name"} in
  let+ ((stream, locations), Command.ComputedHash _handle) = Command.commit_and_perform stream locations command_write2 in
  let+ ((stream, locations), Command.ComputedHash handle) = Command.commit_and_perform stream locations command_write3 in
  let command_write4: Command.t =
    {kind = Command.WRITE; timestamp = 10.0; hash = ""; content = "Mouse"; filename = "user/last-name"} in
  let+ ((stream, locations), Command.ComputedHash _handle) = Command.commit_and_perform stream locations command_write4 in
  let command_write5: Command.t =
    {kind = Command.WRITE; timestamp = 10.0; hash = ""; content = "Billie"; filename = "user/first-name"} in
  let+ ((stream, locations), Command.ComputedHash handle) = Command.commit_and_perform stream locations command_write5 in
  let command_write6: Command.t =
    {kind = Command.WRITE; timestamp = 10.0; hash = ""; content = "Beans"; filename = "user/last-name"} in
  let+ ((stream, locations), Command.ComputedHash _handle) = Command.commit_and_perform stream locations command_write6 in
  let command_read1: Command.t = {kind = Command.READ; timestamp = 0.0; hash = ""; content = ""; filename = "user/first-name"} in
  let+ (_, Command.Read first_name) = Command.commit_and_perform (List.tl stream) locations command_read1 in
  let command_read2: Command.t = {kind = Command.READ; timestamp = 0.0; hash = ""; content = ""; filename = "user/last-name"} in
  let+ (_, Command.Read last_name) = Command.commit_and_perform (List.tl stream) locations command_read2 in
  (* List.iter (fun a -> print_endline a) (List.map Bytes.to_string first_name); *)
  (* List.iter (fun a -> print_endline a) (List.map Bytes.to_string last_name); *)
  let relation_result: Protocol.relation list =
    [{attribute_name = "user/first-name"; attribute_type = "string"; tuples = List.map Bytes.to_string first_name};
     {attribute_name = "user/last-name"; attribute_type = "string"; tuples = List.map Bytes.to_string last_name}] in
  Ok (Xml.to_string (Protocol.facts_to_xml_light relation_result))
    (* (Sexp.to_string (Protocol.sexp_of_t )) *)
    [@@warning "-8-27"] (* Suppress pattern match incomplete warnings *)
*)

let write_and_retrieve () =
  let open Disk in
  let commit : Executor.commit =
    {
      state = Executor._EMPTY_SHA_HASH_;
      files = Executor.StringMap.empty;
      references = Executor.StringMap.empty;
    }
  in
  let locations : Executor.locations = Executor.StringMap.empty in
  let relation_name = "user" in
  (* let entity_id, _ = Executor.StringMap.find relation_name references |> Executor.IntMap.max_binding in *)
  (* let references =
     Executor.StringMap.empty
     |> Executor.StringMap.add "user" (Executor.StringMap.empty
                                       |> Executor.StringMap.add "entity_0" [handle11; handle12]
                                       |> Executor.StringMap.add "entity_1" [handle21; handle22]
                                       |> Executor.StringMap.add "entity_2" [handle31; handle32]) in *)
  let open Extensions.Result in
  let command_write1 : Command.t =
    {
      kind = Command.WRITE;
      timestamp = 10.0;
      hash = "";
      content = "Daisy";
      entity_id = Some 0L;
      filename = "user/first-name";
    }
  in
  let+ (commit, locations, references), Command.ComputedHash handle11 =
    Command.commit_and_perform commit locations command_write1
  in
  let command_write2 : Command.t =
    {
      kind = Command.WRITE;
      timestamp = 10.0;
      hash = "";
      content = "Blossom";
      entity_id = Some 0L;
      filename = "user/last-name";
    }
  in
  let+ (commit, locations, references), Command.ComputedHash handle12 =
    Command.commit_and_perform commit locations command_write2
  in
  let entity_id = 1L in
  let command_write3 : Command.t =
    {
      kind = Command.WRITE;
      timestamp = 10.0;
      hash = "";
      content = "Minnie";
      entity_id = Some 1L;
      filename = "user/first-name";
    }
  in
  let+ (commit, locations, references), Command.ComputedHash handle21 =
    Command.commit_and_perform commit locations command_write3
  in
  let command_write4 : Command.t =
    {
      kind = Command.WRITE;
      timestamp = 10.0;
      hash = "";
      content = "Mouse";
      entity_id = Some 1L;
      filename = "user/last-name";
    }
  in
  let+ (commit, locations, references), Command.ComputedHash handle22 =
    Command.commit_and_perform commit locations command_write4
  in
  let entity_id = 2L in
  let command_write5 : Command.t =
    {
      kind = Command.WRITE;
      timestamp = 10.0;
      hash = "";
      content = "Billie";
      entity_id = Some 2L;
      filename = "user/first-name";
    }
  in
  let+ (commit, locations, references), Command.ComputedHash handle31 =
    Command.commit_and_perform commit locations command_write5
  in
  let command_write6 : Command.t =
    {
      kind = Command.WRITE;
      timestamp = 10.0;
      hash = "";
      content = "Beans";
      entity_id = Some 2L;
      filename = "user/last-name";
    }
  in
  let+ (commit, locations, references), Command.ComputedHash handle32 =
    Command.commit_and_perform commit locations command_write6
  in
  let command_read : Command.t =
    {
      kind = Command.READ;
      timestamp = 0.0;
      hash = "";
      content = "";
      entity_id = None;
      filename = "user";
    }
  in
  let+ _, (Command.Read content as response) =
    Command.commit_and_perform commit locations command_read
  in
  print_endline (Command.show_return response);
  Ok (commit, locations)
[@@warning "-8-27-26"]
(* Suppress pattern match incomplete warnings *)