This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
compiled_package_repo.go
79 lines (63 loc) · 2.03 KB
/
compiled_package_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
package pkg
import (
"fmt"
"sort"
"strings"
biindex "github.com/cloudfoundry/bosh-init/index"
birelpkg "github.com/cloudfoundry/bosh-init/release/pkg"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type CompiledPackageRecord struct {
BlobID string
BlobSHA1 string
}
type CompiledPackageRepo interface {
Save(birelpkg.Package, CompiledPackageRecord) error
Find(birelpkg.Package) (CompiledPackageRecord, bool, error)
}
type compiledPackageRepo struct {
index biindex.Index
}
func NewCompiledPackageRepo(index biindex.Index) CompiledPackageRepo {
return &compiledPackageRepo{index: index}
}
func (cpr *compiledPackageRepo) Save(pkg birelpkg.Package, record CompiledPackageRecord) error {
err := cpr.index.Save(cpr.pkgKey(pkg), record)
if err != nil {
return bosherr.WrapError(err, "Saving compiled package")
}
return nil
}
func (cpr *compiledPackageRepo) Find(pkg birelpkg.Package) (CompiledPackageRecord, bool, error) {
var record CompiledPackageRecord
err := cpr.index.Find(cpr.pkgKey(pkg), &record)
if err != nil {
if err == biindex.ErrNotFound {
return record, false, nil
}
return record, false, bosherr.WrapError(err, "Finding compiled package")
}
return record, true, nil
}
type packageToCompiledPackageKey struct {
PackageName string
// Fingerprint of a package captures the sorted names of its dependencies
// (but not the dependencies' fingerprints)
PackageFingerprint string
DependencyKey string
}
func (cpr compiledPackageRepo) pkgKey(pkg birelpkg.Package) packageToCompiledPackageKey {
return packageToCompiledPackageKey{
PackageName: pkg.Name,
PackageFingerprint: pkg.Fingerprint,
DependencyKey: cpr.convertToDependencyKey(ResolveDependencies(&pkg)),
}
}
func (cpr compiledPackageRepo) convertToDependencyKey(packages []*birelpkg.Package) string {
dependencyKeys := []string{}
for _, pkg := range packages {
dependencyKeys = append(dependencyKeys, fmt.Sprintf("%s:%s", pkg.Name, pkg.Fingerprint))
}
sort.Strings(dependencyKeys)
return strings.Join(dependencyKeys, ",")
}