1package deb 2 3import ( 4 "sync" 5 6 "github.com/aptly-dev/aptly/database" 7) 8 9// CollectionFactory is a single place to generate all desired collections 10type CollectionFactory struct { 11 *sync.Mutex 12 db database.Storage 13 packages *PackageCollection 14 remoteRepos *RemoteRepoCollection 15 snapshots *SnapshotCollection 16 localRepos *LocalRepoCollection 17 publishedRepos *PublishedRepoCollection 18 checksums *ChecksumCollection 19} 20 21// NewCollectionFactory creates new factory 22func NewCollectionFactory(db database.Storage) *CollectionFactory { 23 return &CollectionFactory{Mutex: &sync.Mutex{}, db: db} 24} 25 26// TemporaryDB creates new temporary DB 27// 28// DB should be closed/droped after being used 29func (factory *CollectionFactory) TemporaryDB() (database.Storage, error) { 30 return factory.db.CreateTemporary() 31} 32 33// PackageCollection returns (or creates) new PackageCollection 34func (factory *CollectionFactory) PackageCollection() *PackageCollection { 35 factory.Lock() 36 defer factory.Unlock() 37 38 if factory.packages == nil { 39 factory.packages = NewPackageCollection(factory.db) 40 } 41 42 return factory.packages 43} 44 45// RemoteRepoCollection returns (or creates) new RemoteRepoCollection 46func (factory *CollectionFactory) RemoteRepoCollection() *RemoteRepoCollection { 47 factory.Lock() 48 defer factory.Unlock() 49 50 if factory.remoteRepos == nil { 51 factory.remoteRepos = NewRemoteRepoCollection(factory.db) 52 } 53 54 return factory.remoteRepos 55} 56 57// SnapshotCollection returns (or creates) new SnapshotCollection 58func (factory *CollectionFactory) SnapshotCollection() *SnapshotCollection { 59 factory.Lock() 60 defer factory.Unlock() 61 62 if factory.snapshots == nil { 63 factory.snapshots = NewSnapshotCollection(factory.db) 64 } 65 66 return factory.snapshots 67} 68 69// LocalRepoCollection returns (or creates) new LocalRepoCollection 70func (factory *CollectionFactory) LocalRepoCollection() *LocalRepoCollection { 71 factory.Lock() 72 defer factory.Unlock() 73 74 if factory.localRepos == nil { 75 factory.localRepos = NewLocalRepoCollection(factory.db) 76 } 77 78 return factory.localRepos 79} 80 81// PublishedRepoCollection returns (or creates) new PublishedRepoCollection 82func (factory *CollectionFactory) PublishedRepoCollection() *PublishedRepoCollection { 83 factory.Lock() 84 defer factory.Unlock() 85 86 if factory.publishedRepos == nil { 87 factory.publishedRepos = NewPublishedRepoCollection(factory.db) 88 } 89 90 return factory.publishedRepos 91} 92 93// ChecksumCollection returns (or creates) new ChecksumCollection 94func (factory *CollectionFactory) ChecksumCollection() *ChecksumCollection { 95 factory.Lock() 96 defer factory.Unlock() 97 98 if factory.checksums == nil { 99 factory.checksums = NewChecksumCollection(factory.db) 100 } 101 102 return factory.checksums 103} 104 105// Flush removes all references to collections, so that memory could be reclaimed 106func (factory *CollectionFactory) Flush() { 107 factory.Lock() 108 defer factory.Unlock() 109 110 factory.localRepos = nil 111 factory.snapshots = nil 112 factory.remoteRepos = nil 113 factory.publishedRepos = nil 114 factory.packages = nil 115 factory.checksums = nil 116} 117