-
Notifications
You must be signed in to change notification settings - Fork 682
/
allow_list.go
54 lines (45 loc) · 1.4 KB
/
allow_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
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2022 SeMI Technologies B.V. All rights reserved.
//
// CONTACT: hello@semi.technology
//
package helpers
// AllowList groups a list of possible indexIDs to be passed to a secondary
// index. The secondary index must make sure that it only returns result
// present on the AllowList
type AllowList map[uint64]struct{}
// Inserting and reading is not thread-safe. However, if inserting has
// completed, and the list can be considered read-only, it is safe to read from
// it concurrently
func (al AllowList) Insert(id uint64) {
// no need to check if it's already present, simply overwrite
al[id] = struct{}{}
}
// Contains is not thread-safe if the list is still being filled. However, if
// you can guarantee that the list is no longer being inserted into and it
// effectively becomes read-only, you can safely read concurrently
func (al AllowList) Contains(id uint64) bool {
_, ok := al[id]
return ok
}
func (al AllowList) DeepCopy() AllowList {
out := AllowList{}
for id := range al {
out[id] = struct{}{}
}
return out
}
func (al AllowList) Slice() []uint64 {
out := make([]uint64, len(al))
i := 0
for id := range al {
out[i] = id
i++
}
return out
}