-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathrepo.go
98 lines (89 loc) · 3.01 KB
/
repo.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
package store
import (
"log"
"github.com/golang/protobuf/proto"
"github.com/golangplus/bytes"
"github.com/golangplus/errors"
"github.com/daviddengcn/bolthelper"
gpb "github.com/daviddengcn/gcse/shared/proto"
)
// Returns an empty (non-nil) PackageInfo if not found.
func ReadRepository(site, user, repo string) (*gpb.Repository, error) {
doc := &gpb.Repository{}
if err := box.View(func(tx bh.Tx) error {
return tx.Value([][]byte{reposRoot, []byte(site), []byte(user), []byte(repo)}, func(bs bytesp.Slice) error {
if err := errorsp.WithStacksAndMessage(proto.Unmarshal(bs, doc), "Unmarshal %d bytes failed", len(bs)); err != nil {
log.Printf("Unmarshal failed: %v", err)
*doc = gpb.Repository{}
}
return nil
})
}); err != nil {
return nil, err
}
return doc, nil
}
func UpdateRepository(site, user, repo string, f func(doc *gpb.Repository) error) error {
return box.Update(func(tx bh.Tx) error {
b, err := tx.CreateBucketIfNotExists([][]byte{reposRoot, []byte(site), []byte(user)})
if err != nil {
return err
}
doc := &gpb.Repository{}
if err := b.Value([][]byte{[]byte(repo)}, func(bs bytesp.Slice) error {
if err := errorsp.WithStacksAndMessage(proto.Unmarshal(bs, doc), "Unmarshal %d bytes", len(bs)); err != nil {
log.Printf("Unmarshaling failed: %v", err)
*doc = gpb.Repository{}
}
return nil
}); err != nil {
return err
}
if err := errorsp.WithStacks(f(doc)); err != nil {
return err
}
bs, err := proto.Marshal(doc)
if err != nil {
return errorsp.WithStacksAndMessage(err, "marshaling %v failed: %v", doc, err)
}
return b.Put([][]byte{[]byte(repo)}, bs)
})
}
func DeleteRepository(site, user, repo string) error {
return box.Update(func(tx bh.Tx) error {
return tx.Delete([][]byte{reposRoot, []byte(site), []byte(user), []byte(repo)})
})
}
func ForEachRepositorySite(f func(string) error) error {
return box.View(func(tx bh.Tx) error {
return tx.ForEach([][]byte{reposRoot}, 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 ForEachRepositoryOfSite(site string, f func(user, name string, doc *gpb.Repository) error) error {
return box.View(func(tx bh.Tx) error {
return tx.ForEach([][]byte{reposRoot, []byte(site)}, func(b bh.Bucket, user, v bytesp.Slice) error {
if v != nil {
log.Printf("Unexpected value %q for key %q, ignored", string(v), string(user))
return nil
}
return b.ForEach([][]byte{user}, func(name, bs bytesp.Slice) error {
if bs == nil {
log.Printf("Unexpected nil value for key %q, ignored", string(name))
return nil
}
doc := &gpb.Repository{}
if err := errorsp.WithStacksAndMessage(proto.Unmarshal(bs, doc), "Unmarshal %d bytes", len(bs)); err != nil {
log.Printf("Unmarshaling value for %v failed, ignored: %v", name, err)
return nil
}
return errorsp.WithStacks(f(string(user), string(name), doc))
})
})
})
}