Skip to content
This repository was archived by the owner on Apr 5, 2023. It is now read-only.

Commit 842cc19

Browse files
authored
Merge pull request #30 from fqutishat/29
feat: Add couchdb prefix
2 parents 488d222 + 1a32442 commit 842cc19

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

pkg/storage/couchdb/couchdbstore.go

+37-8
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,33 @@ import (
2323
"github.com/trustbloc/edge-core/pkg/storage"
2424
)
2525

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+
2642
// Provider represents an CouchDB implementation of the storage.Provider interface
2743
type Provider struct {
2844
hostURL string
2945
couchDBClient *kivik.Client
3046
dbs map[string]*CouchDBStore
47+
dbPrefix string
3148
mux sync.RWMutex
3249
}
3350

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-
4051
// NewProvider instantiates Provider
41-
func NewProvider(hostURL string) (*Provider, error) {
52+
func NewProvider(hostURL string, opts ...Option) (*Provider, error) {
4253
if hostURL == "" {
4354
return nil, errors.New(blankHostErrMsg)
4455
}
@@ -48,13 +59,23 @@ func NewProvider(hostURL string) (*Provider, error) {
4859
return nil, err
4960
}
5061

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
5269
}
5370

5471
// CreateStore creates a new store with the given name.
5572
func (p *Provider) CreateStore(name string) error {
5673
p.mux.Lock()
5774

75+
if p.dbPrefix != "" {
76+
name = p.dbPrefix + "_" + name
77+
}
78+
5879
err := p.couchDBClient.CreateDB(context.Background(), name)
5980

6081
p.mux.Unlock()
@@ -71,6 +92,10 @@ func (p *Provider) OpenStore(name string) (storage.Store, error) {
7192
p.mux.Lock()
7293
defer p.mux.Unlock()
7394

95+
if p.dbPrefix != "" {
96+
name = p.dbPrefix + "_" + name
97+
}
98+
7499
// Check cache first
75100
cachedStore, existsInCache := p.dbs[name]
76101
if existsInCache {
@@ -106,6 +131,10 @@ func (p *Provider) CloseStore(name string) error {
106131
p.mux.Lock()
107132
defer p.mux.Unlock()
108133

134+
if p.dbPrefix != "" {
135+
name = p.dbPrefix + "_" + name
136+
}
137+
109138
store, exists := p.dbs[name]
110139
if !exists {
111140
return storage.ErrStoreNotFound

pkg/storage/couchdb/couchdbstore_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313
"testing"
1414
"time"
1515

16-
"github.com/trustbloc/edge-core/pkg/storage"
17-
1816
"github.com/go-kivik/kivik"
1917
log "github.com/sirupsen/logrus"
2018
"github.com/stretchr/testify/require"
19+
20+
"github.com/trustbloc/edge-core/pkg/storage"
2121
)
2222

2323
const (
@@ -160,7 +160,7 @@ func TestProvider_OpenStore(t *testing.T) {
160160

161161
func TestProvider_CloseStore(t *testing.T) {
162162
t.Run("Successfully close a store", func(t *testing.T) {
163-
provider := initializeTest(t)
163+
provider := initializeTest(t, WithDBPrefix("prefixdb"))
164164

165165
_ = createAndOpenTestStore(t, provider)
166166

@@ -475,8 +475,8 @@ func TestCouchDBStore_ResultsIterator(t *testing.T) {
475475
})
476476
}
477477

478-
func initializeTest(t *testing.T) *Provider {
479-
provider, err := NewProvider(couchDBURL)
478+
func initializeTest(t *testing.T, opts ...Option) *Provider {
479+
provider, err := NewProvider(couchDBURL, opts...)
480480
require.NoError(t, err)
481481

482482
resetCouchDB(t, provider)

0 commit comments

Comments
 (0)