aboutsummaryrefslogtreecommitdiffstats
path: root/lib/drl
diff options
context:
space:
mode:
Diffstat (limited to 'lib/drl')
-rw-r--r--lib/drl/ast.ml3
-rw-r--r--lib/drl/executor.ml33
-rw-r--r--lib/drl/parser.ml10
-rw-r--r--lib/drl/sublanguage.ml7
4 files changed, 53 insertions, 0 deletions
diff --git a/lib/drl/ast.ml b/lib/drl/ast.ml
new file mode 100644
index 0000000..d7eb089
--- /dev/null
+++ b/lib/drl/ast.ml
@@ -0,0 +1,3 @@
+open Sexplib.Std
+
+type query = Base of string | Project of string list * query [@@deriving sexp]
diff --git a/lib/drl/executor.ml b/lib/drl/executor.ml
new file mode 100644
index 0000000..e472ec5
--- /dev/null
+++ b/lib/drl/executor.ml
@@ -0,0 +1,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}) ) )
diff --git a/lib/drl/parser.ml b/lib/drl/parser.ml
new file mode 100644
index 0000000..f7d4512
--- /dev/null
+++ b/lib/drl/parser.ml
@@ -0,0 +1,10 @@
+module Error = struct
+ open Condition
+
+ let parse_error msg = condition "parse-error" msg empty
+end
+
+let of_sexp sexp =
+ match Ast.query_of_sexp sexp with
+ | q -> Ok q
+ | exception exn -> Error (Error.parse_error (Printexc.to_string exn))
diff --git a/lib/drl/sublanguage.ml b/lib/drl/sublanguage.ml
new file mode 100644
index 0000000..64b25cc
--- /dev/null
+++ b/lib/drl/sublanguage.ml
@@ -0,0 +1,7 @@
+type configuration = unit
+type ast = Ast.query
+
+let name = "drl"
+let parse _ = Ok ()
+let parse_sexp = Parser.of_sexp
+let execute = Executor.execute