Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove id in node #40

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/resource/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBuilder_Build(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down
2 changes: 1 addition & 1 deletion cmd/uniflow/apply/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestExecute(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down
2 changes: 1 addition & 1 deletion cmd/uniflow/start/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestExecute(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type IndexView interface {

// IndexModel represents a model for an index.
type IndexModel struct {
Name string
Keys []string
Unique bool
Name string
Keys []string
Unique bool
Partial *Filter
}
4 changes: 2 additions & 2 deletions pkg/hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestHook_LoadHook(t *testing.T) {
hooks := New()

n := node.NewOneToOneNode(node.OneToOneNodeConfig{})
n := node.NewOneToOneNode(nil)

count := 0
h := symbol.LoadHookFunc(func(_ node.Node) error {
Expand All @@ -29,7 +29,7 @@ func TestHook_LoadHook(t *testing.T) {
func TestHook_UnloadHook(t *testing.T) {
hooks := New()

n := node.NewOneToOneNode(node.OneToOneNodeConfig{})
n := node.NewOneToOneNode(nil)

count := 0
h := symbol.UnloadHookFunc(func(_ node.Node) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestLoader_LoadOne(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestLoader_LoadOne(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestLoader_LoadOne(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestLoader_LoadAll(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/loader/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestReconciler_Reconcile(t *testing.T) {
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
Expand Down
2 changes: 0 additions & 2 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package node

import (
"github.com/oklog/ulid/v2"
"github.com/siyul-park/uniflow/pkg/port"
)

// Node is an operational unit that processes *packet.Packet.
type Node interface {
ID() ulid.ULID
Port(name string) (*port.Port, bool)
Close() error
}
Expand Down
25 changes: 1 addition & 24 deletions pkg/node/onetomany.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ package node
import (
"sync"

"github.com/oklog/ulid/v2"
"github.com/siyul-park/uniflow/pkg/packet"
"github.com/siyul-park/uniflow/pkg/port"
"github.com/siyul-park/uniflow/pkg/process"
)

// OneToManyNodeConfig holds the configuration for OneToManyNode.
type OneToManyNodeConfig struct {
ID ulid.ULID
Action func(*process.Process, *packet.Packet) ([]*packet.Packet, *packet.Packet)
}

// OneToManyNode represents a node that processes *packet.Packet with one input and many outputs.
type OneToManyNode struct {
id ulid.ULID
action func(*process.Process, *packet.Packet) ([]*packet.Packet, *packet.Packet)
inPort *port.Port
outPorts []*port.Port
Expand All @@ -28,21 +20,14 @@ type OneToManyNode struct {
var _ Node = (*OneToManyNode)(nil)

// NewOneToManyNode creates a new OneToManyNode with the given configuration.
func NewOneToManyNode(config OneToManyNodeConfig) *OneToManyNode {
id := config.ID
action := config.Action

if id == (ulid.ULID{}) {
id = ulid.Make()
}
func NewOneToManyNode(action func(*process.Process, *packet.Packet) ([]*packet.Packet, *packet.Packet)) *OneToManyNode {
if action == nil {
action = func(_ *process.Process, _ *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return nil, nil
}
}

n := &OneToManyNode{
id: id,
action: action,
inPort: port.New(),
outPorts: nil,
Expand All @@ -62,14 +47,6 @@ func NewOneToManyNode(config OneToManyNodeConfig) *OneToManyNode {
return n
}

// ID returns the ID of the OneToManyNode.
func (n *OneToManyNode) ID() ulid.ULID {
n.mu.RLock()
defer n.mu.RUnlock()

return n.id
}

// Port returns the specified port of the OneToManyNode.
func (n *OneToManyNode) Port(name string) (*port.Port, bool) {
n.mu.Lock()
Expand Down
31 changes: 8 additions & 23 deletions pkg/node/onetomany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,14 @@ import (
)

func TestNewOneToManyNode(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
})
n := NewOneToManyNode(nil)
assert.NotNil(t, n)
assert.NotZero(t, n.ID())

assert.NoError(t, n.Close())
}

func TestOneToManyNode_Port(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
})
n := NewOneToManyNode(nil)
defer func() { _ = n.Close() }()

p, ok := n.Port(PortIn)
Expand All @@ -48,10 +39,8 @@ func TestOneToManyNode_Port(t *testing.T) {

func TestOneToManyNode_Send(t *testing.T) {
t.Run("return out", func(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
})
defer func() { _ = n.Close() }()

Expand Down Expand Up @@ -93,10 +82,8 @@ func TestOneToManyNode_Send(t *testing.T) {
})

t.Run("return err", func(t *testing.T) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return nil, packet.New(primitive.NewString(faker.Word()))
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return nil, packet.New(primitive.NewString(faker.Word()))
})
defer func() { _ = n.Close() }()

Expand Down Expand Up @@ -132,10 +119,8 @@ func TestOneToManyNode_Send(t *testing.T) {
}

func BenchmarkOneToManyNode_Send(b *testing.B) {
n := NewOneToManyNode(OneToManyNodeConfig{
Action: func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
},
n := NewOneToManyNode(func(_ *process.Process, inPck *packet.Packet) ([]*packet.Packet, *packet.Packet) {
return []*packet.Packet{inPck}, nil
})
defer func() { _ = n.Close() }()

Expand Down
25 changes: 1 addition & 24 deletions pkg/node/onetoone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ package node
import (
"sync"

"github.com/oklog/ulid/v2"
"github.com/siyul-park/uniflow/pkg/packet"
"github.com/siyul-park/uniflow/pkg/port"
"github.com/siyul-park/uniflow/pkg/process"
)

// OneToOneNodeConfig holds the configuration for OneToOneNode.
type OneToOneNodeConfig struct {
ID ulid.ULID
Action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet)
}

// OneToOneNode represents a node that processes *packet.Packet with one input and one output.
type OneToOneNode struct {
id ulid.ULID
action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet)
ioPort *port.Port
inPort *port.Port
Expand All @@ -29,21 +21,14 @@ type OneToOneNode struct {
var _ Node = (*OneToOneNode)(nil)

// NewOneToOneNode creates a new OneToOneNode with the given configuration.
func NewOneToOneNode(config OneToOneNodeConfig) *OneToOneNode {
id := config.ID
action := config.Action

if id == (ulid.ULID{}) {
id = ulid.Make()
}
func NewOneToOneNode(action func(*process.Process, *packet.Packet) (*packet.Packet, *packet.Packet)) *OneToOneNode {
if action == nil {
action = func(_ *process.Process, _ *packet.Packet) (*packet.Packet, *packet.Packet) {
return nil, nil
}
}

n := &OneToOneNode{
id: id,
action: action,
ioPort: port.New(),
inPort: port.New(),
Expand Down Expand Up @@ -88,14 +73,6 @@ func NewOneToOneNode(config OneToOneNodeConfig) *OneToOneNode {
return n
}

// ID returns the ID of the OneToOneNode.
func (n *OneToOneNode) ID() ulid.ULID {
n.mu.RLock()
defer n.mu.RUnlock()

return n.id
}

// Port returns the specified port of the OneToOneNode.
func (n *OneToOneNode) Port(name string) (*port.Port, bool) {
n.mu.RLock()
Expand Down
Loading
Loading