-
Notifications
You must be signed in to change notification settings - Fork 52
feat: query with empty filter #863
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
base: main
Are you sure you want to change the base?
Conversation
How to use the Graphite Merge QueueAdd the label graphite/merge to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather we create a new, explicit match type, e.g. SearchAll instead doing it implicitly if find is empty. In this case, find can be empty only when match type is all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, updated the code
pkg/cardinal/ecs/search.go
Outdated
| Find []string // List of component names to search for. Empty = all entities. | ||
| Match SearchMatch // A match type to use for the search. Ignored when Find is empty. | ||
| Where string // Optional expr language string to filter the results. | ||
| Limit int // Maximum number of results to return (default: 50, 0 = use default, max: 10000) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a hard limit of 2^32 entities, so limit should also be type uint32. Limit and offset can never be negative, we should take advantage of the type system so we don't have to write extra code like check int boundaries, e.g. the validations and normalizeLimit can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what a great feedback, updated the code
pkg/cardinal/ecs/search.go
Outdated
| skipped *int, | ||
| results *[]map[string]any, | ||
| collected *int, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that this function need all these pointers as args very often means it's not meant to be factored out. I'd put this back into NewSearch so you can see the whole filtering logic in one place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to NewSearch for readability
pkg/cardinal/ecs/search.go
Outdated
|
|
||
| results := make([]map[string]any, 0) | ||
| limit := normalizeLimit(params.Limit) | ||
| results := make([]map[string]any, 0, limit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can omit the size and limit, make for maps doesn't accept the capacity field. Limit is huge if not specified, so we'll just use Go's map default size. A downside is this can result in more allocations than if we specified the size, but we'll worry about this later if somehow the allocations affects the tickrate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the code
pkg/cardinal/ecs/search.go
Outdated
| // Query limit constants. | ||
| const ( | ||
| // DefaultQueryLimit is the default maximum number of results returned when Limit is 0 or not specified. | ||
| DefaultQueryLimit = 50 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to make the default limit unlimited (max u32), which seems more intuitive as having a non-unlimited default limit means we have more work to educate the users. We can worry about memory usage much later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the code
| } | ||
|
|
||
| // NewSearch returns a map of entities that match the given search parameters. | ||
| func (w *World) NewSearch(params SearchParam) ([]map[string]any, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Move the public APIs to the top of the file since that's the natural reading order. Helper functions go after since they're not too important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the code
ab9ec70 to
59cfc30
Compare
59cfc30 to
72dc3ca
Compare
|

Add Pagination and Query All Entities Support to Cardinal ECS
Summary
This PR adds pagination support (
limitandoffset) to the Cardinal ECS query system and enables querying all entities without specifying component filters. The changes include refactoring the search implementation for better maintainability and adding comprehensive test coverage.Features
1. Pagination Support
2. Query All Entities
Findlist now queries all entities regardless of componentsMatchparameter is ignored whenFindis empty (backward compatible)3. Code Refactoring
buildEntityResult(): Creates result map from entity and componentsmatchesFilter(): Checks if entity matches filter expressionnormalizeLimit(): Applies default and enforces maximum limitprocessEntity(): Processes single entity with filter, offset, and limit logicChanges
ECS Layer (
pkg/cardinal/ecs/search.go)LimitandOffsetfields toSearchParamstructDefaultQueryLimit(50),MaxQueryLimit(10000),MinQueryLimit(0),MinQueryOffset(0)validateAndGetFilter()to allow emptyFindlistfindMatchingArchetypes()to return all archetype IDs whenFindis emptyNewSearch()to implement pagination with early terminationService Layer (
pkg/cardinal/service/query.go)LimitandOffsetfields toQuerystructparseQuery()to parselimitandoffsetfrom protobuf payloadProtobuf Schema (
proto/worldengine/isc/v1/query.proto)min_len: 1validation fromfindfield to allow empty arraysnot_in: [0]validation frommatchfield to allowMATCH_UNSPECIFIEDwhenfindis emptylimitfield (int32, field 4) with validation:gte: 0, lte: 10000offsetfield (int32, field 5) with validation:gte: 0Testing
Findno longer errors)Findqueries (querying all entities)LimitandOffset)WherefiltersAPI Changes
Request Format
{ "address": { "realm": "REALM_WORLD", "region": "us-west-2", "organization": "organization", "project": "query-demo", "serviceId": "game" }, "query": { "find": ["charactertag", "health"], // Empty array = all entities "match": "MATCH_CONTAINS", // Ignored when find is empty "where": "health.hp > 50", // Optional filter "limit": 10, // New: max results (default: 50) "offset": 0 // New: skip N results (default: 0) } }Response Format
Unchanged - returns
QueryResultwithentitiesarray.Backward Compatibility
✅ Fully backward compatible
Findlist: Previously returned error, now returns all entitieslimit/offset: Defaults applied automatically (limit: 50, offset: 0)Performance Considerations
Known Issues / Notes
limitandoffsetfields from the JSON request. Currently, these fields may not be extracted from the nestedqueryobject during JSON-to-protobuf conversion. This is a gateway issue, not a Cardinal issue.Workaround: Once the gateway is updated to parse these fields correctly, pagination will work as expected.
Testing
All existing tests pass. New tests added:
TestSearch_EmptyFind- Query all entitiesTestSearch_Pagination- Comprehensive pagination testsExample Usage
Query All Entities
{ "query": { "find": [], "limit": 10 } }Pagination - Page 1
{ "query": { "find": ["charactertag", "health"], "match": "MATCH_CONTAINS", "limit": 5, "offset": 0 } }Pagination - Page 2
{ "query": { "find": ["charactertag", "health"], "match": "MATCH_CONTAINS", "limit": 5, "offset": 5 } }