every go routine needs to have its own collection factory

this is needed so concurrent reads and writes are possible.
This commit is contained in:
Oliver Sauder
2016-11-18 15:46:49 +01:00
committed by Lorenzo Bolla
parent 4a6d53e16d
commit 208a2151c1
48 changed files with 305 additions and 387 deletions

View File

@@ -4,7 +4,6 @@ package api
import ( import (
"fmt" "fmt"
"sort" "sort"
"time"
"github.com/aptly-dev/aptly/aptly" "github.com/aptly-dev/aptly/aptly"
"github.com/aptly-dev/aptly/deb" "github.com/aptly-dev/aptly/deb"
@@ -35,43 +34,6 @@ type dbRequest struct {
err chan<- error err chan<- error
} }
// Flushes all collections which cache in-memory objects
func flushColections() {
// lock everything to eliminate in-progress calls
r := context.CollectionFactory().RemoteRepoCollection()
r.Lock()
defer r.Unlock()
l := context.CollectionFactory().LocalRepoCollection()
l.Lock()
defer l.Unlock()
s := context.CollectionFactory().SnapshotCollection()
s.Lock()
defer s.Unlock()
p := context.CollectionFactory().PublishedRepoCollection()
p.Lock()
defer p.Unlock()
// all collections locked, flush them
context.CollectionFactory().Flush()
}
// Periodically flushes CollectionFactory to free up memory used by
// collections, flushing caches.
//
// Should be run in goroutine!
func cacheFlusher() {
ticker := time.Tick(15 * time.Minute)
for {
<-ticker
flushColections()
}
}
// Acquire database lock and release it when not needed anymore. // Acquire database lock and release it when not needed anymore.
// //
// Should be run in a goroutine! // Should be run in a goroutine!
@@ -94,7 +56,6 @@ func acquireDatabase(requests <-chan dbRequest) {
case releasedb: case releasedb:
clients-- clients--
if clients == 0 { if clients == 0 {
flushColections()
err = context.CloseDatabase() err = context.CloseDatabase()
} else { } else {
err = nil err = nil
@@ -107,10 +68,10 @@ func acquireDatabase(requests <-chan dbRequest) {
// Common piece of code to show list of packages, // Common piece of code to show list of packages,
// with searching & details if requested // with searching & details if requested
func showPackages(c *gin.Context, reflist *deb.PackageRefList) { func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory *deb.CollectionFactory) {
result := []*deb.Package{} result := []*deb.Package{}
list, err := deb.NewPackageListFromRefList(reflist, context.CollectionFactory().PackageCollection(), nil) list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), nil)
if err != nil { if err != nil {
c.AbortWithError(404, err) c.AbortWithError(404, err)
return return

View File

@@ -21,17 +21,7 @@ func apiGraph(c *gin.Context) {
ext := c.Params.ByName("ext") ext := c.Params.ByName("ext")
layout := c.Request.URL.Query().Get("layout") layout := c.Request.URL.Query().Get("layout")
factory := context.NewCollectionFactory()
factory := context.CollectionFactory()
factory.RemoteRepoCollection().Lock()
defer factory.RemoteRepoCollection().Unlock()
factory.LocalRepoCollection().Lock()
defer factory.LocalRepoCollection().Unlock()
factory.SnapshotCollection().Lock()
defer factory.SnapshotCollection().Unlock()
factory.PublishedRepoCollection().Lock()
defer factory.PublishedRepoCollection().Unlock()
graph, err := deb.BuildGraph(factory, layout) graph, err := deb.BuildGraph(factory, layout)
if err != nil { if err != nil {

View File

@@ -6,7 +6,8 @@ import (
// GET /api/packages/:key // GET /api/packages/:key
func apiPackagesShow(c *gin.Context) { func apiPackagesShow(c *gin.Context) {
p, err := context.CollectionFactory().PackageCollection().ByKey([]byte(c.Params.ByName("key"))) collectionFactory := context.NewCollectionFactory()
p, err := collectionFactory.PackageCollection().ByKey([]byte(c.Params.ByName("key")))
if err != nil { if err != nil {
c.AbortWithError(404, err) c.AbortWithError(404, err)
return return

View File

@@ -51,22 +51,13 @@ func parseEscapedPath(path string) string {
// GET /publish // GET /publish
func apiPublishList(c *gin.Context) { func apiPublishList(c *gin.Context) {
localCollection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
localCollection.RLock() collection := collectionFactory.PublishedRepoCollection()
defer localCollection.RUnlock()
snapshotCollection := context.CollectionFactory().SnapshotCollection()
snapshotCollection.RLock()
defer snapshotCollection.RUnlock()
collection := context.CollectionFactory().PublishedRepoCollection()
collection.Lock()
defer collection.Unlock()
result := make([]*deb.PublishedRepo, 0, collection.Len()) result := make([]*deb.PublishedRepo, 0, collection.Len())
err := collection.ForEach(func(repo *deb.PublishedRepo) error { err := collection.ForEach(func(repo *deb.PublishedRepo) error {
err := collection.LoadComplete(repo, context.CollectionFactory()) err := collection.LoadComplete(repo, collectionFactory)
if err != nil { if err != nil {
return err return err
} }
@@ -124,13 +115,12 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
var components []string var components []string
var sources []interface{} var sources []interface{}
collectionFactory := context.NewCollectionFactory()
if b.SourceKind == "snapshot" { if b.SourceKind == "snapshot" {
var snapshot *deb.Snapshot var snapshot *deb.Snapshot
snapshotCollection := context.CollectionFactory().SnapshotCollection() snapshotCollection := collectionFactory.SnapshotCollection()
snapshotCollection.Lock()
defer snapshotCollection.Unlock()
for _, source := range b.Sources { for _, source := range b.Sources {
components = append(components, source.Component) components = append(components, source.Component)
@@ -152,9 +142,7 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
} else if b.SourceKind == deb.SourceLocalRepo { } else if b.SourceKind == deb.SourceLocalRepo {
var localRepo *deb.LocalRepo var localRepo *deb.LocalRepo
localCollection := context.CollectionFactory().LocalRepoCollection() localCollection := collectionFactory.LocalRepoCollection()
localCollection.Lock()
defer localCollection.Unlock()
for _, source := range b.Sources { for _, source := range b.Sources {
components = append(components, source.Component) components = append(components, source.Component)
@@ -177,11 +165,11 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
return return
} }
collection := context.CollectionFactory().PublishedRepoCollection() collection := collectionFactory.PublishedRepoCollection()
collection.Lock() collection.Lock()
defer collection.Unlock() defer collection.Unlock()
published, err := deb.NewPublishedRepo(storage, prefix, b.Distribution, b.Architectures, components, sources, context.CollectionFactory()) published, err := deb.NewPublishedRepo(storage, prefix, b.Distribution, b.Architectures, components, sources, collectionFactory)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to publish: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to publish: %s", err))
return return
@@ -208,12 +196,12 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
duplicate := collection.CheckDuplicate(published) duplicate := collection.CheckDuplicate(published)
if duplicate != nil { if duplicate != nil {
context.CollectionFactory().PublishedRepoCollection().LoadComplete(duplicate, context.CollectionFactory()) collectionFactory.PublishedRepoCollection().LoadComplete(duplicate, collectionFactory)
c.AbortWithError(400, fmt.Errorf("prefix/distribution already used by another published repo: %s", duplicate)) c.AbortWithError(400, fmt.Errorf("prefix/distribution already used by another published repo: %s", duplicate))
return return
} }
err = published.Publish(context.PackagePool(), context, context.CollectionFactory(), signer, nil, b.ForceOverwrite) err = published.Publish(context.PackagePool(), context, collectionFactory, signer, nil, b.ForceOverwrite)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to publish: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to publish: %s", err))
return return
@@ -256,25 +244,15 @@ func apiPublishUpdateSwitch(c *gin.Context) {
return return
} }
// published.LoadComplete would touch local repo collection collectionFactory := context.NewCollectionFactory()
localRepoCollection := context.CollectionFactory().LocalRepoCollection() collection := collectionFactory.PublishedRepoCollection()
localRepoCollection.Lock()
defer localRepoCollection.Unlock()
snapshotCollection := context.CollectionFactory().SnapshotCollection()
snapshotCollection.Lock()
defer snapshotCollection.Unlock()
collection := context.CollectionFactory().PublishedRepoCollection()
collection.Lock()
defer collection.Unlock()
published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution) published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil { if err != nil {
c.AbortWithError(404, fmt.Errorf("unable to update: %s", err)) c.AbortWithError(404, fmt.Errorf("unable to update: %s", err))
return return
} }
err = collection.LoadComplete(published, context.CollectionFactory()) err = collection.LoadComplete(published, collectionFactory)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to update: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to update: %s", err))
return return
@@ -299,6 +277,7 @@ func apiPublishUpdateSwitch(c *gin.Context) {
return return
} }
snapshotCollection := collectionFactory.SnapshotCollection()
snapshot, err2 := snapshotCollection.ByName(snapshotInfo.Name) snapshot, err2 := snapshotCollection.ByName(snapshotInfo.Name)
if err2 != nil { if err2 != nil {
c.AbortWithError(404, err2) c.AbortWithError(404, err2)
@@ -327,7 +306,7 @@ func apiPublishUpdateSwitch(c *gin.Context) {
published.AcquireByHash = *b.AcquireByHash published.AcquireByHash = *b.AcquireByHash
} }
err = published.Publish(context.PackagePool(), context, context.CollectionFactory(), signer, nil, b.ForceOverwrite) err = published.Publish(context.PackagePool(), context, collectionFactory, signer, nil, b.ForceOverwrite)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to update: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to update: %s", err))
return return
@@ -341,7 +320,7 @@ func apiPublishUpdateSwitch(c *gin.Context) {
if b.SkipCleanup == nil || !*b.SkipCleanup { if b.SkipCleanup == nil || !*b.SkipCleanup {
err = collection.CleanupPrefixComponentFiles(published.Prefix, updatedComponents, err = collection.CleanupPrefixComponentFiles(published.Prefix, updatedComponents,
context.GetPublishedStorage(storage), context.CollectionFactory(), nil) context.GetPublishedStorage(storage), collectionFactory, nil)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to update: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to update: %s", err))
return return
@@ -360,17 +339,11 @@ func apiPublishDrop(c *gin.Context) {
storage, prefix := deb.ParsePrefix(param) storage, prefix := deb.ParsePrefix(param)
distribution := c.Params.ByName("distribution") distribution := c.Params.ByName("distribution")
// published.LoadComplete would touch local repo collection collectionFactory := context.NewCollectionFactory()
localRepoCollection := context.CollectionFactory().LocalRepoCollection() collection := collectionFactory.PublishedRepoCollection()
localRepoCollection.Lock()
defer localRepoCollection.Unlock()
collection := context.CollectionFactory().PublishedRepoCollection()
collection.Lock()
defer collection.Unlock()
err := collection.Remove(context, storage, prefix, distribution, err := collection.Remove(context, storage, prefix, distribution,
context.CollectionFactory(), context.Progress(), force, skipCleanup) collectionFactory, context.Progress(), force, skipCleanup)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to drop: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to drop: %s", err))
return return

View File

@@ -17,11 +17,9 @@ import (
func apiReposList(c *gin.Context) { func apiReposList(c *gin.Context) {
result := []*deb.LocalRepo{} result := []*deb.LocalRepo{}
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.RLock() collection := collectionFactory.LocalRepoCollection()
defer collection.RUnlock() collection.ForEach(func(r *deb.LocalRepo) error {
context.CollectionFactory().LocalRepoCollection().ForEach(func(r *deb.LocalRepo) error {
result = append(result, r) result = append(result, r)
return nil return nil
}) })
@@ -46,11 +44,9 @@ func apiReposCreate(c *gin.Context) {
repo.DefaultComponent = b.DefaultComponent repo.DefaultComponent = b.DefaultComponent
repo.DefaultDistribution = b.DefaultDistribution repo.DefaultDistribution = b.DefaultDistribution
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.LocalRepoCollection()
defer collection.Unlock() err := collection.Add(repo)
err := context.CollectionFactory().LocalRepoCollection().Add(repo)
if err != nil { if err != nil {
c.AbortWithError(400, err) c.AbortWithError(400, err)
return return
@@ -71,9 +67,8 @@ func apiReposEdit(c *gin.Context) {
return return
} }
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.LocalRepoCollection()
defer collection.Unlock()
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -102,9 +97,8 @@ func apiReposEdit(c *gin.Context) {
// GET /api/repos/:name // GET /api/repos/:name
func apiReposShow(c *gin.Context) { func apiReposShow(c *gin.Context) {
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.RLock() collection := collectionFactory.LocalRepoCollection()
defer collection.RUnlock()
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -119,17 +113,10 @@ func apiReposShow(c *gin.Context) {
func apiReposDrop(c *gin.Context) { func apiReposDrop(c *gin.Context) {
force := c.Request.URL.Query().Get("force") == "1" force := c.Request.URL.Query().Get("force") == "1"
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.LocalRepoCollection()
defer collection.Unlock() snapshotCollection := collectionFactory.SnapshotCollection()
publishedCollection := collectionFactory.PublishedRepoCollection()
snapshotCollection := context.CollectionFactory().SnapshotCollection()
snapshotCollection.RLock()
defer snapshotCollection.RUnlock()
publishedCollection := context.CollectionFactory().PublishedRepoCollection()
publishedCollection.RLock()
defer publishedCollection.RUnlock()
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -162,9 +149,8 @@ func apiReposDrop(c *gin.Context) {
// GET /api/repos/:name/packages // GET /api/repos/:name/packages
func apiReposPackagesShow(c *gin.Context) { func apiReposPackagesShow(c *gin.Context) {
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.LocalRepoCollection()
defer collection.Unlock()
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -178,7 +164,7 @@ func apiReposPackagesShow(c *gin.Context) {
return return
} }
showPackages(c, repo.RefList()) showPackages(c, repo.RefList(), collectionFactory)
} }
// Handler for both add and delete // Handler for both add and delete
@@ -191,9 +177,8 @@ func apiReposPackagesAddDelete(c *gin.Context, cb func(list *deb.PackageList, p
return return
} }
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.LocalRepoCollection()
defer collection.Unlock()
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -207,7 +192,7 @@ func apiReposPackagesAddDelete(c *gin.Context, cb func(list *deb.PackageList, p
return return
} }
list, err := deb.NewPackageListFromRefList(repo.RefList(), context.CollectionFactory().PackageCollection(), nil) list, err := deb.NewPackageListFromRefList(repo.RefList(), collectionFactory.PackageCollection(), nil)
if err != nil { if err != nil {
c.AbortWithError(500, err) c.AbortWithError(500, err)
return return
@@ -217,7 +202,7 @@ func apiReposPackagesAddDelete(c *gin.Context, cb func(list *deb.PackageList, p
for _, ref := range b.PackageRefs { for _, ref := range b.PackageRefs {
var p *deb.Package var p *deb.Package
p, err = context.CollectionFactory().PackageCollection().ByKey([]byte(ref)) p, err = collectionFactory.PackageCollection().ByKey([]byte(ref))
if err != nil { if err != nil {
if err == database.ErrNotFound { if err == database.ErrNotFound {
c.AbortWithError(404, fmt.Errorf("package %s: %s", ref, err)) c.AbortWithError(404, fmt.Errorf("package %s: %s", ref, err))
@@ -235,7 +220,7 @@ func apiReposPackagesAddDelete(c *gin.Context, cb func(list *deb.PackageList, p
repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list)) repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list))
err = context.CollectionFactory().LocalRepoCollection().Update(repo) err = collectionFactory.LocalRepoCollection().Update(repo)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to save: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to save: %s", err))
return return
@@ -281,9 +266,8 @@ func apiReposPackageFromDir(c *gin.Context) {
return return
} }
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.LocalRepoCollection()
defer collection.Unlock()
repo, err := collection.ByName(c.Params.ByName("name")) repo, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -320,14 +304,14 @@ func apiReposPackageFromDir(c *gin.Context) {
packageFiles, otherFiles, failedFiles = deb.CollectPackageFiles(sources, reporter) packageFiles, otherFiles, failedFiles = deb.CollectPackageFiles(sources, reporter)
list, err = deb.NewPackageListFromRefList(repo.RefList(), context.CollectionFactory().PackageCollection(), nil) list, err = deb.NewPackageListFromRefList(repo.RefList(), collectionFactory.PackageCollection(), nil)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to load packages: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to load packages: %s", err))
return return
} }
processedFiles, failedFiles2, err = deb.ImportPackageFiles(list, packageFiles, forceReplace, verifier, context.PackagePool(), processedFiles, failedFiles2, err = deb.ImportPackageFiles(list, packageFiles, forceReplace, verifier, context.PackagePool(),
context.CollectionFactory().PackageCollection(), reporter, nil, context.CollectionFactory().ChecksumCollection) collectionFactory.PackageCollection(), reporter, nil, collectionFactory.ChecksumCollection)
failedFiles = append(failedFiles, failedFiles2...) failedFiles = append(failedFiles, failedFiles2...)
processedFiles = append(processedFiles, otherFiles...) processedFiles = append(processedFiles, otherFiles...)
@@ -339,7 +323,7 @@ func apiReposPackageFromDir(c *gin.Context) {
repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list)) repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list))
err = context.CollectionFactory().LocalRepoCollection().Update(repo) err = collectionFactory.LocalRepoCollection().Update(repo)
if err != nil { if err != nil {
c.AbortWithError(500, fmt.Errorf("unable to save: %s", err)) c.AbortWithError(500, fmt.Errorf("unable to save: %s", err))
return return
@@ -412,15 +396,12 @@ func apiReposIncludePackageFromDir(c *gin.Context) {
sources = []string{filepath.Join(context.UploadPath(), c.Params.ByName("dir"), c.Params.ByName("file"))} sources = []string{filepath.Join(context.UploadPath(), c.Params.ByName("dir"), c.Params.ByName("file"))}
} }
localRepoCollection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
localRepoCollection.Lock()
defer localRepoCollection.Unlock()
changesFiles, failedFiles = deb.CollectChangesFiles(sources, reporter) changesFiles, failedFiles = deb.CollectChangesFiles(sources, reporter)
_, failedFiles2, err = deb.ImportChangesFiles( _, failedFiles2, err = deb.ImportChangesFiles(
changesFiles, reporter, acceptUnsigned, ignoreSignature, forceReplace, noRemoveFiles, verifier, changesFiles, reporter, acceptUnsigned, ignoreSignature, forceReplace, noRemoveFiles, verifier,
repoTemplateString, context.Progress(), localRepoCollection, context.CollectionFactory().PackageCollection(), repoTemplateString, context.Progress(), collectionFactory.LocalRepoCollection(), collectionFactory.PackageCollection(),
context.PackagePool(), context.CollectionFactory().ChecksumCollection, nil, query.Parse) context.PackagePool(), collectionFactory.ChecksumCollection, nil, query.Parse)
failedFiles = append(failedFiles, failedFiles2...) failedFiles = append(failedFiles, failedFiles2...)
if err != nil { if err != nil {

View File

@@ -46,9 +46,6 @@ func Router(c *ctx.AptlyContext) http.Handler {
c.Next() c.Next()
}) })
} else {
go cacheFlusher()
} }
root := router.Group("/api") root := router.Group("/api")

View File

@@ -12,9 +12,8 @@ import (
func apiSnapshotsList(c *gin.Context) { func apiSnapshotsList(c *gin.Context) {
SortMethodString := c.Request.URL.Query().Get("sort") SortMethodString := c.Request.URL.Query().Get("sort")
collection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
collection.RLock() collection := collectionFactory.SnapshotCollection()
defer collection.RUnlock()
if SortMethodString == "" { if SortMethodString == "" {
SortMethodString = "name" SortMethodString = "name"
@@ -46,13 +45,9 @@ func apiSnapshotsCreateFromMirror(c *gin.Context) {
return return
} }
collection := context.CollectionFactory().RemoteRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.RemoteRepoCollection()
defer collection.Unlock() snapshotCollection := collectionFactory.SnapshotCollection()
snapshotCollection := context.CollectionFactory().SnapshotCollection()
snapshotCollection.Lock()
defer snapshotCollection.Unlock()
repo, err = collection.ByName(c.Params.ByName("name")) repo, err = collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -115,9 +110,8 @@ func apiSnapshotsCreate(c *gin.Context) {
} }
} }
snapshotCollection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
snapshotCollection.Lock() snapshotCollection := collectionFactory.SnapshotCollection()
defer snapshotCollection.Unlock()
sources := make([]*deb.Snapshot, len(b.SourceSnapshots)) sources := make([]*deb.Snapshot, len(b.SourceSnapshots))
@@ -141,7 +135,7 @@ func apiSnapshotsCreate(c *gin.Context) {
for _, ref := range b.PackageRefs { for _, ref := range b.PackageRefs {
var p *deb.Package var p *deb.Package
p, err = context.CollectionFactory().PackageCollection().ByKey([]byte(ref)) p, err = collectionFactory.PackageCollection().ByKey([]byte(ref))
if err != nil { if err != nil {
if err == database.ErrNotFound { if err == database.ErrNotFound {
c.AbortWithError(404, fmt.Errorf("package %s: %s", ref, err)) c.AbortWithError(404, fmt.Errorf("package %s: %s", ref, err))
@@ -185,13 +179,9 @@ func apiSnapshotsCreateFromRepository(c *gin.Context) {
return return
} }
collection := context.CollectionFactory().LocalRepoCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.LocalRepoCollection()
defer collection.Unlock() snapshotCollection := collectionFactory.SnapshotCollection()
snapshotCollection := context.CollectionFactory().SnapshotCollection()
snapshotCollection.Lock()
defer snapshotCollection.Unlock()
repo, err = collection.ByName(c.Params.ByName("name")) repo, err = collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -240,9 +230,8 @@ func apiSnapshotsUpdate(c *gin.Context) {
return return
} }
collection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.SnapshotCollection()
defer collection.Unlock()
snapshot, err = collection.ByName(c.Params.ByName("name")) snapshot, err = collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -264,7 +253,7 @@ func apiSnapshotsUpdate(c *gin.Context) {
snapshot.Description = b.Description snapshot.Description = b.Description
} }
err = context.CollectionFactory().SnapshotCollection().Update(snapshot) err = collectionFactory.SnapshotCollection().Update(snapshot)
if err != nil { if err != nil {
c.AbortWithError(500, err) c.AbortWithError(500, err)
return return
@@ -275,9 +264,8 @@ func apiSnapshotsUpdate(c *gin.Context) {
// GET /api/snapshots/:name // GET /api/snapshots/:name
func apiSnapshotsShow(c *gin.Context) { func apiSnapshotsShow(c *gin.Context) {
collection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.SnapshotCollection()
defer collection.Unlock()
snapshot, err := collection.ByName(c.Params.ByName("name")) snapshot, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -299,13 +287,9 @@ func apiSnapshotsDrop(c *gin.Context) {
name := c.Params.ByName("name") name := c.Params.ByName("name")
force := c.Request.URL.Query().Get("force") == "1" force := c.Request.URL.Query().Get("force") == "1"
snapshotCollection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
snapshotCollection.Lock() snapshotCollection := collectionFactory.SnapshotCollection()
defer snapshotCollection.Unlock() publishedCollection := collectionFactory.PublishedRepoCollection()
publishedCollection := context.CollectionFactory().PublishedRepoCollection()
publishedCollection.RLock()
defer publishedCollection.RUnlock()
snapshot, err := snapshotCollection.ByName(name) snapshot, err := snapshotCollection.ByName(name)
if err != nil { if err != nil {
@@ -341,9 +325,8 @@ func apiSnapshotsDrop(c *gin.Context) {
func apiSnapshotsDiff(c *gin.Context) { func apiSnapshotsDiff(c *gin.Context) {
onlyMatching := c.Request.URL.Query().Get("onlyMatching") == "1" onlyMatching := c.Request.URL.Query().Get("onlyMatching") == "1"
collection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.SnapshotCollection()
defer collection.Unlock()
snapshotA, err := collection.ByName(c.Params.ByName("name")) snapshotA, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -370,7 +353,7 @@ func apiSnapshotsDiff(c *gin.Context) {
} }
// Calculate diff // Calculate diff
diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), context.CollectionFactory().PackageCollection()) diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), collectionFactory.PackageCollection())
if err != nil { if err != nil {
c.AbortWithError(500, err) c.AbortWithError(500, err)
return return
@@ -391,9 +374,8 @@ func apiSnapshotsDiff(c *gin.Context) {
// GET /api/snapshots/:name/packages // GET /api/snapshots/:name/packages
func apiSnapshotsSearchPackages(c *gin.Context) { func apiSnapshotsSearchPackages(c *gin.Context) {
collection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
collection.Lock() collection := collectionFactory.SnapshotCollection()
defer collection.Unlock()
snapshot, err := collection.ByName(c.Params.ByName("name")) snapshot, err := collection.ByName(c.Params.ByName("name"))
if err != nil { if err != nil {
@@ -407,5 +389,5 @@ func apiSnapshotsSearchPackages(c *gin.Context) {
return return
} }
showPackages(c, snapshot.RefList()) showPackages(c, snapshot.RefList(), collectionFactory)
} }

View File

@@ -21,14 +21,14 @@ const (
) )
// ListPackagesRefList shows list of packages in PackageRefList // ListPackagesRefList shows list of packages in PackageRefList
func ListPackagesRefList(reflist *deb.PackageRefList) (err error) { func ListPackagesRefList(reflist *deb.PackageRefList, collectionFactory *deb.CollectionFactory) (err error) {
fmt.Printf("Packages:\n") fmt.Printf("Packages:\n")
if reflist == nil { if reflist == nil {
return return
} }
list, err := deb.NewPackageListFromRefList(reflist, context.CollectionFactory().PackageCollection(), context.Progress()) list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }

View File

@@ -21,6 +21,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
verbose := context.Flags().Lookup("verbose").Value.Get().(bool) verbose := context.Flags().Lookup("verbose").Value.Get().(bool)
dryRun := context.Flags().Lookup("dry-run").Value.Get().(bool) dryRun := context.Flags().Lookup("dry-run").Value.Get().(bool)
collectionFactory := context.NewCollectionFactory()
// collect information about references packages... // collect information about references packages...
existingPackageRefs := deb.NewPackageRefList() existingPackageRefs := deb.NewPackageRefList()
@@ -32,12 +33,12 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
if verbose { if verbose {
context.Progress().ColoredPrintf("@{y}Loading mirrors:@|") context.Progress().ColoredPrintf("@{y}Loading mirrors:@|")
} }
err = context.CollectionFactory().RemoteRepoCollection().ForEach(func(repo *deb.RemoteRepo) error { err = collectionFactory.RemoteRepoCollection().ForEach(func(repo *deb.RemoteRepo) error {
if verbose { if verbose {
context.Progress().ColoredPrintf("- @{g}%s@|", repo.Name) context.Progress().ColoredPrintf("- @{g}%s@|", repo.Name)
} }
e := context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo) e := collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if e != nil { if e != nil {
return e return e
} }
@@ -59,17 +60,17 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err return err
} }
context.CollectionFactory().Flush() collectionFactory.Flush()
if verbose { if verbose {
context.Progress().ColoredPrintf("@{y}Loading local repos:@|") context.Progress().ColoredPrintf("@{y}Loading local repos:@|")
} }
err = context.CollectionFactory().LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error { err = collectionFactory.LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error {
if verbose { if verbose {
context.Progress().ColoredPrintf("- @{g}%s@|", repo.Name) context.Progress().ColoredPrintf("- @{g}%s@|", repo.Name)
} }
e := context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) e := collectionFactory.LocalRepoCollection().LoadComplete(repo)
if e != nil { if e != nil {
return e return e
} }
@@ -92,17 +93,17 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err return err
} }
context.CollectionFactory().Flush() collectionFactory.Flush()
if verbose { if verbose {
context.Progress().ColoredPrintf("@{y}Loading snapshots:@|") context.Progress().ColoredPrintf("@{y}Loading snapshots:@|")
} }
err = context.CollectionFactory().SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error { err = collectionFactory.SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error {
if verbose { if verbose {
context.Progress().ColoredPrintf("- @{g}%s@|", snapshot.Name) context.Progress().ColoredPrintf("- @{g}%s@|", snapshot.Name)
} }
e := context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) e := collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if e != nil { if e != nil {
return e return e
} }
@@ -122,19 +123,19 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err return err
} }
context.CollectionFactory().Flush() collectionFactory.Flush()
if verbose { if verbose {
context.Progress().ColoredPrintf("@{y}Loading published repositories:@|") context.Progress().ColoredPrintf("@{y}Loading published repositories:@|")
} }
err = context.CollectionFactory().PublishedRepoCollection().ForEach(func(published *deb.PublishedRepo) error { err = collectionFactory.PublishedRepoCollection().ForEach(func(published *deb.PublishedRepo) error {
if verbose { if verbose {
context.Progress().ColoredPrintf("- @{g}%s:%s/%s{|}", published.Storage, published.Prefix, published.Distribution) context.Progress().ColoredPrintf("- @{g}%s:%s/%s{|}", published.Storage, published.Prefix, published.Distribution)
} }
if published.SourceKind != deb.SourceLocalRepo { if published.SourceKind != deb.SourceLocalRepo {
return nil return nil
} }
e := context.CollectionFactory().PublishedRepoCollection().LoadComplete(published, context.CollectionFactory()) e := collectionFactory.PublishedRepoCollection().LoadComplete(published, collectionFactory)
if e != nil { if e != nil {
return e return e
} }
@@ -156,11 +157,11 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err return err
} }
context.CollectionFactory().Flush() collectionFactory.Flush()
// ... and compare it to the list of all packages // ... and compare it to the list of all packages
context.Progress().ColoredPrintf("@{w!}Loading list of all packages...@|") context.Progress().ColoredPrintf("@{w!}Loading list of all packages...@|")
allPackageRefs := context.CollectionFactory().PackageCollection().AllPackageRefs() allPackageRefs := collectionFactory.PackageCollection().AllPackageRefs()
toDelete := allPackageRefs.Subtract(existingPackageRefs) toDelete := allPackageRefs.Subtract(existingPackageRefs)
@@ -185,7 +186,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
if !dryRun { if !dryRun {
batch := db.CreateBatch() batch := db.CreateBatch()
err = toDelete.ForEach(func(ref []byte) error { err = toDelete.ForEach(func(ref []byte) error {
return context.CollectionFactory().PackageCollection().DeleteByKey(ref, batch) return collectionFactory.PackageCollection().DeleteByKey(ref, batch)
}) })
if err != nil { if err != nil {
return err return err
@@ -200,7 +201,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
} }
} }
context.CollectionFactory().Flush() collectionFactory.Flush()
// now, build a list of files that should be present in Repository (package pool) // now, build a list of files that should be present in Repository (package pool)
context.Progress().ColoredPrintf("@{w!}Building list of files referenced by packages...@|") context.Progress().ColoredPrintf("@{w!}Building list of files referenced by packages...@|")
@@ -208,7 +209,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
context.Progress().InitBar(int64(existingPackageRefs.Len()), false) context.Progress().InitBar(int64(existingPackageRefs.Len()), false)
err = existingPackageRefs.ForEach(func(key []byte) error { err = existingPackageRefs.ForEach(func(key []byte) error {
pkg, err2 := context.CollectionFactory().PackageCollection().ByKey(key) pkg, err2 := collectionFactory.PackageCollection().ByKey(key)
if err2 != nil { if err2 != nil {
tail := "" tail := ""
if verbose { if verbose {

View File

@@ -28,8 +28,8 @@ func aptlyGraph(cmd *commander.Command, args []string) error {
layout := context.Flags().Lookup("layout").Value.String() layout := context.Flags().Lookup("layout").Value.String()
fmt.Printf("Generating graph...\n") fmt.Printf("Generating graph...\n")
graph, err := deb.BuildGraph(context.CollectionFactory(), layout) collectionFactory := context.NewCollectionFactory()
graph, err := deb.BuildGraph(collectionFactory, layout)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -64,7 +64,8 @@ func aptlyMirrorCreate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to fetch mirror: %s", err) return fmt.Errorf("unable to fetch mirror: %s", err)
} }
err = context.CollectionFactory().RemoteRepoCollection().Add(repo) collectionFactory := context.NewCollectionFactory()
err = collectionFactory.RemoteRepoCollection().Add(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to add mirror: %s", err) return fmt.Errorf("unable to add mirror: %s", err)
} }

View File

@@ -15,8 +15,9 @@ func aptlyMirrorDrop(cmd *commander.Command, args []string) error {
} }
name := args[0] name := args[0]
collectionFactory := context.NewCollectionFactory()
repo, err := context.CollectionFactory().RemoteRepoCollection().ByName(name) repo, err := collectionFactory.RemoteRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to drop: %s", err) return fmt.Errorf("unable to drop: %s", err)
} }
@@ -28,7 +29,7 @@ func aptlyMirrorDrop(cmd *commander.Command, args []string) error {
force := context.Flags().Lookup("force").Value.Get().(bool) force := context.Flags().Lookup("force").Value.Get().(bool)
if !force { if !force {
snapshots := context.CollectionFactory().SnapshotCollection().ByRemoteRepoSource(repo) snapshots := collectionFactory.SnapshotCollection().ByRemoteRepoSource(repo)
if len(snapshots) > 0 { if len(snapshots) > 0 {
fmt.Printf("Mirror `%s` was used to create following snapshots:\n", repo.Name) fmt.Printf("Mirror `%s` was used to create following snapshots:\n", repo.Name)
@@ -40,7 +41,7 @@ func aptlyMirrorDrop(cmd *commander.Command, args []string) error {
} }
} }
err = context.CollectionFactory().RemoteRepoCollection().Drop(repo) err = collectionFactory.RemoteRepoCollection().Drop(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to drop: %s", err) return fmt.Errorf("unable to drop: %s", err)
} }

View File

@@ -16,7 +16,8 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error {
return commander.ErrCommandError return commander.ErrCommandError
} }
repo, err := context.CollectionFactory().RemoteRepoCollection().ByName(args[0]) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.RemoteRepoCollection().ByName(args[0])
if err != nil { if err != nil {
return fmt.Errorf("unable to edit: %s", err) return fmt.Errorf("unable to edit: %s", err)
} }
@@ -74,7 +75,7 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error {
} }
} }
err = context.CollectionFactory().RemoteRepoCollection().Update(repo) err = collectionFactory.RemoteRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to edit: %s", err) return fmt.Errorf("unable to edit: %s", err)
} }

View File

@@ -28,10 +28,11 @@ func aptlyMirrorListTxt(cmd *commander.Command, args []string) error {
var err error var err error
raw := cmd.Flag.Lookup("raw").Value.Get().(bool) raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
collectionFactory := context.NewCollectionFactory()
repos := make([]string, context.CollectionFactory().RemoteRepoCollection().Len()) repos := make([]string, collectionFactory.RemoteRepoCollection().Len())
i := 0 i := 0
context.CollectionFactory().RemoteRepoCollection().ForEach(func(repo *deb.RemoteRepo) error { collectionFactory.RemoteRepoCollection().ForEach(func(repo *deb.RemoteRepo) error {
if raw { if raw {
repos[i] = repo.Name repos[i] = repo.Name
} else { } else {

View File

@@ -20,7 +20,8 @@ func aptlyMirrorRename(cmd *commander.Command, args []string) error {
oldName, newName := args[0], args[1] oldName, newName := args[0], args[1]
repo, err = context.CollectionFactory().RemoteRepoCollection().ByName(oldName) collectionFactory := context.NewCollectionFactory()
repo, err = collectionFactory.RemoteRepoCollection().ByName(oldName)
if err != nil { if err != nil {
return fmt.Errorf("unable to rename: %s", err) return fmt.Errorf("unable to rename: %s", err)
} }
@@ -30,13 +31,13 @@ func aptlyMirrorRename(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to rename: %s", err) return fmt.Errorf("unable to rename: %s", err)
} }
_, err = context.CollectionFactory().RemoteRepoCollection().ByName(newName) _, err = collectionFactory.RemoteRepoCollection().ByName(newName)
if err == nil { if err == nil {
return fmt.Errorf("unable to rename: mirror %s already exists", newName) return fmt.Errorf("unable to rename: mirror %s already exists", newName)
} }
repo.Name = newName repo.Name = newName
err = context.CollectionFactory().RemoteRepoCollection().Update(repo) err = collectionFactory.RemoteRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to rename: %s", err) return fmt.Errorf("unable to rename: %s", err)
} }

View File

@@ -32,12 +32,13 @@ func aptlyMirrorShowTxt(cmd *commander.Command, args []string) error {
name := args[0] name := args[0]
repo, err := context.CollectionFactory().RemoteRepoCollection().ByName(name) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.RemoteRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to show: %s", err) return fmt.Errorf("unable to show: %s", err)
} }
err = context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo) err = collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to show: %s", err) return fmt.Errorf("unable to show: %s", err)
} }
@@ -85,7 +86,7 @@ func aptlyMirrorShowTxt(cmd *commander.Command, args []string) error {
if repo.LastDownloadDate.IsZero() { if repo.LastDownloadDate.IsZero() {
fmt.Printf("Unable to show package list, mirror hasn't been downloaded yet.\n") fmt.Printf("Unable to show package list, mirror hasn't been downloaded yet.\n")
} else { } else {
ListPackagesRefList(repo.RefList()) ListPackagesRefList(repo.RefList(), collectionFactory)
} }
} }

View File

@@ -22,12 +22,13 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
name := args[0] name := args[0]
repo, err := context.CollectionFactory().RemoteRepoCollection().ByName(name) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.RemoteRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
err = context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo) err = collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
@@ -53,7 +54,7 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
} }
context.Progress().Printf("Downloading & parsing package files...\n") context.Progress().Printf("Downloading & parsing package files...\n")
err = repo.DownloadPackageIndexes(context.Progress(), context.Downloader(), verifier, context.CollectionFactory(), ignoreMismatch) err = repo.DownloadPackageIndexes(context.Progress(), context.Downloader(), verifier, collectionFactory, ignoreMismatch)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
@@ -83,8 +84,8 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
skipExistingPackages := context.Flags().Lookup("skip-existing-packages").Value.Get().(bool) skipExistingPackages := context.Flags().Lookup("skip-existing-packages").Value.Get().(bool)
context.Progress().Printf("Building download queue...\n") context.Progress().Printf("Building download queue...\n")
queue, downloadSize, err = repo.BuildDownloadQueue(context.PackagePool(), context.CollectionFactory().PackageCollection(), queue, downloadSize, err = repo.BuildDownloadQueue(context.PackagePool(), collectionFactory.PackageCollection(),
context.CollectionFactory().ChecksumCollection(nil), skipExistingPackages) collectionFactory.ChecksumCollection(nil), skipExistingPackages)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
@@ -95,12 +96,12 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
err = context.ReOpenDatabase() err = context.ReOpenDatabase()
if err == nil { if err == nil {
repo.MarkAsIdle() repo.MarkAsIdle()
context.CollectionFactory().RemoteRepoCollection().Update(repo) collectionFactory.RemoteRepoCollection().Update(repo)
} }
}() }()
repo.MarkAsUpdating() repo.MarkAsUpdating()
err = context.CollectionFactory().RemoteRepoCollection().Update(repo) err = collectionFactory.RemoteRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
@@ -210,7 +211,7 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
} }
// and import it back to the pool // and import it back to the pool
task.File.PoolPath, err = context.PackagePool().Import(task.TempDownPath, task.File.Filename, &task.File.Checksums, true, context.CollectionFactory().ChecksumCollection(nil)) task.File.PoolPath, err = context.PackagePool().Import(task.TempDownPath, task.File.Filename, &task.File.Checksums, true, collectionFactory.ChecksumCollection(nil))
if err != nil { if err != nil {
return fmt.Errorf("unable to import file: %s", err) return fmt.Errorf("unable to import file: %s", err)
} }
@@ -234,8 +235,8 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to update: download errors:\n %s", strings.Join(errors, "\n ")) return fmt.Errorf("unable to update: download errors:\n %s", strings.Join(errors, "\n "))
} }
repo.FinalizeDownload(context.CollectionFactory(), context.Progress()) repo.FinalizeDownload(collectionFactory, context.Progress())
err = context.CollectionFactory().RemoteRepoCollection().Update(repo) err = collectionFactory.RemoteRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }

View File

@@ -29,7 +29,8 @@ func aptlyPackageSearch(cmd *commander.Command, args []string) error {
q = &deb.MatchAllQuery{} q = &deb.MatchAllQuery{}
} }
result := q.Query(context.CollectionFactory().PackageCollection()) collectionFactory := context.NewCollectionFactory()
result := q.Query(collectionFactory.PackageCollection())
if result.Len() == 0 { if result.Len() == 0 {
return fmt.Errorf("no results") return fmt.Errorf("no results")
} }

View File

@@ -12,9 +12,9 @@ import (
"github.com/smira/flag" "github.com/smira/flag"
) )
func printReferencesTo(p *deb.Package) (err error) { func printReferencesTo(p *deb.Package, collectionFactory *deb.CollectionFactory) (err error) {
err = context.CollectionFactory().RemoteRepoCollection().ForEach(func(repo *deb.RemoteRepo) error { err = collectionFactory.RemoteRepoCollection().ForEach(func(repo *deb.RemoteRepo) error {
e := context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo) e := collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if e != nil { if e != nil {
return e return e
} }
@@ -29,8 +29,8 @@ func printReferencesTo(p *deb.Package) (err error) {
return err return err
} }
err = context.CollectionFactory().LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error { err = collectionFactory.LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error {
e := context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) e := collectionFactory.LocalRepoCollection().LoadComplete(repo)
if e != nil { if e != nil {
return e return e
} }
@@ -45,8 +45,8 @@ func printReferencesTo(p *deb.Package) (err error) {
return err return err
} }
err = context.CollectionFactory().SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error { err = collectionFactory.SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error {
e := context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) e := collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if e != nil { if e != nil {
return e return e
} }
@@ -76,7 +76,8 @@ func aptlyPackageShow(cmd *commander.Command, args []string) error {
w := bufio.NewWriter(os.Stdout) w := bufio.NewWriter(os.Stdout)
result := q.Query(context.CollectionFactory().PackageCollection()) collectionFactory := context.NewCollectionFactory()
result := q.Query(collectionFactory.PackageCollection())
err = result.ForEach(func(p *deb.Package) error { err = result.ForEach(func(p *deb.Package) error {
p.Stanza().WriteTo(w, p.IsSource, false, false) p.Stanza().WriteTo(w, p.IsSource, false, false)
@@ -104,7 +105,7 @@ func aptlyPackageShow(cmd *commander.Command, args []string) error {
if withReferences { if withReferences {
fmt.Printf("References to package:\n") fmt.Printf("References to package:\n")
printReferencesTo(p) printReferencesTo(p, collectionFactory)
fmt.Printf("\n") fmt.Printf("\n")
} }

View File

@@ -23,8 +23,9 @@ func aptlyPublishDrop(cmd *commander.Command, args []string) error {
storage, prefix := deb.ParsePrefix(param) storage, prefix := deb.ParsePrefix(param)
err = context.CollectionFactory().PublishedRepoCollection().Remove(context, storage, prefix, distribution, collectionFactory := context.NewCollectionFactory()
context.CollectionFactory(), context.Progress(), err = collectionFactory.PublishedRepoCollection().Remove(context, storage, prefix, distribution,
collectionFactory, context.Progress(),
context.Flags().Lookup("force-drop").Value.Get().(bool), context.Flags().Lookup("force-drop").Value.Get().(bool),
context.Flags().Lookup("skip-cleanup").Value.Get().(bool)) context.Flags().Lookup("skip-cleanup").Value.Get().(bool))
if err != nil { if err != nil {

View File

@@ -29,10 +29,11 @@ func aptlyPublishListTxt(cmd *commander.Command, args []string) error {
raw := cmd.Flag.Lookup("raw").Value.Get().(bool) raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
published := make([]string, 0, context.CollectionFactory().PublishedRepoCollection().Len()) collectionFactory := context.NewCollectionFactory()
published := make([]string, 0, collectionFactory.PublishedRepoCollection().Len())
err = context.CollectionFactory().PublishedRepoCollection().ForEach(func(repo *deb.PublishedRepo) error { err = collectionFactory.PublishedRepoCollection().ForEach(func(repo *deb.PublishedRepo) error {
e := context.CollectionFactory().PublishedRepoCollection().LoadComplete(repo, context.CollectionFactory()) e := collectionFactory.PublishedRepoCollection().LoadComplete(repo, collectionFactory)
if e != nil { if e != nil {
return e return e
} }

View File

@@ -36,7 +36,8 @@ func aptlyPublishShowTxt(cmd *commander.Command, args []string) error {
storage, prefix := deb.ParsePrefix(param) storage, prefix := deb.ParsePrefix(param)
repo, err := context.CollectionFactory().PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil { if err != nil {
return fmt.Errorf("unable to show: %s", err) return fmt.Errorf("unable to show: %s", err)
} }
@@ -54,13 +55,13 @@ func aptlyPublishShowTxt(cmd *commander.Command, args []string) error {
for component, sourceID := range repo.Sources { for component, sourceID := range repo.Sources {
var name string var name string
if repo.SourceKind == deb.SourceSnapshot { if repo.SourceKind == deb.SourceSnapshot {
source, e := context.CollectionFactory().SnapshotCollection().ByUUID(sourceID) source, e := collectionFactory.SnapshotCollection().ByUUID(sourceID)
if e != nil { if e != nil {
continue continue
} }
name = source.Name name = source.Name
} else if repo.SourceKind == deb.SourceLocalRepo { } else if repo.SourceKind == deb.SourceLocalRepo {
source, e := context.CollectionFactory().LocalRepoCollection().ByUUID(sourceID) source, e := collectionFactory.LocalRepoCollection().ByUUID(sourceID)
if e != nil { if e != nil {
continue continue
} }

View File

@@ -15,6 +15,7 @@ func aptlyPublishSnapshotOrRepo(cmd *commander.Command, args []string) error {
var err error var err error
components := strings.Split(context.Flags().Lookup("component").Value.String(), ",") components := strings.Split(context.Flags().Lookup("component").Value.String(), ",")
collectionFactory := context.NewCollectionFactory()
if len(args) < len(components) || len(args) > len(components)+1 { if len(args) < len(components) || len(args) > len(components)+1 {
cmd.Usage() cmd.Usage()
@@ -43,12 +44,12 @@ func aptlyPublishSnapshotOrRepo(cmd *commander.Command, args []string) error {
) )
for _, name := range args { for _, name := range args {
snapshot, err = context.CollectionFactory().SnapshotCollection().ByName(name) snapshot, err = collectionFactory.SnapshotCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) err = collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
@@ -79,12 +80,12 @@ func aptlyPublishSnapshotOrRepo(cmd *commander.Command, args []string) error {
) )
for _, name := range args { for _, name := range args {
localRepo, err = context.CollectionFactory().LocalRepoCollection().ByName(name) localRepo, err = collectionFactory.LocalRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(localRepo) err = collectionFactory.LocalRepoCollection().LoadComplete(localRepo)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
@@ -116,7 +117,7 @@ func aptlyPublishSnapshotOrRepo(cmd *commander.Command, args []string) error {
notAutomatic := context.Flags().Lookup("notautomatic").Value.String() notAutomatic := context.Flags().Lookup("notautomatic").Value.String()
butAutomaticUpgrades := context.Flags().Lookup("butautomaticupgrades").Value.String() butAutomaticUpgrades := context.Flags().Lookup("butautomaticupgrades").Value.String()
published, err := deb.NewPublishedRepo(storage, prefix, distribution, context.ArchitecturesList(), components, sources, context.CollectionFactory()) published, err := deb.NewPublishedRepo(storage, prefix, distribution, context.ArchitecturesList(), components, sources, collectionFactory)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
@@ -142,9 +143,9 @@ func aptlyPublishSnapshotOrRepo(cmd *commander.Command, args []string) error {
published.AcquireByHash = context.Flags().Lookup("acquire-by-hash").Value.Get().(bool) published.AcquireByHash = context.Flags().Lookup("acquire-by-hash").Value.Get().(bool)
} }
duplicate := context.CollectionFactory().PublishedRepoCollection().CheckDuplicate(published) duplicate := collectionFactory.PublishedRepoCollection().CheckDuplicate(published)
if duplicate != nil { if duplicate != nil {
context.CollectionFactory().PublishedRepoCollection().LoadComplete(duplicate, context.CollectionFactory()) collectionFactory.PublishedRepoCollection().LoadComplete(duplicate, collectionFactory)
return fmt.Errorf("prefix/distribution already used by another published repo: %s", duplicate) return fmt.Errorf("prefix/distribution already used by another published repo: %s", duplicate)
} }
@@ -159,12 +160,12 @@ func aptlyPublishSnapshotOrRepo(cmd *commander.Command, args []string) error {
"the same package pool.\n") "the same package pool.\n")
} }
err = published.Publish(context.PackagePool(), context, context.CollectionFactory(), signer, context.Progress(), forceOverwrite) err = published.Publish(context.PackagePool(), context, collectionFactory, signer, context.Progress(), forceOverwrite)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
err = context.CollectionFactory().PublishedRepoCollection().Add(published) err = collectionFactory.PublishedRepoCollection().Add(published)
if err != nil { if err != nil {
return fmt.Errorf("unable to save to DB: %s", err) return fmt.Errorf("unable to save to DB: %s", err)
} }

View File

@@ -39,7 +39,8 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
var published *deb.PublishedRepo var published *deb.PublishedRepo
published, err = context.CollectionFactory().PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution) collectionFactory := context.NewCollectionFactory()
published, err = collectionFactory.PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
@@ -48,7 +49,7 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to update: not a snapshot publish") return fmt.Errorf("unable to update: not a snapshot publish")
} }
err = context.CollectionFactory().PublishedRepoCollection().LoadComplete(published, context.CollectionFactory()) err = collectionFactory.PublishedRepoCollection().LoadComplete(published, collectionFactory)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
@@ -67,12 +68,12 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to switch: component %s is not in published repository", component) return fmt.Errorf("unable to switch: component %s is not in published repository", component)
} }
snapshot, err = context.CollectionFactory().SnapshotCollection().ByName(names[i]) snapshot, err = collectionFactory.SnapshotCollection().ByName(names[i])
if err != nil { if err != nil {
return fmt.Errorf("unable to switch: %s", err) return fmt.Errorf("unable to switch: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) err = collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to switch: %s", err) return fmt.Errorf("unable to switch: %s", err)
} }
@@ -95,20 +96,20 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
published.SkipContents = context.Flags().Lookup("skip-contents").Value.Get().(bool) published.SkipContents = context.Flags().Lookup("skip-contents").Value.Get().(bool)
} }
err = published.Publish(context.PackagePool(), context, context.CollectionFactory(), signer, context.Progress(), forceOverwrite) err = published.Publish(context.PackagePool(), context, collectionFactory, signer, context.Progress(), forceOverwrite)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
err = context.CollectionFactory().PublishedRepoCollection().Update(published) err = collectionFactory.PublishedRepoCollection().Update(published)
if err != nil { if err != nil {
return fmt.Errorf("unable to save to DB: %s", err) return fmt.Errorf("unable to save to DB: %s", err)
} }
skipCleanup := context.Flags().Lookup("skip-cleanup").Value.Get().(bool) skipCleanup := context.Flags().Lookup("skip-cleanup").Value.Get().(bool)
if !skipCleanup { if !skipCleanup {
err = context.CollectionFactory().PublishedRepoCollection().CleanupPrefixComponentFiles(published.Prefix, components, err = collectionFactory.PublishedRepoCollection().CleanupPrefixComponentFiles(published.Prefix, components,
context.GetPublishedStorage(storage), context.CollectionFactory(), context.Progress()) context.GetPublishedStorage(storage), collectionFactory, context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }

View File

@@ -25,7 +25,8 @@ func aptlyPublishUpdate(cmd *commander.Command, args []string) error {
var published *deb.PublishedRepo var published *deb.PublishedRepo
published, err = context.CollectionFactory().PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution) collectionFactory := context.NewCollectionFactory()
published, err = collectionFactory.PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
@@ -34,7 +35,7 @@ func aptlyPublishUpdate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to update: not a local repository publish") return fmt.Errorf("unable to update: not a local repository publish")
} }
err = context.CollectionFactory().PublishedRepoCollection().LoadComplete(published, context.CollectionFactory()) err = collectionFactory.PublishedRepoCollection().LoadComplete(published, collectionFactory)
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }
@@ -59,20 +60,20 @@ func aptlyPublishUpdate(cmd *commander.Command, args []string) error {
published.SkipContents = context.Flags().Lookup("skip-contents").Value.Get().(bool) published.SkipContents = context.Flags().Lookup("skip-contents").Value.Get().(bool)
} }
err = published.Publish(context.PackagePool(), context, context.CollectionFactory(), signer, context.Progress(), forceOverwrite) err = published.Publish(context.PackagePool(), context, collectionFactory, signer, context.Progress(), forceOverwrite)
if err != nil { if err != nil {
return fmt.Errorf("unable to publish: %s", err) return fmt.Errorf("unable to publish: %s", err)
} }
err = context.CollectionFactory().PublishedRepoCollection().Update(published) err = collectionFactory.PublishedRepoCollection().Update(published)
if err != nil { if err != nil {
return fmt.Errorf("unable to save to DB: %s", err) return fmt.Errorf("unable to save to DB: %s", err)
} }
skipCleanup := context.Flags().Lookup("skip-cleanup").Value.Get().(bool) skipCleanup := context.Flags().Lookup("skip-cleanup").Value.Get().(bool)
if !skipCleanup { if !skipCleanup {
err = context.CollectionFactory().PublishedRepoCollection().CleanupPrefixComponentFiles(published.Prefix, components, err = collectionFactory.PublishedRepoCollection().CleanupPrefixComponentFiles(published.Prefix, components,
context.GetPublishedStorage(storage), context.CollectionFactory(), context.Progress()) context.GetPublishedStorage(storage), collectionFactory, context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to update: %s", err) return fmt.Errorf("unable to update: %s", err)
} }

View File

@@ -22,19 +22,20 @@ func aptlyRepoAdd(cmd *commander.Command, args []string) error {
verifier := context.GetVerifier() verifier := context.GetVerifier()
repo, err := context.CollectionFactory().LocalRepoCollection().ByName(name) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.LocalRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to add: %s", err) return fmt.Errorf("unable to add: %s", err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) err = collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to add: %s", err) return fmt.Errorf("unable to add: %s", err)
} }
context.Progress().Printf("Loading packages...\n") context.Progress().Printf("Loading packages...\n")
list, err := deb.NewPackageListFromRefList(repo.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) list, err := deb.NewPackageListFromRefList(repo.RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
@@ -48,8 +49,8 @@ func aptlyRepoAdd(cmd *commander.Command, args []string) error {
var processedFiles, failedFiles2 []string var processedFiles, failedFiles2 []string
processedFiles, failedFiles2, err = deb.ImportPackageFiles(list, packageFiles, forceReplace, verifier, context.PackagePool(), processedFiles, failedFiles2, err = deb.ImportPackageFiles(list, packageFiles, forceReplace, verifier, context.PackagePool(),
context.CollectionFactory().PackageCollection(), &aptly.ConsoleResultReporter{Progress: context.Progress()}, nil, collectionFactory.PackageCollection(), &aptly.ConsoleResultReporter{Progress: context.Progress()}, nil,
context.CollectionFactory().ChecksumCollection) collectionFactory.ChecksumCollection)
failedFiles = append(failedFiles, failedFiles2...) failedFiles = append(failedFiles, failedFiles2...)
if err != nil { if err != nil {
return fmt.Errorf("unable to import package files: %s", err) return fmt.Errorf("unable to import package files: %s", err)
@@ -59,7 +60,7 @@ func aptlyRepoAdd(cmd *commander.Command, args []string) error {
repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list)) repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list))
err = context.CollectionFactory().LocalRepoCollection().Update(repo) err = collectionFactory.LocalRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to save: %s", err) return fmt.Errorf("unable to save: %s", err)
} }

View File

@@ -27,15 +27,16 @@ func aptlyRepoCreate(cmd *commander.Command, args []string) error {
} }
} }
collectionFactory := context.NewCollectionFactory()
if len(args) == 4 { if len(args) == 4 {
var snapshot *deb.Snapshot var snapshot *deb.Snapshot
snapshot, err = context.CollectionFactory().SnapshotCollection().ByName(args[3]) snapshot, err = collectionFactory.SnapshotCollection().ByName(args[3])
if err != nil { if err != nil {
return fmt.Errorf("unable to load source snapshot: %s", err) return fmt.Errorf("unable to load source snapshot: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) err = collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to load source snapshot: %s", err) return fmt.Errorf("unable to load source snapshot: %s", err)
} }
@@ -43,7 +44,7 @@ func aptlyRepoCreate(cmd *commander.Command, args []string) error {
repo.UpdateRefList(snapshot.RefList()) repo.UpdateRefList(snapshot.RefList())
} }
err = context.CollectionFactory().LocalRepoCollection().Add(repo) err = collectionFactory.LocalRepoCollection().Add(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to add local repo: %s", err) return fmt.Errorf("unable to add local repo: %s", err)
} }

View File

@@ -15,17 +15,18 @@ func aptlyRepoDrop(cmd *commander.Command, args []string) error {
} }
name := args[0] name := args[0]
collectionFactory := context.NewCollectionFactory()
repo, err := context.CollectionFactory().LocalRepoCollection().ByName(name) repo, err := collectionFactory.LocalRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to drop: %s", err) return fmt.Errorf("unable to drop: %s", err)
} }
published := context.CollectionFactory().PublishedRepoCollection().ByLocalRepo(repo) published := collectionFactory.PublishedRepoCollection().ByLocalRepo(repo)
if len(published) > 0 { if len(published) > 0 {
fmt.Printf("Local repo `%s` is published currently:\n", repo.Name) fmt.Printf("Local repo `%s` is published currently:\n", repo.Name)
for _, repo := range published { for _, repo := range published {
err = context.CollectionFactory().PublishedRepoCollection().LoadComplete(repo, context.CollectionFactory()) err = collectionFactory.PublishedRepoCollection().LoadComplete(repo, collectionFactory)
if err != nil { if err != nil {
return fmt.Errorf("unable to load published: %s", err) return fmt.Errorf("unable to load published: %s", err)
} }
@@ -37,7 +38,7 @@ func aptlyRepoDrop(cmd *commander.Command, args []string) error {
force := context.Flags().Lookup("force").Value.Get().(bool) force := context.Flags().Lookup("force").Value.Get().(bool)
if !force { if !force {
snapshots := context.CollectionFactory().SnapshotCollection().ByLocalRepoSource(repo) snapshots := collectionFactory.SnapshotCollection().ByLocalRepoSource(repo)
if len(snapshots) > 0 { if len(snapshots) > 0 {
fmt.Printf("Local repo `%s` was used to create following snapshots:\n", repo.Name) fmt.Printf("Local repo `%s` was used to create following snapshots:\n", repo.Name)
@@ -49,7 +50,7 @@ func aptlyRepoDrop(cmd *commander.Command, args []string) error {
} }
} }
err = context.CollectionFactory().LocalRepoCollection().Drop(repo) err = collectionFactory.LocalRepoCollection().Drop(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to drop: %s", err) return fmt.Errorf("unable to drop: %s", err)
} }

View File

@@ -16,12 +16,13 @@ func aptlyRepoEdit(cmd *commander.Command, args []string) error {
return commander.ErrCommandError return commander.ErrCommandError
} }
repo, err := context.CollectionFactory().LocalRepoCollection().ByName(args[0]) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.LocalRepoCollection().ByName(args[0])
if err != nil { if err != nil {
return fmt.Errorf("unable to edit: %s", err) return fmt.Errorf("unable to edit: %s", err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) err = collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to edit: %s", err) return fmt.Errorf("unable to edit: %s", err)
} }
@@ -52,7 +53,7 @@ func aptlyRepoEdit(cmd *commander.Command, args []string) error {
} }
} }
err = context.CollectionFactory().LocalRepoCollection().Update(repo) err = collectionFactory.LocalRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to edit: %s", err) return fmt.Errorf("unable to edit: %s", err)
} }

View File

@@ -31,6 +31,7 @@ func aptlyRepoInclude(cmd *commander.Command, args []string) error {
ignoreSignatures := context.Flags().Lookup("ignore-signatures").Value.Get().(bool) ignoreSignatures := context.Flags().Lookup("ignore-signatures").Value.Get().(bool)
noRemoveFiles := context.Flags().Lookup("no-remove-files").Value.Get().(bool) noRemoveFiles := context.Flags().Lookup("no-remove-files").Value.Get().(bool)
repoTemplateString := context.Flags().Lookup("repo").Value.Get().(string) repoTemplateString := context.Flags().Lookup("repo").Value.Get().(string)
collectionFactory := context.NewCollectionFactory()
uploaders := (*deb.Uploaders)(nil) uploaders := (*deb.Uploaders)(nil)
uploadersFile := context.Flags().Lookup("uploaders-file").Value.Get().(string) uploadersFile := context.Flags().Lookup("uploaders-file").Value.Get().(string)
@@ -55,8 +56,8 @@ func aptlyRepoInclude(cmd *commander.Command, args []string) error {
changesFiles, failedFiles = deb.CollectChangesFiles(args, reporter) changesFiles, failedFiles = deb.CollectChangesFiles(args, reporter)
_, failedFiles2, err = deb.ImportChangesFiles( _, failedFiles2, err = deb.ImportChangesFiles(
changesFiles, reporter, acceptUnsigned, ignoreSignatures, forceReplace, noRemoveFiles, verifier, repoTemplateString, changesFiles, reporter, acceptUnsigned, ignoreSignatures, forceReplace, noRemoveFiles, verifier, repoTemplateString,
context.Progress(), context.CollectionFactory().LocalRepoCollection(), context.CollectionFactory().PackageCollection(), context.Progress(), collectionFactory.LocalRepoCollection(), collectionFactory.PackageCollection(),
context.PackagePool(), context.CollectionFactory().ChecksumCollection, context.PackagePool(), collectionFactory.ChecksumCollection,
uploaders, query.Parse) uploaders, query.Parse)
failedFiles = append(failedFiles, failedFiles2...) failedFiles = append(failedFiles, failedFiles2...)

View File

@@ -29,13 +29,14 @@ func aptlyRepoListTxt(cmd *commander.Command, args []string) error {
raw := cmd.Flag.Lookup("raw").Value.Get().(bool) raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
repos := make([]string, context.CollectionFactory().LocalRepoCollection().Len()) collectionFactory := context.NewCollectionFactory()
repos := make([]string, collectionFactory.LocalRepoCollection().Len())
i := 0 i := 0
context.CollectionFactory().LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error { collectionFactory.LocalRepoCollection().ForEach(func(repo *deb.LocalRepo) error {
if raw { if raw {
repos[i] = repo.Name repos[i] = repo.Name
} else { } else {
e := context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) e := collectionFactory.LocalRepoCollection().LoadComplete(repo)
if e != nil { if e != nil {
return e return e
} }

View File

@@ -19,12 +19,13 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
command := cmd.Name() command := cmd.Name()
dstRepo, err := context.CollectionFactory().LocalRepoCollection().ByName(args[1]) collectionFactory := context.NewCollectionFactory()
dstRepo, err := collectionFactory.LocalRepoCollection().ByName(args[1])
if err != nil { if err != nil {
return fmt.Errorf("unable to %s: %s", command, err) return fmt.Errorf("unable to %s: %s", command, err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(dstRepo) err = collectionFactory.LocalRepoCollection().LoadComplete(dstRepo)
if err != nil { if err != nil {
return fmt.Errorf("unable to %s: %s", command, err) return fmt.Errorf("unable to %s: %s", command, err)
} }
@@ -35,7 +36,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
) )
if command == "copy" || command == "move" { // nolint: goconst if command == "copy" || command == "move" { // nolint: goconst
srcRepo, err = context.CollectionFactory().LocalRepoCollection().ByName(args[0]) srcRepo, err = collectionFactory.LocalRepoCollection().ByName(args[0])
if err != nil { if err != nil {
return fmt.Errorf("unable to %s: %s", command, err) return fmt.Errorf("unable to %s: %s", command, err)
} }
@@ -44,7 +45,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to %s: source and destination are the same", command) return fmt.Errorf("unable to %s: source and destination are the same", command)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(srcRepo) err = collectionFactory.LocalRepoCollection().LoadComplete(srcRepo)
if err != nil { if err != nil {
return fmt.Errorf("unable to %s: %s", command, err) return fmt.Errorf("unable to %s: %s", command, err)
} }
@@ -53,12 +54,12 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
} else if command == "import" { // nolint: goconst } else if command == "import" { // nolint: goconst
var srcRemoteRepo *deb.RemoteRepo var srcRemoteRepo *deb.RemoteRepo
srcRemoteRepo, err = context.CollectionFactory().RemoteRepoCollection().ByName(args[0]) srcRemoteRepo, err = collectionFactory.RemoteRepoCollection().ByName(args[0])
if err != nil { if err != nil {
return fmt.Errorf("unable to %s: %s", command, err) return fmt.Errorf("unable to %s: %s", command, err)
} }
err = context.CollectionFactory().RemoteRepoCollection().LoadComplete(srcRemoteRepo) err = collectionFactory.RemoteRepoCollection().LoadComplete(srcRemoteRepo)
if err != nil { if err != nil {
return fmt.Errorf("unable to %s: %s", command, err) return fmt.Errorf("unable to %s: %s", command, err)
} }
@@ -74,12 +75,12 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
context.Progress().Printf("Loading packages...\n") context.Progress().Printf("Loading packages...\n")
dstList, err := deb.NewPackageListFromRefList(dstRepo.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) dstList, err := deb.NewPackageListFromRefList(dstRepo.RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
srcList, err := deb.NewPackageListFromRefList(srcRefList, context.CollectionFactory().PackageCollection(), context.Progress()) srcList, err := deb.NewPackageListFromRefList(srcRefList, collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
@@ -151,7 +152,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
} else { } else {
dstRepo.UpdateRefList(deb.NewPackageRefListFromPackageList(dstList)) dstRepo.UpdateRefList(deb.NewPackageRefListFromPackageList(dstList))
err = context.CollectionFactory().LocalRepoCollection().Update(dstRepo) err = collectionFactory.LocalRepoCollection().Update(dstRepo)
if err != nil { if err != nil {
return fmt.Errorf("unable to save: %s", err) return fmt.Errorf("unable to save: %s", err)
} }
@@ -159,7 +160,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
if command == "move" { // nolint: goconst if command == "move" { // nolint: goconst
srcRepo.UpdateRefList(deb.NewPackageRefListFromPackageList(srcList)) srcRepo.UpdateRefList(deb.NewPackageRefListFromPackageList(srcList))
err = context.CollectionFactory().LocalRepoCollection().Update(srcRepo) err = collectionFactory.LocalRepoCollection().Update(srcRepo)
if err != nil { if err != nil {
return fmt.Errorf("unable to save: %s", err) return fmt.Errorf("unable to save: %s", err)
} }

View File

@@ -18,19 +18,20 @@ func aptlyRepoRemove(cmd *commander.Command, args []string) error {
name := args[0] name := args[0]
repo, err := context.CollectionFactory().LocalRepoCollection().ByName(name) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.LocalRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to remove: %s", err) return fmt.Errorf("unable to remove: %s", err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) err = collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to remove: %s", err) return fmt.Errorf("unable to remove: %s", err)
} }
context.Progress().Printf("Loading packages...\n") context.Progress().Printf("Loading packages...\n")
list, err := deb.NewPackageListFromRefList(repo.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) list, err := deb.NewPackageListFromRefList(repo.RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
@@ -60,7 +61,7 @@ func aptlyRepoRemove(cmd *commander.Command, args []string) error {
} else { } else {
repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list)) repo.UpdateRefList(deb.NewPackageRefListFromPackageList(list))
err = context.CollectionFactory().LocalRepoCollection().Update(repo) err = collectionFactory.LocalRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to save: %s", err) return fmt.Errorf("unable to save: %s", err)
} }

View File

@@ -20,18 +20,19 @@ func aptlyRepoRename(cmd *commander.Command, args []string) error {
oldName, newName := args[0], args[1] oldName, newName := args[0], args[1]
repo, err = context.CollectionFactory().LocalRepoCollection().ByName(oldName) collectionFactory := context.NewCollectionFactory()
repo, err = collectionFactory.LocalRepoCollection().ByName(oldName)
if err != nil { if err != nil {
return fmt.Errorf("unable to rename: %s", err) return fmt.Errorf("unable to rename: %s", err)
} }
_, err = context.CollectionFactory().LocalRepoCollection().ByName(newName) _, err = collectionFactory.LocalRepoCollection().ByName(newName)
if err == nil { if err == nil {
return fmt.Errorf("unable to rename: local repo %s already exists", newName) return fmt.Errorf("unable to rename: local repo %s already exists", newName)
} }
repo.Name = newName repo.Name = newName
err = context.CollectionFactory().LocalRepoCollection().Update(repo) err = collectionFactory.LocalRepoCollection().Update(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to rename: %s", err) return fmt.Errorf("unable to rename: %s", err)
} }

View File

@@ -30,12 +30,13 @@ func aptlyRepoShowTxt(cmd *commander.Command, args []string) error {
name := args[0] name := args[0]
repo, err := context.CollectionFactory().LocalRepoCollection().ByName(name) collectionFactory := context.NewCollectionFactory()
repo, err := collectionFactory.LocalRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to show: %s", err) return fmt.Errorf("unable to show: %s", err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) err = collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to show: %s", err) return fmt.Errorf("unable to show: %s", err)
} }
@@ -51,7 +52,7 @@ func aptlyRepoShowTxt(cmd *commander.Command, args []string) error {
withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool) withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool)
if withPackages { if withPackages {
ListPackagesRefList(repo.RefList()) ListPackagesRefList(repo.RefList(), collectionFactory)
} }
return err return err

View File

@@ -34,7 +34,8 @@ func aptlyServe(cmd *commander.Command, args []string) error {
return err return err
} }
if context.CollectionFactory().PublishedRepoCollection().Len() == 0 { collectionFactory := context.NewCollectionFactory()
if collectionFactory.PublishedRepoCollection().Len() == 0 {
fmt.Printf("No published repositories, unable to serve.\n") fmt.Printf("No published repositories, unable to serve.\n")
return nil return nil
} }
@@ -56,11 +57,11 @@ func aptlyServe(cmd *commander.Command, args []string) error {
fmt.Printf("Serving published repositories, recommended apt sources list:\n\n") fmt.Printf("Serving published repositories, recommended apt sources list:\n\n")
sources := make(sort.StringSlice, 0, context.CollectionFactory().PublishedRepoCollection().Len()) sources := make(sort.StringSlice, 0, collectionFactory.PublishedRepoCollection().Len())
published := make(map[string]*deb.PublishedRepo, context.CollectionFactory().PublishedRepoCollection().Len()) published := make(map[string]*deb.PublishedRepo, collectionFactory.PublishedRepoCollection().Len())
err = context.CollectionFactory().PublishedRepoCollection().ForEach(func(repo *deb.PublishedRepo) error { err = collectionFactory.PublishedRepoCollection().ForEach(func(repo *deb.PublishedRepo) error {
e := context.CollectionFactory().PublishedRepoCollection().LoadComplete(repo, context.CollectionFactory()) e := collectionFactory.PublishedRepoCollection().LoadComplete(repo, collectionFactory)
if e != nil { if e != nil {
return e return e
} }

View File

@@ -13,13 +13,14 @@ func aptlySnapshotCreate(cmd *commander.Command, args []string) error {
snapshot *deb.Snapshot snapshot *deb.Snapshot
) )
collectionFactory := context.NewCollectionFactory()
if len(args) == 4 && args[1] == "from" && args[2] == "mirror" { // nolint: goconst if len(args) == 4 && args[1] == "from" && args[2] == "mirror" { // nolint: goconst
// aptly snapshot create snap from mirror mirror // aptly snapshot create snap from mirror mirror
var repo *deb.RemoteRepo var repo *deb.RemoteRepo
repoName, snapshotName := args[3], args[0] repoName, snapshotName := args[3], args[0]
repo, err = context.CollectionFactory().RemoteRepoCollection().ByName(repoName) repo, err = collectionFactory.RemoteRepoCollection().ByName(repoName)
if err != nil { if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }
@@ -29,7 +30,7 @@ func aptlySnapshotCreate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }
err = context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo) err = collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }
@@ -44,12 +45,12 @@ func aptlySnapshotCreate(cmd *commander.Command, args []string) error {
localRepoName, snapshotName := args[3], args[0] localRepoName, snapshotName := args[3], args[0]
repo, err = context.CollectionFactory().LocalRepoCollection().ByName(localRepoName) repo, err = collectionFactory.LocalRepoCollection().ByName(localRepoName)
if err != nil { if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) err = collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }
@@ -70,7 +71,7 @@ func aptlySnapshotCreate(cmd *commander.Command, args []string) error {
return commander.ErrCommandError return commander.ErrCommandError
} }
err = context.CollectionFactory().SnapshotCollection().Add(snapshot) err = collectionFactory.SnapshotCollection().Add(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to add snapshot: %s", err) return fmt.Errorf("unable to add snapshot: %s", err)
} }

View File

@@ -15,31 +15,32 @@ func aptlySnapshotDiff(cmd *commander.Command, args []string) error {
} }
onlyMatching := context.Flags().Lookup("only-matching").Value.Get().(bool) onlyMatching := context.Flags().Lookup("only-matching").Value.Get().(bool)
collectionFactory := context.NewCollectionFactory()
// Load <name-a> snapshot // Load <name-a> snapshot
snapshotA, err := context.CollectionFactory().SnapshotCollection().ByName(args[0]) snapshotA, err := collectionFactory.SnapshotCollection().ByName(args[0])
if err != nil { if err != nil {
return fmt.Errorf("unable to load snapshot A: %s", err) return fmt.Errorf("unable to load snapshot A: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshotA) err = collectionFactory.SnapshotCollection().LoadComplete(snapshotA)
if err != nil { if err != nil {
return fmt.Errorf("unable to load snapshot A: %s", err) return fmt.Errorf("unable to load snapshot A: %s", err)
} }
// Load <name-b> snapshot // Load <name-b> snapshot
snapshotB, err := context.CollectionFactory().SnapshotCollection().ByName(args[1]) snapshotB, err := collectionFactory.SnapshotCollection().ByName(args[1])
if err != nil { if err != nil {
return fmt.Errorf("unable to load snapshot B: %s", err) return fmt.Errorf("unable to load snapshot B: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshotB) err = collectionFactory.SnapshotCollection().LoadComplete(snapshotB)
if err != nil { if err != nil {
return fmt.Errorf("unable to load snapshot B: %s", err) return fmt.Errorf("unable to load snapshot B: %s", err)
} }
// Calculate diff // Calculate diff
diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), context.CollectionFactory().PackageCollection()) diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), collectionFactory.PackageCollection())
if err != nil { if err != nil {
return fmt.Errorf("unable to calculate diff: %s", err) return fmt.Errorf("unable to calculate diff: %s", err)
} }

View File

@@ -15,18 +15,19 @@ func aptlySnapshotDrop(cmd *commander.Command, args []string) error {
} }
name := args[0] name := args[0]
collectionFactory := context.NewCollectionFactory()
snapshot, err := context.CollectionFactory().SnapshotCollection().ByName(name) snapshot, err := collectionFactory.SnapshotCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to drop: %s", err) return fmt.Errorf("unable to drop: %s", err)
} }
published := context.CollectionFactory().PublishedRepoCollection().BySnapshot(snapshot) published := collectionFactory.PublishedRepoCollection().BySnapshot(snapshot)
if len(published) > 0 { if len(published) > 0 {
fmt.Printf("Snapshot `%s` is published currently:\n", snapshot.Name) fmt.Printf("Snapshot `%s` is published currently:\n", snapshot.Name)
for _, repo := range published { for _, repo := range published {
err = context.CollectionFactory().PublishedRepoCollection().LoadComplete(repo, context.CollectionFactory()) err = collectionFactory.PublishedRepoCollection().LoadComplete(repo, collectionFactory)
if err != nil { if err != nil {
return fmt.Errorf("unable to load published: %s", err) return fmt.Errorf("unable to load published: %s", err)
} }
@@ -38,7 +39,7 @@ func aptlySnapshotDrop(cmd *commander.Command, args []string) error {
force := context.Flags().Lookup("force").Value.Get().(bool) force := context.Flags().Lookup("force").Value.Get().(bool)
if !force { if !force {
snapshots := context.CollectionFactory().SnapshotCollection().BySnapshotSource(snapshot) snapshots := collectionFactory.SnapshotCollection().BySnapshotSource(snapshot)
if len(snapshots) > 0 { if len(snapshots) > 0 {
fmt.Printf("Snapshot `%s` was used as a source in following snapshots:\n", snapshot.Name) fmt.Printf("Snapshot `%s` was used as a source in following snapshots:\n", snapshot.Name)
for _, snap := range snapshots { for _, snap := range snapshots {
@@ -49,7 +50,7 @@ func aptlySnapshotDrop(cmd *commander.Command, args []string) error {
} }
} }
err = context.CollectionFactory().SnapshotCollection().Drop(snapshot) err = collectionFactory.SnapshotCollection().Drop(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to drop: %s", err) return fmt.Errorf("unable to drop: %s", err)
} }

View File

@@ -19,21 +19,22 @@ func aptlySnapshotFilter(cmd *commander.Command, args []string) error {
} }
withDeps := context.Flags().Lookup("with-deps").Value.Get().(bool) withDeps := context.Flags().Lookup("with-deps").Value.Get().(bool)
collectionFactory := context.NewCollectionFactory()
// Load <source> snapshot // Load <source> snapshot
source, err := context.CollectionFactory().SnapshotCollection().ByName(args[0]) source, err := collectionFactory.SnapshotCollection().ByName(args[0])
if err != nil { if err != nil {
return fmt.Errorf("unable to filter: %s", err) return fmt.Errorf("unable to filter: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(source) err = collectionFactory.SnapshotCollection().LoadComplete(source)
if err != nil { if err != nil {
return fmt.Errorf("unable to filter: %s", err) return fmt.Errorf("unable to filter: %s", err)
} }
// Convert snapshot to package list // Convert snapshot to package list
context.Progress().Printf("Loading packages (%d)...\n", source.RefList().Len()) context.Progress().Printf("Loading packages (%d)...\n", source.RefList().Len())
packageList, err := deb.NewPackageListFromRefList(source.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) packageList, err := deb.NewPackageListFromRefList(source.RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
@@ -75,7 +76,7 @@ func aptlySnapshotFilter(cmd *commander.Command, args []string) error {
destination := deb.NewSnapshotFromPackageList(args[1], []*deb.Snapshot{source}, result, destination := deb.NewSnapshotFromPackageList(args[1], []*deb.Snapshot{source}, result,
fmt.Sprintf("Filtered '%s', query was: '%s'", source.Name, strings.Join(args[2:], " "))) fmt.Sprintf("Filtered '%s', query was: '%s'", source.Name, strings.Join(args[2:], " ")))
err = context.CollectionFactory().SnapshotCollection().Add(destination) err = collectionFactory.SnapshotCollection().Add(destination)
if err != nil { if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }

View File

@@ -29,7 +29,8 @@ func aptlySnapshotListTxt(cmd *commander.Command, args []string) error {
raw := cmd.Flag.Lookup("raw").Value.Get().(bool) raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
sortMethodString := cmd.Flag.Lookup("sort").Value.Get().(string) sortMethodString := cmd.Flag.Lookup("sort").Value.Get().(string)
collection := context.CollectionFactory().SnapshotCollection() collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.SnapshotCollection()
if raw { if raw {
collection.ForEachSorted(sortMethodString, func(snapshot *deb.Snapshot) error { collection.ForEachSorted(sortMethodString, func(snapshot *deb.Snapshot) error {

View File

@@ -15,15 +15,16 @@ func aptlySnapshotMerge(cmd *commander.Command, args []string) error {
return commander.ErrCommandError return commander.ErrCommandError
} }
collectionFactory := context.NewCollectionFactory()
sources := make([]*deb.Snapshot, len(args)-1) sources := make([]*deb.Snapshot, len(args)-1)
for i := 0; i < len(args)-1; i++ { for i := 0; i < len(args)-1; i++ {
sources[i], err = context.CollectionFactory().SnapshotCollection().ByName(args[i+1]) sources[i], err = collectionFactory.SnapshotCollection().ByName(args[i+1])
if err != nil { if err != nil {
return fmt.Errorf("unable to load snapshot: %s", err) return fmt.Errorf("unable to load snapshot: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(sources[i]) err = collectionFactory.SnapshotCollection().LoadComplete(sources[i])
if err != nil { if err != nil {
return fmt.Errorf("unable to load snapshot: %s", err) return fmt.Errorf("unable to load snapshot: %s", err)
} }
@@ -56,7 +57,7 @@ func aptlySnapshotMerge(cmd *commander.Command, args []string) error {
destination := deb.NewSnapshotFromRefList(args[0], sources, result, destination := deb.NewSnapshotFromRefList(args[0], sources, result,
fmt.Sprintf("Merged from sources: %s", strings.Join(sourceDescription, ", "))) fmt.Sprintf("Merged from sources: %s", strings.Join(sourceDescription, ", ")))
err = context.CollectionFactory().SnapshotCollection().Add(destination) err = collectionFactory.SnapshotCollection().Add(destination)
if err != nil { if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }

View File

@@ -21,25 +21,26 @@ func aptlySnapshotPull(cmd *commander.Command, args []string) error {
noDeps := context.Flags().Lookup("no-deps").Value.Get().(bool) noDeps := context.Flags().Lookup("no-deps").Value.Get().(bool)
noRemove := context.Flags().Lookup("no-remove").Value.Get().(bool) noRemove := context.Flags().Lookup("no-remove").Value.Get().(bool)
allMatches := context.Flags().Lookup("all-matches").Value.Get().(bool) allMatches := context.Flags().Lookup("all-matches").Value.Get().(bool)
collectionFactory := context.NewCollectionFactory()
// Load <name> snapshot // Load <name> snapshot
snapshot, err := context.CollectionFactory().SnapshotCollection().ByName(args[0]) snapshot, err := collectionFactory.SnapshotCollection().ByName(args[0])
if err != nil { if err != nil {
return fmt.Errorf("unable to pull: %s", err) return fmt.Errorf("unable to pull: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) err = collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to pull: %s", err) return fmt.Errorf("unable to pull: %s", err)
} }
// Load <source> snapshot // Load <source> snapshot
source, err := context.CollectionFactory().SnapshotCollection().ByName(args[1]) source, err := collectionFactory.SnapshotCollection().ByName(args[1])
if err != nil { if err != nil {
return fmt.Errorf("unable to pull: %s", err) return fmt.Errorf("unable to pull: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(source) err = collectionFactory.SnapshotCollection().LoadComplete(source)
if err != nil { if err != nil {
return fmt.Errorf("unable to pull: %s", err) return fmt.Errorf("unable to pull: %s", err)
} }
@@ -49,12 +50,12 @@ func aptlySnapshotPull(cmd *commander.Command, args []string) error {
// Convert snapshot to package list // Convert snapshot to package list
context.Progress().Printf("Loading packages (%d)...\n", snapshot.RefList().Len()+source.RefList().Len()) context.Progress().Printf("Loading packages (%d)...\n", snapshot.RefList().Len()+source.RefList().Len())
packageList, err := deb.NewPackageListFromRefList(snapshot.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) packageList, err := deb.NewPackageListFromRefList(snapshot.RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
sourcePackageList, err := deb.NewPackageListFromRefList(source.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) sourcePackageList, err := deb.NewPackageListFromRefList(source.RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
@@ -137,7 +138,7 @@ func aptlySnapshotPull(cmd *commander.Command, args []string) error {
destination := deb.NewSnapshotFromPackageList(args[2], []*deb.Snapshot{snapshot, source}, packageList, destination := deb.NewSnapshotFromPackageList(args[2], []*deb.Snapshot{snapshot, source}, packageList,
fmt.Sprintf("Pulled into '%s' with '%s' as source, pull request was: '%s'", snapshot.Name, source.Name, strings.Join(args[3:], " "))) fmt.Sprintf("Pulled into '%s' with '%s' as source, pull request was: '%s'", snapshot.Name, source.Name, strings.Join(args[3:], " ")))
err = context.CollectionFactory().SnapshotCollection().Add(destination) err = collectionFactory.SnapshotCollection().Add(destination)
if err != nil { if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err) return fmt.Errorf("unable to create snapshot: %s", err)
} }

View File

@@ -19,19 +19,20 @@ func aptlySnapshotRename(cmd *commander.Command, args []string) error {
} }
oldName, newName := args[0], args[1] oldName, newName := args[0], args[1]
collectionFactory := context.NewCollectionFactory()
snapshot, err = context.CollectionFactory().SnapshotCollection().ByName(oldName) snapshot, err = collectionFactory.SnapshotCollection().ByName(oldName)
if err != nil { if err != nil {
return fmt.Errorf("unable to rename: %s", err) return fmt.Errorf("unable to rename: %s", err)
} }
_, err = context.CollectionFactory().SnapshotCollection().ByName(newName) _, err = collectionFactory.SnapshotCollection().ByName(newName)
if err == nil { if err == nil {
return fmt.Errorf("unable to rename: snapshot %s already exists", newName) return fmt.Errorf("unable to rename: snapshot %s already exists", newName)
} }
snapshot.Name = newName snapshot.Name = newName
err = context.CollectionFactory().SnapshotCollection().Update(snapshot) err = collectionFactory.SnapshotCollection().Update(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to rename: %s", err) return fmt.Errorf("unable to rename: %s", err)
} }

View File

@@ -23,17 +23,18 @@ func aptlySnapshotMirrorRepoSearch(cmd *commander.Command, args []string) error
name := args[0] name := args[0]
command := cmd.Parent.Name() command := cmd.Parent.Name()
collectionFactory := context.NewCollectionFactory()
var reflist *deb.PackageRefList var reflist *deb.PackageRefList
if command == "snapshot" { // nolint: goconst if command == "snapshot" { // nolint: goconst
var snapshot *deb.Snapshot var snapshot *deb.Snapshot
snapshot, err = context.CollectionFactory().SnapshotCollection().ByName(name) snapshot, err = collectionFactory.SnapshotCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to search: %s", err) return fmt.Errorf("unable to search: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) err = collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to search: %s", err) return fmt.Errorf("unable to search: %s", err)
} }
@@ -41,12 +42,12 @@ func aptlySnapshotMirrorRepoSearch(cmd *commander.Command, args []string) error
reflist = snapshot.RefList() reflist = snapshot.RefList()
} else if command == "mirror" { } else if command == "mirror" {
var repo *deb.RemoteRepo var repo *deb.RemoteRepo
repo, err = context.CollectionFactory().RemoteRepoCollection().ByName(name) repo, err = collectionFactory.RemoteRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to search: %s", err) return fmt.Errorf("unable to search: %s", err)
} }
err = context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo) err = collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to search: %s", err) return fmt.Errorf("unable to search: %s", err)
} }
@@ -54,12 +55,12 @@ func aptlySnapshotMirrorRepoSearch(cmd *commander.Command, args []string) error
reflist = repo.RefList() reflist = repo.RefList()
} else if command == "repo" { // nolint: goconst } else if command == "repo" { // nolint: goconst
var repo *deb.LocalRepo var repo *deb.LocalRepo
repo, err = context.CollectionFactory().LocalRepoCollection().ByName(name) repo, err = collectionFactory.LocalRepoCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to search: %s", err) return fmt.Errorf("unable to search: %s", err)
} }
err = context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) err = collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil { if err != nil {
return fmt.Errorf("unable to search: %s", err) return fmt.Errorf("unable to search: %s", err)
} }
@@ -69,7 +70,7 @@ func aptlySnapshotMirrorRepoSearch(cmd *commander.Command, args []string) error
panic("unknown command") panic("unknown command")
} }
list, err := deb.NewPackageListFromRefList(reflist, context.CollectionFactory().PackageCollection(), context.Progress()) list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to search: %s", err) return fmt.Errorf("unable to search: %s", err)
} }

View File

@@ -28,13 +28,14 @@ func aptlySnapshotShow(cmd *commander.Command, args []string) error {
func aptlySnapshotShowTxt(cmd *commander.Command, args []string) error { func aptlySnapshotShowTxt(cmd *commander.Command, args []string) error {
var err error var err error
name := args[0] name := args[0]
collectionFactory := context.NewCollectionFactory()
snapshot, err := context.CollectionFactory().SnapshotCollection().ByName(name) snapshot, err := collectionFactory.SnapshotCollection().ByName(name)
if err != nil { if err != nil {
return fmt.Errorf("unable to show: %s", err) return fmt.Errorf("unable to show: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) err = collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil { if err != nil {
return fmt.Errorf("unable to show: %s", err) return fmt.Errorf("unable to show: %s", err)
} }
@@ -49,21 +50,21 @@ func aptlySnapshotShowTxt(cmd *commander.Command, args []string) error {
var name string var name string
if snapshot.SourceKind == deb.SourceSnapshot { if snapshot.SourceKind == deb.SourceSnapshot {
var source *deb.Snapshot var source *deb.Snapshot
source, err = context.CollectionFactory().SnapshotCollection().ByUUID(sourceID) source, err = collectionFactory.SnapshotCollection().ByUUID(sourceID)
if err != nil { if err != nil {
continue continue
} }
name = source.Name name = source.Name
} else if snapshot.SourceKind == deb.SourceLocalRepo { } else if snapshot.SourceKind == deb.SourceLocalRepo {
var source *deb.LocalRepo var source *deb.LocalRepo
source, err = context.CollectionFactory().LocalRepoCollection().ByUUID(sourceID) source, err = collectionFactory.LocalRepoCollection().ByUUID(sourceID)
if err != nil { if err != nil {
continue continue
} }
name = source.Name name = source.Name
} else if snapshot.SourceKind == deb.SourceRemoteRepo { } else if snapshot.SourceKind == deb.SourceRemoteRepo {
var source *deb.RemoteRepo var source *deb.RemoteRepo
source, err = context.CollectionFactory().RemoteRepoCollection().ByUUID(sourceID) source, err = collectionFactory.RemoteRepoCollection().ByUUID(sourceID)
if err != nil { if err != nil {
continue continue
} }
@@ -78,7 +79,7 @@ func aptlySnapshotShowTxt(cmd *commander.Command, args []string) error {
withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool) withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool)
if withPackages { if withPackages {
ListPackagesRefList(snapshot.RefList()) ListPackagesRefList(snapshot.RefList(), collectionFactory)
} }
return err return err

View File

@@ -16,13 +16,14 @@ func aptlySnapshotVerify(cmd *commander.Command, args []string) error {
} }
snapshots := make([]*deb.Snapshot, len(args)) snapshots := make([]*deb.Snapshot, len(args))
collectionFactory := context.NewCollectionFactory()
for i := range snapshots { for i := range snapshots {
snapshots[i], err = context.CollectionFactory().SnapshotCollection().ByName(args[i]) snapshots[i], err = collectionFactory.SnapshotCollection().ByName(args[i])
if err != nil { if err != nil {
return fmt.Errorf("unable to verify: %s", err) return fmt.Errorf("unable to verify: %s", err)
} }
err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshots[i]) err = collectionFactory.SnapshotCollection().LoadComplete(snapshots[i])
if err != nil { if err != nil {
return fmt.Errorf("unable to verify: %s", err) return fmt.Errorf("unable to verify: %s", err)
} }
@@ -30,7 +31,7 @@ func aptlySnapshotVerify(cmd *commander.Command, args []string) error {
context.Progress().Printf("Loading packages...\n") context.Progress().Printf("Loading packages...\n")
packageList, err := deb.NewPackageListFromRefList(snapshots[0].RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) packageList, err := deb.NewPackageListFromRefList(snapshots[0].RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }
@@ -43,7 +44,7 @@ func aptlySnapshotVerify(cmd *commander.Command, args []string) error {
var pL *deb.PackageList var pL *deb.PackageList
for i := 1; i < len(snapshots); i++ { for i := 1; i < len(snapshots); i++ {
pL, err = deb.NewPackageListFromRefList(snapshots[i].RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) pL, err = deb.NewPackageListFromRefList(snapshots[i].RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil { if err != nil {
return fmt.Errorf("unable to load packages: %s", err) return fmt.Errorf("unable to load packages: %s", err)
} }

View File

@@ -44,7 +44,6 @@ type AptlyContext struct {
database database.Storage database database.Storage
packagePool aptly.PackagePool packagePool aptly.PackagePool
publishedStorages map[string]aptly.PublishedStorage publishedStorages map[string]aptly.PublishedStorage
collectionFactory *deb.CollectionFactory
dependencyOptions int dependencyOptions int
architecturesList []string architecturesList []string
// Debug features // Debug features
@@ -309,20 +308,16 @@ func (context *AptlyContext) ReOpenDatabase() error {
return err return err
} }
// CollectionFactory builds factory producing all kinds of collections // NewCollectionFactory builds factory producing all kinds of collections
func (context *AptlyContext) CollectionFactory() *deb.CollectionFactory { func (context *AptlyContext) NewCollectionFactory() *deb.CollectionFactory {
context.Lock() context.Lock()
defer context.Unlock() defer context.Unlock()
if context.collectionFactory == nil { db, err := context._database()
db, err := context._database() if err != nil {
if err != nil { Fatal(err)
Fatal(err)
}
context.collectionFactory = deb.NewCollectionFactory(db)
} }
return deb.NewCollectionFactory(db)
return context.collectionFactory
} }
// PackagePool returns instance of PackagePool // PackagePool returns instance of PackagePool