-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathstore.go
178 lines (163 loc) · 4.96 KB
/
store.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Package store handlings all the storage in GCSE backend.
package store
import (
"log"
"time"
"github.com/golangplus/bytes"
"github.com/golangplus/errors"
"github.com/daviddengcn/bolthelper"
"github.com/daviddengcn/gcse/configs"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
gpb "github.com/daviddengcn/gcse/shared/proto"
)
var (
// pkgs
// - <site>
// - <path> -> PackageInfo
// persons
// - <site>
// - <id> -> PersonInfo
// history
// - pkgs
// - <site/path> -> HistoryInfo
// - persons
// - <site/id> -> HistoryInfo
// repos
// - <site>
// - <user>
// - <repo> -> Repository
pkgsRoot = []byte("pkgs")
personsRoot = []byte("persons")
historyRoot = []byte("history")
reposRoot = []byte("repos")
)
var box = &bh.RefCountBox{
DataPath: configs.StoreBoltPath,
}
func RepoInfoAge(r *gpb.RepoInfo) time.Duration {
t, _ := ptypes.Timestamp(r.CrawlingTime)
return time.Now().Sub(t)
}
// Returns all the sites one by one by calling the provided func.
func ForEachPackageSite(f func(string) error) error {
return box.View(func(tx bh.Tx) error {
return tx.ForEach([][]byte{pkgsRoot}, func(_ bh.Bucket, k, v bytesp.Slice) error {
if v != nil {
log.Printf("Unexpected value %q for key %q, ignored", string(v), string(k))
return nil
}
return errorsp.WithStacks(f(string(k)))
})
})
}
func ForEachPackageOfSite(site string, f func(string, *gpb.PackageInfo) error) error {
return box.View(func(tx bh.Tx) error {
return tx.ForEach([][]byte{pkgsRoot, []byte(site)}, func(_ bh.Bucket, k, v bytesp.Slice) error {
if v == nil {
log.Printf("Unexpected nil value for key %q, ignored", string(k))
return nil
}
info := &gpb.PackageInfo{}
if err := errorsp.WithStacksAndMessage(proto.Unmarshal(v, info), "Unmarshal %d bytes failed", len(v)); err != nil {
log.Printf("Unmarshal failed: %v, ignored", err)
return nil
}
return errorsp.WithStacks(f(string(k), info))
})
})
}
// Returns an empty (non-nil) PackageInfo if not found.
func ReadPackage(site, path string) (*gpb.PackageInfo, error) {
info := &gpb.PackageInfo{}
if err := box.View(func(tx bh.Tx) error {
return tx.Value([][]byte{pkgsRoot, []byte(site), []byte(path)}, 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.PackageInfo{}
}
return nil
})
}); err != nil {
return nil, err
}
return info, nil
}
func UpdatePackage(site, path string, f func(*gpb.PackageInfo) error) error {
return box.Update(func(tx bh.Tx) error {
b, err := tx.CreateBucketIfNotExists([][]byte{pkgsRoot, []byte(site)})
if err != nil {
return err
}
info := &gpb.PackageInfo{}
if err := b.Value([][]byte{[]byte(path)}, func(bs bytesp.Slice) error {
if err := errorsp.WithStacksAndMessage(proto.Unmarshal(bs, info), "Unmarshal %d bytes", len(bs)); err != nil {
log.Printf("Unmarshaling failed: %v", err)
*info = gpb.PackageInfo{}
}
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(path)}, bs)
})
}
func DeletePackage(site, path string) error {
return box.Update(func(tx bh.Tx) error {
return tx.Delete([][]byte{pkgsRoot, []byte(site), []byte(path)})
})
}
func ReadPerson(site, id string) (*gpb.PersonInfo, error) {
info := &gpb.PersonInfo{}
if err := box.View(func(tx bh.Tx) error {
return tx.Value([][]byte{personsRoot, []byte(site), []byte(id)}, 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.PersonInfo{}
}
return nil
})
}); err != nil {
return nil, err
}
return info, nil
}
func UpdatePerson(site, id string, f func(*gpb.PersonInfo) error) error {
return box.Update(func(tx bh.Tx) error {
b, err := tx.CreateBucketIfNotExists([][]byte{personsRoot, []byte(site)})
if err != nil {
return err
}
info := &gpb.PersonInfo{}
if err := b.Value([][]byte{[]byte(id)}, 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.PersonInfo{}
}
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(id)}, bs)
})
}
func DeletePerson(site, id string) error {
return box.Update(func(tx bh.Tx) error {
return tx.Delete([][]byte{personsRoot, []byte(site), []byte(id)})
})
}