-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathidb_factory.go
37 lines (30 loc) · 1.32 KB
/
idb_factory.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
package idb
import (
"fmt"
log "github.com/sirupsen/logrus"
)
// IndexerDbFactory is used to install an IndexerDb implementation.
type IndexerDbFactory interface {
Name() string
Build(arg string, opts IndexerDbOptions, log *log.Logger) (IndexerDb, chan struct{}, error)
}
// This layer of indirection allows for different db integrations to be compiled in or compiled out by `go build --tags ...`
var indexerFactories map[string]IndexerDbFactory
// RegisterFactory is used by IndexerDb implementations to register their implementations. This mechanism allows
// for loose coupling between the configuration and the implementation. It is extremely similar to the way sql.DB
// driver's are configured and used.
func RegisterFactory(name string, factory IndexerDbFactory) {
indexerFactories[name] = factory
}
// IndexerDbByName is used to construct an IndexerDb object by name.
// Returns an IndexerDb object, an availability channel that closes when the database
// becomes available, and an error object.
func IndexerDbByName(name, arg string, opts IndexerDbOptions, log *log.Logger) (IndexerDb, chan struct{}, error) {
if val, ok := indexerFactories[name]; ok {
return val.Build(arg, opts, log)
}
return nil, nil, fmt.Errorf("no IndexerDb factory for %s", name)
}
func init() {
indexerFactories = make(map[string]IndexerDbFactory)
}