-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_registry.go
59 lines (50 loc) · 1.95 KB
/
entry_registry.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
package wal
import "fmt"
// The EntryRegistry keeps track of all known Entry implementations.
// This is necessary in order to instantiate the correct types when loading WAL
// segments.
type EntryRegistry struct {
constructors map[EntryType]EntryConstructor
}
// EntryConstructor is the constructor function of a specific Entry implementation.
type EntryConstructor func() Entry
// NewEntryRegistry creates a new EntryRegistry. If you pass any constructor to
// this function, each must create a unique Entry implementation (i.e. one which
// returns a unique EntryType). Otherwise, this function panics.
//
// Alternatively, you can register the constructor functions using EntryRegistry.Register(…).
func NewEntryRegistry(constructors ...EntryConstructor) *EntryRegistry {
r := &EntryRegistry{constructors: map[EntryType]EntryConstructor{}}
for _, newEntry := range constructors {
err := r.Register(newEntry)
if err != nil {
panic(err)
}
}
return r
}
// Register an EntryConstructor function. Each Entry will be registered with
// the EntryType that is returned by the corresponding Entry.Type().
//
// An error is returned if this constructor was already registered; i.e. a
// constructor was already registered that creates an Entry with the same
// EntryType as this constructor's Entry.
func (r *EntryRegistry) Register(constructor EntryConstructor) error {
entry := constructor()
typ := entry.Type()
if existing, ok := r.constructors[typ]; ok {
return fmt.Errorf(`EntryType %d was already registered to type "%T"`, typ, existing())
}
r.constructors[typ] = constructor
return nil
}
// New instantiates a new Entry implementation that was previously registered
// for the requested EntryType. An error is returned if no Entry was registered
// for this type.
func (r *EntryRegistry) New(typ EntryType) (Entry, error) {
newEntry, ok := r.constructors[typ]
if !ok {
return nil, fmt.Errorf("unknown WAL entry type %d", typ)
}
return newEntry(), nil
}