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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
package query
import (
"bytes"
"slices"
"strings"
"sync"
"github.com/jpappel/atlas/pkg/util"
)
type Optimizer struct {
workers uint
root *Clause
isSorted bool // current sort state of statement for all clauses
}
func StatementCmp(a Statement, b Statement) int {
catDiff := int(a.Category - b.Category)
opDiff := int(a.Operator - b.Operator)
negatedDiff := 0
if a.Negated && !b.Negated {
negatedDiff = 1
} else if !a.Negated && b.Negated {
negatedDiff = -1
}
var valDiff int
if a.Value != nil && b.Value != nil {
valDiff = a.Value.Compare(b.Value)
}
return catDiff*100_000 + opDiff*100 + negatedDiff*10 + valDiff
}
func StatementEq(a Statement, b Statement) bool {
a.Simplify()
b.Simplify()
return a.Category == b.Category && a.Operator == b.Operator && a.Negated == b.Negated && a.Value.Compare(b.Value) == 0
}
func NewOptimizer(root *Clause, workers uint) Optimizer {
return Optimizer{
root: root,
workers: workers,
}
}
// Optimize clause according to level.
// level 0 is automatic and levels < 0 do nothing.
func (o Optimizer) Optimize(level int) {
o.Simplify()
if level < 0 {
return
} else if level == 0 {
level = o.root.Depth()
}
oldDepth := o.root.Depth()
for range level {
// clause level parallel
o.Compact()
o.StrictEquality()
o.Tighten()
o.Contradictions()
o.MergeRegex()
o.MergeApproximateMatches()
// parallel + serial
o.Tidy()
// purely serial
o.Flatten()
depth := o.root.Depth()
if depth == oldDepth {
break
} else {
oldDepth = depth
}
}
}
// Perform optimizations in parallel. They should **NOT** mutate the tree
func (o Optimizer) parallel(optimize func(*Clause)) {
jobs := make(chan *Clause, o.workers)
wg := &sync.WaitGroup{}
wg.Add(int(o.workers))
for range o.workers {
go func(jobs <-chan *Clause, wg *sync.WaitGroup) {
for clause := range jobs {
optimize(clause)
}
wg.Done()
}(jobs, wg)
}
for clause := range o.root.DFS() {
jobs <- clause
}
close(jobs)
wg.Wait()
}
// Perform Optimizations serially. Only use this if the tree is being modified.
// When modifying a clause set children that should not be explored to nil
func (o *Optimizer) serial(optimize func(*Clause)) {
stack := make([]*Clause, 0, len(o.root.Clauses))
stack = append(stack, o.root)
for len(stack) != 0 {
top := len(stack) - 1
node := stack[top]
stack = stack[:top]
optimize(node)
node.Clauses = slices.DeleteFunc(node.Clauses, func(child *Clause) bool {
return child == nil
})
stack = append(stack, node.Clauses...)
}
}
func (o *Optimizer) SortStatements() {
o.parallel(func(c *Clause) {
slices.SortFunc(c.Statements, StatementCmp)
})
o.isSorted = true
}
// Simplify all statements
func (o *Optimizer) Simplify() {
o.parallel(func(c *Clause) {
for i := range c.Statements {
(&c.Statements[i]).Simplify()
}
})
}
// Merge child clauses with their parents when applicable
func (o *Optimizer) Flatten() {
o.serial(func(node *Clause) {
// merge if only child clause
if len(node.Statements) == 0 && len(node.Clauses) == 1 {
child := node.Clauses[0]
node.Operator = child.Operator
node.Statements = child.Statements
node.Clauses = child.Clauses
}
// cannot be "modernized", node.Clauses is modified in loop
for i := 0; i < len(node.Clauses); i++ {
child := node.Clauses[i]
isSingleStmt := len(child.Clauses) == 0 && len(child.Statements) == 1
// merge because of commutativity or leaf node with single statement
if node.Operator == child.Operator || isSingleStmt {
node.Statements = append(node.Statements, child.Statements...)
node.Clauses = append(node.Clauses, child.Clauses...)
node.Clauses[i] = nil
}
}
})
}
// Remove multiples of equivalent statements within the same clause
//
// Examples
//
// (and a="Fred Flinstone" a="Fred Flinstone") --> (and a="Fred Flinstone")
// (or a=Shaggy -a!=Shaggy) --> (or a=Shaggy)
func (o *Optimizer) Compact() {
o.parallel(func(c *Clause) {
c.Statements = slices.CompactFunc(c.Statements, StatementEq)
})
o.isSorted = false
}
// Remove noop statements and clauses
func (o *Optimizer) Tidy() {
// ensure ordering
if !o.isSorted {
o.SortStatements()
}
marked := make(map[*Clause]bool, 0)
markedLock := &sync.Mutex{}
// slice away noops
o.parallel(func(c *Clause) {
// PERF: should be benchmarked against binary seach, likely no performance gain
// for typical length of Statements
start := slices.IndexFunc(c.Statements, func(s Statement) bool {
// NOTE: this breaks if valid categories exist between
// CAT_UNKNOWN + CAT_TITLE or after CAT_META
return s.Category > CAT_UNKNOWN && s.Category <= CAT_META
})
// this means no valid categories in statements
if start == -1 {
c.Statements = nil
markedLock.Lock()
marked[c] = true
markedLock.Unlock()
return
}
stop := len(c.Statements)
for i := stop; i > 0; i-- {
// NOTE: this breaks if valid categories exist after CAT_META
if c.Statements[i-1].Category <= CAT_META {
stop = i
break
}
}
c.Statements = c.Statements[start:stop]
})
o.serial(func(c *Clause) {
for i, child := range c.Clauses {
if !marked[child] {
continue
}
if c.Operator == COP_AND {
c.Statements = nil
c.Clauses = nil
break
} else {
c.Clauses[i] = nil
}
}
})
}
func inverseEq(s1, s2 Statement) bool {
s1.Negated = true
return StatementEq(s1, s2)
}
// Replace contradictions with noops
func (o *Optimizer) Contradictions() {
if !o.isSorted {
o.SortStatements()
}
o.parallel(func(c *Clause) {
removals := make(map[int]bool, 8)
var isContradiction func(s1, s2 Statement) bool
for category, stmts := range c.Statements.CategoryPartition() {
if c.Operator == COP_AND && !category.IsSet() {
isContradiction = func(s1, s2 Statement) bool {
return (s1.Operator == OP_EQ && s1.Operator == s2.Operator) || inverseEq(s1, s2)
}
} else {
isContradiction = inverseEq
}
clear(removals)
for i := range stmts {
a := stmts[i]
a.Negated = !a.Negated
for j := i + 1; j < len(stmts); j++ {
b := stmts[j]
if isContradiction(a, b) {
removals[i] = true
removals[j] = true
}
}
}
for idx := range removals {
stmts[idx] = Statement{}
}
if len(removals) > 0 {
o.isSorted = false
}
}
})
}
// Remove fuzzy/range based statements when possible.
// Does not remove contradictions.
//
// Examples:
//
// (and d="May 1, 1886" d>="January 1, 1880") --> (and d="May 1, 1886")
// (and T=notes T:"monday standup") --> (and T=notes)
// (and T="Meeting Notes" T:notes) --> (and T="Meeting Notes")
// (and a="Alonzo Church" a="Alan Turing" a:turing) --> (and a="Alonzo Church" a="Alan Turing")
// (and a="Alonzo Church" a="Alan Turing" a:djikstra) --> (and a="Alonzo Church" a="Alan Turing" a:djikstra)
// (and T=foo T=bar T:foobar) --> (and T=foo T=bar)
func (o Optimizer) StrictEquality() {
if !o.isSorted {
o.SortStatements()
}
o.parallel(func(c *Clause) {
if c.Operator != COP_AND {
return
}
stricts := make([]string, 0)
for category, stmts := range c.Statements.CategoryPartition() {
if category.IsSet() {
clear(stricts)
for i, s := range stmts {
val := strings.ToLower(s.Value.(StringValue).S)
switch s.Operator {
case OP_EQ:
stricts = append(stricts, val)
case OP_AP:
if slices.ContainsFunc(stricts, func(strictStr string) bool {
return util.ContainsSliced(strictStr, val, 1, len(val)-1) || util.ContainsSliced(val, strictStr, 1, len(strictStr)-1)
}) {
stmts[i] = Statement{}
o.isSorted = false
}
}
}
} else {
hasEq := false
for i, s := range stmts {
hasEq = hasEq || (s.Operator == OP_EQ)
if hasEq && s.Operator != OP_EQ {
stmts[i] = Statement{}
o.isSorted = false
}
}
}
}
})
}
// Merge regular expressions within a clause
func (o *Optimizer) MergeRegex() {
if !o.isSorted {
o.SortStatements()
}
pool := &sync.Pool{}
pool.New = func() any {
return &bytes.Buffer{}
}
o.parallel(func(c *Clause) {
if c.Operator != COP_OR {
return
}
buf := pool.Get().(*bytes.Buffer)
defer pool.Put(buf)
defer buf.Reset()
sortChanged := false
for _, catStmts := range c.Statements.CategoryPartition() {
for op, opStmts := range catStmts.OperatorPartition() {
if op != OP_RE {
continue
}
for _, stmts := range opStmts.NegatedPartition() {
if len(stmts) < 2 {
continue
}
sortChanged = true
for i, stmt := range stmts {
if i == 0 {
buf.WriteByte('(')
buf.WriteString(stmt.Value.(StringValue).S)
buf.WriteByte('|')
} else if i == len(stmts)-1 {
buf.WriteString(stmt.Value.(StringValue).S)
buf.WriteByte(')')
stmts[i] = Statement{}
} else {
buf.WriteString(stmt.Value.(StringValue).S)
buf.WriteByte('|')
stmts[i] = Statement{}
}
}
stmts[0].Value = StringValue{S: buf.String()}
buf.Reset()
}
break
}
}
if sortChanged {
o.isSorted = false
}
})
}
func (o *Optimizer) MergeApproximateMatches() {
if !o.isSorted {
o.SortStatements()
}
pool := &sync.Pool{}
pool.New = func() any {
return &strings.Builder{}
}
o.parallel(func(c *Clause) {
var delim string
switch c.Operator {
case COP_AND:
delim = " AND "
case COP_OR:
delim = " OR "
}
b := pool.Get().(*strings.Builder)
defer pool.Put(b)
defer b.Reset()
changeSort := false
for category, catStmts := range c.Statements.CategoryPartition() {
if len(catStmts) < 2 || category.IsOrdered() {
continue
}
for op, opStmts := range catStmts.OperatorPartition() {
if op != OP_AP || len(opStmts) < 2 {
continue
}
changeSort = true
for i, stmt := range opStmts {
b.WriteString(stmt.Value.(StringValue).S)
if i != len(opStmts)-1 {
b.WriteString(delim)
}
if i != 0 {
opStmts[i] = Statement{}
}
}
opStmts[0].Value = StringValue{S: b.String()}
b.Reset()
}
}
if changeSort {
o.isSorted = false
}
})
}
// Shrink approximate statements and ranges
//
// Examples:
//
// (or d>"2025-01-01 d>"2025-02-02") --> (or d>"2025-01-01")
// (and d>"2025-01-01 d>"2025-02-02") --> (and d>"2025-02-02")
// (or T:"Das Kapital I" T:"Das Kapital") --> (and T:"Das Kapital")
// (and T:"Das Kapital I" T:"Das Kapital") --> (and T:"Das Kapital I")
func (o *Optimizer) Tighten() {
if !o.isSorted {
o.SortStatements()
}
o.parallel(func(c *Clause) {
for category, stmts := range c.Statements.CategoryPartition() {
if len(stmts) < 2 {
continue
}
if c.Operator == COP_AND {
if category.IsOrdered() {
minLT, minLE := -1, -1
maxGT, maxGE := -1, -1
for i, s := range stmts {
if s.Operator == OP_LT && minLT == -1 {
minLT = i
} else if s.Operator == OP_LE && minLE == -1 {
minLE = i
} else if s.Operator == OP_GE {
maxGE = i
} else if s.Operator == OP_GT {
maxGT = i
}
}
lowerIdx, upperIdx := -1, -1
if minLT != -1 && minLE != -1 {
ltStmt := stmts[minLT]
leStmt := stmts[minLE]
leDate := leStmt.Value.(DatetimeValue).D
ltDate := ltStmt.Value.(DatetimeValue).D
if ltDate.After(leDate) {
upperIdx = minLE
} else {
upperIdx = minLT
}
} else if minLT != -1 {
upperIdx = minLT
} else if minLE != -1 {
upperIdx = minLE
}
if maxGT != -1 && maxGE != -1 {
gtStmt := stmts[maxGT]
geStmt := stmts[maxGE]
geDate := geStmt.Value.(DatetimeValue).D
gtDate := gtStmt.Value.(DatetimeValue).D
if geDate.After(gtDate) {
lowerIdx = maxGE
} else {
lowerIdx = maxGT
}
} else if maxGT != -1 {
lowerIdx = maxGT
} else if maxGE != -1 {
lowerIdx = maxGE
}
for i, s := range stmts {
if !s.Operator.IsOrder() || i == lowerIdx || i == upperIdx {
continue
}
stmts[i] = Statement{}
}
} else {
removals := make(map[int]bool)
for i, s1 := range util.FilterIter(stmts, func(s Statement) bool { return s.Operator == OP_AP }) {
val1 := strings.ToLower(s1.Value.(StringValue).S)
for j, s2 := range util.FilterIter(stmts[i+1:], func(s Statement) bool { return s.Operator == OP_AP }) {
val2 := strings.ToLower(s2.Value.(StringValue).S)
if util.ContainsSliced(val2, val1, 1, len(val1)-1) {
removals[i] = true
} else if util.ContainsSliced(val1, val2, 1, len(val2)-1) {
removals[j] = true
}
}
}
for idx := range removals {
stmts[idx] = Statement{}
}
if len(removals) > 0 {
o.isSorted = false
}
}
} else {
if category.IsOrdered() {
// NOTE: doesn't handle fuzzy dates
minIdx := slices.IndexFunc(stmts, func(s Statement) bool {
return s.Operator.IsOrder()
})
maxIdx := len(stmts) - 1
for i, s := range slices.Backward(stmts) {
if s.Operator.IsOrder() {
maxIdx = i
break
}
}
if minIdx != -1 {
o.isSorted = false
start, stop := minIdx, maxIdx
if minS := stmts[minIdx]; minS.Operator == OP_GE || minS.Operator == OP_GT {
start++
}
if maxS := stmts[maxIdx]; maxS.Operator == OP_LT || maxS.Operator == OP_LE {
stop--
}
for i := start; i <= stop; i++ {
stmts[i] = Statement{}
}
}
} else {
// NOTE: this has to be all pairs for correctness,
// but it doesn't have to be this sloppy...... :|
removals := make(map[int]bool)
for i, s1 := range util.FilterIter(stmts, func(s Statement) bool { return s.Operator == OP_AP }) {
val1 := strings.ToLower(s1.Value.(StringValue).S)
for j, s2 := range util.FilterIter(stmts[i+1:], func(s Statement) bool { return s.Operator == OP_AP }) {
val2 := strings.ToLower(s2.Value.(StringValue).S)
if util.ContainsSliced(val2, val1, 1, len(val1)-1) {
// NOTE: slicing stmts offsets the all indices by 1, hence the correction
removals[j+1] = true
} else if util.ContainsSliced(val1, val2, 1, len(val2)-1) {
removals[i] = true
}
}
}
for idx := range removals {
stmts[idx] = Statement{}
}
if len(removals) > 0 {
o.isSorted = false
}
}
}
}
})
}
|