forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventstore.go
128 lines (108 loc) · 3.63 KB
/
eventstore.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
// Copyright (c) 2014 - The Event Horizon authors.
//
// 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 eventhorizon
import (
"context"
"errors"
"fmt"
"strings"
"github.com/looplab/eventhorizon/uuid"
)
// EventStore is an interface for an event sourcing event store.
type EventStore interface {
// Save appends all events in the event stream to the store.
Save(ctx context.Context, events []Event, originalVersion int) error
// Load loads all events for the aggregate id from the store.
Load(context.Context, uuid.UUID) ([]Event, error)
// Close closes the EventStore.
Close() error
}
var (
// Missing events for save operation.
ErrMissingEvents = errors.New("missing events")
// Events in the same save operation is for different aggregate IDs.
ErrMismatchedEventAggregateIDs = errors.New("mismatched event aggregate IDs")
// Events in the same save operation is for different aggregate types.
ErrMismatchedEventAggregateTypes = errors.New("mismatched event aggregate types")
// Events in the same operation have non-serial versions or is not matching the original version.
ErrIncorrectEventVersion = errors.New("incorrect event version")
// Other events has been saved for this aggregate since the operation started.
ErrEventConflictFromOtherSave = errors.New("event conflict from other save")
// No matching event could be found (for maintenance operations etc).
ErrEventNotFound = errors.New("event not found")
)
// EventStoreOperation is the operation done when an error happened.
type EventStoreOperation string
const (
// Errors during loading of events.
EventStoreOpLoad = "load"
// Errors during saving of events.
EventStoreOpSave = "save"
// Errors during replacing of events.
EventStoreOpReplace = "replace"
// Errors during renaming of event types.
EventStoreOpRename = "rename"
// Errors during clearing of the event store.
EventStoreOpClear = "clear"
)
// EventStoreError is an error in the event store.
type EventStoreError struct {
// Err is the error.
Err error
// Op is the operation for the error.
Op EventStoreOperation
// AggregateType of related operation.
AggregateType AggregateType
// AggregateID of related operation.
AggregateID uuid.UUID
// AggregateVersion of related operation.
AggregateVersion int
// Events of the related operation.
Events []Event
}
// Error implements the Error method of the errors.Error interface.
func (e *EventStoreError) Error() string {
str := "event store: "
if e.Op != "" {
str += string(e.Op) + ": "
}
if e.Err != nil {
str += e.Err.Error()
} else {
str += "unknown error"
}
if e.AggregateID != uuid.Nil {
at := "Aggregate"
if e.AggregateType != "" {
at = string(e.AggregateType)
}
str += fmt.Sprintf(", %s(%s, v%d)", at, e.AggregateID, e.AggregateVersion)
}
if len(e.Events) > 0 {
var es []string
for _, e := range e.Events {
es = append(es, e.String())
}
str += " [" + strings.Join(es, ", ") + "]"
}
return str
}
// Unwrap implements the errors.Unwrap method.
func (e *EventStoreError) Unwrap() error {
return e.Err
}
// Cause implements the github.com/pkg/errors Unwrap method.
func (e *EventStoreError) Cause() error {
return e.Unwrap()
}