github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/swarm/storage/database.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-dubxcoin library.
     3  //
     4  // The go-dubxcoin library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-dubxcoin library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-dubxcoin library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package storage
    18  
    19  // this is a clone of an earlier state of the ethereum ethdb/database
    20  // no need for queueing/caching
    21  
    22  import (
    23  	"github.com/alexdevranger/node-1.8.27/metrics"
    24  	"github.com/syndtr/goleveldb/leveldb"
    25  	"github.com/syndtr/goleveldb/leveldb/iterator"
    26  	"github.com/syndtr/goleveldb/leveldb/opt"
    27  )
    28  
    29  const openFileLimit = 128
    30  
    31  type LDBDatabase struct {
    32  	db *leveldb.DB
    33  }
    34  
    35  func NewLDBDatabase(file string) (*LDBDatabase, error) {
    36  	// Open the db
    37  	db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: openFileLimit})
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	database := &LDBDatabase{db: db}
    43  
    44  	return database, nil
    45  }
    46  
    47  func (db *LDBDatabase) Put(key []byte, value []byte) error {
    48  	metrics.GetOrRegisterCounter("ldbdatabase.put", nil).Inc(1)
    49  
    50  	return db.db.Put(key, value, nil)
    51  }
    52  
    53  func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
    54  	metrics.GetOrRegisterCounter("ldbdatabase.get", nil).Inc(1)
    55  
    56  	dat, err := db.db.Get(key, nil)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	return dat, nil
    61  }
    62  
    63  func (db *LDBDatabase) Delete(key []byte) error {
    64  	return db.db.Delete(key, nil)
    65  }
    66  
    67  func (db *LDBDatabase) NewIterator() iterator.Iterator {
    68  	metrics.GetOrRegisterCounter("ldbdatabase.newiterator", nil).Inc(1)
    69  
    70  	return db.db.NewIterator(nil, nil)
    71  }
    72  
    73  func (db *LDBDatabase) Write(batch *leveldb.Batch) error {
    74  	metrics.GetOrRegisterCounter("ldbdatabase.write", nil).Inc(1)
    75  
    76  	return db.db.Write(batch, nil)
    77  }
    78  
    79  func (db *LDBDatabase) Close() {
    80  	// Close the leveldb database
    81  	db.db.Close()
    82  }