-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathhistory.go
136 lines (118 loc) · 3.75 KB
/
history.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package store
import (
"log"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golangplus/bytes"
"github.com/golangplus/errors"
"github.com/daviddengcn/bolthelper"
gpb "github.com/daviddengcn/gcse/shared/proto"
)
func SaveSnapshot(path string) error {
return box.Update(func(tx bh.Tx) error {
return tx.CopyFile(path, 0644)
})
}
const (
maxHistoryEvents = 10
)
func readHistoryOf(box *bh.RefCountBox, root []byte, site, idOrPath string) (*gpb.HistoryInfo, error) {
info := &gpb.HistoryInfo{}
if err := box.View(func(tx bh.Tx) error {
return tx.Value([][]byte{historyRoot, root, []byte(site), []byte(idOrPath)}, func(bs bytesp.Slice) error {
if err := errorsp.WithStacksAndMessage(proto.Unmarshal(bs, info), "Unmarshal %d bytes failed", len(bs)); err != nil {
log.Printf("Unmarshal failed: %v", err)
*info = gpb.HistoryInfo{}
}
return nil
})
}); err != nil {
return nil, err
}
return info, nil
}
func readHistory(root []byte, site, idOrPath string) (*gpb.HistoryInfo, error) {
return readHistoryOf(box, root, site, idOrPath)
}
func ReadPackageHistory(site, path string) (*gpb.HistoryInfo, error) {
return readHistory(pkgsRoot, site, path)
}
func ReadPackageHistoryOf(box *bh.RefCountBox, site, path string) (*gpb.HistoryInfo, error) {
return readHistoryOf(box, pkgsRoot, site, path)
}
func ReadPersonHistory(site, path string) (*gpb.HistoryInfo, error) {
return readHistory(personsRoot, site, path)
}
func updateHistory(root []byte, site, idOrPath string, f func(*gpb.HistoryInfo) error) error {
return box.Update(func(tx bh.Tx) error {
b, err := tx.CreateBucketIfNotExists([][]byte{historyRoot, root, []byte(site)})
if err != nil {
return err
}
info := &gpb.HistoryInfo{}
if err := b.Value([][]byte{[]byte(idOrPath)}, func(bs bytesp.Slice) error {
err := errorsp.WithStacksAndMessage(proto.Unmarshal(bs, info), "Unmarshal %d bytes", len(bs))
if err != nil {
log.Printf("Unmarshaling failed: %v", err)
*info = gpb.HistoryInfo{}
}
return nil
}); err != nil {
return err
}
if err := errorsp.WithStacks(f(info)); err != nil {
return err
}
bs, err := proto.Marshal(info)
if err != nil {
return errorsp.WithStacksAndMessage(err, "marshaling %v failed: %v", info, err)
}
return b.Put([][]byte{[]byte(idOrPath)}, bs)
})
}
func UpdatePackageHistory(site, path string, f func(*gpb.HistoryInfo) error) error {
return updateHistory(pkgsRoot, site, path, f)
}
func AppendPackageEvent(site, path, foundWay string, t time.Time, a gpb.HistoryEvent_Action_Enum) error {
return UpdatePackageHistory(site, path, func(hi *gpb.HistoryInfo) error {
if hi.FoundTime == nil {
// The first time the package was found
hi.FoundTime, _ = ptypes.TimestampProto(t)
hi.FoundWay = foundWay
}
if a == gpb.HistoryEvent_Action_None {
return nil
}
// Insert the event
tsp, _ := ptypes.TimestampProto(t)
hi.Events = append([]*gpb.HistoryEvent{{
Action: a,
Timestamp: tsp,
}}, hi.Events...)
if len(hi.Events) > maxHistoryEvents {
hi.Events = hi.Events[:maxHistoryEvents]
}
switch a {
case gpb.HistoryEvent_Action_Success:
hi.LatestSuccess = tsp
case gpb.HistoryEvent_Action_Failed:
hi.LatestFailed = tsp
}
return nil
})
}
func UpdatePersonHistory(site, path string, f func(*gpb.HistoryInfo) error) error {
return updateHistory(personsRoot, site, path, f)
}
func deleteHistory(root []byte, site, idOrPath string) error {
return box.Update(func(tx bh.Tx) error {
return tx.Delete([][]byte{historyRoot, root, []byte(site), []byte(idOrPath)})
})
}
func DeletePackageHistory(site, path string) error {
return deleteHistory(pkgsRoot, site, path)
}
func DeletePersonHistory(site, path string) error {
return deleteHistory(personsRoot, site, path)
}