perf(db): keyset pagination for revision prefixes
Keyset по id/ord вместо OFFSET; anti-join для RevisionDiff. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -795,31 +793,37 @@ func (m *Memory) ListRevisionPrefixes(tenantID, revisionID string, cursor string
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
afterID, off, useOffset := ParsePrefixPageCursor(cursor)
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if _, err := m.getRevisionLocked(tenantID, revisionID); err != nil {
|
||||
return nil, "", false
|
||||
}
|
||||
all := m.revPrefixes[revisionID]
|
||||
off := 0
|
||||
if cursor != "" {
|
||||
if n, err := strconv.Atoi(cursor); err == nil && n >= 0 {
|
||||
off = n
|
||||
}
|
||||
allRows := m.revPrefixes[revisionID]
|
||||
start := 0
|
||||
if useOffset {
|
||||
start = off
|
||||
} else if afterID != nil {
|
||||
start = int(*afterID) + 1
|
||||
}
|
||||
end := off + limit
|
||||
next := ""
|
||||
more := false
|
||||
if end > len(all) {
|
||||
end = len(all)
|
||||
} else {
|
||||
more = true
|
||||
next = fmt.Sprintf("%d", end)
|
||||
}
|
||||
if off >= len(all) {
|
||||
if start > len(allRows) {
|
||||
return nil, "", false
|
||||
}
|
||||
return all[off:end], next, more
|
||||
end := start + limit
|
||||
next := ""
|
||||
more := false
|
||||
if end > len(allRows) {
|
||||
end = len(allRows)
|
||||
} else {
|
||||
more = true
|
||||
next = FormatPrefixPageCursor(int64(end - 1))
|
||||
}
|
||||
if start >= end {
|
||||
return nil, "", false
|
||||
}
|
||||
out := make([]PrefixRow, end-start)
|
||||
copy(out, allRows[start:end])
|
||||
return out, next, more
|
||||
}
|
||||
|
||||
func (m *Memory) ListGlobalSettings(tenantID string) (map[string]any, error) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ParsePrefixPageCursor decodes opaque cursors for revision prefix pagination.
|
||||
func ParsePrefixPageCursor(cursor string) (afterID *int64, offset int, useOffset bool) {
|
||||
cursor = strings.TrimSpace(cursor)
|
||||
if cursor == "" {
|
||||
return nil, 0, false
|
||||
}
|
||||
if strings.HasPrefix(cursor, "o:") {
|
||||
n, err := strconv.Atoi(strings.TrimPrefix(cursor, "o:"))
|
||||
if err != nil || n < 0 {
|
||||
return nil, 0, false
|
||||
}
|
||||
return nil, n, true
|
||||
}
|
||||
if n, err := strconv.ParseInt(cursor, 10, 64); err == nil && n >= 0 {
|
||||
return &n, 0, false
|
||||
}
|
||||
return nil, 0, false
|
||||
}
|
||||
|
||||
// FormatPrefixPageCursor encodes the keyset cursor (last row id or slice index).
|
||||
func FormatPrefixPageCursor(lastID int64) string {
|
||||
return strconv.FormatInt(lastID, 10)
|
||||
}
|
||||
Reference in New Issue
Block a user