-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.go
62 lines (53 loc) · 1.21 KB
/
utils.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
package archives
import (
"context"
"fmt"
"time"
"github.com/jmoiron/sqlx"
)
// helper method to safely execute an IN query in the passed in transaction
func executeInQuery(ctx context.Context, tx *sqlx.Tx, query string, ids []int64) error {
q, vs, err := sqlx.In(query, ids)
if err != nil {
return err
}
q = tx.Rebind(q)
_, err = tx.ExecContext(ctx, q, vs...)
if err != nil {
tx.Rollback()
}
return err
}
// counts the records in the given archives
func countRecords(as []*Archive) int {
n := 0
for _, a := range as {
n += a.RecordCount
}
return n
}
// removes duplicates from a slice of archives
func removeDuplicates(as []*Archive) []*Archive {
unique := make([]*Archive, 0, len(as))
seen := make(map[string]bool)
for _, a := range as {
key := fmt.Sprintf("%s:%s:%s", a.ArchiveType, a.Period, a.StartDate.Format(time.RFC3339))
if !seen[key] {
unique = append(unique, a)
seen[key] = true
}
}
return unique
}
// chunks a slice of in64 IDs
func chunkIDs(ids []int64, size int) [][]int64 {
chunks := make([][]int64, 0, len(ids)/size+1)
for i := 0; i < len(ids); i += size {
end := i + size
if end > len(ids) {
end = len(ids)
}
chunks = append(chunks, ids[i:end])
}
return chunks
}