/
list.go
63 lines (52 loc) · 1.67 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package server
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"go.etcd.io/etcd/api/v3/etcdserverpb"
)
func (l *LimitedServer) list(ctx context.Context, r *etcdserverpb.RangeRequest) (*RangeResponse, error) {
if len(r.RangeEnd) == 0 {
return nil, fmt.Errorf("invalid range end length of 0")
}
prefix := string(append(r.RangeEnd[:len(r.RangeEnd)-1], r.RangeEnd[len(r.RangeEnd)-1]-1))
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}
start := string(bytes.TrimRight(r.Key, "\x00"))
revision := r.Revision
if r.CountOnly {
rev, count, err := l.backend.Count(ctx, prefix, start, revision)
resp := &RangeResponse{
Header: txnHeader(rev),
Count: count,
}
logrus.Tracef("LIST COUNT key=%s, end=%s, revision=%d, currentRev=%d count=%d", r.Key, r.RangeEnd, revision, rev, count)
return resp, err
}
limit := r.Limit
if limit > 0 {
limit++
}
rev, kvs, err := l.backend.List(ctx, prefix, start, limit, revision)
logrus.Tracef("LIST key=%s, end=%s, revision=%d, currentRev=%d count=%d, limit=%d", r.Key, r.RangeEnd, revision, rev, len(kvs), r.Limit)
resp := &RangeResponse{
Header: txnHeader(rev),
Count: int64(len(kvs)),
Kvs: kvs,
}
// if the number of items returned exceeds the limit, count the keys remaining that follow the start key
if limit > 0 && resp.Count > r.Limit {
resp.More = true
resp.Kvs = kvs[0 : limit-1]
if revision == 0 {
revision = rev
}
rev, resp.Count, err = l.backend.Count(ctx, prefix, start, revision)
logrus.Tracef("LIST COUNT key=%s, end=%s, revision=%d, currentRev=%d count=%d", r.Key, r.RangeEnd, revision, rev, resp.Count)
resp.Header = txnHeader(rev)
}
return resp, err
}