From 753a5ed358cd4ca9ee568519711cf835609cafdd Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Wed, 21 Jun 2023 12:26:22 -0600 Subject: [PATCH] Changed resources needed by an event from a slice to a map --- event.go | 66 +++++++++++++-------------------------------------- graph_test.go | 2 +- resource.go | 19 +++++++++++++++ 3 files changed, 37 insertions(+), 50 deletions(-) diff --git a/event.go b/event.go index 095077b..7b7bbfb 100644 --- a/event.go +++ b/event.go @@ -71,6 +71,7 @@ type Event interface { Action(action string) (func()(string, error), bool) Handler(signal_type string) (func(GraphSignal) (string, error), bool) Resources() []Resource + Resource(id string) Resource AddResource(Resource) error DoneResource() Resource SetTimeout(end_time time.Time, action string) @@ -89,13 +90,12 @@ func (event * BaseEvent) AddResource(resource Resource) error { event.resources_lock.Lock() defer event.resources_lock.Unlock() - for _, r := range(event.resources) { - if r.ID() == resource.ID() { - return fmt.Errorf("%s is already required for %s, cannot add again", resource.Name(), event.Name()) - } + _, exists := event.resources[resource.ID()] + if exists == true { + return fmt.Errorf("%s is already required for %s, cannot add again", resource.Name(), event.Name()) } - event.resources = append(event.resources, resource) + event.resources[resource.ID()] = resource return nil } @@ -126,47 +126,6 @@ func (event * BaseEvent) Handler(signal_type string) (func(GraphSignal)(string, return handler, exists } -func FindResources(event Event, resource_type reflect.Type) []Resource { - resources := event.Resources() - found := []Resource{} - for _, resource := range(resources) { - if reflect.TypeOf(resource) == resource_type { - found = append(found, resource) - } - } - - for _, child := range(event.Children()) { - found = append(found, FindResources(child, resource_type)...) - } - - m := map[string]Resource{} - for _, resource := range(found) { - m[resource.ID()] = resource - } - ret := []Resource{} - for _, resource := range(m) { - ret = append(ret, resource) - } - return ret -} - -func FindRequiredResource(event Event, id string) Resource { - for _, resource := range(event.Resources()) { - if resource.ID() == id { - return resource - } - } - - for _, child := range(event.Children()) { - result := FindRequiredResource(child, id) - if result != nil { - return result - } - } - - return nil -} - func FindChild(event Event, id string) Event { if id == event.ID() { return event @@ -345,7 +304,7 @@ type BaseEvent struct { BaseNode done_resource Resource rr_lock sync.Mutex - resources []Resource + resources map[string]Resource resources_lock sync.Mutex children []Event children_lock sync.Mutex @@ -392,7 +351,7 @@ func NewBaseEvent(name string, description string) (BaseEvent) { children: []Event{}, child_info: map[string]EventInfo{}, done_resource: done_resource, - resources: []Resource{}, + resources: map[string]Resource{}, Actions: map[string]func()(string, error){}, Handlers: map[string]func(GraphSignal)(string, error){}, abort: make(chan string, 1), @@ -566,7 +525,16 @@ func (event * BaseEvent) Parent() Event { } func (event * BaseEvent) Resources() []Resource { - return event.resources + resources := []Resource{} + for _, val := range(event.resources) { + resources = append(resources, val) + } + return resources +} + +func (event * BaseEvent) Resource(id string) Resource { + resource, _ := event.resources[id] + return resource } func (event * BaseEvent) DoneResource() Resource { diff --git a/graph_test.go b/graph_test.go index aca1fbc..2bf65f9 100644 --- a/graph_test.go +++ b/graph_test.go @@ -64,7 +64,7 @@ func TestNewEventWithResource(t *testing.T) { t.Fatal(err) } - res := FindRequiredResource(root_event, test_resource.ID()) + res := FindResource(root_event, test_resource.ID()) if res == nil { t.Fatal("Failed to find Resource in EventManager after adding") } diff --git a/resource.go b/resource.go index 85263c4..745f778 100644 --- a/resource.go +++ b/resource.go @@ -259,6 +259,25 @@ func NewBaseResource(name string, description string) BaseResource { return resource } +func FindResource(root Event, id string) Resource { + if root == nil || id == ""{ + panic("invalid input") + } + + for _, resource := range(root.Resources()) { + if resource.ID() == id { + return resource + } + } + for _, child := range(root.Children()) { + resource := FindResource(child, id) + if resource != nil { + return resource + } + } + return nil +} + func LinkResource(resource Resource, child Resource) error { if child == nil || resource == nil { return fmt.Errorf("Will not connect nil to resource DAG")