Skip to content

Commit

Permalink
refactor(cardinal): untangle iterator and filter package
Browse files Browse the repository at this point in the history
  • Loading branch information
smsunarto committed Jul 24, 2024
1 parent c2a0fbc commit f25625b
Show file tree
Hide file tree
Showing 41 changed files with 108 additions and 119 deletions.
2 changes: 1 addition & 1 deletion cardinal/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down
10 changes: 5 additions & 5 deletions cardinal/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"github.com/rotisserie/eris"

"pkg.world.dev/world-engine/cardinal/component"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/gamestate"
"pkg.world.dev/world-engine/cardinal/types"
"pkg.world.dev/world-engine/cardinal/worldstage"
)

var (
ErrEntityMutationOnReadOnly = errors.New("cannot modify state with read only context")
ErrEntitiesCreatedBeforeReady = errors.New("entities should not be created before world is ready")
ErrEntityDoesNotExist = iterators.ErrEntityDoesNotExist
ErrEntityMustHaveAtLeastOneComponent = iterators.ErrEntityMustHaveAtLeastOneComponent
ErrComponentNotOnEntity = iterators.ErrComponentNotOnEntity
ErrComponentAlreadyOnEntity = iterators.ErrComponentAlreadyOnEntity
ErrEntityDoesNotExist = gamestate.ErrEntityDoesNotExist
ErrEntityMustHaveAtLeastOneComponent = gamestate.ErrEntityMustHaveAtLeastOneComponent
ErrComponentNotOnEntity = gamestate.ErrComponentNotOnEntity
ErrComponentAlreadyOnEntity = gamestate.ErrComponentAlreadyOnEntity
)

// FilterFunction wrap your component filter function of func(comp T) bool inside FilterFunction to use
Expand Down
2 changes: 1 addition & 1 deletion cardinal/cardinal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/router/mocks"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/testutils"
"pkg.world.dev/world-engine/cardinal/types"
"pkg.world.dev/world-engine/sign"
Expand Down
6 changes: 3 additions & 3 deletions cardinal/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/gamestate"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -276,7 +276,7 @@ func TestErrorWhenAccessingComponentNotOnEntity(t *testing.T) {
id, err := cardinal.Create(wCtx, foundComp{})
assert.NilError(t, err)
_, err = cardinal.GetComponent[notFoundComp](wCtx, id)
assert.ErrorIs(t, err, iterators.ErrComponentNotOnEntity)
assert.ErrorIs(t, err, gamestate.ErrComponentNotOnEntity)
}

