-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer_context.go
More file actions
183 lines (147 loc) · 5.41 KB
/
Copy pathconsumer_context.go
File metadata and controls
183 lines (147 loc) · 5.41 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package streams
import (
"context"
"github.com/fgrzl/enumerators"
"github.com/fgrzl/lexkey"
"github.com/fgrzl/tickle"
)
// Create a new consumer context
func NewConsumerContext(ctx context.Context, client Client) ConsumerContext {
// Create a new cancelContext with cancel function
cancelContext, cancel := context.WithCancel(ctx)
return &consumerContext{
Context: cancelContext,
cancel: cancel,
client: client,
tickler: tickle.NewTickler(),
}
}
type ConsumerContext interface {
context.Context
// Enumerate multiple spaces from a given minimum time (inclusive).
NewMultiSpaceEnumeratorFromTime(spaces []string, min int64) (enumerators.Enumerator[*Entry], *tickle.Subscription)
// Enumerate multiple spaces using specific offsets for each space (exclusive).
NewMultiSpaceEnumeratorFromOffsets(offsetsBySpace map[string]lexkey.LexKey) (enumerators.Enumerator[*Entry], *tickle.Subscription)
// Enumerate a single space from a given minimum time (inclusive).
NewSpaceEnumeratorFromTime(space string, min int64) (enumerators.Enumerator[*Entry], *tickle.Subscription)
// Enumerate a single space starting from a specific offset (exclusive).
NewSpaceEnumeratorFromOffset(space string, offset lexkey.LexKey) (enumerators.Enumerator[*Entry], *tickle.Subscription)
// Enumerate a single segment from a given minimum sequence (exclusive).
NewSegmentEnumeratorFromSequence(space, segment string, sequence uint64) (enumerators.Enumerator[*Entry], *tickle.Subscription)
// Enumerate a single segment from a given minimum time (inclusive).
NewSegmentEnumeratorFromTime(space, segment string, min int64) (enumerators.Enumerator[*Entry], *tickle.Subscription)
// Cancel the context
Cancel()
}
type consumerContext struct {
context.Context
cancel context.CancelFunc
client Client
tickler *tickle.Tickler
}
// NewMultiSpaceEnumeratorFromOffsets implements ConsumerContext.
func (c *consumerContext) NewMultiSpaceEnumeratorFromOffsets(offsetsBySpace map[string]lexkey.LexKey) (enumerators.Enumerator[*Entry], *tickle.Subscription) {
if len(offsetsBySpace) == 0 {
return enumerators.Empty[*Entry](), nil
}
spaces := make([]string, 0, len(offsetsBySpace))
for space := range offsetsBySpace {
spaces = append(spaces, space)
}
args := &Consume{
Offsets: offsetsBySpace,
}
enumerator := c.client.Consume(c, args)
sub := c.setupSpaceTickler(spaces...)
return enumerator, sub
}
// NewMultiSpaceEnumeratorFromTime implements ConsumerContext.
func (c *consumerContext) NewMultiSpaceEnumeratorFromTime(spaces []string, min int64) (enumerators.Enumerator[*Entry], *tickle.Subscription) {
if len(spaces) == 0 {
return enumerators.Empty[*Entry](), nil
}
offsetsBySpace := make(map[string]lexkey.LexKey, len(spaces))
for _, space := range spaces {
offsetsBySpace[space] = lexkey.LexKey{}
}
args := &Consume{
MinTimestamp: min,
Offsets: offsetsBySpace,
}
enumerator := c.client.Consume(c, args)
sub := c.setupSpaceTickler(spaces...)
return enumerator, sub
}
// NewSpaceEnumeratorFromOffset implements ConsumerContext.
func (c *consumerContext) NewSpaceEnumeratorFromOffset(space string, offset lexkey.LexKey) (enumerators.Enumerator[*Entry], *tickle.Subscription) {
if space == "" {
return enumerators.Empty[*Entry](), nil
}
args := &ConsumeSpace{
Space: space,
Offset: offset,
}
enumerator := c.client.ConsumeSpace(c, args)
sub := c.setupSpaceTickler(space)
return enumerator, sub
}
// NewSpaceEnumeratorFromTime implements ConsumerContext.
func (c *consumerContext) NewSpaceEnumeratorFromTime(space string, min int64) (enumerators.Enumerator[*Entry], *tickle.Subscription) {
if space == "" {
return enumerators.Empty[*Entry](), nil
}
args := &ConsumeSpace{
Space: space,
MinTimestamp: min,
}
enumerator := c.client.ConsumeSpace(c, args)
sub := c.setupSpaceTickler(space)
return enumerator, sub
}
// NewSpaceEnumeratorFromTime implements ConsumerContext.
func (c *consumerContext) NewSegmentEnumeratorFromSequence(space, segment string, sequence uint64) (enumerators.Enumerator[*Entry], *tickle.Subscription) {
if space == "" || segment == "" {
return enumerators.Empty[*Entry](), nil
}
args := &ConsumeSegment{
Space: space,
Segment: segment,
MinSequence: sequence,
}
enumerator := c.client.ConsumeSegment(c, args)
sub := c.setupSegmentTickler(space, segment)
return enumerator, sub
}
// NewSpaceEnumeratorFromTime implements ConsumerContext.
func (c *consumerContext) NewSegmentEnumeratorFromTime(space, segment string, min int64) (enumerators.Enumerator[*Entry], *tickle.Subscription) {
if space == "" || segment == "" {
return enumerators.Empty[*Entry](), nil
}
args := &ConsumeSegment{
Space: space,
Segment: segment,
MinTimestamp: min,
}
enumerator := c.client.ConsumeSegment(c, args)
sub := c.setupSegmentTickler(space, segment)
return enumerator, sub
}
func (c *consumerContext) Cancel() {
c.cancel()
}
func (c *consumerContext) setupSpaceTickler(spaces ...string) *tickle.Subscription {
for _, space := range spaces {
c.client.SubcribeToSpace(c, space, c.tickle)
}
sub := c.tickler.Subscribe(c, spaces...)
return sub
}
func (c *consumerContext) setupSegmentTickler(space, segment string) *tickle.Subscription {
c.client.SubcribeToSegment(c, space, segment, c.tickle)
token := space + "." + segment
sub := c.tickler.Subscribe(c, token)
return sub
}
func (c *consumerContext) tickle(s *SegmentStatus) {
c.tickler.Tickle(s.Space, s.Space+"."+s.Segment)
}