@@ -29,12 +29,6 @@ type BoltDB struct {
2929 dbIndex uint64
3030 path string
3131 timeout time.Duration
32- // By default libkv opens and closes the bolt DB connection for every
33- // get/put operation. This allows multiple apps to use a Bolt DB at the
34- // same time.
35- // PersistConnection flag provides an option to override ths behavior.
36- // ie: open the connection in New and use it till Close is called.
37- PersistConnection bool
3832}
3933
4034const (
@@ -53,15 +47,11 @@ func New(endpoint string, options *store.Config) (store.Store, error) {
5347 return nil , err
5448 }
5549
56- var db * bolt.DB
57- if options .PersistConnection {
58- var err error
59- db , err = bolt .Open (endpoint , filePerm , & bolt.Options {
60- Timeout : options .ConnectionTimeout ,
61- })
62- if err != nil {
63- return nil , err
64- }
50+ db , err := bolt .Open (endpoint , filePerm , & bolt.Options {
51+ Timeout : options .ConnectionTimeout ,
52+ })
53+ if err != nil {
54+ return nil , err
6555 }
6656
6757 timeout := transientTimeout
@@ -70,38 +60,19 @@ func New(endpoint string, options *store.Config) (store.Store, error) {
7060 }
7161
7262 b := & BoltDB {
73- client : db ,
74- path : endpoint ,
75- boltBucket : []byte (options .Bucket ),
76- timeout : timeout ,
77- PersistConnection : options .PersistConnection ,
63+ client : db ,
64+ path : endpoint ,
65+ boltBucket : []byte (options .Bucket ),
66+ timeout : timeout ,
7867 }
7968
8069 return b , nil
8170}
8271
83- func (b * BoltDB ) reset () {
84- b .path = ""
85- b .boltBucket = []byte {}
86- }
87-
8872func (b * BoltDB ) getDBhandle () (* bolt.DB , error ) {
89- if ! b .PersistConnection {
90- db , err := bolt .Open (b .path , filePerm , & bolt.Options {Timeout : b .timeout })
91- if err != nil {
92- return nil , err
93- }
94- b .client = db
95- }
9673 return b .client , nil
9774}
9875
99- func (b * BoltDB ) releaseDBhandle () {
100- if ! b .PersistConnection {
101- b .client .Close ()
102- }
103- }
104-
10576// Put the key, value pair. index number metadata is prepended to the value
10677func (b * BoltDB ) Put (key string , value []byte ) error {
10778 b .mu .Lock ()
@@ -111,7 +82,6 @@ func (b *BoltDB) Put(key string, value []byte) error {
11182 if err != nil {
11283 return err
11384 }
114- defer b .releaseDBhandle ()
11585
11686 return db .Update (func (tx * bolt.Tx ) error {
11787 bucket , err := tx .CreateBucketIfNotExists (b .boltBucket )
@@ -137,7 +107,6 @@ func (b *BoltDB) Exists(key string) (bool, error) {
137107 if err != nil {
138108 return false , err
139109 }
140- defer b .releaseDBhandle ()
141110
142111 var exists bool
143112 err = db .View (func (tx * bolt.Tx ) error {
@@ -167,7 +136,6 @@ func (b *BoltDB) List(keyPrefix string) ([]*store.KVPair, error) {
167136 if err != nil {
168137 return nil , err
169138 }
170- defer b .releaseDBhandle ()
171139
172140 var kv []* store.KVPair
173141 err = db .View (func (tx * bolt.Tx ) error {
@@ -216,7 +184,6 @@ func (b *BoltDB) AtomicDelete(key string, previous *store.KVPair) error {
216184 if err != nil {
217185 return err
218186 }
219- defer b .releaseDBhandle ()
220187
221188 return db .Update (func (tx * bolt.Tx ) error {
222189 bucket := tx .Bucket (b .boltBucket )
@@ -246,7 +213,6 @@ func (b *BoltDB) AtomicPut(key string, value []byte, previous *store.KVPair) (*s
246213 if err != nil {
247214 return nil , err
248215 }
249- defer b .releaseDBhandle ()
250216
251217 var dbIndex uint64
252218 dbval := make ([]byte , libkvmetadatalen )
@@ -293,9 +259,5 @@ func (b *BoltDB) Close() {
293259 b .mu .Lock ()
294260 defer b .mu .Unlock ()
295261
296- if ! b .PersistConnection {
297- b .reset ()
298- } else {
299- b .client .Close ()
300- }
262+ b .client .Close ()
301263}
0 commit comments