-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.go
63 lines (60 loc) · 1.56 KB
/
index.go
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
package main
import (
"fmt"
"log"
)
func index(store Store, filenames []string) error {
var didWork bool
store.Begin()
aggregator := NewDNSAggregator()
var emptyStoreResult UpdateResult
aggMap := make(map[string]aggregationResult)
for _, fn := range filenames {
indexed, err := store.IsLogIndexed(fn)
if err != nil {
return fmt.Errorf("store.IsLogIndexed: %w", err)
}
if indexed {
log.Printf("%s: Already indexed", fn)
continue
}
fileAgg := NewDNSAggregator()
err = aggregate(fileAgg, fn)
if err != nil {
return fmt.Errorf("Error Aggregating %s: %w", fn, err)
}
aggregator.Merge(fileAgg)
aggregated := fileAgg.GetResult()
log.Printf("%s: Aggregation: Duration=%0.1f TotalRecords=%d SkippedRecords=%d Tuples=%d Individual=%d",
fn,
aggregated.Duration.Seconds(),
aggregated.TotalRecords,
aggregated.SkippedRecords,
aggregated.TuplesLen,
aggregated.IndividualLen,
)
aggMap[fn] = aggregated.ShallowCopy()
didWork = true
}
if !didWork {
return nil
//TODO: rollback transaction
}
aggregated := aggregator.GetResult()
result, err := store.Update(aggregated)
if err != nil {
return fmt.Errorf("store.Update: %w", err)
}
log.Printf("batch: Store: Duration=%0.1f Inserted=%d Updated=%d", result.Duration.Seconds(), result.Inserted, result.Updated)
for fn, aggregated := range aggMap {
err = store.SetLogIndexed(fn, aggregated, emptyStoreResult)
if err != nil {
return fmt.Errorf("store.SetLogIndexed: %w", err)
}
}
err = store.Commit()
if err != nil {
return fmt.Errorf("store.Commit: %w", err)
}
return nil
}