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
190
191
192
193
194
195
196
type 'a t =
| Node of 'a
| And of ('a t * 'a t)
| Or of ('a t * 'a t)
| Not of 'a t
let node n = Node n
let ( land ) lhs rhs = And (lhs, rhs)
let ( lor ) lhs rhs = Or (lhs, rhs)
let lnot lhs = Not lhs
let equal : 'a t -> 'a t -> bool = Stdlib.( = )
let verum = node @@ Node.field @@ Field.id @@ Range.range None None
let always = verum
let falsum = lnot verum
let never = falsum
let union = ( lor )
let diff lhs rhs = lhs land lnot rhs
let rec to_string_f f = function
| Node node ->
f node
| And (lhs, rhs) ->
Printf.sprintf "(%s and %s)" (to_string_f f lhs) (to_string_f f rhs)
| Or (lhs, rhs) ->
Printf.sprintf "(%s || %s)" (to_string_f f lhs) (to_string_f f rhs)
| Not e ->
Printf.sprintf "not %s" (to_string_f f e)
let to_string = to_string_f Node.to_string
let rec to_sexp_f f = function
| Node node ->
f node
| And (lhs, rhs) ->
Printf.sprintf "(and %s %s)" (to_sexp_f f lhs) (to_sexp_f f rhs)
| Or (lhs, rhs) ->
Printf.sprintf "(or %s %s)" (to_sexp_f f lhs) (to_sexp_f f rhs)
| Not e ->
Printf.sprintf "(not %s)" (to_sexp_f f e)
let to_sexp = to_sexp_f Node.to_sexp
let to_sexp_keys e =
let ( $ ) g f s = g (f s) in
let s = Field.String.(Key.to_string $ key) in
let b = Field.Bool.(Key.to_string $ key) in
let i = Field.Int.(Key.to_string $ key) in
let i64 = Field.Int64.(Key.to_string $ key) in
let float = Field.Float.(Key.to_string $ key) in
to_sexp_f (Field.fold s b i i64 float) e
let comsume combine node s =
match Angstrom.parse_string ~consume:All Parser.node_cli s with
| Ok node' ->
combine node (Node node')
| Error _ ->
node
let of_string_keys s =
let parser = Angstrom.(sep_by Parser.pwhitespace Parser.key_string) in
Result.to_option @@ Angstrom.parse_string ~consume:All parser (String.trim s)
let sexp n =
let open Angstrom in
let whitespace = Parser.whitespace in
let parenthesis parser = char '(' *> parser <* char ')' in
Angstrom.fix (fun sexp ->
let sand =
string_ci "and"
*> map2 (whitespace sexp) (whitespace sexp) ~f:(fun lhs rhs ->
And (lhs, rhs)
)
|> parenthesis
in
let sor =
string_ci "or"
*> map2 (whitespace sexp) (whitespace sexp) ~f:(fun lhs rhs ->
Or (lhs, rhs)
)
|> parenthesis
in
let snot =
string_ci "not" *> map (whitespace sexp) ~f:(fun lhs -> Not lhs)
|> parenthesis
in
choice [ sand; sor; snot; map ~f:(fun node -> Node node) n ]
)
let of_sexp s =
Result.to_option
@@ Angstrom.parse_string ~consume:All (sexp Parser.node_sexp) s
let of_sexp_keys s =
Result.to_option
@@ Angstrom.parse_string ~consume:All (sexp Parser.key_unit_node) s
let rec of_strings_advance = function
| [] ->
(None, [])
| ("," | "^") :: q ->
of_strings_advance q
| t :: q ->
(Some t, q)
let rec of_strings' node s =
match s with
| [] ->
node
| "," :: q ->
let head, q = of_strings_advance q in
let node =
match head with
| None ->
node
| Some t ->
comsume (fun lhs rhs -> Or (lhs, rhs)) node t
in
of_strings' node q
| "^" :: q ->
let head, q = of_strings_advance q in
let node =
match head with
| None ->
node
| Some t ->
comsume (fun lhs rhs -> And (lhs, Not rhs)) node t
in
of_strings' node q
| s :: q ->
let node = comsume (fun lhs rhs -> And (lhs, rhs)) node s in
of_strings' node q
let rec of_strings s =
match s with
| [] ->
None
| "," :: q ->
of_strings q
| "^" :: q -> (
let head, q = of_strings_advance q in
match head with
| None ->
of_strings q
| Some t -> (
match Angstrom.parse_string ~consume:All Parser.node_cli t with
| Ok node' ->
Some (of_strings' (Not (Node node')) q)
| Error _ ->
of_strings q
)
)
| t :: q -> (
match Angstrom.parse_string ~consume:All Parser.node_cli t with
| Ok node' ->
Some (of_strings' (Node node') q)
| Error _ ->
of_strings q
)
let rec matches f query =
match query with
| Node node ->
f node
| And (lhs, rhs) ->
matches f lhs && matches f rhs
| Or (lhs, rhs) ->
matches f lhs || matches f rhs
| Not t ->
not (matches f t)
let matches_musics subfields music_file album participants properties query =
matches
(QueryMusic.matches_node subfields
(music_file, album, participants, properties)
)
query
let matches_albums subfields album stats participants query =
matches (QueryAlbum.matches_node subfields (album, stats, participants)) query
let matches_artists subfields artist query =
matches (QueryArtist.matches_node subfields artist) query
let matches_metadata_subset bmetadata m2 query =
matches (QueryMusic.matches_metadata bmetadata m2) query