-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
115 lines (91 loc) · 2.52 KB
/
common.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package mapper
import (
"strings"
"time"
"google.golang.org/appengine/datastore"
)
type (
// common contains properties that are common across all
// mapper entities (job, iterator, namespace and shard)
common struct {
// Counters holds the task counters map
Counters Counters `datastore:"-"`
// Query is the datastore query spec
Query *Query `datastore:"-"`
// Active indicates if this task is still active
Active bool `datastore:"active,noindex"`
// Count is the number of records processed
Count int64 `datastore:"count,noindex"`
// Started is when the task began
Started time.Time `datastore:"started"`
// Updated is when the task was last updated
Updated time.Time `datastore:"updated"`
// ProcessTime is the time that the task spent executing
ProcessTime time.Duration `datastore:"process_time,noindex"`
// WallTime is the wall-time that the task takes
WallTime time.Duration `datastore:"wall_time,noindex"`
// private fields used by local instance
id string
job *job
startTime time.Time
}
)
func (c *common) getCommon() *common {
return c
}
func (c *common) start(query *Query) {
c.Active = true
c.Counters = NewCounters()
c.Query = query
c.Count = 0
c.Started = getTime()
c.Updated = c.Started
c.startTime = c.Started
}
func (c *common) complete() {
c.Active = false
c.Updated = getTime()
c.WallTime = c.Updated.Sub(c.Started)
}
func (c *common) rollup(r common) {
c.Count += r.Count
c.ProcessTime += r.ProcessTime
c.Counters.Add(r.Counters)
}
/* datastore */
func (c *common) Load(props []datastore.Property) error {
datastore.LoadStruct(c, props)
c.Counters = make(map[string]int64)
for _, prop := range props {
switch prop.Name {
case "query":
c.Query = &Query{}
if err := c.Query.GobDecode(prop.Value.([]byte)); err != nil {
return err
}
default:
if strings.HasPrefix(prop.Name, "counters.") {
key := prop.Name[9:len(prop.Name)]
c.Counters[key] = prop.Value.(int64)
}
}
}
c.startTime = getTime()
return nil
}
func (c *common) Save() ([]datastore.Property, error) {
c.ProcessTime += getTime().Sub(c.startTime)
props, err := datastore.SaveStruct(c)
if err != nil {
return nil, err
}
for key, value := range c.Counters {
props = append(props, datastore.Property{Name: "counters." + key, Value: value, NoIndex: true, Multiple: false})
}
b, err := c.Query.GobEncode()
if err != nil {
return nil, err
}
props = append(props, datastore.Property{Name: "query", Value: b, NoIndex: true, Multiple: false})
return props, nil
}