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

View File

@ -208,7 +208,7 @@ func update(datastore database.Datastore, firstUpdate bool) {
namespaces = append(namespaces, ns) 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") log.WithError(err).Error("Unable to insert namespaces")
return 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) { 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 { if err != nil {
return return
} }
@ -141,16 +141,16 @@ func persistProcessResult(datastore database.Datastore, results map[string]*proc
features = database.DeduplicateFeatures(features...) features = database.DeduplicateFeatures(features...)
namespaces = database.DeduplicateNamespaces(namespaces...) namespaces = database.DeduplicateNamespaces(namespaces...)
if err := database.PersistNamespaces(datastore, namespaces); err != nil { if err := database.PersistNamespacesAndCommit(datastore, namespaces); err != nil {
return err return err
} }
if err := database.PersistFeatures(datastore, features); err != nil { if err := database.PersistFeaturesAndCommit(datastore, features); err != nil {
return err return err
} }
for _, layer := range results { for _, layer := range results {
if err := database.PersistPartialLayer(datastore, layer.newLayerContent); err != nil { if err := database.PersistPartialLayerAndCommit(datastore, layer.newLayerContent); err != nil {
return err return err
} }
} }
@ -202,7 +202,7 @@ func getProcessResultLayers(results map[string]*processResult) map[string]databa
} }
func isAncestryProcessed(datastore database.Datastore, name string) (bool, error) { 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 { if err != nil || !ok {
return ok, err return ok, err
} }
@ -262,17 +262,17 @@ func processAncestry(datastore database.Datastore, name string, layers []databas
"layer count": len(ancestry.Layers), "layer count": len(ancestry.Layers),
}).Debug("compute ancestry features") }).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") log.WithField("ancestry", name).WithError(err).Error("could not persist namespaced features for ancestry")
return err 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") log.WithField("ancestry", name).WithError(err).Error("failed to cache feature related vulnerability")
return err 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") log.WithField("ancestry", name).WithError(err).Error("could not upsert ancestry")
return err return err
} }