@@ -23,22 +23,33 @@ import (
23
23
"github.com/trustbloc/edge-core/pkg/storage"
24
24
)
25
25
26
+ const (
27
+ blankHostErrMsg = "hostURL for new CouchDB provider can't be blank"
28
+ failToCloseProviderErrMsg = "failed to close provider"
29
+ couchDBNotFoundErr = "Not Found:"
30
+ )
31
+
32
+ // Option configures the couchdb provider
33
+ type Option func (opts * Provider )
34
+
35
+ // WithDBPrefix option is for adding prefix to db name
36
+ func WithDBPrefix (dbPrefix string ) Option {
37
+ return func (opts * Provider ) {
38
+ opts .dbPrefix = dbPrefix
39
+ }
40
+ }
41
+
26
42
// Provider represents an CouchDB implementation of the storage.Provider interface
27
43
type Provider struct {
28
44
hostURL string
29
45
couchDBClient * kivik.Client
30
46
dbs map [string ]* CouchDBStore
47
+ dbPrefix string
31
48
mux sync.RWMutex
32
49
}
33
50
34
- const (
35
- blankHostErrMsg = "hostURL for new CouchDB provider can't be blank"
36
- failToCloseProviderErrMsg = "failed to close provider"
37
- couchDBNotFoundErr = "Not Found:"
38
- )
39
-
40
51
// NewProvider instantiates Provider
41
- func NewProvider (hostURL string ) (* Provider , error ) {
52
+ func NewProvider (hostURL string , opts ... Option ) (* Provider , error ) {
42
53
if hostURL == "" {
43
54
return nil , errors .New (blankHostErrMsg )
44
55
}
@@ -48,13 +59,23 @@ func NewProvider(hostURL string) (*Provider, error) {
48
59
return nil , err
49
60
}
50
61
51
- return & Provider {hostURL : hostURL , couchDBClient : client , dbs : map [string ]* CouchDBStore {}}, nil
62
+ p := & Provider {hostURL : hostURL , couchDBClient : client , dbs : map [string ]* CouchDBStore {}}
63
+
64
+ for _ , opt := range opts {
65
+ opt (p )
66
+ }
67
+
68
+ return p , nil
52
69
}
53
70
54
71
// CreateStore creates a new store with the given name.
55
72
func (p * Provider ) CreateStore (name string ) error {
56
73
p .mux .Lock ()
57
74
75
+ if p .dbPrefix != "" {
76
+ name = p .dbPrefix + "_" + name
77
+ }
78
+
58
79
err := p .couchDBClient .CreateDB (context .Background (), name )
59
80
60
81
p .mux .Unlock ()
@@ -71,6 +92,10 @@ func (p *Provider) OpenStore(name string) (storage.Store, error) {
71
92
p .mux .Lock ()
72
93
defer p .mux .Unlock ()
73
94
95
+ if p .dbPrefix != "" {
96
+ name = p .dbPrefix + "_" + name
97
+ }
98
+
74
99
// Check cache first
75
100
cachedStore , existsInCache := p .dbs [name ]
76
101
if existsInCache {
@@ -106,6 +131,10 @@ func (p *Provider) CloseStore(name string) error {
106
131
p .mux .Lock ()
107
132
defer p .mux .Unlock ()
108
133
134
+ if p .dbPrefix != "" {
135
+ name = p .dbPrefix + "_" + name
136
+ }
137
+
109
138
store , exists := p .dbs [name ]
110
139
if ! exists {
111
140
return storage .ErrStoreNotFound
0 commit comments