Skip to content

Commit

Permalink
implemented the new interface methods in the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Dec 3, 2024
1 parent 8cdb824 commit ba05937
Show file tree
Hide file tree
Showing 7 changed files with 661 additions and 576 deletions.
38 changes: 36 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
package cache

import (
"sync"
"time"

"github.com/caffix/queue"
"github.com/owasp-amass/asset-db/repository"
"github.com/owasp-amass/asset-db/types"
"github.com/owasp-amass/open-asset-model/property"
)

type Cache struct {
sync.Mutex
start time.Time
freq time.Duration
done chan struct{}
Expand Down Expand Up @@ -98,3 +98,37 @@ loop:
}
})
}

func (c *Cache) createCacheEntityTag(entity *types.Entity, name string, since time.Time) error {
_, err := c.cache.CreateEntityProperty(entity, &property.SimpleProperty{
PropertyName: name,
PropertyValue: since.Format(time.RFC3339Nano),
})
return err
}

func (c *Cache) checkCacheEntityTag(entity *types.Entity, name string) (*types.EntityTag, time.Time, bool) {
if tags, err := c.cache.GetEntityTags(entity, time.Time{}, name); err == nil && len(tags) == 1 {
if t, err := time.Parse(time.RFC3339Nano, tags[0].Property.Value()); err == nil {
return tags[0], t, true
}
}
return nil, time.Time{}, false
}

func (c *Cache) createCacheEdgeTag(edge *types.Edge, name string, since time.Time) error {
_, err := c.cache.CreateEdgeProperty(edge, &property.SimpleProperty{
PropertyName: name,
PropertyValue: since.Format(time.RFC3339Nano),
})
return err
}

func (c *Cache) checkCacheEdgeTag(edge *types.Edge, name string) (*types.EdgeTag, time.Time, bool) {
if tags, err := c.cache.GetEdgeTags(edge, time.Time{}, name); err == nil && len(tags) == 1 {
if t, err := time.Parse(time.RFC3339Nano, tags[0].Property.Value()); err == nil {
return tags[0], t, true
}
}
return nil, time.Time{}, false
}
25 changes: 0 additions & 25 deletions cache/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (

// CreateEdge implements the Repository interface.
func (c *Cache) CreateEdge(edge *types.Edge) (*types.Edge, error) {
c.Lock()
defer c.Unlock()

e, err := c.cache.CreateEdge(edge)
if err != nil {
return nil, err
Expand Down Expand Up @@ -63,9 +60,6 @@ func (c *Cache) CreateEdge(edge *types.Edge) (*types.Edge, error) {

// FindEdgeById implements the Repository interface.
func (c *Cache) FindEdgeById(id string) (*types.Edge, error) {
c.Lock()
defer c.Unlock()

return c.cache.FindEdgeById(id)
}

Expand All @@ -74,15 +68,13 @@ func (c *Cache) IncomingEdges(entity *types.Entity, since time.Time, labels ...s
var dbquery bool

if since.IsZero() || since.Before(c.start) {
c.Lock()
if tag, last, found := c.checkCacheEntityTag(entity, "cache_incoming_edges"); !found || since.Before(last) {
dbquery = true
if found {
_ = c.cache.DeleteEntityTag(tag.ID)
}
_ = c.createCacheEntityTag(entity, "cache_incoming_edges", since)
}
c.Unlock()
}

if dbquery {
Expand All @@ -106,9 +98,6 @@ func (c *Cache) IncomingEdges(entity *types.Entity, since time.Time, labels ...s
<-done
close(done)

c.Lock()
defer c.Unlock()

if dberr == nil && len(dbedges) > 0 {
for _, edge := range dbedges {
e, err := c.cache.CreateEntity(&types.Entity{
Expand All @@ -128,9 +117,6 @@ func (c *Cache) IncomingEdges(entity *types.Entity, since time.Time, labels ...s
}
}
}
} else {
c.Lock()
defer c.Unlock()
}

return c.cache.IncomingEdges(entity, since, labels...)
Expand All @@ -141,15 +127,13 @@ func (c *Cache) OutgoingEdges(entity *types.Entity, since time.Time, labels ...s
var dbquery bool

if since.IsZero() || since.Before(c.start) {
c.Lock()
if tag, last, found := c.checkCacheEntityTag(entity, "cache_outgoing_edges"); !found || since.Before(last) {
dbquery = true
if found {
_ = c.cache.DeleteEntityTag(tag.ID)
}
_ = c.createCacheEntityTag(entity, "cache_outgoing_edges", since)
}
c.Unlock()
}

if dbquery {
Expand All @@ -173,9 +157,6 @@ func (c *Cache) OutgoingEdges(entity *types.Entity, since time.Time, labels ...s
<-done
close(done)

c.Lock()
defer c.Unlock()

if dberr == nil && len(dbedges) > 0 {
for _, edge := range dbedges {
e, err := c.cache.CreateEntity(&types.Entity{
Expand All @@ -195,19 +176,13 @@ func (c *Cache) OutgoingEdges(entity *types.Entity, since time.Time, labels ...s
}
}
}
} else {
c.Lock()
defer c.Unlock()
}

return c.cache.OutgoingEdges(entity, since, labels...)
}

// DeleteEdge implements the Repository interface.
func (c *Cache) DeleteEdge(id string) error {
c.Lock()
defer c.Unlock()

edge, err := c.cache.FindEdgeById(id)
if err != nil {
return err
Expand Down
Loading

0 comments on commit ba05937

Please sign in to comment.