Skip to content

Commit

Permalink
all cache edge unit test finished
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Nov 20, 2024
1 parent 4334486 commit 5651ed0
Show file tree
Hide file tree
Showing 4 changed files with 486 additions and 11 deletions.
25 changes: 16 additions & 9 deletions cache/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Cache) CreateEdge(edge *types.Edge) (*types.Edge, error) {
}

if _, _, found := c.checkCacheEdgeTag(edge, "cache_create_edge"); !found {
_ = c.createCacheEdgeTag(edge, "cache_create_edge", time.Now())
_ = c.createCacheEdgeTag(e, "cache_create_edge", time.Now())

c.appendToDBQueue(func() {
s, err := c.db.FindEntityByContent(sub.Asset, time.Time{})
Expand All @@ -58,6 +58,14 @@ func (c *Cache) CreateEdge(edge *types.Edge) (*types.Edge, error) {
return e, nil
}

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

return c.cache.FindEdgeById(id)
}

// IncomingEdges implements the Repository interface.
func (c *Cache) IncomingEdges(entity *types.Entity, since time.Time, labels ...string) ([]*types.Edge, error) {
var dbquery bool
Expand Down Expand Up @@ -150,7 +158,7 @@ func (c *Cache) OutgoingEdges(entity *types.Entity, since time.Time, labels ...s
defer func() { done <- struct{}{} }()

if e, err := c.db.FindEntityByContent(entity.Asset, time.Time{}); err == nil && len(e) == 1 {
dbedges, dberr = c.db.IncomingEdges(e[0], since, labels...)
dbedges, dberr = c.db.OutgoingEdges(e[0], since, labels...)

for i, edge := range dbedges {
if e, err := c.db.FindEntityById(edge.ToEntity.ID); err == nil && e != nil {
Expand Down Expand Up @@ -189,19 +197,14 @@ func (c *Cache) OutgoingEdges(entity *types.Entity, since time.Time, labels ...s
defer c.Unlock()
}

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

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

err := c.cache.DeleteEdge(id)
if err != nil {
return err
}

edge, err := c.cache.FindEdgeById(id)
if err != nil {
return nil
Expand All @@ -217,6 +220,10 @@ func (c *Cache) DeleteEdge(id string) error {
return nil
}

if err := c.cache.DeleteEdge(id); err != nil {
return err
}

c.appendToDBQueue(func() {
s, err := c.db.FindEntityByContent(sub.Asset, time.Time{})
if err != nil || len(s) != 1 {
Expand All @@ -235,7 +242,7 @@ func (c *Cache) DeleteEdge(id string) error {

var target *types.Edge
for _, e := range edges {
if e.ID == o[0].ID && reflect.DeepEqual(e.Relation, edge.Relation) {
if e.ToEntity.ID == o[0].ID && reflect.DeepEqual(e.Relation, edge.Relation) {
target = e
break
}
Expand Down
Loading

0 comments on commit 5651ed0

Please sign in to comment.