Skip to content

Commit

Permalink
database: Use LayerWithContent as Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyboardNerd committed Sep 13, 2018
1 parent ff93039 commit e160616
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 203 deletions.
2 changes: 1 addition & 1 deletion api/v3/clairpb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func VulnerabilityWithFixedInFromDatabaseModel(dbVuln database.VulnerabilityWith
}

// LayerFromDatabaseModel converts database layer to api layer.
func LayerFromDatabaseModel(dbLayer database.Layer) *Layer {
func LayerFromDatabaseModel(dbLayer database.LayerMetadata) *Layer {
layer := Layer{Hash: dbLayer.Hash}
return &layer
}
Expand Down
14 changes: 4 additions & 10 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,16 @@ type Session interface {
// PersistNamespaces inserts a set of namespaces if not in the database.
PersistNamespaces([]Namespace) error

// PersistLayer creates a layer using the blob Sum hash.
PersistLayer(hash string) error

// PersistLayerContent persists a layer's content in the database. The given
// PersistLayer persists a layer's content in the database. The given
// namespaces and features can be partial content of this layer.
//
// The layer, namespaces and features are expected to be already existing
// in the database.
PersistLayerContent(hash string, namespaces []Namespace, features []Feature, processedBy Processors) error

// FindLayer retrieves the metadata of a layer.
FindLayer(hash string) (layer Layer, found bool, err error)
PersistLayer(hash string, namespaces []Namespace, features []Feature, processedBy Processors) error

// FindLayerWithContent returns a layer with all detected features and
// FindLayer returns a layer with all detected features and
// namespaces.
FindLayerWithContent(hash string) (layer LayerWithContent, found bool, err error)
FindLayer(hash string) (layer Layer, found bool, err error)

// InsertVulnerabilities inserts a set of UNIQUE vulnerabilities with
// affected features into database, assuming that all vulnerabilities
Expand Down
22 changes: 3 additions & 19 deletions database/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ type MockSession struct {
FctPersistFeatures func([]Feature) error
FctPersistNamespacedFeatures func([]NamespacedFeature) error
FctCacheAffectedNamespacedFeatures func([]NamespacedFeature) error
FctPersistLayer func(hash string) error
FctPersistLayerContent func(hash string, namespaces []Namespace, features []Feature, processedBy Processors) error
FctPersistLayer func(hash string, namespaces []Namespace, features []Feature, processedBy Processors) error
FctFindLayer func(name string) (Layer, bool, error)
FctFindLayerWithContent func(name string) (LayerWithContent, bool, error)
FctInsertVulnerabilities func([]VulnerabilityWithAffected) error
FctFindVulnerabilities func([]VulnerabilityID) ([]NullableVulnerability, error)
FctDeleteVulnerabilities func([]VulnerabilityID) error
Expand Down Expand Up @@ -115,16 +113,9 @@ func (ms *MockSession) CacheAffectedNamespacedFeatures(namespacedFeatures []Name
panic("required mock function not implemented")
}

func (ms *MockSession) PersistLayer(layer string) error {
func (ms *MockSession) PersistLayer(hash string, namespaces []Namespace, features []Feature, processedBy Processors) error {
if ms.FctPersistLayer != nil {
return ms.FctPersistLayer(layer)
}
panic("required mock function not implemented")
}

func (ms *MockSession) PersistLayerContent(hash string, namespaces []Namespace, features []Feature, processedBy Processors) error {
if ms.FctPersistLayerContent != nil {
return ms.FctPersistLayerContent(hash, namespaces, features, processedBy)
return ms.FctPersistLayer(hash, namespaces, features, processedBy)
}
panic("required mock function not implemented")
}
Expand All @@ -136,13 +127,6 @@ func (ms *MockSession) FindLayer(name string) (Layer, bool, error) {
panic("required mock function not implemented")
}

func (ms *MockSession) FindLayerWithContent(name string) (LayerWithContent, bool, error) {
if ms.FctFindLayerWithContent != nil {
return ms.FctFindLayerWithContent(name)
}
panic("required mock function not implemented")
}

func (ms *MockSession) InsertVulnerabilities(vulnerabilities []VulnerabilityWithAffected) error {
if ms.FctInsertVulnerabilities != nil {
return ms.FctInsertVulnerabilities(vulnerabilities)
Expand Down
12 changes: 6 additions & 6 deletions database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ type Ancestry struct {

// AncestryLayer is a layer with all detected namespaced features.
type AncestryLayer struct {
Layer
LayerMetadata

// DetectedFeatures are the features introduced by this layer when it was
// processed.
DetectedFeatures []NamespacedFeature
}

// Layer contains the metadata of a layer.
type Layer struct {
// LayerMetadata contains the metadata of a layer.
type LayerMetadata struct {
// Hash is content hash of the layer.
Hash string
// ProcessedBy contains the processors that processed this layer.
ProcessedBy Processors
}

// LayerWithContent is a layer with its detected namespaces and features by
// Layer is a layer with its detected namespaces and features by
// ProcessedBy.
type LayerWithContent struct {
Layer
type Layer struct {
LayerMetadata

Namespaces []Namespace
Features []Feature
Expand Down
2 changes: 1 addition & 1 deletion database/pgsql/ancestry.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (tx *pgSession) findAncestryLayers(id int64) ([]database.AncestryLayer, err
}

if !index.Valid || !id.Valid {
return nil, commonerr.ErrNotFound
panic("null ancestry ID or ancestry index violates database constraints")
}

if _, ok := layers[index.Int64]; ok {
Expand Down
16 changes: 8 additions & 8 deletions database/pgsql/ancestry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestUpsertAncestry(t *testing.T) {
Name: "a1",
Layers: []database.AncestryLayer{
{
Layer: database.Layer{
LayerMetadata: database.LayerMetadata{
Hash: "layer-N",
},
},
Expand All @@ -43,7 +43,7 @@ func TestUpsertAncestry(t *testing.T) {
Name: "a",
Layers: []database.AncestryLayer{
{
Layer: database.Layer{
LayerMetadata: database.LayerMetadata{
Hash: "layer-0",
},
},
Expand All @@ -54,7 +54,7 @@ func TestUpsertAncestry(t *testing.T) {
Name: "a",
Layers: []database.AncestryLayer{
{
Layer: database.Layer{
LayerMetadata: database.LayerMetadata{
Hash: "layer-1",
},
},
Expand Down Expand Up @@ -137,7 +137,7 @@ func assertAncestryEqual(t *testing.T, expected database.Ancestry, actual databa
}

func assertAncestryLayerEqual(t *testing.T, expected database.AncestryLayer, actual database.AncestryLayer) bool {
return assertLayerEqual(t, expected.Layer, actual.Layer) &&
return assertLayerEqual(t, expected.LayerMetadata, actual.LayerMetadata) &&
assertNamespacedFeatureEqual(t, expected.DetectedFeatures, actual.DetectedFeatures)
}

Expand All @@ -159,7 +159,7 @@ func TestFindAncestry(t *testing.T) {
},
Layers: []database.AncestryLayer{
{
Layer: database.Layer{
LayerMetadata: database.LayerMetadata{
Hash: "layer-0",
},
DetectedFeatures: []database.NamespacedFeature{
Expand Down Expand Up @@ -188,17 +188,17 @@ func TestFindAncestry(t *testing.T) {
},
},
{
Layer: database.Layer{
LayerMetadata: database.LayerMetadata{
Hash: "layer-1",
},
},
{
Layer: database.Layer{
LayerMetadata: database.LayerMetadata{
Hash: "layer-2",
},
},
{
Layer: database.Layer{
LayerMetadata: database.LayerMetadata{
Hash: "layer-3b",
},
},
Expand Down
50 changes: 26 additions & 24 deletions database/pgsql/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@ import (
)

func (tx *pgSession) FindLayer(hash string) (database.Layer, bool, error) {
layer, _, ok, err := tx.findLayer(hash)
return layer, ok, err
}

func (tx *pgSession) FindLayerWithContent(hash string) (database.LayerWithContent, bool, error) {
var (
layer database.LayerWithContent
layer database.Layer
layerID int64
ok bool
err error
)

layer.Layer, layerID, ok, err = tx.findLayer(hash)
layer.LayerMetadata, layerID, ok, err = tx.findLayer(hash)
if err != nil {
return layer, false, err
}
Expand All @@ -49,46 +44,53 @@ func (tx *pgSession) FindLayerWithContent(hash string) (database.LayerWithConten
return layer, true, nil
}

func (tx *pgSession) PersistLayer(hash string) error {
func (tx *pgSession) persistLayer(hash string) (int64, error) {
if hash == "" {
return commonerr.NewBadRequestError("Empty Layer Hash is not allowed")
return -1, commonerr.NewBadRequestError("Empty Layer Hash is not allowed")
}

_, err := tx.Exec(queryPersistLayer(1), hash)
if err != nil {
return handleError("queryPersistLayer", err)
id := sql.NullInt64{}
if err := tx.QueryRow(soiLayer, hash).Scan(&id); err != nil {
return -1, handleError("queryPersistLayer", err)
}

return nil
if !id.Valid {
panic("null layer.id violates database constraint")
}

return id.Int64, nil
}

// PersistLayerContent relates layer identified by hash with namespaces,
// PersistLayer relates layer identified by hash with namespaces,
// features and processors provided. If the layer, namespaces, features are not
// in database, the function returns an error.
func (tx *pgSession) PersistLayerContent(hash string, namespaces []database.Namespace, features []database.Feature, processedBy database.Processors) error {
func (tx *pgSession) PersistLayer(hash string, namespaces []database.Namespace, features []database.Feature, processedBy database.Processors) error {
if hash == "" {
return commonerr.NewBadRequestError("Empty layer hash is not allowed")
}

var layerID int64
err := tx.QueryRow(searchLayer, hash).Scan(&layerID)
if err != nil {
var (
err error
id int64
)

if id, err = tx.persistLayer(hash); err != nil {
return err
}

if err = tx.persistLayerNamespace(layerID, namespaces); err != nil {
if err = tx.persistLayerNamespace(id, namespaces); err != nil {
return err
}

if err = tx.persistLayerFeatures(layerID, features); err != nil {
if err = tx.persistLayerFeatures(id, features); err != nil {
return err
}

if err = tx.persistLayerDetectors(layerID, processedBy.Detectors); err != nil {
if err = tx.persistLayerDetectors(id, processedBy.Detectors); err != nil {
return err
}

if err = tx.persistLayerListers(layerID, processedBy.Listers); err != nil {
if err = tx.persistLayerListers(id, processedBy.Listers); err != nil {
return err
}

Expand Down Expand Up @@ -275,10 +277,10 @@ func (tx *pgSession) findLayerFeatures(layerID int64) ([]database.Feature, error
return features, nil
}

func (tx *pgSession) findLayer(hash string) (database.Layer, int64, bool, error) {
func (tx *pgSession) findLayer(hash string) (database.LayerMetadata, int64, bool, error) {
var (
layerID int64
layer = database.Layer{Hash: hash, ProcessedBy: database.Processors{}}
layer = database.LayerMetadata{Hash: hash, ProcessedBy: database.Processors{}}
)

if hash == "" {
Expand Down
Loading

0 comments on commit e160616

Please sign in to comment.