db batch may not be a global resource

This way db usage is safe.
This commit is contained in:
Oliver Sauder
2016-11-21 12:03:32 +01:00
committed by Lorenzo Bolla
parent f7f42a9cd8
commit 1c7c07ace7
5 changed files with 19 additions and 82 deletions

View File

@@ -188,9 +188,6 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
err = toDelete.ForEach(func(ref []byte) error {
return collectionFactory.PackageCollection().DeleteByKey(ref, batch)
})
if err != nil {
return err
}
err = batch.Write()
if err != nil {

View File

@@ -159,15 +159,9 @@ func (collection *LocalRepoCollection) Add(repo *LocalRepo) error {
// Update stores updated information about repo in DB
func (collection *LocalRepoCollection) Update(repo *LocalRepo) error {
batch := collection.db.CreateBatch()
err := batch.Put(repo.Key(), repo.Encode())
if err != nil {
return err
}
batch.Put(repo.Key(), repo.Encode())
if repo.packageRefs != nil {
err = batch.Put(repo.RefKey(), repo.packageRefs.Encode())
if err != nil {
return err
}
batch.Put(repo.RefKey(), repo.packageRefs.Encode())
}
return batch.Write()
}
@@ -252,15 +246,7 @@ func (collection *LocalRepoCollection) Drop(repo *LocalRepo) error {
delete(collection.cache, repo.UUID)
batch := collection.db.CreateBatch()
err = batch.Delete(repo.Key())
if err != nil {
return err
}
err = batch.Delete(repo.RefKey())
if err != nil {
return err
}
batch.Delete(repo.Key())
batch.Delete(repo.RefKey())
return batch.Write()
}

View File

@@ -637,8 +637,7 @@ func (p *PublishedRepo) Publish(packagePool aptly.PackagePool, publishedStorageP
// to push each path of the package into the database.
// We'll want this batched so as to avoid an excessive
// amount of write() calls.
tempBatch := tempDB.CreateBatch()
defer tempBatch.Write()
batch := tempDB.CreateBatch()
for _, arch := range p.Architectures {
if pkg.MatchesArchitecture(arch) {
@@ -657,7 +656,7 @@ func (p *PublishedRepo) Publish(packagePool aptly.PackagePool, publishedStorageP
contentIndexesMap[key] = contentIndex
}
contentIndex.Push(qualifiedName, contents, tempBatch)
contentIndex.Push(qualifiedName, contents, batch)
}
}
@@ -682,7 +681,7 @@ func (p *PublishedRepo) Publish(packagePool aptly.PackagePool, publishedStorageP
pkg.extra = nil
pkg.contents = nil
return nil
return batch.Write()
})
if err != nil {
@@ -940,23 +939,11 @@ func (collection *PublishedRepoCollection) CheckDuplicate(repo *PublishedRepo) *
// Update stores updated information about repo in DB
func (collection *PublishedRepoCollection) Update(repo *PublishedRepo) error {
batch := collection.db.CreateBatch()
err := batch.Put(repo.Key(), repo.Encode())
if err != nil {
return err
}
err = batch.Put(repo.Key(), repo.Encode())
if err != nil {
return err
}
batch.Put(repo.Key(), repo.Encode())
if repo.SourceKind == SourceLocalRepo {
for component, item := range repo.sourceItems {
err = batch.Put(repo.RefKey(component), item.packageRefs.Encode())
if err != nil {
return err
}
batch.Put(repo.RefKey(component), item.packageRefs.Encode())
}
}
return batch.Write()
@@ -1254,16 +1241,10 @@ func (collection *PublishedRepoCollection) Remove(publishedStorageProvider aptly
}
batch := collection.db.CreateBatch()
err = batch.Delete(repo.Key())
if err != nil {
return err
}
batch.Delete(repo.Key())
for _, component := range repo.Components() {
err = batch.Delete(repo.RefKey(component))
if err != nil {
return err
}
batch.Delete(repo.RefKey(component))
}
return batch.Write()

View File

@@ -819,15 +819,9 @@ func (collection *RemoteRepoCollection) Add(repo *RemoteRepo) error {
func (collection *RemoteRepoCollection) Update(repo *RemoteRepo) error {
batch := collection.db.CreateBatch()
err := batch.Put(repo.Key(), repo.Encode())
if err != nil {
return err
}
batch.Put(repo.Key(), repo.Encode())
if repo.packageRefs != nil {
err = batch.Put(repo.RefKey(), repo.packageRefs.Encode())
if err != nil {
return err
}
batch.Put(repo.RefKey(), repo.packageRefs.Encode())
}
return batch.Write()
}
@@ -919,14 +913,7 @@ func (collection *RemoteRepoCollection) Drop(repo *RemoteRepo) error {
delete(collection.cache, repo.UUID)
batch := collection.db.CreateBatch()
err = batch.Delete(repo.Key())
if err != nil {
return err
}
err = batch.Delete(repo.RefKey())
if err != nil {
return err
}
batch.Delete(repo.Key())
batch.Delete(repo.RefKey())
return batch.Write()
}

View File

@@ -221,15 +221,9 @@ func (collection *SnapshotCollection) Add(snapshot *Snapshot) error {
func (collection *SnapshotCollection) Update(snapshot *Snapshot) error {
batch := collection.db.CreateBatch()
err := batch.Put(snapshot.Key(), snapshot.Encode())
if err != nil {
return err
}
batch.Put(snapshot.Key(), snapshot.Encode())
if snapshot.packageRefs != nil {
err = batch.Put(snapshot.RefKey(), snapshot.packageRefs.Encode())
if err != nil {
return err
}
batch.Put(snapshot.RefKey(), snapshot.packageRefs.Encode())
}
return batch.Write()
@@ -405,16 +399,8 @@ func (collection *SnapshotCollection) Drop(snapshot *Snapshot) error {
delete(collection.cache, snapshot.UUID)
batch := collection.db.CreateBatch()
err = batch.Delete(snapshot.Key())
if err != nil {
return err
}
err = batch.Delete(snapshot.RefKey())
if err != nil {
return err
}
batch.Delete(snapshot.Key())
batch.Delete(snapshot.RefKey())
return batch.Write()
}