-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprojectionManagement.go
More file actions
391 lines (315 loc) · 8.78 KB
/
Copy pathprojectionManagement.go
File metadata and controls
391 lines (315 loc) · 8.78 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package samples
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/google/uuid"
"github.com/kurrent-io/KurrentDB-Client-Go/kurrentdb"
)
func CreateClient(connectionString string) {
// region createClient
conf, err := kurrentdb.ParseConnectionString(connectionString)
if err != nil {
panic(err)
}
client, err := kurrentdb.NewProjectionClient(conf)
if err != nil {
panic(err)
}
// endregion createClient
defer client.Close()
}
func Disable(client *kurrentdb.ProjectionClient) {
// region Disable
err := client.Disable(context.Background(), "$by_category", kurrentdb.GenericProjectionOptions{})
if err != nil {
panic(err)
}
// endregion Disable
}
func DisableNotFound(client *kurrentdb.ProjectionClient) {
// region DisableNotFound
err := client.Disable(context.Background(), "projection that doesn't exist", kurrentdb.GenericProjectionOptions{})
if esdbError, ok := kurrentdb.FromError(err); !ok {
if esdbError.IsErrorCode(kurrentdb.ErrorCodeResourceNotFound) {
log.Printf("projection not found")
return
}
}
// endregion DisableNotFound
}
func Enable(client *kurrentdb.ProjectionClient) {
// region Enable
err := client.Enable(context.Background(), "$by_category", kurrentdb.GenericProjectionOptions{})
if err != nil {
panic(err)
}
// endregion Enable
}
func EnableNotFound(client *kurrentdb.ProjectionClient) {
// region EnableNotFound
err := client.Enable(context.Background(), "projection that doesn't exist", kurrentdb.GenericProjectionOptions{})
if esdbError, ok := kurrentdb.FromError(err); !ok {
if esdbError.IsErrorCode(kurrentdb.ErrorCodeResourceNotFound) {
log.Printf("projection not found")
return
}
}
// endregion EnableNotFound
}
func Delete(client *kurrentdb.ProjectionClient) {
// region Delete
err := client.Delete(context.Background(), "$by_category", kurrentdb.DeleteProjectionOptions{})
if err != nil {
panic(err)
}
// endregion Delete
}
func DeleteNotFound(client *kurrentdb.ProjectionClient) {
// region DeleteNotFound
err := client.Delete(context.Background(), "projection that doesn't exist", kurrentdb.DeleteProjectionOptions{})
if esdbError, ok := kurrentdb.FromError(err); !ok {
if esdbError.IsErrorCode(kurrentdb.ErrorCodeResourceNotFound) {
log.Printf("projection not found")
return
}
}
// endregion DeleteNotFound
}
func Abort(client *kurrentdb.ProjectionClient) {
// region Abort
err := client.Abort(context.Background(), "$by_category", kurrentdb.GenericProjectionOptions{})
if err != nil {
panic(err)
}
// endregion Abort
}
func AbortNotFound(client *kurrentdb.ProjectionClient) {
// region Abort_NotFound
err := client.Abort(context.Background(), "projection that doesn't exist", kurrentdb.GenericProjectionOptions{})
if esdbError, ok := kurrentdb.FromError(err); !ok {
if esdbError.IsErrorCode(kurrentdb.ErrorCodeResourceNotFound) {
log.Printf("projection not found")
return
}
}
// endregion Abort_NotFound
}
func Reset(client *kurrentdb.ProjectionClient) {
// region Reset
err := client.Reset(context.Background(), "$by_category", kurrentdb.ResetProjectionOptions{})
if err != nil {
panic(err)
}
// endregion Reset
}
func ResetNotFound(client *kurrentdb.ProjectionClient) {
// region Reset_NotFound
err := client.Reset(context.Background(), "projection that doesn't exist", kurrentdb.ResetProjectionOptions{})
if esdbError, ok := kurrentdb.FromError(err); !ok {
if esdbError.IsErrorCode(kurrentdb.ErrorCodeResourceNotFound) {
log.Printf("projection not found")
return
}
}
// endregion Reset_NotFound
}
func Create(client *kurrentdb.ProjectionClient) {
// region CreateContinuous
script := `
fromAll()
.when({
$init:function(){
return {
count: 0
}
},
myEventUpdatedType: function(state, event){
state.count += 1;
}
})
.transformBy(function(state){
state.count = 10;
})
.outputState()
`
name := fmt.Sprintf("countEvent_Create_%s", uuid.New())
err := client.Create(context.Background(), name, script, kurrentdb.CreateProjectionOptions{})
if err != nil {
panic(err)
}
// endregion CreateContinuous
}
func CreateV2Engine(client *kurrentdb.ProjectionClient) {
// region CreateContinuous_V2Engine
script := `fromAll().when({$init: function (state, ev) {return {};}});`
name := fmt.Sprintf("countEvent_CreateV2_%s", uuid.New())
err := client.Create(context.Background(), name, script, kurrentdb.CreateProjectionOptions{
EngineVersion: kurrentdb.ProjectionEngineVersionV2,
})
if err != nil {
panic(err)
}
// endregion CreateContinuous_V2Engine
}
func CreateWithMetadata(client *kurrentdb.ProjectionClient) {
// region CreateContinuous_Metadata
script := `fromAll().when({$init: function (state, ev) {return {};}});`
name := fmt.Sprintf("countEvent_CreateMeta_%s", uuid.New())
err := client.Create(context.Background(), name, script, kurrentdb.CreateProjectionOptions{
Metadata: map[string]interface{}{
"deploy": "abc123",
"tool": "gaffer",
},
})
if err != nil {
panic(err)
}
// endregion CreateContinuous_Metadata
}
func CreateConflict(client *kurrentdb.ProjectionClient) {
script := ""
name := ""
// region CreateContinuous_Conflict
err := client.Create(context.Background(), name, script, kurrentdb.CreateProjectionOptions{})
if esdbErr, ok := kurrentdb.FromError(err); !ok {
if esdbErr.IsErrorCode(kurrentdb.ErrorCodeUnknown) && strings.Contains(esdbErr.Err().Error(), "Conflict") {
log.Printf("projection %s already exists", name)
return
}
}
// endregion CreateContinuous_Conflict
}
func Update(client *kurrentdb.ProjectionClient) {
script := ""
newScript := ""
name := ""
// region Update
err := client.Create(context.Background(), name, script, kurrentdb.CreateProjectionOptions{})
if err != nil {
panic(err)
}
err = client.Update(context.Background(), name, newScript, kurrentdb.UpdateProjectionOptions{})
if err != nil {
panic(err)
}
// endregion Update
}
func UpdateNotFound(client *kurrentdb.ProjectionClient) {
script := ""
// region Update_NotFound
err := client.Update(context.Background(), "projection that doesn't exist", script, kurrentdb.UpdateProjectionOptions{})
if esdbError, ok := kurrentdb.FromError(err); !ok {
if esdbError.IsErrorCode(kurrentdb.ErrorCodeResourceNotFound) {
log.Printf("projection not found")
return
}
}
// endregion Update_NotFound
}
func ListAll(client *kurrentdb.ProjectionClient) {
// region ListAll
projections, err := client.ListAll(context.Background(), kurrentdb.GenericProjectionOptions{})
if err != nil {
panic(err)
}
for i := range projections {
projection := projections[i]
log.Printf(
"%s, %s, %s, %s, %f",
projection.Name,
projection.Status,
projection.CheckpointStatus,
projection.Mode,
projection.Progress,
)
}
// endregion ListAll
}
func List(client *kurrentdb.ProjectionClient) {
// region ListContinuous
projections, err := client.ListContinuous(context.Background(), kurrentdb.GenericProjectionOptions{})
if err != nil {
panic(err)
}
for i := range projections {
projection := projections[i]
log.Printf(
"%s, %s, %s, %s, %f",
projection.Name,
projection.Status,
projection.CheckpointStatus,
projection.Mode,
projection.Progress,
)
}
// endregion ListContinuous
}
func GetStatus(client *kurrentdb.ProjectionClient) {
// region GetStatus
projection, err := client.GetStatus(context.Background(), "$by_category", kurrentdb.GenericProjectionOptions{})
if err != nil {
panic(err)
}
log.Printf(
"%s, %s, %s, %s, %f",
projection.Name,
projection.Status,
projection.CheckpointStatus,
projection.Mode,
projection.Progress,
)
// endregion GetStatus
}
func GetState(client *kurrentdb.ProjectionClient) {
projectionName := ""
// region GetState
type Foobar struct {
Count int64
}
value, err := client.GetState(context.Background(), projectionName, kurrentdb.GetStateProjectionOptions{})
if err != nil {
panic(err)
}
jsonContent, err := value.MarshalJSON()
if err != nil {
panic(err)
}
var foobar Foobar
if err = json.Unmarshal(jsonContent, &foobar); err != nil {
panic(err)
}
log.Printf("count %d", foobar.Count)
// endregion GetState
}
func GetResult(client *kurrentdb.ProjectionClient) {
projectionName := ""
// region GetResult
type Baz struct {
Result int64
}
value, err := client.GetResult(context.Background(), projectionName, kurrentdb.GetResultProjectionOptions{})
if err != nil {
panic(err)
}
jsonContent, err := value.MarshalJSON()
if err != nil {
panic(err)
}
var baz Baz
if err = json.Unmarshal(jsonContent, &baz); err != nil {
panic(err)
}
log.Printf("result %d", baz.Result)
// endregion GetResult
}
func RestartSubSystem(client *kurrentdb.ProjectionClient) {
// region RestartSubSystem
err := client.RestartSubsystem(context.Background(), kurrentdb.GenericProjectionOptions{})
if err != nil {
panic(err)
}
// endregion RestartSubSystem
}