forked from hazelcast/hazelcast-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_queue.go
331 lines (298 loc) · 11.7 KB
/
proxy_queue.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hazelcast
import (
"context"
"time"
pubcluster "github.com/hazelcast/hazelcast-go-client/cluster"
"github.com/hazelcast/hazelcast-go-client/internal/check"
"github.com/hazelcast/hazelcast-go-client/internal/proto"
"github.com/hazelcast/hazelcast-go-client/internal/proto/codec"
iserialization "github.com/hazelcast/hazelcast-go-client/internal/serialization"
"github.com/hazelcast/hazelcast-go-client/types"
)
/*
Queue is a concurrent, blocking, distributed, observable queue.
Queue is not a partitioned data-structure.
All of the Queue content is stored in a single machine (and in the backup).
Queue will not scale by adding more members in the cluster.
For details see https://docs.hazelcast.com/imdg/latest/data-structures/queue.html
*/
type Queue struct {
*proxy
partitionID int32
}
func newQueue(p *proxy) (*Queue, error) {
if partitionID, err := p.stringToPartitionID(p.name); err != nil {
return nil, err
} else {
return &Queue{proxy: p, partitionID: partitionID}, nil
}
}
// Add adds the specified item to this queue if there is available space.
// Returns true when element is successfully added
func (q *Queue) Add(ctx context.Context, value interface{}) (bool, error) {
return q.add(ctx, value, 0)
}
// AddWithTimeout adds the specified item to this queue if there is available space.
// Returns true when element is successfully added
func (q *Queue) AddWithTimeout(ctx context.Context, value interface{}, timeout time.Duration) (bool, error) {
return q.add(ctx, value, timeout.Milliseconds())
}
// AddAll adds the elements in the specified collection to this queue.
// Returns true if the queue is changed after the call.
func (q *Queue) AddAll(ctx context.Context, values ...interface{}) (bool, error) {
if len(values) == 0 {
return false, nil
}
if valuesData, err := q.validateAndSerializeValues(values); err != nil {
return false, err
} else {
request := codec.EncodeQueueAddAllRequest(q.name, valuesData)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, err
} else {
return codec.DecodeQueueAddAllResponse(response), nil
}
}
}
// AddItemListener adds an item listener for this queue. Listener will be notified for all queue add/remove events.
// Received events include the updated item if includeValue is true.
func (q *Queue) AddItemListener(ctx context.Context, includeValue bool, handler QueueItemNotifiedHandler) (types.UUID, error) {
return q.addListener(ctx, includeValue, handler)
}
// Clear Clear this queue. Queue will be empty after this call.
func (q *Queue) Clear(ctx context.Context) error {
request := codec.EncodeQueueClearRequest(q.name)
_, err := q.invokeOnPartition(ctx, request, q.partitionID)
return err
}
// Contains returns true if the queue includes the given value.
func (q *Queue) Contains(ctx context.Context, value interface{}) (bool, error) {
if valueData, err := q.validateAndSerialize(value); err != nil {
return false, err
} else {
request := codec.EncodeQueueContainsRequest(q.name, valueData)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, err
} else {
return codec.DecodeQueueContainsResponse(response), nil
}
}
}
// ContainsAll returns true if the queue includes all given values.
func (q *Queue) ContainsAll(ctx context.Context, values ...interface{}) (bool, error) {
if len(values) == 0 {
return false, nil
}
if valuesData, err := q.validateAndSerializeValues(values); err != nil {
return false, err
} else {
request := codec.EncodeQueueContainsAllRequest(q.name, valuesData)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, err
} else {
return codec.DecodeQueueContainsAllResponse(response), nil
}
}
}
// Drain returns all items in the queue and empties it.
func (q *Queue) Drain(ctx context.Context) ([]interface{}, error) {
request := codec.EncodeQueueDrainToRequest(q.name)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return nil, err
} else {
return q.convertToObjects(codec.DecodeQueueDrainToResponse(response))
}
}
// DrainWithMaxSize returns maximum maxSize items in tne queue and removes returned items from the queue.
func (q *Queue) DrainWithMaxSize(ctx context.Context, maxSize int) ([]interface{}, error) {
maxSizeAsInt32, err := check.NonNegativeInt32(maxSize)
if err != nil {
return nil, err
}
request := codec.EncodeQueueDrainToMaxSizeRequest(q.name, maxSizeAsInt32)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return nil, err
} else {
return q.convertToObjects(codec.DecodeQueueDrainToMaxSizeResponse(response))
}
}
// GetAll returns all of the items in this queue.
func (q *Queue) GetAll(ctx context.Context) ([]interface{}, error) {
request := codec.EncodeQueueIteratorRequest(q.name)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return nil, err
} else {
return q.convertToObjects(codec.DecodeQueueIteratorResponse(response))
}
}
// IsEmpty returns true if the queue is empty.
func (q *Queue) IsEmpty(ctx context.Context) (bool, error) {
request := codec.EncodeQueueIsEmptyRequest(q.name)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, err
} else {
return codec.DecodeQueueIsEmptyResponse(response), nil
}
}
// Peek retrieves the head of queue without removing it from the queue.
func (q *Queue) Peek(ctx context.Context) (interface{}, error) {
request := codec.EncodeQueuePeekRequest(q.name)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return nil, err
} else {
return q.convertToObject(codec.DecodeQueuePeekResponse(response))
}
}
// Poll retrieves and removes the head of this queue.
func (q *Queue) Poll(ctx context.Context) (interface{}, error) {
return q.poll(ctx, 0)
}
// PollWithTimeout retrieves and removes the head of this queue.
// Waits until this timeout elapses and returns the result.
func (q *Queue) PollWithTimeout(ctx context.Context, timeout time.Duration) (interface{}, error) {
return q.poll(ctx, timeout.Milliseconds())
}
// Put adds the specified element into this queue.
// If there is no space, it waits until necessary space becomes available.
func (q *Queue) Put(ctx context.Context, value interface{}) error {
if valueData, err := q.validateAndSerialize(value); err != nil {
return err
} else {
request := codec.EncodeQueuePutRequest(q.name, valueData)
_, err := q.invokeOnPartition(ctx, request, q.partitionID)
return err
}
}
// RemainingCapacity returns the remaining capacity of this queue.
func (q *Queue) RemainingCapacity(ctx context.Context) (int, error) {
request := codec.EncodeQueueRemainingCapacityRequest(q.name)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return 0, err
} else {
return int(codec.DecodeQueueRemainingCapacityResponse(response)), nil
}
}
// Remove removes the specified element from the queue if it exists.
func (q *Queue) Remove(ctx context.Context, value interface{}) (bool, error) {
if data, err := q.validateAndSerialize(value); err != nil {
return false, err
} else {
request := codec.EncodeQueueRemoveRequest(q.name, data)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, nil
} else {
return codec.DecodeQueueRemoveResponse(response), nil
}
}
}
// RemoveAll removes all of the elements of the specified collection from this queue.
func (q *Queue) RemoveAll(ctx context.Context, values ...interface{}) (bool, error) {
if len(values) == 0 {
return false, nil
}
if valuesData, err := q.validateAndSerializeValues(values); err != nil {
return false, err
} else {
request := codec.EncodeQueueCompareAndRemoveAllRequest(q.name, valuesData)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, err
} else {
return codec.DecodeQueueCompareAndRemoveAllResponse(response), nil
}
}
}
// RemoveListener removes the specified listener.
func (q *Queue) RemoveListener(ctx context.Context, subscriptionID types.UUID) error {
return q.listenerBinder.Remove(ctx, subscriptionID)
}
// RetainAll removes the items which are not contained in the specified collection.
func (q *Queue) RetainAll(ctx context.Context, values ...interface{}) (bool, error) {
if len(values) == 0 {
return false, nil
}
if valuesData, err := q.validateAndSerializeValues(values); err != nil {
return false, err
} else {
request := codec.EncodeQueueCompareAndRetainAllRequest(q.name, valuesData)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, err
} else {
return codec.DecodeQueueCompareAndRetainAllResponse(response), nil
}
}
}
// Size returns the number of elements in this collection.
func (q *Queue) Size(ctx context.Context) (int, error) {
request := codec.EncodeQueueSizeRequest(q.name)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return 0, err
} else {
return int(codec.DecodeQueueSizeResponse(response)), nil
}
}
// Take retrieves and removes the head of this queue, if necessary, waits until an item becomes available.
func (q *Queue) Take(ctx context.Context) (interface{}, error) {
request := codec.EncodeQueueTakeRequest(q.name)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return nil, err
} else {
return q.convertToObject(codec.DecodeQueueTakeResponse(response))
}
}
func (q *Queue) add(ctx context.Context, value interface{}, timeout int64) (bool, error) {
if valueData, err := q.validateAndSerialize(value); err != nil {
return false, nil
} else {
request := codec.EncodeQueueOfferRequest(q.name, valueData, timeout)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return false, err
} else {
return codec.DecodeQueueOfferResponse(response), nil
}
}
}
func (q *Queue) addListener(ctx context.Context, includeValue bool, handler QueueItemNotifiedHandler) (types.UUID, error) {
subscriptionID := types.NewUUID()
addRequest := codec.EncodeQueueAddListenerRequest(q.name, includeValue, q.smart)
removeRequest := codec.EncodeQueueRemoveListenerRequest(q.name, subscriptionID)
listenerHandler := func(msg *proto.ClientMessage) {
codec.HandleQueueAddListener(msg, func(itemData iserialization.Data, uuid types.UUID, eventType int32) {
item, err := q.convertToObject(itemData)
if err != nil {
q.logger.Warnf("cannot convert data to Go value")
return
}
// prevent panic if member not found
var member pubcluster.MemberInfo
if m := q.clusterService.GetMemberByUUID(uuid); m != nil {
member = *m
}
handler(newQueueItemNotified(q.name, item, member, eventType))
})
}
err := q.listenerBinder.Add(ctx, subscriptionID, addRequest, removeRequest, listenerHandler)
return subscriptionID, err
}
func (q *Queue) poll(ctx context.Context, timeout int64) (interface{}, error) {
request := codec.EncodeQueuePollRequest(q.name, timeout)
if response, err := q.invokeOnPartition(ctx, request, q.partitionID); err != nil {
return nil, err
} else {
return q.convertToObject(codec.DecodeQueuePollResponse(response))
}
}