-
Notifications
You must be signed in to change notification settings - Fork 88
GO-5800 Support sort by object relations #2666
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
Conversation
Coverage provided by https://github.com/seriousben/go-patch-cover-action |
| s.skl.Set(e, nil) | ||
| } | ||
|
|
||
| defer s.diff.reset() |
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.
It's a large chunk of complex duplicated code from onChange, let's extract it to function
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 separate method onSklChange
core/subscription/sorted.go
Outdated
| return | ||
| } | ||
|
|
||
| entries := s.getActiveEntries() |
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 have to use all entries, not only the active
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.
Thank you! Now first we retrieve all entries by iteration over SkipList elements from Front, only then reset Skiplist
core/subscription/dep.go
Outdated
| return | ||
| } | ||
|
|
||
| func (ds *dependencyService) enregisterObjectSorts(subId string, sorts []database.SortRequest) { |
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.
maybe use verb register? It's much more common
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.
Done!
core/subscription/dep.go
Outdated
| return keys | ||
| } | ||
| for _, sort := range sorts { | ||
| keys = append(keys, sort.orderKey()) |
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 also use if !slices.Contains here, lo.Uniq is a much heavier operation
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.
Done!
core/subscription/dep.go
Outdated
| }) | ||
| } | ||
|
|
||
| if len(sortRelations) != 0 { |
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.
len > 0 by code style
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.
SortsMap was refactored
core/subscription/dep.go
Outdated
| depIds = append(depIds, depId) | ||
| } | ||
| if isSortKey { | ||
| if ds.depOrderObjects[depId] == nil { |
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.
maybe extract it to a function?
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.
Done!
core/subscription/dep.go
Outdated
|
|
||
| type sortsMap map[string][]sortKey // subId -> sortRelationKeys | ||
|
|
||
| func (m sortsMap) getSortKey(subId string, key domain.RelationKey) (sortKey, bool) { |
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.
maybe add a symmetrical function setSortKeys?
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.
SortsMap was refactored. Please take a look, maybe it is not necessary yet
core/subscription/dep.go
Outdated
| return bundle.RelationKeyName | ||
| } | ||
|
|
||
| type sortsMap map[string][]sortKey // subId -> sortRelationKeys |
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.
Can we benefit from using a map for keys? It seems so
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.
Done!
| } | ||
| } | ||
|
|
||
| if hasName { |
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.
It will be simpler if you'll add case hasName && hasOrderId here
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.
Done
core/subscription/sorted.go
Outdated
| return | ||
| s.afterId = "" | ||
| if err := s.setBeforeAndAfterElements(); err != nil { | ||
| panic(fmt.Errorf("failed to set before and after elements: %w", err)) |
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.
why to panic here?
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.
Replaced with log
pkg/lib/database/order.go
Outdated
| return "" | ||
| } | ||
|
|
||
| var result string |
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.
Maybe use strings.Builder here?
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.
Even with strings.Builder this code will be not effective as it could be. Consider using some kind of reusable buffer. Notice that this function is called inside a sorting procedure which has O(n*log n) worst case scenario .
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.
Done
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.
It's better to use shared buffer here
pkg/lib/database/order.go
Outdated
| var result string | ||
| for _, id := range ids { | ||
| if details, ok := m.data[id]; ok { | ||
| result += details.GetString(key) |
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.
This code implies that arrays [1,2,3] and [2,3,1] are not equal. Does it conform with business rules? Or maybe we must sort sets?
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.
It is stated by products to sort by first value, so it works like that already
pkg/lib/database/order.go
Outdated
| Value: domain.StringList(idsToSet), | ||
| }}}) | ||
| if err != nil { | ||
| log.Errorf("failed to query records to update orders: %v", err) |
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.
Consider returning the error and log it on a some upper level. Function will become slightly cleaner
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.
Done
pkg/lib/database/filter.go
Outdated
| RelationKey: key, | ||
| Condition: model.BlockContentDataviewFilter_NotEmpty, | ||
| }}}, func(details *domain.Details) { | ||
| targetIds = append(targetIds, details.GetStringList(key)...) |
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.
Maybe use a map here? Then build a slice from it
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.
Done!
https://linear.app/anytype/issue/GO-5800/support-sorting-by-object-relations
Right now any sort upon relations with format tag/status/object/file use object ids to calculate order of objects in subscription.
To support ordering by object names and orderIds of tags following changes were made: