-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
resourceLinksProjection.go
133 lines (118 loc) · 3.76 KB
/
resourceLinksProjection.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
package service
import (
"context"
"sync"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"github.com/plgd-dev/hub/v2/resource-aggregate/cqrs/eventstore"
"github.com/plgd-dev/hub/v2/resource-aggregate/events"
"github.com/plgd-dev/kit/v2/strings"
)
// protected by lock in Projection struct in resource-aggregate/cqrs/eventstore/projection.go
type resourceLinksProjection struct {
deviceID string
private struct {
lock sync.RWMutex // protects snapshot
snapshot *events.ResourceLinksSnapshotTaken
}
}
func NewResourceLinksProjection(deviceID string) eventstore.Model {
return &resourceLinksProjection{
deviceID: deviceID,
}
}
func (rlp *resourceLinksProjection) GetDeviceID() string {
return rlp.deviceID
}
func (rlp *resourceLinksProjection) EventType() string {
s := &events.ResourceLinksSnapshotTaken{}
return s.EventType()
}
func (rlp *resourceLinksProjection) IterateOverResources(onResource func(res *commands.Resource) (wantNext bool)) {
rlp.private.lock.RLock()
defer rlp.private.lock.RUnlock()
for _, res := range rlp.private.snapshot.GetResources() {
rlp.private.lock.RUnlock()
wantNext := onResource(res)
rlp.private.lock.RLock()
if !wantNext {
return
}
}
}
func (rlp *resourceLinksProjection) GetResource(href string) *commands.Resource {
rlp.private.lock.RLock()
defer rlp.private.lock.RUnlock()
if rlp.private.snapshot.GetResources() == nil {
return nil
}
return rlp.private.snapshot.GetResources()[href]
}
func (rlp *resourceLinksProjection) LenResources() int {
rlp.private.lock.RLock()
defer rlp.private.lock.RUnlock()
return len(rlp.private.snapshot.GetResources())
}
func (rlp *resourceLinksProjection) HandleEventLocked(eu eventstore.EventUnmarshaler) error {
if rlp.private.snapshot == nil {
rlp.private.snapshot = events.NewResourceLinksSnapshotTaken()
}
eventMetadata := rlp.private.snapshot.GetEventMetadata().Clone()
eventMetadata.Version = eu.Version()
rlp.private.snapshot.EventMetadata = eventMetadata
switch eu.EventType() {
case (&events.ResourceLinksSnapshotTaken{}).EventType():
var e events.ResourceLinksSnapshotTaken
if err := eu.Unmarshal(&e); err != nil {
return err
}
rlp.private.snapshot = &e
case (&events.ResourceLinksPublished{}).EventType():
var e events.ResourceLinksPublished
if err := eu.Unmarshal(&e); err != nil {
return err
}
rlp.private.snapshot.HandleEventResourceLinksPublished(&e)
case (&events.ResourceLinksUnpublished{}).EventType():
var e events.ResourceLinksUnpublished
if err := eu.Unmarshal(&e); err != nil {
return err
}
rlp.private.snapshot.HandleEventResourceLinksUnpublished(nil, &e)
}
return nil
}
func (rlp *resourceLinksProjection) Handle(ctx context.Context, iter eventstore.Iter) error {
rlp.private.lock.Lock()
defer rlp.private.lock.Unlock()
for {
eu, ok := iter.Next(ctx)
if !ok {
break
}
log.Debugf("resourceLinksProjection.Handle deviceID=%v eventype%v version=%v", eu.GroupID(), eu.EventType(), eu.Version())
if err := rlp.HandleEventLocked(eu); err != nil {
return err
}
}
return nil
}
func (rlp *resourceLinksProjection) ToResourceLinksPublished(typeFilter strings.Set) *events.ResourceLinksPublished {
resources := make([]*commands.Resource, 0, len(rlp.private.snapshot.GetResources()))
rlp.private.lock.RLock()
defer rlp.private.lock.RUnlock()
for _, resource := range rlp.private.snapshot.GetResources() {
if hasMatchingType(resource.GetResourceTypes(), typeFilter) {
resources = append(resources, resource)
}
}
if len(resources) == 0 {
return nil
}
return &events.ResourceLinksPublished{
DeviceId: rlp.GetDeviceID(),
EventMetadata: rlp.private.snapshot.GetEventMetadata(),
Resources: resources,
AuditContext: rlp.private.snapshot.GetAuditContext(),
}
}