-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds ordering capability in the API and web UI. There is also some refactoring of the web UI that has significantly improved performance. To get proper ordering (particularly by seeders/leechers), the index must be reprocessed. I'm aware that certain combinations of filters and orders can be slow, but for most performance is acceptable. For example, ordering a large filtered result by size (ascending) seems very slow, though descending seems okay - I've experimented with a few indexing and database tweaks without much luck and so decided to leave as-is, it can possibly be addressed later or it may be an inherent limitation of Postgres.
- Loading branch information
Showing
39 changed files
with
2,057 additions
and
907 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ fragment Torrent on Torrent { | |
infoHash | ||
name | ||
size | ||
private | ||
filesStatus | ||
filesCount | ||
hasFilesInfo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package search | ||
|
||
import ( | ||
"github.com/bitmagnet-io/bitmagnet/internal/database/query" | ||
"github.com/bitmagnet-io/bitmagnet/internal/maps" | ||
"github.com/bitmagnet-io/bitmagnet/internal/model" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
//go:generate go run github.com/abice/go-enum --marshal --names --nocase --nocomments --sql --sqlnullstr --values -f order_torrent_content.go | ||
|
||
// TorrentContentOrderBy represents sort orders for torrent content search results | ||
// ENUM(Relevance, PublishedAt, UpdatedAt, Size, Files, Seeders, Leechers, Name, InfoHash) | ||
type TorrentContentOrderBy string | ||
|
||
// OrderDirection represents sort order directions | ||
// ENUM(Ascending, Descending) | ||
type OrderDirection string | ||
|
||
func (ob TorrentContentOrderBy) Clauses(direction OrderDirection) []clause.OrderByColumn { | ||
desc := direction == OrderDirectionDescending | ||
switch ob { | ||
case TorrentContentOrderByRelevance: | ||
return []clause.OrderByColumn{{ | ||
Column: clause.Column{ | ||
Name: query.QueryStringRankField, | ||
}, | ||
Desc: desc, | ||
}} | ||
case TorrentContentOrderByPublishedAt: | ||
return []clause.OrderByColumn{{ | ||
Column: clause.Column{ | ||
Table: model.TableNameTorrentContent, | ||
Name: "published_at", | ||
}, | ||
Desc: desc, | ||
}} | ||
case TorrentContentOrderByUpdatedAt: | ||
return []clause.OrderByColumn{{ | ||
Column: clause.Column{ | ||
Table: model.TableNameTorrentContent, | ||
Name: "updated_at", | ||
}, | ||
Desc: desc, | ||
}} | ||
case TorrentContentOrderBySize: | ||
return []clause.OrderByColumn{{ | ||
Column: clause.Column{ | ||
Table: model.TableNameTorrent, | ||
Name: "size", | ||
}, | ||
Desc: desc, | ||
}} | ||
case TorrentContentOrderByFiles: | ||
return []clause.OrderByColumn{{ | ||
Column: clause.Column{ | ||
Name: "CASE WHEN " + model.TableNameTorrent + ".files_status = 'single' THEN 1 ELSE COALESCE(" + model.TableNameTorrent + ".files_count, -1) END", | ||
Raw: true, | ||
}, | ||
Desc: desc, | ||
}} | ||
case TorrentContentOrderBySeeders: | ||
return []clause.OrderByColumn{ | ||
{ | ||
Column: clause.Column{ | ||
Name: model.TableNameTorrentContent + ".seeders IS NULL", | ||
Raw: true, | ||
}, | ||
Desc: !desc, | ||
}, | ||
{ | ||
Column: clause.Column{ | ||
Table: model.TableNameTorrentContent, | ||
Name: "seeders", | ||
}, | ||
Desc: desc, | ||
}, | ||
} | ||
case TorrentContentOrderByLeechers: | ||
return []clause.OrderByColumn{ | ||
{ | ||
Column: clause.Column{ | ||
Name: model.TableNameTorrentContent + ".leechers IS NULL", | ||
Raw: true, | ||
}, | ||
Desc: !desc, | ||
}, | ||
{ | ||
Column: clause.Column{ | ||
Table: model.TableNameTorrentContent, | ||
Name: "leechers", | ||
}, | ||
Desc: desc, | ||
}, | ||
} | ||
case TorrentContentOrderByName: | ||
return []clause.OrderByColumn{{ | ||
Column: clause.Column{ | ||
Table: model.TableNameTorrent, | ||
Name: "name", | ||
}, | ||
Desc: desc, | ||
}} | ||
case TorrentContentOrderByInfoHash: | ||
return []clause.OrderByColumn{{ | ||
Column: clause.Column{ | ||
Table: model.TableNameTorrentContent, | ||
Name: "info_hash", | ||
}, | ||
Desc: desc, | ||
}} | ||
default: | ||
return []clause.OrderByColumn{} | ||
} | ||
} | ||
|
||
type TorrentContentFullOrderBy maps.InsertMap[TorrentContentOrderBy, OrderDirection] | ||
|
||
func (fob TorrentContentFullOrderBy) Clauses() []clause.OrderByColumn { | ||
im := maps.InsertMap[TorrentContentOrderBy, OrderDirection](fob) | ||
clauses := make([]clause.OrderByColumn, 0, im.Len()+2) | ||
for _, ob := range im.Entries() { | ||
clauses = append(clauses, ob.Key.Clauses(ob.Value)...) | ||
} | ||
// make ordering alphabetical and deterministic when not already specified: | ||
if !im.Has(TorrentContentOrderByName) { | ||
clauses = append(clauses, TorrentContentOrderByName.Clauses(OrderDirectionAscending)...) | ||
} | ||
if !im.Has(TorrentContentOrderByInfoHash) { | ||
clauses = append(clauses, TorrentContentOrderByInfoHash.Clauses(OrderDirectionAscending)...) | ||
} | ||
return clauses | ||
} | ||
|
||
func (fob TorrentContentFullOrderBy) Option() query.Option { | ||
return query.Options( | ||
query.RequireJoin(model.TableNameTorrent), | ||
query.OrderBy(fob.Clauses()...), | ||
) | ||
} |
Oops, something went wrong.