aboutsummaryrefslogtreecommitdiffstats
path: root/lib/drl/executor.ml
blob: e472ec53e30bca58ae866ec69a957d0fef019f34 (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
open Utilities.Result

let page_limit = 16

(* [mg:rel] names a relation on the session's branch; a bare name, one the session created. *)
let path_of (ctx : Sublanguage_context.t) name =
  match String.index_opt name ':' with
  | None -> ctx.session#namespace ^ "\\" ^ name
  | Some i ->
      String.concat "\\"
        [ "";
          "branch";
          ctx.session#branch;
          "multigroup";
          String.sub name 0 i;
          "relation";
          String.sub name (i + 1) (String.length name - i - 1) ]

let rec compile ctx = function
  | Ast.Base name -> RNT.Base (path_of ctx name)
  | Ast.Project (attributes, query) -> RNT.Project (compile ctx query, attributes)

let execute (ctx : Sublanguage_context.t) query =
  let* relation = RNT.execute ctx.kernel (compile ctx query) in
  Fun.protect
    ~finally:(fun () -> RNT.release relation)
    (fun () ->
      let* cursor = RNT.enumerate relation in
      Fun.protect
        ~finally:(fun () -> RNT.release cursor)
        (fun () ->
          let* {RNT.tuples; exhausted} = RNT.fetch cursor page_limit in
          Ok (Sublanguage_types.Cursor {rows= tuples; has_more= not exhausted}) ) )