github.com/samgwo/go-ethereum@v1.8.2-0.20180302101319-49bcb5fbd55e/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/compression/rle"
    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  	comp bool
    36  }
    37  
    38  func NewLDBDatabase(file string) (*LDBDatabase, error) {
    39  	// Open the db
    40  	db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: openFileLimit})
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	database := &LDBDatabase{db: db, comp: false}
    46  
    47  	return database, nil
    48  }
    49  
    50  func (self *LDBDatabase) Put(key []byte, value []byte) {
    51  	if self.comp {
    52  		value = rle.Compress(value)
    53  	}
    54  
    55  	err := self.db.Put(key, value, nil)
    56  	if err != nil {
    57  		fmt.Println("Error put", err)
    58  	}
    59  }
    60  
    61  func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
    62  	dat, err := self.db.Get(key, nil)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	if self.comp {
    68  		return rle.Decompress(dat)
    69  	}
    70  
    71  	return dat, nil
    72  }
    73  
    74  func (self *LDBDatabase) Delete(key []byte) error {
    75  	return self.db.Delete(key, nil)
    76  }
    77  
    78  func (self *LDBDatabase) LastKnownTD() []byte {
    79  	data, _ := self.Get([]byte("LTD"))
    80  
    81  	if len(data) == 0 {
    82  		data = []byte{0x0}
    83  	}
    84  
    85  	return data
    86  }
    87  
    88  func (self *LDBDatabase) NewIterator() iterator.Iterator {
    89  	return self.db.NewIterator(nil, nil)
    90  }
    91  
    92  func (self *LDBDatabase) Write(batch *leveldb.Batch) error {
    93  	return self.db.Write(batch, nil)
    94  }
    95  
    96  func (self *LDBDatabase) Close() {
    97  	// Close the leveldb database
    98  	self.db.Close()
    99  }