/
prefetcher.go
177 lines (144 loc) · 6.58 KB
/
prefetcher.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
// Copyright 2019 Aporeto Inc.
// 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 manipvortex
import (
"context"
"fmt"
"sync"
"testing"
"go.aporeto.io/elemental"
"go.aporeto.io/manipulate"
)
// A Prefetcher is used to perform prefetching operations
// in a Vortex manipulator. Vortex will call the Prefetch method
// before performing a Retrieve (elemental.OperationRetrieve) or a
// RetrieveMany (elemental.OperationRetrieveMany) operation to eventually
// lazy load more data than the one requested so they'll be cached in advanced.
type Prefetcher interface {
// WarmUp is called during Vortex initialization.
// Implementations can use the given manipulator to retrieve all the
// objects to add into the Vortex cache before starting.
WarmUp(context.Context, manipulate.Manipulator, elemental.ModelManager, elemental.Identity) (elemental.Identifiables, error)
// Prefetch is called before a Retrieve or RetrieveMany operation to perform
// cache prefetching. The given operation will be either elemental.OperationRetrieve or
// elemental.OperationRetrieveMany. The requested identity is passed, alongside
// with the request manipulate.Context allowing to do additional conditional logic based on
// the initial request. It is a copy of the original context, so you cannot change anything
// in the original request.
//
// If Prefetch returns some elemental.Identifiables, all of them will be added to the local cache
// before peforming the initial request.
//
// If Prefetch returns nil, the original operation continues with not additional processing. According
// to the requested consistency, the data will be either retrieved locally or from upstream.
//
// If prefetch returns an error, the upstream operation will be canceled and the error returned.
// You can use the provided manipulator to retrieve the needed data.
Prefetch(context.Context, elemental.Operation, elemental.Identity, manipulate.Manipulator, manipulate.Context) (elemental.Identifiables, error)
// If the prefetcher uses some internal state
// if must reset it when this is called.
Flush()
}
// A DefaultPrefetcher will load everything
// during a warm up.
type defaultPrefetcher struct {
}
// NewDefaultPrefetcher returns a new Prefetcher that
// will simply load everything into memory
func NewDefaultPrefetcher() Prefetcher {
return &defaultPrefetcher{}
}
func (p *defaultPrefetcher) WarmUp(ctx context.Context, m manipulate.Manipulator, manager elemental.ModelManager, identity elemental.Identity) (elemental.Identifiables, error) {
out := manager.Identifiables(identity)
if err := manipulate.IterFunc(
ctx,
m,
manager.Identifiables(identity),
manipulate.NewContext(ctx, manipulate.ContextOptionRecursive(true)),
func(block elemental.Identifiables) error {
out = out.Append(block.List()...)
return nil
},
1000,
); err != nil {
return nil, fmt.Errorf("unable to warm up identity '%s': %s", identity.Name, err)
}
return out, nil
}
func (p *defaultPrefetcher) Prefetch(context.Context, elemental.Operation, elemental.Identity, manipulate.Manipulator, manipulate.Context) (elemental.Identifiables, error) {
return nil, nil
}
func (p *defaultPrefetcher) Flush() {}
// A TestPrefetcher is prefetcher that can be used for
// testing purposes.
type TestPrefetcher interface {
Prefetcher
MockWarmUp(t *testing.T, impl func(context.Context, manipulate.Manipulator, elemental.ModelManager, elemental.Identity) (elemental.Identifiables, error))
MockPrefetch(t *testing.T, impl func(context.Context, elemental.Operation, elemental.Identity, manipulate.Manipulator, manipulate.Context) (elemental.Identifiables, error))
MockFlush(t *testing.T, impl func())
}
type mockedPrefetcherMethods struct {
warmUpMock func(context.Context, manipulate.Manipulator, elemental.ModelManager, elemental.Identity) (elemental.Identifiables, error)
prefetchMock func(context.Context, elemental.Operation, elemental.Identity, manipulate.Manipulator, manipulate.Context) (elemental.Identifiables, error)
flushMock func()
}
type testPrefetcher struct {
mocks map[*testing.T]*mockedPrefetcherMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestPrefetcher returns a new TestPrefetcher.
func NewTestPrefetcher() TestPrefetcher {
return &testPrefetcher{
lock: &sync.Mutex{},
mocks: map[*testing.T]*mockedPrefetcherMethods{},
}
}
// MockPrefetch sets the mocked implementation of Prefetch.
func (p *testPrefetcher) MockWarmUp(t *testing.T, impl func(context.Context, manipulate.Manipulator, elemental.ModelManager, elemental.Identity) (elemental.Identifiables, error)) {
p.currentMocks(t).warmUpMock = impl
}
// MockPrefetch sets the mocked implementation of Prefetch.
func (p *testPrefetcher) MockPrefetch(t *testing.T, impl func(context.Context, elemental.Operation, elemental.Identity, manipulate.Manipulator, manipulate.Context) (elemental.Identifiables, error)) {
p.currentMocks(t).prefetchMock = impl
}
// MockPrefetch sets the mocked implementation of Prefetch.
func (p *testPrefetcher) MockFlush(t *testing.T, impl func()) {
p.currentMocks(t).flushMock = impl
}
func (p *testPrefetcher) WarmUp(ctx context.Context, m manipulate.Manipulator, manager elemental.ModelManager, identity elemental.Identity) (elemental.Identifiables, error) {
if mock := p.currentMocks(p.currentTest); mock != nil && mock.warmUpMock != nil {
return mock.warmUpMock(ctx, m, manager, identity)
}
return nil, nil
}
func (p *testPrefetcher) Prefetch(ctx context.Context, op elemental.Operation, identity elemental.Identity, m manipulate.Manipulator, mctx manipulate.Context) (elemental.Identifiables, error) {
if mock := p.currentMocks(p.currentTest); mock != nil && mock.prefetchMock != nil {
return mock.prefetchMock(ctx, op, identity, m, mctx)
}
return nil, nil
}
func (p *testPrefetcher) Flush() {
if mock := p.currentMocks(p.currentTest); mock != nil && mock.flushMock != nil {
mock.flushMock()
}
}
func (p *testPrefetcher) currentMocks(t *testing.T) *mockedPrefetcherMethods {
p.lock.Lock()
defer p.lock.Unlock()
mocks := p.mocks[t]
if mocks == nil {
mocks = &mockedPrefetcherMethods{}
p.mocks[t] = mocks
}
p.currentTest = t
return mocks
}