File: collections.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (121 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download | duplicates (4)
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
package deb

import (
	"sync"

	"github.com/aptly-dev/aptly/aptly"
	"github.com/aptly-dev/aptly/database"
)

// CollectionFactory is a single place to generate all desired collections
type CollectionFactory struct {
	*sync.Mutex
	db             database.Storage
	packages       *PackageCollection
	remoteRepos    *RemoteRepoCollection
	snapshots      *SnapshotCollection
	localRepos     *LocalRepoCollection
	publishedRepos *PublishedRepoCollection
	checksums      *ChecksumCollection
}

// NewCollectionFactory creates new factory
func NewCollectionFactory(db database.Storage) *CollectionFactory {
	return &CollectionFactory{Mutex: &sync.Mutex{}, db: db}
}

// TemporaryDB creates new temporary DB
//
// DB should be closed/droped after being used
func (factory *CollectionFactory) TemporaryDB() (database.Storage, error) {
	return factory.db.CreateTemporary()
}

// PackageCollection returns (or creates) new PackageCollection
func (factory *CollectionFactory) PackageCollection() *PackageCollection {
	factory.Lock()
	defer factory.Unlock()

	if factory.packages == nil {
		factory.packages = NewPackageCollection(factory.db)
	}

	return factory.packages
}

// RemoteRepoCollection returns (or creates) new RemoteRepoCollection
func (factory *CollectionFactory) RemoteRepoCollection() *RemoteRepoCollection {
	factory.Lock()
	defer factory.Unlock()

	if factory.remoteRepos == nil {
		factory.remoteRepos = NewRemoteRepoCollection(factory.db)
	}

	return factory.remoteRepos
}

// SnapshotCollection returns (or creates) new SnapshotCollection
func (factory *CollectionFactory) SnapshotCollection() *SnapshotCollection {
	factory.Lock()
	defer factory.Unlock()

	if factory.snapshots == nil {
		factory.snapshots = NewSnapshotCollection(factory.db)
	}

	return factory.snapshots
}

// LocalRepoCollection returns (or creates) new LocalRepoCollection
func (factory *CollectionFactory) LocalRepoCollection() *LocalRepoCollection {
	factory.Lock()
	defer factory.Unlock()

	if factory.localRepos == nil {
		factory.localRepos = NewLocalRepoCollection(factory.db)
	}

	return factory.localRepos
}

// PublishedRepoCollection returns (or creates) new PublishedRepoCollection
func (factory *CollectionFactory) PublishedRepoCollection() *PublishedRepoCollection {
	factory.Lock()
	defer factory.Unlock()

	if factory.publishedRepos == nil {
		factory.publishedRepos = NewPublishedRepoCollection(factory.db)
	}

	return factory.publishedRepos
}

// ChecksumCollection returns (or creates) new ChecksumCollection
func (factory *CollectionFactory) ChecksumCollection(db database.ReaderWriter) aptly.ChecksumStorage {
	factory.Lock()
	defer factory.Unlock()

	if db != nil {
		return NewChecksumCollection(db)
	}

	if factory.checksums == nil {
		factory.checksums = NewChecksumCollection(factory.db)
	}

	return factory.checksums
}

// Flush removes all references to collections, so that memory could be reclaimed
func (factory *CollectionFactory) Flush() {
	factory.Lock()
	defer factory.Unlock()

	factory.localRepos = nil
	factory.snapshots = nil
	factory.remoteRepos = nil
	factory.publishedRepos = nil
	factory.packages = nil
	factory.checksums = nil
}