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

Make haystack cursors consistent with listing #994

Merged
merged 1 commit into from
Mar 22, 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Consistent validation of override operator in runtime leaderboard record writes.
- Correctly filter open/closed groups in the listing API.
- Ensure direct message channel message listing is correctly scoped to participants only.
- Make next and previous cursor of leaderboard and tournament records around owner operations consistent with record listing.
- Make next and previous cursor of leaderboard and tournament records haystack operations consistent with record listing.

## [3.15.0] - 2023-01-04
### Added
Expand Down
64 changes: 32 additions & 32 deletions server/core_leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,6 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *
ownerRecord.ExpiryTime = &timestamppb.Timestamp{Seconds: expiryTime}
}

if limit == 1 {
ownerRecord.Rank = rankCache.Get(leaderboardId, expiryTime.Unix(), ownerID)
return &api.LeaderboardRecordList{Records: []*api.LeaderboardRecord{ownerRecord}}, nil
}

query := `SELECT leaderboard_id, owner_id, username, score, subscore, num_score, max_num_score, metadata, create_time, update_time, expiry_time
FROM leaderboard_record
WHERE leaderboard_id = $1
Expand Down Expand Up @@ -793,10 +788,10 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *
return nil, err
}

setNextCursor := false
setPrevCursor := false
if len(firstRecords) > limit {
// Check if there might be a next cursor
setNextCursor = true
setPrevCursor = true
firstRecords = firstRecords[:len(firstRecords)-1]
}

Expand Down Expand Up @@ -832,10 +827,10 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *
return nil, err
}

setPrevCursor := false
setNextCursor := false
if len(secondRecords) > secondLimit {
// Check if there might be a prev cursor
setPrevCursor = true
setNextCursor = true
secondRecords = secondRecords[:len(secondRecords)-1]
}

Expand All @@ -844,53 +839,58 @@ func getLeaderboardRecordsHaystack(ctx context.Context, logger *zap.Logger, db *

numRecords := len(records)
start := numRecords - limit
if start < 0 || len(firstRecords) < limit/2 {
if start < 0 || len(firstRecords) < secondLimit {
start = 0
}
end := start + limit
if end > numRecords {
end = numRecords
}

if start > 0 {
// There was a previous result that was discarded, the prev_cursor should be set.
setPrevCursor = true
}

records = records[start:end]
rankCache.Fill(leaderboardId, expiryTime.Unix(), records)

var nextCursorStr string
if setNextCursor {
firstRecord := records[0]
var prevCursorStr string
if setPrevCursor {
record := records[0]

nextCursor := &leaderboardRecordListCursor{
prevCursor := &leaderboardRecordListCursor{
IsNext: false,
LeaderboardId: firstRecord.LeaderboardId,
LeaderboardId: record.LeaderboardId,
ExpiryTime: expiryTime.Unix(),
Score: firstRecord.Score,
Subscore: firstRecord.Subscore,
OwnerId: firstRecord.OwnerId,
Rank: firstRecord.Rank,
Score: record.Score,
Subscore: record.Subscore,
OwnerId: record.OwnerId,
Rank: record.Rank,
}
nextCursorStr, err = marshalLeaderboardRecordsListCursor(nextCursor)
prevCursorStr, err = marshalLeaderboardRecordsListCursor(prevCursor)
if err != nil {
logger.Error("Error creating leaderboard records list next cursor", zap.Error(err))
logger.Error("Error creating leaderboard records list previous cursor", zap.Error(err))
return nil, err
}
}

var prevCursorStr string
if setPrevCursor {
lastRecord := records[len(records)-1]
var nextCursorStr string
if setNextCursor {
record := records[len(records)-1]

prevCursor := &leaderboardRecordListCursor{
nextCursor := &leaderboardRecordListCursor{
IsNext: true,
LeaderboardId: lastRecord.LeaderboardId,
LeaderboardId: record.LeaderboardId,
ExpiryTime: expiryTime.Unix(),
Score: lastRecord.Score,
Subscore: lastRecord.Subscore,
OwnerId: lastRecord.OwnerId,
Rank: lastRecord.Rank,
Score: record.Score,
Subscore: record.Subscore,
OwnerId: record.OwnerId,
Rank: record.Rank,
}
prevCursorStr, err = marshalLeaderboardRecordsListCursor(prevCursor)
nextCursorStr, err = marshalLeaderboardRecordsListCursor(nextCursor)
if err != nil {
logger.Error("Error creating leaderboard records list previous cursor", zap.Error(err))
logger.Error("Error creating leaderboard records list next cursor", zap.Error(err))
return nil, err
}
}
Expand Down