-
Notifications
You must be signed in to change notification settings - Fork 280
/
record_collection.go
71 lines (60 loc) · 1.77 KB
/
record_collection.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
package inmemory
import (
"container/list"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
)
type recordCollectionNode struct {
*databroker.Record
insertionOrderPtr *list.Element
}
// A RecordCollection is a collection of records which supports lookup by (record id) as well as enforcing capacity
// by insertion order. The collection is *not* thread safe.
type RecordCollection struct {
records map[string]recordCollectionNode
insertionOrder *list.List
}
// NewRecordCollection creates a new RecordCollection.
func NewRecordCollection() *RecordCollection {
return &RecordCollection{
records: map[string]recordCollectionNode{},
insertionOrder: list.New(),
}
}
// Delete deletes a record from the collection.
func (c *RecordCollection) Delete(recordID string) {
node, ok := c.records[recordID]
if !ok {
return
}
delete(c.records, recordID)
c.insertionOrder.Remove(node.insertionOrderPtr)
}
// Get gets a record from the collection.
func (c *RecordCollection) Get(recordID string) *databroker.Record {
node, ok := c.records[recordID]
if !ok {
return nil
}
return node.Record
}
// Len returns the length of the collection.
func (c *RecordCollection) Len() int {
return len(c.records)
}
// List lists all the records in the collection in insertion order.
func (c *RecordCollection) List() []*databroker.Record {
var all []*databroker.Record
for el := c.insertionOrder.Front(); el != nil; el = el.Next() {
all = append(all, c.records[el.Value.(string)].Record)
}
return all
}
// Put puts a record in the collection.
func (c *RecordCollection) Put(record *databroker.Record) {
c.Delete(record.GetId())
el := c.insertionOrder.PushBack(record.GetId())
c.records[record.GetId()] = recordCollectionNode{
Record: record,
insertionOrderPtr: el,
}
}