Page research without exact counts and bound candidate scans [skip ci]
This commit is contained in:
+15
-20
@@ -49,20 +49,20 @@ type Filters struct {
|
||||
SearchIn string
|
||||
Topic string
|
||||
Sort string
|
||||
Page int
|
||||
Cursor string
|
||||
PageSize int
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
Items []Paper `json:"items"`
|
||||
Total int64 `json:"total"`
|
||||
Topics []string `json:"topics"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Items []Paper `json:"items"`
|
||||
NextCursor string `json:"nextCursor"`
|
||||
PreviousCursor string `json:"previousCursor"`
|
||||
Topics []string `json:"topics"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
// Repository implementations must use a read-only transaction for each List,
|
||||
// so its count, topics and page describe the same database snapshot.
|
||||
// so its metadata and page describe the same database snapshot.
|
||||
type Repository interface {
|
||||
List(context.Context, Filters) (Page, error)
|
||||
Get(context.Context, int64) (Paper, error)
|
||||
@@ -102,7 +102,7 @@ var topicPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9.-]{0,79}$`)
|
||||
var positiveInteger = regexp.MustCompile(`^[1-9][0-9]*$`)
|
||||
|
||||
func parseFilters(raw string) (Filters, error) {
|
||||
f := Filters{SearchIn: "text", Sort: "recent", Page: 1, PageSize: 8}
|
||||
f := Filters{SearchIn: "text", Sort: "recent", PageSize: 8}
|
||||
values, err := url.ParseQuery(raw)
|
||||
if err != nil {
|
||||
return f, err
|
||||
@@ -133,28 +133,23 @@ func parseFilters(raw string) (Filters, error) {
|
||||
return f, invalid
|
||||
}
|
||||
f.Sort = value[0]
|
||||
case "page", "pageSize":
|
||||
case "cursor":
|
||||
f.Cursor = value[0]
|
||||
case "pageSize":
|
||||
if !positiveInteger.MatchString(value[0]) {
|
||||
return f, invalid
|
||||
}
|
||||
n, err := strconv.Atoi(value[0])
|
||||
limit := 1000
|
||||
if key == "pageSize" {
|
||||
limit = 50
|
||||
}
|
||||
if err != nil || n > limit {
|
||||
if err != nil || n > 50 {
|
||||
return f, invalid
|
||||
}
|
||||
if key == "page" {
|
||||
f.Page = n
|
||||
} else {
|
||||
f.PageSize = n
|
||||
}
|
||||
f.PageSize = n
|
||||
default:
|
||||
return f, invalid
|
||||
}
|
||||
}
|
||||
return f, nil
|
||||
_, err = decodeCursor(f)
|
||||
return f, err
|
||||
}
|
||||
|
||||
func (s *service) serve(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user