type ValueComponent struct {
Expand Down
2 changes: 1 addition & 1 deletion cardinal/composedsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cardinal
import (
"github.com/rotisserie/eris"

"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down
7 changes: 3 additions & 4 deletions cardinal/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -367,7 +366,7 @@ func TestAddingAComponentThatAlreadyExistsIsError(t *testing.T) {
wCtx := cardinal.NewWorldContext(world)
ent, err := cardinal.Create(wCtx, EnergyComponent{})
assert.NilError(t, err)
assert.ErrorIs(t, cardinal.AddComponentTo[EnergyComponent](wCtx, ent), iterators.ErrComponentAlreadyOnEntity)
assert.ErrorIs(t, cardinal.AddComponentTo[EnergyComponent](wCtx, ent), cardinal.ErrComponentAlreadyOnEntity)
}

func TestRemovingAMissingComponentIsError(t *testing.T) {
Expand All @@ -380,7 +379,7 @@ func TestRemovingAMissingComponentIsError(t *testing.T) {
ent, err := cardinal.Create(wCtx, ReactorEnergy{})
assert.NilError(t, err)

assert.ErrorIs(t, cardinal.RemoveComponentFrom[WeaponEnergy](wCtx, ent), iterators.ErrComponentNotOnEntity)
assert.ErrorIs(t, cardinal.RemoveComponentFrom[WeaponEnergy](wCtx, ent), cardinal.ErrComponentNotOnEntity)
}

func TestVerifyAutomaticCreationOfArchetypesWorks(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cardinal/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal/component"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
)

// TestSystemsReturnNonFatalErrors ensures system will surface non-fatal read and write errors to the user.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/server/handler/cql"
"pkg.world.dev/world-engine/cardinal/types"
)
Expand Down Expand Up @@ -87,7 +86,7 @@ func TestCanFilterByArchetype(t *testing.T) {
count++
// Make sure the gamma component is not on this entity
_, err = cardinal.GetComponent[Gamma](wCtx, id)
assert.ErrorIs(t, err, iterators.ErrComponentNotOnEntity)
assert.ErrorIs(t, err, cardinal.ErrComponentNotOnEntity)
return true
},
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 9 additions & 10 deletions cardinal/gamestate/ecb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
"go.opentelemetry.io/otel/trace"

"pkg.world.dev/world-engine/cardinal/codec"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/filter"
ecslog "pkg.world.dev/world-engine/cardinal/log"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -231,7 +230,7 @@ func (m *EntityCommandBuffer) SetComponentForEntity(
return err
}
if !filter.MatchComponentMetadata(comps, cType) {
return eris.Wrap(iterators.ErrComponentNotOnEntity, "")
return eris.Wrap(ErrComponentNotOnEntity, "")
}

key := compKey{cType.ID(), id}
Expand All @@ -252,7 +251,7 @@ func (m *EntityCommandBuffer) GetComponentForEntity(cType types.ComponentMetadat
return nil, err
}
if !filter.MatchComponentMetadata(comps, cType) {
return nil, eris.Wrap(iterators.ErrComponentNotOnEntity, "")
return nil, eris.Wrap(ErrComponentNotOnEntity, "")
}

// Fetch the value from storage
Expand Down Expand Up @@ -297,7 +296,7 @@ func (m *EntityCommandBuffer) AddComponentToEntity(cType types.ComponentMetadata
return err
}
if filter.MatchComponentMetadata(fromComps, cType) {
return eris.Wrap(iterators.ErrComponentAlreadyOnEntity, "")
return eris.Wrap(ErrComponentAlreadyOnEntity, "")
}
toComps := append(fromComps, cType) //nolint:gocritic // easier this way.
if err = sortComponentSet(toComps); err != nil {
Expand Down Expand Up @@ -332,10 +331,10 @@ func (m *EntityCommandBuffer) RemoveComponentFromEntity(cType types.ComponentMet
newCompSet = append(newCompSet, comp)
}
if !found {
return eris.Wrap(iterators.ErrComponentNotOnEntity, "")
return eris.Wrap(ErrComponentNotOnEntity, "")
}
if len(newCompSet) == 0 {
return eris.Wrap(iterators.ErrEntityMustHaveAtLeastOneComponent, "")
return eris.Wrap(ErrEntityMustHaveAtLeastOneComponent, "")
}
key := compKey{cType.ID(), id}
err = m.compValues.Delete(key)
Expand Down Expand Up @@ -409,8 +408,8 @@ func (m *EntityCommandBuffer) GetEntitiesForArchID(archID types.ArchetypeID) ([]

// SearchFrom returns an ArchetypeIterator based on a component filter. The iterator will iterate over all archetypes
// that match the given filter.
func (m *EntityCommandBuffer) SearchFrom(filter filter.ComponentFilter, start int) *iterators.ArchetypeIterator {
itr := &iterators.ArchetypeIterator{}
func (m *EntityCommandBuffer) SearchFrom(filter filter.ComponentFilter, start int) *ArchetypeIterator {
itr := &ArchetypeIterator{}
for i := start; i < m.archIDToComps.Len(); i++ {
archID := types.ArchetypeID(i)
// TODO: error was swallowed here.
Expand Down Expand Up @@ -454,7 +453,7 @@ func (m *EntityCommandBuffer) getArchetypeForEntity(id types.EntityID) (types.Ar
if err != nil {
// todo: Make redis.Nil a general error on storage
if errors.Is(err, redis.Nil) {
return 0, eris.Wrap(redis.Nil, iterators.ErrEntityDoesNotExist.Error())
return 0, eris.Wrap(redis.Nil, ErrEntityDoesNotExist.Error())
}
return 0, eris.Wrap(err, "")
}
Expand Down
11 changes: 5 additions & 6 deletions cardinal/gamestate/ecb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/component"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/gamestate"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -224,7 +223,7 @@ func TestCannotGetComponentOnEntityThatIsMissingTheComponent(t *testing.T) {
assert.NilError(t, err)
// barComp has not been assigned to this entity
_, err = manager.GetComponentForEntity(barComp, id)
assert.ErrorIs(t, err, iterators.ErrComponentNotOnEntity)
assert.ErrorIs(t, err, gamestate.ErrComponentNotOnEntity)
}

func TestCannotSetComponentOnEntityThatIsMissingTheComponent(t *testing.T) {
Expand All @@ -233,15 +232,15 @@ func TestCannotSetComponentOnEntityThatIsMissingTheComponent(t *testing.T) {
assert.NilError(t, err)
// barComp has not been assigned to this entity
err = manager.SetComponentForEntity(barComp, id, Bar{100})
assert.ErrorIs(t, err, iterators.ErrComponentNotOnEntity)
assert.ErrorIs(t, err, gamestate.ErrComponentNotOnEntity)
}

func TestCannotRemoveAComponentFromAnEntityThatDoesNotHaveThatComponent(t *testing.T) {
manager := newCmdBufferForTest(t)
id, err := manager.CreateEntity(fooComp)
assert.NilError(t, err)
err = manager.RemoveComponentFromEntity(barComp, id)
assert.ErrorIs(t, err, iterators.ErrComponentNotOnEntity)
assert.ErrorIs(t, err, gamestate.ErrComponentNotOnEntity)
}

func TestCanAddAComponentToAnEntity(t *testing.T) {
Expand Down Expand Up @@ -288,7 +287,7 @@ func TestCannotAddComponentToEntityThatAlreadyHasTheComponent(t *testing.T) {
assert.NilError(t, err)

err = manager.AddComponentToEntity(fooComp, id)
assert.ErrorIs(t, err, iterators.ErrComponentAlreadyOnEntity)
assert.ErrorIs(t, err, gamestate.ErrComponentAlreadyOnEntity)
}

type Health struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package iterators
package gamestate

import (
"errors"
"math"

"pkg.world.dev/world-engine/cardinal/types"
)

var (
BadID types.EntityID = math.MaxUint64

ErrEntityDoesNotExist = errors.New("entity does not exist")
ErrComponentAlreadyOnEntity = errors.New("component already on entity")
ErrComponentNotOnEntity = errors.New("component not on entity")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package iterators
package gamestate

import (
"pkg.world.dev/world-engine/cardinal/types"
Expand Down
5 changes: 2 additions & 3 deletions cardinal/gamestate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"encoding/json"

"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand All @@ -25,7 +24,7 @@ type Reader interface {
GetEntitiesForArchID(archID types.ArchetypeID) ([]types.EntityID, error)

// Misc
SearchFrom(filter filter.ComponentFilter, start int) *iterators.ArchetypeIterator
SearchFrom(filter filter.ComponentFilter, start int) *ArchetypeIterator
ArchetypeCount() int
}

Expand Down
7 changes: 3 additions & 4 deletions cardinal/gamestate/read_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"github.com/rotisserie/eris"

"pkg.world.dev/world-engine/cardinal/codec"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -149,8 +148,8 @@ func (r *readOnlyManager) GetEntitiesForArchID(archID types.ArchetypeID) ([]type
return ids, nil
}

func (r *readOnlyManager) SearchFrom(filter filter.ComponentFilter, start int) *iterators.ArchetypeIterator {
itr := &iterators.ArchetypeIterator{}
func (r *readOnlyManager) SearchFrom(filter filter.ComponentFilter, start int) *ArchetypeIterator {
itr := &ArchetypeIterator{}
if err := r.refreshArchIDToCompTypes(); err != nil {
return itr
}
Expand Down
2 changes: 1 addition & 1 deletion cardinal/gamestate/read_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/filter"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down
3 changes: 1 addition & 2 deletions cardinal/gamestate/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"pkg.world.dev/world-engine/assert"
"pkg.world.dev/world-engine/cardinal/gamestate"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -256,7 +255,7 @@ func TestRemovedComponentDataCanBeRecovered(t *testing.T) {

// Make sure we can no longer get the foo component
_, err = manager.GetComponentForEntity(fooComp, id)
assert.ErrorIs(t, err, iterators.ErrComponentNotOnEntity)
assert.ErrorIs(t, err, gamestate.ErrComponentNotOnEntity)
// But uhoh, there was a problem. This means the removal of the Foo component
// will be undone, and the original value can be found
err = manager.DiscardPending()
Expand Down
3 changes: 1 addition & 2 deletions cardinal/gamestate/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"pkg.world.dev/world-engine/cardinal/codec"
"pkg.world.dev/world-engine/cardinal/iterators"
"pkg.world.dev/world-engine/cardinal/types"
)

Expand Down Expand Up @@ -395,7 +394,7 @@ func getArchIDToCompTypesFromRedis(
for _, compTypeID := range compTypeIDs {
currComp, err := typeToComp.Get(compTypeID)
if err != nil {
return nil, false, eris.Wrap(iterators.ErrComponentMismatchWithSavedState, "")
return nil, false, eris.Wrap(ErrComponentMismatchWithSavedState, "")
}
currComps = append(currComps, currComp)
}
Expand Down
37 changes: 0 additions & 37 deletions cardinal/iterators/iterator.go

This file was deleted.

Loading

0 comments on commit f25625b

Please sign in to comment.