/
library.go
365 lines (298 loc) · 8.23 KB
/
library.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package siva
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
borges "github.com/src-d/go-borges"
"github.com/src-d/go-borges/util"
billy "gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/osfs"
butil "gopkg.in/src-d/go-billy.v4/util"
errors "gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
)
// ErrLocationExists when the location to be created already exists.
var ErrLocationExists = errors.NewKind("location %s already exists")
// Library represents a borges.Library implementation based on siva files.
type Library struct {
id borges.LibraryID
fs billy.Filesystem
tmp billy.Filesystem
locReg *locationRegistry
locMu sync.Mutex
options *LibraryOptions
metadata *libMetadata
}
// LibraryOptions hold configuration options for the library.
type LibraryOptions struct {
// Transactional enables transactions for repository writes.
Transactional bool
// TransactionTimeout is the time it will wait while another transaction
// is being done before error. 0 means default.
TransactionTimeout time.Duration
// Timeout set a timeout for library operations. Some operations could
// potentially take long so timing out them will make an error be
// returned. A 0 value sets a default value of 20 seconds.
Timeout time.Duration
// RegistryCache is the maximum number of locations in the cache. A value
// of 0 will be set a default value of 10000.
RegistryCache int
// TempFS is the temporary filesystem to do transactions and write files.
TempFS billy.Filesystem
// Bucket level to use to search and create siva files.
Bucket int
// RootedRepo makes the repository show only the references for the remote
// named with the repository ID.
RootedRepo bool
// Cache specifies the shared cache used in repositories. If not defined
// a new default cache will be created for each repository.
Cache cache.Object
// Performance enables performance options in read only git repositories
// (ExclusiveAccess and KeepDescriptors).
Performance bool
// MetadataReadOnly doesn't create or modify metadata for the library.
MetadataReadOnly bool
}
var _ borges.Library = (*Library)(nil)
const (
timeout = 20 * time.Second
txTimeout = 60 * time.Second
registryCacheSize = 10000
)
// NewLibrary creates a new siva.Library. When is not in MetadataReadOnly it
// will generate an id if not provided the first time the metadata is created.
func NewLibrary(
id string,
fs billy.Filesystem,
options *LibraryOptions,
) (*Library, error) {
var ops *LibraryOptions
if options == nil {
ops = &LibraryOptions{}
} else {
ops = &(*options)
}
var (
metadata *libMetadata
err error
)
if ops.MetadataReadOnly {
metadata, err = loadLibraryMetadata(fs)
} else {
metadata, err = loadOrCreateLibraryMetadata(id, fs)
}
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if metadata != nil && id == "" {
id = metadata.ID
}
if ops.RegistryCache <= 0 {
ops.RegistryCache = registryCacheSize
}
lr, err := newLocationRegistry(ops.RegistryCache)
if err != nil {
return nil, err
}
if ops.TransactionTimeout == 0 {
ops.TransactionTimeout = txTimeout
}
if ops.Timeout == 0 {
ops.Timeout = timeout
}
tmp := ops.TempFS
if tmp == nil {
dir, err := ioutil.TempDir("", "go-borges")
if err != nil {
return nil, err
}
tmp = osfs.New(dir)
}
return &Library{
id: borges.LibraryID(id),
fs: fs,
tmp: tmp,
locReg: lr,
options: ops,
metadata: metadata,
}, nil
}
// ID implements borges.Library interface.
func (l *Library) ID() borges.LibraryID {
return l.id
}
// Init implements borges.Library interface.
func (l *Library) Init(borges.RepositoryID) (borges.Repository, error) {
return nil, borges.ErrNotImplemented.New()
}
// Get implements borges.Library interface.
func (l *Library) Get(repoID borges.RepositoryID, mode borges.Mode) (borges.Repository, error) {
ok, _, locID, err := l.Has(repoID)
if err != nil {
return nil, err
}
if !ok {
return nil, borges.ErrRepositoryNotExists.New(repoID)
}
loc, err := l.Location(locID)
if err != nil {
return nil, err
}
return loc.Get(repoID, mode)
}
// GetOrInit implements borges.Library interface.
func (l *Library) GetOrInit(borges.RepositoryID) (borges.Repository, error) {
return nil, borges.ErrNotImplemented.New()
}
func toRepoID(endpoint string) borges.RepositoryID {
name, _ := borges.NewRepositoryID(endpoint)
return borges.RepositoryID(strings.TrimSuffix(name.String(), ".git"))
}
func toLocID(file string) borges.LocationID {
id := strings.TrimSuffix(file, ".siva")
return borges.LocationID(id)
}
// Has implements borges.Library interface.
func (l *Library) Has(name borges.RepositoryID) (bool, borges.LibraryID, borges.LocationID, error) {
ctx, cancel := context.WithTimeout(context.Background(), l.options.Timeout)
defer cancel()
locs, err := l.locations(ctx)
if err != nil {
return false, "", "", err
}
it := util.NewLocationIterator(locs)
defer it.Close()
for {
location, err := it.Next()
if err == io.EOF {
return false, "", "", nil
}
if err != nil {
return false, "", "", err
}
loc, _ := location.(*Location)
has, err := loc.has(ctx, name)
if err != nil {
return false, "", "", err
}
if has {
return true, l.id, loc.ID(), nil
}
}
}
// Repositories implements borges.Library interface.
func (l *Library) Repositories(mode borges.Mode) (borges.RepositoryIterator, error) {
ctx, cancel := context.WithTimeout(context.Background(), l.options.Timeout)
defer cancel()
locs, err := l.locations(ctx)
if err != nil {
return nil, err
}
return util.NewLocationRepositoryIterator(locs, mode), nil
}
// Location implements borges.Library interface.
func (l *Library) Location(id borges.LocationID) (borges.Location, error) {
return l.location(id, false)
}
// AddLocation creates a new borges.Location if it does not exist.
func (l *Library) AddLocation(id borges.LocationID) (borges.Location, error) {
l.locMu.Lock()
defer l.locMu.Unlock()
_, err := l.Location(id)
if err == nil {
return nil, ErrLocationExists.New(id)
}
return l.location(id, true)
}
func (l *Library) location(id borges.LocationID, create bool) (borges.Location, error) {
if loc, ok := l.locReg.Get(id); ok {
return loc, nil
}
path := buildSivaPath(id, l.options.Bucket)
loc, err := newLocation(id, l, path, create)
if err != nil {
return nil, err
}
l.locReg.Add(loc)
return loc, nil
}
func buildSivaPath(id borges.LocationID, bucket int) string {
return buildPath(id, bucket, ".siva")
}
func buildSivaMetadataPath(id borges.LocationID, bucket int) string {
return buildPath(id, bucket, locMetadataFileExt)
}
func buildPath(id borges.LocationID, bucket int, suffix string) string {
name := string(id) + suffix
if bucket == 0 {
return name
}
r := []rune(id)
var bucketDir string
if len(r) < bucket {
bucketDir = string(id) + strings.Repeat("-", bucket-len(r))
} else {
bucketDir = string(r[:bucket])
}
return filepath.Join(bucketDir, name)
}
// Locations implements borges.Library interface.
func (l *Library) Locations() (borges.LocationIterator, error) {
ctx, cancel := context.WithTimeout(context.Background(), l.options.Timeout)
defer cancel()
locs, err := l.locations(ctx)
if err != nil {
return nil, err
}
return util.NewLocationIterator(locs), nil
}
func (l *Library) locations(ctx context.Context) ([]borges.Location, error) {
var locs []borges.Location
pattern := filepath.Join(
strings.Repeat("?", l.options.Bucket),
"*.siva",
)
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
sivas, err := butil.Glob(l.fs, pattern)
if err != nil {
return nil, err
}
for _, s := range sivas {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
siva := filepath.Base(s)
loc, err := l.Location(toLocID(siva))
if err != nil {
continue
}
locs = append(locs, loc)
}
return locs, nil
}
// Version returns version stored in metadata or -1 if not defined.
func (l *Library) Version() (int, error) {
if l.metadata != nil {
return l.metadata.version()
}
return -1, nil
}
// SetVersion sets the current version to the given number.
func (l *Library) SetVersion(n int) error {
if l.metadata == nil {
return nil
}
l.metadata.setVersion(n)
return l.metadata.save()
}