forked from splitsh/lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
174 lines (147 loc) · 3.27 KB
/
cache.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
package splitter
import (
"crypto/sha1"
"fmt"
"io"
"path/filepath"
"time"
"github.com/boltdb/bolt"
"github.com/libgit2/git2go"
)
type cache struct {
key []byte
branch string
db *bolt.DB
}
func newCache(branch string, config *Config) (*cache, error) {
var err error
db := config.DB
if db == nil {
db, err = bolt.Open(filepath.Join(GitDirectory(config.Path), "splitsh.db"), 0644, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
return nil, err
}
}
c := &cache{
db: db,
branch: branch,
key: key(config),
}
err = db.Update(func(tx *bolt.Tx) error {
_, err1 := tx.CreateBucketIfNotExists(c.key)
return err1
})
if err != nil {
return nil, fmt.Errorf("Impossible to create bucket: %s", err)
}
return c, nil
}
func (c *cache) close() error {
err := c.db.Close()
if err != nil {
return err
}
return nil
}
func key(config *Config) []byte {
h := sha1.New()
if config.Commit != "" {
io.WriteString(h, config.Commit)
} else {
// value does not matter, should just be always the same
io.WriteString(h, "oldest")
}
if config.Legacy {
io.WriteString(h, "legacy")
}
for _, prefix := range config.Prefixes {
io.WriteString(h, prefix.From)
io.WriteString(h, prefix.To)
}
return h.Sum(nil)
}
func (c *cache) setHead(head *git.Oid) error {
return c.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(c.key).Put([]byte("head/"+c.branch), head[0:20])
})
}
func (c *cache) getHead() *git.Oid {
var oid *git.Oid
c.db.View(func(tx *bolt.Tx) error {
result := tx.Bucket(c.key).Get([]byte("head/" + c.branch))
if result != nil {
oid = git.NewOidFromBytes(result)
}
return nil
})
return oid
}
// which is newest or oldest
func (c *cache) reverse(rev *git.Oid, which string) *git.Oid {
var oid *git.Oid
c.db.View(func(tx *bolt.Tx) error {
result := tx.Bucket(c.key).Get(append(rev[0:20], []byte("/"+which)...))
if result == nil && which == "newest" {
result = tx.Bucket(c.key).Get(append(rev[0:20], []byte("/oldest")...))
}
if result != nil {
oid = git.NewOidFromBytes(result)
}
return nil
})
return oid
}
func (c *cache) get(rev *git.Oid) *git.Oid {
var oid *git.Oid
c.db.View(func(tx *bolt.Tx) error {
result := tx.Bucket(c.key).Get(rev[0:20])
if result != nil {
oid = git.NewOidFromBytes(result)
}
return nil
})
return oid
}
func (c *cache) set(rev, newrev *git.Oid, created bool) error {
return c.db.Update(func(tx *bolt.Tx) error {
err := tx.Bucket(c.key).Put(rev[0:20], newrev[0:20])
if err != nil {
return err
}
postfix := "/newest"
if created {
postfix = "/oldest"
}
key := append(newrev[0:20], []byte(postfix)...)
return tx.Bucket(c.key).Put(key, rev[0:20])
})
}
func (c *cache) gets(commits []*git.Oid) []*git.Oid {
var oids []*git.Oid
c.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(c.key)
for _, commit := range commits {
result := b.Get(commit[0:20])
if result != nil {
oids = append(oids, git.NewOidFromBytes(result))
}
}
return nil
})
return oids
}
func (c *cache) flush() error {
return c.db.Update(func(tx *bolt.Tx) error {
if tx.Bucket(c.key) != nil {
err := tx.DeleteBucket(c.key)
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists(c.key)
if err != nil {
return err
}
}
return nil
})
}