-
Notifications
You must be signed in to change notification settings - Fork 2
/
meta.go
101 lines (92 loc) · 2.59 KB
/
meta.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
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
package bqwt
import (
"sort"
"strings"
"time"
)
type Meta struct {
URL string
DatasetID string
Tables []*WindowedTable
Expression string `description:"represents recently changed tables ranged decorator relative expression (without project id)"`
AbsoluteExpression string `description:"represents recently changed tables ranged decorator absolute expression (with project id)"`
indexTables map[string]*WindowedTable
isTemp bool
}
func (m *Meta) ResetChangeFlag() {
for _, table := range m.Tables {
table.Changed = false
}
}
func (m *Meta) Match(matchExpressions []string) []*WindowedTable {
if len(matchExpressions) == 0 {
return m.Tables
}
var result = make([]*WindowedTable, 0)
for _, candidate := range m.Tables {
for _, expression := range matchExpressions {
if strings.Contains(candidate.Name, expression) {
result = append(result, candidate)
break
}
}
}
return result
}
func (m *Meta) SortLastModifiedDesc() {
if len(m.Tables) > 0 {
sort.Slice(m.Tables, func(i, j int) bool {
return m.Tables[i].Window.To.Unix() > m.Tables[j].Window.To.Unix()
})
}
}
//Update updates table info
func (m *Meta) Update(table *TableInfo, currentTime time.Time) *WindowedTable {
if len(m.indexTables) == 0 {
m.indexTables = make(map[string]*WindowedTable)
for _, table := range m.Tables {
m.indexTables[table.Name] = table
}
}
windowed, has := m.indexTables[table.TableID]
if !has {
windowed = NewWindowedTable(table, currentTime)
m.indexTables[windowed.Name] = windowed
m.Tables = append(m.Tables, windowed)
return windowed
}
if windowed.Window.To.Equal(table.LastModified) {
windowed.Changed = false
return windowed
}
nextBoundary := windowed.Window.To.Add(time.Millisecond)
windowed.Window.From = nextBoundary
windowed.Window.To = table.LastModified
windowed.LastChanged = currentTime
windowed.Expression = windowed.FormatExpr()
windowed.AbsoluteExpression = windowed.FormatAbsoluteExpr()
windowed.Changed = true
return windowed
}
//Prune removes windowed table info that have not been update since: now - threshold
func (m *Meta) Prune(threshold time.Duration, now time.Time) {
if threshold == 0 {
return
}
var tables = make([]*WindowedTable, 0)
for _, candidate := range m.Tables {
if now.Sub(candidate.Window.To) > threshold {
continue
}
tables = append(tables, candidate)
}
m.Tables = tables
}
//NewMeta creates a new window table meta instance
func NewMeta(URL, datasetID string) *Meta {
return &Meta{
URL: URL,
DatasetID: datasetID,
Tables: make([]*WindowedTable, 0),
}
}