github.com/pslzym/go-ethereum@v1.8.17-0.20180926104442-4b6824e07b1b/swarm/storage/database.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum 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  	"fmt"
    24  
    25  	"github.com/ethereum/go-ethereum/metrics"
    26  	"github.com/syndtr/goleveldb/leveldb"
    27  	"github.com/syndtr/goleveldb/leveldb/iterator"
    28  	"github.com/syndtr/goleveldb/leveldb/opt"
    29  )
    30  
    31  const openFileLimit = 128
    32  
    33  type LDBDatabase struct {
    34  	db *leveldb.DB
    35  }
    36  
    37  func NewLDBDatabase(file string) (*LDBDatabase, error) {
    38  	// Open the db
    39  	db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: openFileLimit})
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	database := &LDBDatabase{db: db}
    45  
    46  	return database, nil
    47  }
    48  
    49  func (db *LDBDatabase) Put(key []byte, value []byte) {
    50  	metrics.GetOrRegisterCounter("ldbdatabase.put", nil).Inc(1)
    51  
    52  	err := db.db.Put(key, value, nil)
    53  	if err != nil {
    54  		fmt.Println("Error put", err)
    55  	}
    56  }
    57  
    58  func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
    59  	metrics.GetOrRegisterCounter("ldbdatabase.get", nil).Inc(1)
    60  
    61  	dat, err := db.db.Get(key, nil)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return dat, nil
    66  }
    67  
    68  func (db *LDBDatabase) Delete(key []byte) error {
    69  	return db.db.Delete(key, nil)
    70  }
    71  
    72  func (db *LDBDatabase) LastKnownTD() []byte {
    73  	data, _ := db.Get([]byte("LTD"))
    74  
    75  	if len(data) == 0 {
    76  		data = []byte{0x0}
    77  	}
    78  
    79  	return data
    80  }
    81  
    82  func (db *LDBDatabase) NewIterator() iterator.Iterator {
    83  	metrics.GetOrRegisterCounter("ldbdatabase.newiterator", nil).Inc(1)
    84  
    85  	return db.db.NewIterator(nil, nil)
    86  }
    87  
    88  func (db *LDBDatabase) Write(batch *leveldb.Batch) error {
    89  	metrics.GetOrRegisterCounter("ldbdatabase.write", nil).Inc(1)
    90  
    91  	return db.db.Write(batch, nil)
    92  }
    93  
    94  func (db *LDBDatabase) Close() {
    95  	// Close the leveldb database
    96  	db.db.Close()
    97  }