Changed resources needed by an event from a slice to a map

graph-rework
noah metz 2023-06-21 12:26:22 -06:00
parent d8446a2159
commit 753a5ed358
3 changed files with 37 additions and 50 deletions

@ -71,6 +71,7 @@ type Event interface {
Action(action string) (func()(string, error), bool) Action(action string) (func()(string, error), bool)
Handler(signal_type string) (func(GraphSignal) (string, error), bool) Handler(signal_type string) (func(GraphSignal) (string, error), bool)
Resources() []Resource Resources() []Resource
Resource(id string) Resource
AddResource(Resource) error AddResource(Resource) error
DoneResource() Resource DoneResource() Resource
SetTimeout(end_time time.Time, action string) SetTimeout(end_time time.Time, action string)
@ -89,13 +90,12 @@ func (event * BaseEvent) AddResource(resource Resource) error {
event.resources_lock.Lock() event.resources_lock.Lock()
defer event.resources_lock.Unlock() defer event.resources_lock.Unlock()
for _, r := range(event.resources) { _, exists := event.resources[resource.ID()]
if r.ID() == resource.ID() { if exists == true {
return fmt.Errorf("%s is already required for %s, cannot add again", resource.Name(), event.Name()) 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 return nil
} }
@ -126,47 +126,6 @@ func (event * BaseEvent) Handler(signal_type string) (func(GraphSignal)(string,
return handler, exists 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 { func FindChild(event Event, id string) Event {
if id == event.ID() { if id == event.ID() {
return event return event
@ -345,7 +304,7 @@ type BaseEvent struct {
BaseNode BaseNode
done_resource Resource done_resource Resource
rr_lock sync.Mutex rr_lock sync.Mutex
resources []Resource resources map[string]Resource
resources_lock sync.Mutex resources_lock sync.Mutex
children []Event children []Event
children_lock sync.Mutex children_lock sync.Mutex
@ -392,7 +351,7 @@ func NewBaseEvent(name string, description string) (BaseEvent) {
children: []Event{}, children: []Event{},
child_info: map[string]EventInfo{}, child_info: map[string]EventInfo{},
done_resource: done_resource, done_resource: done_resource,
resources: []Resource{}, resources: map[string]Resource{},
Actions: map[string]func()(string, error){}, Actions: map[string]func()(string, error){},
Handlers: map[string]func(GraphSignal)(string, error){}, Handlers: map[string]func(GraphSignal)(string, error){},
abort: make(chan string, 1), abort: make(chan string, 1),
@ -566,7 +525,16 @@ func (event * BaseEvent) Parent() Event {
} }
func (event * BaseEvent) Resources() []Resource { 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 { func (event * BaseEvent) DoneResource() Resource {

@ -64,7 +64,7 @@ func TestNewEventWithResource(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
res := FindRequiredResource(root_event, test_resource.ID()) res := FindResource(root_event, test_resource.ID())
if res == nil { if res == nil {
t.Fatal("Failed to find Resource in EventManager after adding") t.Fatal("Failed to find Resource in EventManager after adding")
} }

@ -259,6 +259,25 @@ func NewBaseResource(name string, description string) BaseResource {
return resource 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 { func LinkResource(resource Resource, child Resource) error {
if child == nil || resource == nil { if child == nil || resource == nil {
return fmt.Errorf("Will not connect nil to resource DAG") return fmt.Errorf("Will not connect nil to resource DAG")