-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventstore.go
More file actions
107 lines (77 loc) · 1.97 KB
/
Copy patheventstore.go
File metadata and controls
107 lines (77 loc) · 1.97 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
package eventstore
import (
"errors"
"os"
"github.com/cockroachdb/pebble"
)
var (
ErrRecordNotFound = errors.New("EventStore: record not found")
)
type EventStore struct {
options *Options
dbPath string
stores map[string]*Store
snapshot *SnapshotController
}
func CreateEventStore(options *Options) (*EventStore, error) {
err := os.MkdirAll(options.DatabasePath, os.ModePerm)
if err != nil {
return nil, err
}
eventStore := &EventStore{
stores: make(map[string]*Store),
options: options,
}
if options.EnabledSnapshot {
eventStore.initializeSnapshotController()
}
return eventStore, nil
}
func (eventstore *EventStore) initializeSnapshotController() error {
eventstore.snapshot = NewSnapshotController(eventstore.options.SnapshotOptions)
return nil
}
func (eventstore *EventStore) UnregisterStore(name string) {
delete(eventstore.stores, name)
}
func (eventstore *EventStore) Close() {
for _, store := range eventstore.stores {
store.Close()
}
eventstore.stores = make(map[string]*Store)
}
func (eventstore *EventStore) SetSnapshotHandler(fn func(*SnapshotRequest) error) {
if eventstore.snapshot == nil {
return
}
eventstore.snapshot.SetHandler(fn)
}
func (eventstore *EventStore) GetStore(storeName string, opts ...StoreOpt) (*Store, error) {
if store, ok := eventstore.stores[storeName]; ok {
return store, nil
}
store, err := NewStore(eventstore, storeName, opts...)
if err != nil {
return nil, err
}
eventstore.stores[storeName] = store
return store, nil
}
func (eventstore *EventStore) TakeSnapshot(b *pebble.Batch, store *Store, seq uint64, data []byte) error {
if eventstore.snapshot == nil {
return nil
}
if !store.enabledSnapshot {
return nil
}
return eventstore.snapshot.Request(b, store, seq, data)
}
func (eventstore *EventStore) RecoverSnapshot(store *Store) error {
if eventstore.snapshot == nil {
return nil
}
if !store.enabledSnapshot {
return nil
}
return eventstore.snapshot.RecoverSnapshot(store)
}