aboutsummaryrefslogtreecommitdiffstats
path: root/lib/relation.ml
blob: 8dad75da14fdd2222f248a21c9c6f0def7f22406 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
let print_references (references: (string * Disk.Executor.relational_type * string) list Disk.Executor.IntMap.t Disk.Executor.StringMap.t) =
  Disk.Executor.StringMap.iter
    (fun k v ->
      print_endline ("→ " ^ k ^ ": ");
      Disk.Executor.IntMap.iter
        (fun k v ->
          print_endline ("  ↳ " ^ Int64.to_string k);
          List.iter (fun (v, type', name) -> print_endline ("    → " ^ v ^ "[" ^ name ^ "::" ^ Disk.Executor.show_relational_type type' ^ "]")) v)
        v;
      print_newline ())
    references

let write_and_retrieve () =
  let open Disk in
  let open Executor in
  let schema =
    Executor.StringMap.empty
    |> Executor.StringMap.add "user"
         [
           ("address", Relation "address");
           ("last_name", Text);
           ("first_name", Text);
         ]
    |> Executor.StringMap.add "address"
         [ ("street", Text); ("number", Integer32) ]
  in
  let commit : Executor.commit =
    {
      state = Executor._EMPTY_SHA_HASH_;
      files = Executor.StringMap.empty;
      references = Executor.StringMap.empty;
      schema;
    }
  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+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "Daisy";
      entity_id = Some 0L;
      attribute = "user/first-name";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  print_references commit.references;
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "Duck";
      entity_id = Some 0L;
      attribute = "user/last-name";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  print_references commit.references;
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "Saint James St.";
      entity_id = Some 0L;
      attribute = "address/street";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  print_references commit.references;
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LInteger32 41l;
      entity_id = Some 0L;
      attribute = "address/number";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  print_references commit.references;
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LRelation (0L, "address");
      entity_id = Some 0L;
      attribute = "user/address";
      type' = Executor.Relation "address";
    }
    |> Command.transact commit locations
  in
  print_references commit.references;
  let entity_id = 1L in
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "Minnie";
      entity_id = Some 1L;
      attribute = "user/first-name";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "Mouse";
      entity_id = Some 1L;
      attribute = "user/last-name";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LRelation (0L, "address");
      entity_id = Some 1L;
      attribute = "user/address";
      type' = Executor.Relation "address";
    }
    |> Command.transact commit locations
  in
  let entity_id = 2L in
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "Scrooge";
      entity_id = Some 2L;
      attribute = "user/first-name";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "McDuck";
      entity_id = Some 2L;
      attribute = "user/last-name";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LText "President Julian St.";
      entity_id = Some 1L;
      attribute = "address/street";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  print_references commit.references;
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LInteger32 768l;
      entity_id = Some 1L;
      attribute = "address/number";
      type' = Executor.Text;
    }
    |> Command.transact commit locations
  in
  let+ (commit, locations), _ =
    {
      timestamp = 10.0;
      content = Executor.LRelation (1L, "address");
      entity_id = Some 2L;
      attribute = "user/address";
      type' = Executor.Relation "address";
    }
    |> Command.transact commit locations
  in
  let+ _, (Command.Read content as response) =
    Command.perform commit locations (SequentialRead { relation_name = "user" })
  in
  print_endline (Command.show_return response);
  Ok (commit, locations)
[@@warning "-8-27-26"]
(* Suppress pattern match incomplete warnings *)