database: rename utility functions with commit/rollback

All database utility functions are renamed to explicitly say if it will
commit changes or rollback changes on success.
This commit is contained in:
Sida Chen 2018-10-08 13:12:18 -04:00
parent e657d26313
commit a3e9b5b55d
3 changed files with 27 additions and 26 deletions

View File

@ -48,9 +48,9 @@ func DeduplicateFeatures(features ...Feature) []Feature {
return uniqueFeatures
}
// PersistPartialLayer wraps session PersistLayer function with begin and
// PersistPartialLayerAndCommit wraps session PersistLayer function with begin and
// commit.
func PersistPartialLayer(datastore Datastore, layer *Layer) error {
func PersistPartialLayerAndCommit(datastore Datastore, layer *Layer) error {
tx, err := datastore.Begin()
if err != nil {
return err
@ -64,8 +64,8 @@ func PersistPartialLayer(datastore Datastore, layer *Layer) error {
return tx.Commit()
}
// PersistFeatures wraps session PersistFeatures function with begin and commit.
func PersistFeatures(datastore Datastore, features []Feature) error {
// PersistFeaturesAndCommit wraps session PersistFeaturesAndCommit function with begin and commit.
func PersistFeaturesAndCommit(datastore Datastore, features []Feature) error {
tx, err := datastore.Begin()
if err != nil {
return err
@ -78,9 +78,9 @@ func PersistFeatures(datastore Datastore, features []Feature) error {
return tx.Commit()
}
// PersistNamespaces wraps session PersistNamespaces function with begin and
// commit.
func PersistNamespaces(datastore Datastore, namespaces []Namespace) error {
// PersistNamespacesAndCommit wraps session PersistNamespaces function with
// begin and commit.
func PersistNamespacesAndCommit(datastore Datastore, namespaces []Namespace) error {
tx, err := datastore.Begin()
if err != nil {
return err
@ -94,8 +94,9 @@ func PersistNamespaces(datastore Datastore, namespaces []Namespace) error {
return tx.Commit()
}
// FindAncestry wraps session FindAncestry function with begin and rollback.
func FindAncestry(datastore Datastore, name string) (Ancestry, bool, error) {
// FindAncestryAndRollback wraps session FindAncestry function with begin and
// rollback.
func FindAncestryAndRollback(datastore Datastore, name string) (Ancestry, bool, error) {
tx, err := datastore.Begin()
defer tx.Rollback()
@ -106,8 +107,8 @@ func FindAncestry(datastore Datastore, name string) (Ancestry, bool, error) {
return tx.FindAncestry(name)
}
// FindLayer wraps session FindLayer function with begin and rollback.
func FindLayer(datastore Datastore, hash string) (layer Layer, ok bool, err error) {
// FindLayerAndRollback wraps session FindLayer function with begin and rollback.
func FindLayerAndRollback(datastore Datastore, hash string) (layer Layer, ok bool, err error) {
var tx Session
if tx, err = datastore.Begin(); err != nil {
return
@ -145,8 +146,8 @@ func GetAncestryFeatures(ancestry Ancestry) []NamespacedFeature {
return DeduplicateNamespacedFeatures(features)
}
// UpsertAncestry wraps session UpsertAncestry function with begin and commit.
func UpsertAncestry(datastore Datastore, ancestry Ancestry) error {
// UpsertAncestryAndCommit wraps session UpsertAncestry function with begin and commit.
func UpsertAncestryAndCommit(datastore Datastore, ancestry Ancestry) error {
tx, err := datastore.Begin()
if err != nil {
return err
@ -164,9 +165,9 @@ func UpsertAncestry(datastore Datastore, ancestry Ancestry) error {
return nil
}
// PersistNamespacedFeatures wraps session PersistNamespacedFeatures function
// PersistNamespacedFeaturesAndCommit wraps session PersistNamespacedFeatures function
// with begin and commit.
func PersistNamespacedFeatures(datastore Datastore, features []NamespacedFeature) error {
func PersistNamespacedFeaturesAndCommit(datastore Datastore, features []NamespacedFeature) error {
tx, err := datastore.Begin()
if err != nil {
return err
@ -184,9 +185,9 @@ func PersistNamespacedFeatures(datastore Datastore, features []NamespacedFeature
return nil
}
// CacheRelatedVulnerability wraps session CacheAffectedNamespacedFeatures
// CacheRelatedVulnerabilityAndCommit wraps session CacheAffectedNamespacedFeatures
// function with begin and commit.
func CacheRelatedVulnerability(datastore Datastore, features []NamespacedFeature) error {
func CacheRelatedVulnerabilityAndCommit(datastore Datastore, features []NamespacedFeature) error {
tx, err := datastore.Begin()
if err != nil {
return err

View File

@ -208,7 +208,7 @@ func update(datastore database.Datastore, firstUpdate bool) {
namespaces = append(namespaces, ns)
}
if err := database.PersistNamespaces(datastore, namespaces); err != nil {
if err := database.PersistNamespacesAndCommit(datastore, namespaces); err != nil {
log.WithError(err).Error("Unable to insert namespaces")
return
}

View File

@ -101,7 +101,7 @@ func processRequests(imageFormat string, toDetect map[string]*processRequest) (m
}
func getProcessRequest(datastore database.Datastore, req LayerRequest) (preq *processRequest, err error) {
layer, ok, err := database.FindLayer(datastore, req.Hash)
layer, ok, err := database.FindLayerAndRollback(datastore, req.Hash)
if err != nil {
return
}
@ -141,16 +141,16 @@ func persistProcessResult(datastore database.Datastore, results map[string]*proc
features = database.DeduplicateFeatures(features...)
namespaces = database.DeduplicateNamespaces(namespaces...)
if err := database.PersistNamespaces(datastore, namespaces); err != nil {
if err := database.PersistNamespacesAndCommit(datastore, namespaces); err != nil {
return err
}
if err := database.PersistFeatures(datastore, features); err != nil {
if err := database.PersistFeaturesAndCommit(datastore, features); err != nil {
return err
}
for _, layer := range results {
if err := database.PersistPartialLayer(datastore, layer.newLayerContent); err != nil {
if err := database.PersistPartialLayerAndCommit(datastore, layer.newLayerContent); err != nil {
return err
}
}
@ -202,7 +202,7 @@ func getProcessResultLayers(results map[string]*processResult) map[string]databa
}
func isAncestryProcessed(datastore database.Datastore, name string) (bool, error) {
ancestry, ok, err := database.FindAncestry(datastore, name)
ancestry, ok, err := database.FindAncestryAndRollback(datastore, name)
if err != nil || !ok {
return ok, err
}
@ -262,17 +262,17 @@ func processAncestry(datastore database.Datastore, name string, layers []databas
"layer count": len(ancestry.Layers),
}).Debug("compute ancestry features")
if err := database.PersistNamespacedFeatures(datastore, ancestryFeatures); err != nil {
if err := database.PersistNamespacedFeaturesAndCommit(datastore, ancestryFeatures); err != nil {
log.WithField("ancestry", name).WithError(err).Error("could not persist namespaced features for ancestry")
return err
}
if err := database.CacheRelatedVulnerability(datastore, ancestryFeatures); err != nil {
if err := database.CacheRelatedVulnerabilityAndCommit(datastore, ancestryFeatures); err != nil {
log.WithField("ancestry", name).WithError(err).Error("failed to cache feature related vulnerability")
return err
}
if err := database.UpsertAncestry(datastore, ancestry); err != nil {
if err := database.UpsertAncestryAndCommit(datastore, ancestry); err != nil {
log.WithField("ancestry", name).WithError(err).Error("could not upsert ancestry")
return err
}