9
9
"time"
10
10
11
11
"github.com/micro/go-micro/v3/store"
12
- "github.com/pkg/errors"
13
12
bolt "go.etcd.io/bbolt"
14
13
)
15
14
@@ -18,19 +17,14 @@ func NewBlobStore() (store.BlobStore, error) {
18
17
// ensure the parent directory exists
19
18
os .MkdirAll (DefaultDir , 0700 )
20
19
21
- // open the connection to the database
22
- dbPath := filepath .Join (DefaultDir , "micro.db" )
23
- db , err := bolt .Open (dbPath , 0700 , & bolt.Options {Timeout : 5 * time .Second })
24
- if err != nil {
25
- return nil , errors .Wrap (err , "Error connecting to database" )
26
- }
27
-
28
- // return the blob store
29
- return & blobStore {db }, nil
20
+ return & blobStore {}, nil
30
21
}
31
22
32
- type blobStore struct {
33
- db * bolt.DB
23
+ type blobStore struct {}
24
+
25
+ func (b * blobStore ) db () (* bolt.DB , error ) {
26
+ dbPath := filepath .Join (DefaultDir , "blob.db" )
27
+ return bolt .Open (dbPath , 0700 , & bolt.Options {Timeout : 5 * time .Second })
34
28
}
35
29
36
30
func (b * blobStore ) Read (key string , opts ... store.BlobOption ) (io.Reader , error ) {
@@ -48,6 +42,13 @@ func (b *blobStore) Read(key string, opts ...store.BlobOption) (io.Reader, error
48
42
options .Namespace = "micro"
49
43
}
50
44
45
+ // open a connection to the database
46
+ db , err := b .db ()
47
+ if err != nil {
48
+ return nil , err
49
+ }
50
+ defer db .Close ()
51
+
51
52
// execute the transaction
52
53
var value []byte
53
54
readValue := func (tx * bolt.Tx ) error {
@@ -65,7 +66,7 @@ func (b *blobStore) Read(key string, opts ...store.BlobOption) (io.Reader, error
65
66
66
67
return nil
67
68
}
68
- if err := b . db .View (readValue ); err != nil {
69
+ if err := db .View (readValue ); err != nil {
69
70
return nil , err
70
71
}
71
72
@@ -88,8 +89,15 @@ func (b *blobStore) Write(key string, blob io.Reader, opts ...store.BlobOption)
88
89
options .Namespace = "micro"
89
90
}
90
91
92
+ // open a connection to the database
93
+ db , err := b .db ()
94
+ if err != nil {
95
+ return err
96
+ }
97
+ defer db .Close ()
98
+
91
99
// execute the transaction
92
- return b . db .Update (func (tx * bolt.Tx ) error {
100
+ return db .Update (func (tx * bolt.Tx ) error {
93
101
// create the bucket
94
102
bucket , err := tx .CreateBucketIfNotExists ([]byte (options .Namespace ))
95
103
if err != nil {
@@ -121,8 +129,15 @@ func (b *blobStore) Delete(key string, opts ...store.BlobOption) error {
121
129
options .Namespace = "micro"
122
130
}
123
131
132
+ // open a connection to the database
133
+ db , err := b .db ()
134
+ if err != nil {
135
+ return err
136
+ }
137
+ defer db .Close ()
138
+
124
139
// execute the transaction
125
- return b . db .Update (func (tx * bolt.Tx ) error {
140
+ return db .Update (func (tx * bolt.Tx ) error {
126
141
// check for the namespaces bucket
127
142
bucket := tx .Bucket ([]byte (options .Namespace ))
128
143
if bucket == nil {
0 commit comments