forked from carl-mastrangelo/pixur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_custom_data_task.go
80 lines (68 loc) · 1.54 KB
/
find_custom_data_task.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
package tasks
import (
"context"
"time"
"pixur.org/pixur/be/schema"
"pixur.org/pixur/be/schema/db"
tab "pixur.org/pixur/be/schema/tables"
"pixur.org/pixur/be/status"
)
type FindCustomDataTask struct {
Beg tab.JobBeginner
Now func() time.Time
KeyType int64
KeyPrefix []int64
Capability []schema.User_Capability
Data []*schema.CustomData
}
func (t *FindCustomDataTask) Run(ctx context.Context) (stscap status.S) {
now := t.Now()
j, u, sts := authedJob(ctx, t.Beg, now)
if sts != nil {
return sts
}
defer revert(j, &stscap)
conf, sts := GetConfiguration(ctx)
if sts != nil {
return sts
}
if sts := validateCapability(u, conf, t.Capability...); sts != nil {
return sts
}
data, sts := findCustomData(j, db.LockNone, t.KeyType, t.KeyPrefix...)
if sts != nil {
return sts
}
t.Data = data
return nil
}
func findCustomData(j *tab.Job, lock db.Lock, keyType int64, keyParts ...int64) (
[]*schema.CustomData, status.S) {
prefix := tab.CustomDataPrimary{KeyType: &keyType}
if len(keyParts) > 0 {
prefix.Key1 = &keyParts[0]
}
if len(keyParts) > 1 {
prefix.Key2 = &keyParts[1]
}
if len(keyParts) > 2 {
prefix.Key3 = &keyParts[2]
}
if len(keyParts) > 3 {
prefix.Key4 = &keyParts[3]
}
if len(keyParts) > 4 {
prefix.Key5 = &keyParts[4]
}
if len(keyParts) > 5 {
return nil, status.Internal(nil, "bad number of keyparts", len(keyParts))
}
cds, err := j.FindCustomData(db.Opts{
Lock: lock,
Prefix: prefix,
})
if err != nil {
return nil, status.Internal(err, "can't find custom data")
}
return cds, nil
}