-
Notifications
You must be signed in to change notification settings - Fork 453
/
types.go
344 lines (273 loc) · 9.69 KB
/
types.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
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Package ident provides utilities for working with identifiers.
package ident
import (
"fmt"
"sync"
"github.com/m3db/m3/src/m3ninx/doc"
"github.com/m3db/m3/src/x/checked"
"github.com/m3db/m3/src/x/context"
)
// ID represents an immutable identifier to allow use of byte slice pooling
// for the contents of the ID.
type ID interface {
fmt.Stringer
// Bytes returns the underlying byte slice of the bytes ID unpacked from
// any checked bytes container, callers cannot safely hold a ref to these
// bytes.
Bytes() []byte
// Equal returns whether the ID is equal to a given ID.
Equal(value ID) bool
// NoFinalize makes calls to finalize a no-op, this is useful when you
// would like to share a type with another sub-system that should is not
// allowed to finalize the resource as the resource is kept indefinitely
// until garbage collected (i.e. longly lived).
NoFinalize()
// IsNoFinalize returns whether finalize is a no-op or not, this is useful
// when you know you can use an ID without having to worry to take a copy.
IsNoFinalize() bool
// Finalize releases all resources held by the ID, unless NoFinalize has
// been called previously in which case this is a no-op.
Finalize()
}
// TagName represents the name of a timeseries tag.
type TagName ID
// TagValue represents the value of a timeseries tag.
type TagValue ID
// Tag represents a timeseries tag.
type Tag struct {
Name TagName
Value TagValue
noFinalize bool
}
// NoFinalize makes calls to finalize a no-op, this is useful when you
// would like to share a type with another sub-system that should is not
// allowed to finalize the resource as the resource is kept indefinitely
// until garbage collected (i.e. longly lived).
func (t *Tag) NoFinalize() {
t.noFinalize = true
t.Name.NoFinalize()
t.Value.NoFinalize()
}
// Finalize releases all resources held by the Tag, unless NoFinalize has
// been called previously in which case this is a no-op.
func (t *Tag) Finalize() {
if t.noFinalize {
return
}
if t.Name != nil {
t.Name.Finalize()
t.Name = nil
}
if t.Value != nil {
t.Value.Finalize()
t.Value = nil
}
}
// Equal returns whether the two tags are equal.
func (t Tag) Equal(value Tag) bool {
return t.Name.Equal(value.Name) && t.Value.Equal(value.Value)
}
// Pool represents an automatic pool of `ident` objects.
type Pool interface {
// GetBinaryID will create a new binary ID and take reference to the bytes.
// When the context closes the ID will be finalized and so too will
// the bytes, i.e. it will take ownership of the bytes.
GetBinaryID(c context.Context, data checked.Bytes) ID
// BinaryID will create a new binary ID and take a reference to the bytes.
BinaryID(data checked.Bytes) ID
// GetBinaryTag will create a new binary Tag and take reference to the bytes.
// When the context closes, the Tag will be finalized and so too will
// the bytes, i.e. it will take ownership of the bytes.
GetBinaryTag(c context.Context, name, value checked.Bytes) Tag
// BinaryTag will create a new binary Tag and take a reference to the provided bytes.
BinaryTag(name, value checked.Bytes) Tag
// GetStringID will create a new string ID and create a bytes copy of the
// string. When the context closes the ID will be finalized.
GetStringID(c context.Context, id string) ID
// StringID will create a new string ID and create a bytes copy of the
// string.
StringID(data string) ID
// GetStringTag will create a new string Tag and create a bytes copy of the
// string. When the context closes the ID will be finalized.
GetStringTag(c context.Context, name, value string) Tag
// StringTag will create a new string Tag and create a bytes copy of the
// string.
StringTag(name, value string) Tag
// Tags will create a new array of tags and return it.
Tags() Tags
// GetTagsIterator will create a tag iterator and return it. When the context
// closes the tags array and any tags contained will be finalized.
GetTagsIterator(c context.Context) TagsIterator
// TagsIterator will create a tag iterator and return it.
TagsIterator() TagsIterator
// Put an ID back in the pool.
Put(id ID)
// PutTag puts a tag back in the pool.
PutTag(tag Tag)
// PutTags puts a set of tags back in the pool.
PutTags(tags Tags)
// PutTagsIterator puts a tags iterator back in the pool.
PutTagsIterator(iter TagsIterator)
// Clone replicates a given ID into a pooled ID.
Clone(id ID) ID
// CloneTag replicates a given Tag into a pooled Tag.
CloneTag(tag Tag) Tag
// CloneTags replicates a given set of Tags into a pooled Tags.
CloneTags(tags Tags) Tags
}
// Iterator represents an iterator over `ID` instances. It is not thread-safe.
type Iterator interface {
// Next returns a bool indicating the presence of the next ID instance.
Next() bool
// Current returns the current ID instance.
Current() ID
// CurrentIndex returns the current index at.
CurrentIndex() int
// Close releases any resources held by the iterator.
Close()
// Err returns any errors encountered during iteration.
Err() error
// Len returns the number of elements.
Len() int
// Remaining returns the number of elements remaining to be iterated over.
Remaining() int
// Dupe returns an independent duplicate of the iterator.
Duplicate() Iterator
}
// TagIterator represents an iterator over `Tag` instances. It is not thread-safe.
type TagIterator interface {
// Next returns a bool indicating the presence of the next Tag instance.
Next() bool
// Current returns the current Tag instance.
Current() Tag
// CurrentIndex returns the current index at.
CurrentIndex() int
// Err returns any errors encountered during iteration.
Err() error
// Close releases any resources held by the iterator.
Close()
// Len returns the number of elements.
Len() int
// Remaining returns the number of elements remaining to be iterated over.
Remaining() int
// Duplicate returns an independent duplicate of the iterator.
Duplicate() TagIterator
// Rewind resets the tag iterator to the initial position.
Rewind()
}
// TagsIterator represents a TagIterator that can be reset with a Tags
// collection type. It is not thread-safe.
type TagsIterator interface {
TagIterator
// Reset allows the tag iterator to be reused with a new set of tags.
Reset(tags Tags)
// ResetFields allows tag iterator to be reused from a set of fields.
ResetFields(fields []doc.Field)
}
// Tags is a collection of Tag instances that can be pooled.
type Tags struct {
values []Tag
pool Pool
noFinalize bool
}
// NewTags returns a new set of tags.
func NewTags(values ...Tag) Tags {
return Tags{values: values}
}
// Reset resets the tags for reuse.
func (t *Tags) Reset(values []Tag) {
t.values = values
}
// Values returns the tags values.
func (t Tags) Values() []Tag {
return t.values
}
// Append will append a tag.
func (t *Tags) Append(tag Tag) {
t.values = append(t.values, tag)
}
// NoFinalize makes calls to finalize a no-op, this is useful when you
// would like to share a type with another sub-system that should is not
// allowed to finalize the resource as the resource is kept indefinitely
// until garbage collected (i.e. longly lived).
func (t *Tags) NoFinalize() {
t.noFinalize = true
for _, tag := range t.values {
tag.NoFinalize()
}
}
// Finalize finalizes all Tags, unless NoFinalize has been called previously
// in which case this is a no-op.
func (t *Tags) Finalize() {
if t.noFinalize {
return
}
values := t.values
t.values = nil
for i := range values {
values[i].Finalize()
}
if t.pool == nil {
return
}
t.pool.PutTags(Tags{values: values})
}
// Equal returns a bool indicating if the tags are equal. It requires
// the two slices are ordered the same.
func (t Tags) Equal(other Tags) bool {
if len(t.Values()) != len(other.Values()) {
return false
}
for i := 0; i < len(t.Values()); i++ {
equal := t.values[i].Name.Equal(other.values[i].Name) &&
t.values[i].Value.Equal(other.values[i].Value)
if !equal {
return false
}
}
return true
}
// ShardID is a tuple of series ID and the shard it belongs to.
type ShardID struct {
// Shard is the shard the series belongs to.
Shard uint32
// ID is the series ID.
ID ID
}
// IDBatch is a batch of ShardIDs that is consumed asynchronously.
type IDBatch struct {
wg sync.WaitGroup
// ShardIDs are the ShardIDs for the batch.
ShardIDs []ShardID
}
// ReadyForProcessing indicates this batch is ready for processing.
func (b *IDBatch) ReadyForProcessing() {
b.wg.Add(1)
}
// WaitUntilProcessed waits until the batch has been processed.
func (b *IDBatch) WaitUntilProcessed() {
b.wg.Wait()
}
// Processed indicates that this batch has finished processing.
func (b *IDBatch) Processed() {
b.wg.Done()
}