|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- package dag
-
- import (
- "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
- "gitlink.org.cn/cloudream/common/utils/lo2"
- )
-
- type NodeEnvType string
-
- const (
- EnvUnknown NodeEnvType = ""
- EnvDriver NodeEnvType = "Driver"
- EnvWorker NodeEnvType = "Worker"
- )
-
- type NodeEnv struct {
- Type NodeEnvType
- Worker exec.WorkerInfo
- Pinned bool // 如果为true,则不应该改变这个节点的执行环境
- }
-
- func (e *NodeEnv) ToEnvUnknown() {
- e.Type = EnvUnknown
- e.Worker = nil
- }
-
- func (e *NodeEnv) ToEnvDriver() {
- e.Type = EnvDriver
- e.Worker = nil
- }
-
- func (e *NodeEnv) ToEnvWorker(worker exec.WorkerInfo) {
- e.Type = EnvWorker
- e.Worker = worker
- }
-
- func (e *NodeEnv) Equals(other *NodeEnv) bool {
- if e.Type != other.Type {
- return false
- }
-
- if e.Type != EnvWorker {
- return true
- }
-
- return e.Worker.Equals(other.Worker)
- }
-
- type Node interface {
- Graph() *Graph
- SetGraph(graph *Graph)
- Env() *NodeEnv
- InputStreams() *InputSlots
- OutputStreams() *OutputSlots
- InputValues() *InputSlots
- OutputValues() *OutputSlots
- GenerateOp() (exec.Op, error)
- // String() string
- }
-
- type VarSlots []*Var
-
- func (s *VarSlots) Len() int {
- return len(*s)
- }
-
- func (s *VarSlots) Get(idx int) *Var {
- return (*s)[idx]
- }
-
- func (s *VarSlots) Set(idx int, val *Var) *Var {
- old := (*s)[idx]
- (*s)[idx] = val
- return old
- }
-
- func (s *VarSlots) Append(val *Var) int {
- *s = append(*s, val)
- return s.Len() - 1
- }
-
- func (s *VarSlots) RemoveAt(idx int) {
- (*s) = lo2.RemoveAt(*s, idx)
- }
-
- func (s *VarSlots) Resize(size int) {
- if s.Len() < size {
- *s = append(*s, make([]*Var, size-s.Len())...)
- } else if s.Len() > size {
- *s = (*s)[:size]
- }
- }
-
- func (s *VarSlots) SetRawArray(arr []*Var) {
- *s = arr
- }
-
- func (s *VarSlots) RawArray() []*Var {
- return *s
- }
-
- type InputSlots struct {
- VarSlots
- }
-
- func (s *InputSlots) EnsureSize(cnt int) {
- if s.Len() < cnt {
- s.VarSlots = append(s.VarSlots, make([]*Var, cnt-s.Len())...)
- }
- }
-
- func (s *InputSlots) EnlargeOne() int {
- s.Append(nil)
- return s.Len() - 1
- }
-
- func (s *InputSlots) GetVarIDs() []exec.VarID {
- var ids []exec.VarID
- for _, v := range s.RawArray() {
- if v == nil {
- continue
- }
- ids = append(ids, v.VarID)
- }
-
- return ids
- }
-
- func (s *InputSlots) GetVarIDsRanged(start, end int) []exec.VarID {
- var ids []exec.VarID
- for i := start; i < end; i++ {
- v := s.Get(i)
- if v == nil {
- continue
- }
- ids = append(ids, v.VarID)
- }
-
- return ids
- }
-
- type OutputSlots struct {
- VarSlots
- }
-
- func (s *OutputSlots) Setup(my Node, v *Var, slotIdx int) {
- if s.Len() <= slotIdx {
- s.VarSlots = append(s.VarSlots, make([]*Var, slotIdx-s.Len()+1)...)
- }
-
- s.Set(slotIdx, v)
- *v.From() = EndPoint{
- Node: my,
- SlotIndex: slotIdx,
- }
- }
-
- func (s *OutputSlots) SetupNew(my Node, v *Var) {
- s.Append(v)
- *v.From() = EndPoint{
- Node: my,
- SlotIndex: s.Len() - 1,
- }
- }
-
- func (s *OutputSlots) GetVarIDs() []exec.VarID {
- var ids []exec.VarID
- for _, v := range s.RawArray() {
- if v == nil {
- continue
- }
- ids = append(ids, v.VarID)
- }
-
- return ids
- }
-
- func (s *OutputSlots) GetVarIDsRanged(start, end int) []exec.VarID {
- var ids []exec.VarID
- for i := start; i < end; i++ {
- v := s.Get(i)
- if v == nil {
- continue
- }
- ids = append(ids, v.VarID)
- }
-
- return ids
- }
-
- type Slot struct {
- Var *Var
- Index int
- }
-
- type NodeBase struct {
- env NodeEnv
- inputStreams InputSlots
- outputStreams OutputSlots
- inputValues InputSlots
- outputValues OutputSlots
- graph *Graph
- }
-
- func (n *NodeBase) Graph() *Graph {
- return n.graph
- }
-
- func (n *NodeBase) SetGraph(graph *Graph) {
- n.graph = graph
- }
-
- func (n *NodeBase) Env() *NodeEnv {
- return &n.env
- }
-
- func (n *NodeBase) InputStreams() *InputSlots {
- return &n.inputStreams
- }
-
- func (n *NodeBase) OutputStreams() *OutputSlots {
- return &n.outputStreams
- }
-
- func (n *NodeBase) InputValues() *InputSlots {
- return &n.inputValues
- }
-
- func (n *NodeBase) OutputValues() *OutputSlots {
- return &n.outputValues
- }
|