github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/storage/boltdb/storage.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package boltdb
    19  
    20  import (
    21  	"path/filepath"
    22  	"sync"
    23  
    24  	"github.com/asdine/storm/v3"
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  // Bolt is a wrapper around boltdb
    29  type Bolt struct {
    30  	mux sync.RWMutex
    31  	db  *storm.DB
    32  }
    33  
    34  // NewStorage creates a new BoltDB storage for service promises
    35  func NewStorage(path string) (*Bolt, error) {
    36  	return openDB(filepath.Join(path, "myst.db"))
    37  }
    38  
    39  // openDB creates new or open existing BoltDB
    40  func openDB(name string) (*Bolt, error) {
    41  	db, err := storm.Open(name)
    42  	return &Bolt{
    43  		db: db,
    44  	}, errors.Wrap(err, "failed to open boltDB")
    45  }
    46  
    47  // GetValue gets key value
    48  func (b *Bolt) GetValue(bucket string, key interface{}, to interface{}) error {
    49  	b.mux.RLock()
    50  	defer b.mux.RUnlock()
    51  	return b.db.Get(bucket, key, to)
    52  }
    53  
    54  // SetValue sets key value
    55  func (b *Bolt) SetValue(bucket string, key interface{}, to interface{}) error {
    56  	b.mux.Lock()
    57  	defer b.mux.Unlock()
    58  	return b.db.Set(bucket, key, to)
    59  }
    60  
    61  // Store allows to keep struct grouped by the bucket
    62  func (b *Bolt) Store(bucket string, data interface{}) error {
    63  	b.mux.Lock()
    64  	defer b.mux.Unlock()
    65  	return b.db.From(bucket).Save(data)
    66  }
    67  
    68  // GetAllFrom allows to get all structs from the bucket
    69  func (b *Bolt) GetAllFrom(bucket string, data interface{}) error {
    70  	b.mux.RLock()
    71  	defer b.mux.RUnlock()
    72  	return b.db.From(bucket).All(data)
    73  }
    74  
    75  // Delete removes the given struct from the given bucket
    76  func (b *Bolt) Delete(bucket string, data interface{}) error {
    77  	b.mux.Lock()
    78  	defer b.mux.Unlock()
    79  	return b.db.From(bucket).DeleteStruct(data)
    80  }
    81  
    82  // DeleteKey the given struct from the given bucket
    83  func (b *Bolt) DeleteKey(bucket string, key interface{}) error {
    84  	b.mux.Lock()
    85  	defer b.mux.Unlock()
    86  	return b.db.Delete(bucket, key)
    87  }
    88  
    89  // Update allows to update the struct in the given bucket
    90  func (b *Bolt) Update(bucket string, object interface{}) error {
    91  	b.mux.Lock()
    92  	defer b.mux.Unlock()
    93  	return b.db.From(bucket).Update(object)
    94  }
    95  
    96  // GetOneByField returns an object from the given bucket by the given field
    97  func (b *Bolt) GetOneByField(bucket string, fieldName string, key interface{}, to interface{}) error {
    98  	b.mux.RLock()
    99  	defer b.mux.RUnlock()
   100  	return b.db.From(bucket).One(fieldName, key, to)
   101  }
   102  
   103  // GetLast returns the last entry in the bucket
   104  func (b *Bolt) GetLast(bucket string, to interface{}) error {
   105  	b.mux.RLock()
   106  	defer b.mux.RUnlock()
   107  	return b.db.From(bucket).Select().Reverse().First(to)
   108  }
   109  
   110  // GetBuckets returns a list of buckets
   111  func (b *Bolt) GetBuckets() []string {
   112  	b.mux.RLock()
   113  	defer b.mux.RUnlock()
   114  	return b.db.Bucket()
   115  }
   116  
   117  // DB returns raw storm DB.
   118  func (b *Bolt) DB() *storm.DB {
   119  	return b.db
   120  }
   121  
   122  // Close closes database
   123  func (b *Bolt) Close() error {
   124  	b.mux.Lock()
   125  	defer b.mux.Unlock()
   126  	return b.db.Close()
   127  }
   128  
   129  // RLock locks underlying RWMutex for reading
   130  func (b *Bolt) RLock() {
   131  	b.mux.RLock()
   132  }
   133  
   134  // RUnlock unlocks underlying RWMutex for reading
   135  func (b *Bolt) RUnlock() {
   136  	b.mux.RUnlock()
   137  }
   138  
   139  // Lock locks underlying RWMutex for write
   140  func (b *Bolt) Lock() {
   141  	b.mux.Lock()
   142  }
   143  
   144  // Unlock unlocks underlying RWMutex for write
   145  func (b *Bolt) Unlock() {
   146  	b.mux.Unlock()
   147  }