-
Notifications
You must be signed in to change notification settings - Fork 3
/
qry.go
113 lines (95 loc) · 2.88 KB
/
qry.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-FileCopyrightText: 2021 The margaret Authors
//
// SPDX-License-Identifier: MIT
package margaret // import "github.com/ssbc/margaret"
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mock/qry.go . Query
// Query is the interface implemented by the concrete log implementations that collects the constraints of the query.
type Query interface {
// Gt makes the source return only items with sequence numbers > seq.
Gt(seq int64) error
// Gte makes the source return only items with sequence numbers >= seq.
Gte(seq int64) error
// Lt makes the source return only items with sequence numbers < seq.
Lt(seq int64) error
// Lte makes the source return only items with sequence numbers <= seq.
Lte(seq int64) error
// Limit makes the source return only up to n items.
Limit(n int) error
// Reverse makes the source return the lastest values first
Reverse(yes bool) error
// Live makes the source block at the end of the log and wait for new values
// that are being appended.
Live(bool) error
// SeqWrap makes the source return values that contain both the item and its
// sequence number, instead of the item alone.
SeqWrap(bool) error
}
// QuerySpec is a constraint on the query.
type QuerySpec func(Query) error
// MergeQuerySpec collects several contraints and merges them into one.
func MergeQuerySpec(spec ...QuerySpec) QuerySpec {
return func(qry Query) error {
for _, f := range spec {
err := f(qry)
if err != nil {
return err
}
}
return nil
}
}
// ErrorQuerySpec makes the log.Query call return the passed error.
func ErrorQuerySpec(err error) QuerySpec {
return func(Query) error {
return err
}
}
// Gt makes the source return only items with sequence numbers > seq.
func Gt(s int64) QuerySpec {
return func(q Query) error {
return q.Gt(s)
}
}
// Gte makes the source return only items with sequence numbers >= seq.
func Gte(s int64) QuerySpec {
return func(q Query) error {
return q.Gte(s)
}
}
// Lt makes the source return only items with sequence numbers < seq.
func Lt(s int64) QuerySpec {
return func(q Query) error {
return q.Lt(s)
}
}
// Lte makes the source return only items with sequence numbers <= seq.
func Lte(s int64) QuerySpec {
return func(q Query) error {
return q.Lte(s)
}
}
// Limit makes the source return only up to n items.
func Limit(n int) QuerySpec {
return func(q Query) error {
return q.Limit(n)
}
}
// Live makes the source block at the end of the log and wait for new values
// that are being appended.
func Live(live bool) QuerySpec {
return func(q Query) error {
return q.Live(live)
}
}
// SeqWrap makes the source return values that contain both the item and its
// sequence number, instead of the item alone.
func SeqWrap(wrap bool) QuerySpec {
return func(q Query) error {
return q.SeqWrap(wrap)
}
}
func Reverse(yes bool) QuerySpec {
return func(q Query) error {
return q.Reverse(yes)
}
}