github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/storage/database.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package storage
    13  
    14  // this is a clone of an earlier state of the sberex ethdb/database
    15  // no need for queueing/caching
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/Sberex/go-sberex/compression/rle"
    21  	"github.com/syndtr/goleveldb/leveldb"
    22  	"github.com/syndtr/goleveldb/leveldb/iterator"
    23  	"github.com/syndtr/goleveldb/leveldb/opt"
    24  )
    25  
    26  const openFileLimit = 128
    27  
    28  type LDBDatabase struct {
    29  	db   *leveldb.DB
    30  	comp bool
    31  }
    32  
    33  func NewLDBDatabase(file string) (*LDBDatabase, error) {
    34  	// Open the db
    35  	db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: openFileLimit})
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	database := &LDBDatabase{db: db, comp: false}
    41  
    42  	return database, nil
    43  }
    44  
    45  func (self *LDBDatabase) Put(key []byte, value []byte) {
    46  	if self.comp {
    47  		value = rle.Compress(value)
    48  	}
    49  
    50  	err := self.db.Put(key, value, nil)
    51  	if err != nil {
    52  		fmt.Println("Error put", err)
    53  	}
    54  }
    55  
    56  func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
    57  	dat, err := self.db.Get(key, nil)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	if self.comp {
    63  		return rle.Decompress(dat)
    64  	}
    65  
    66  	return dat, nil
    67  }
    68  
    69  func (self *LDBDatabase) Delete(key []byte) error {
    70  	return self.db.Delete(key, nil)
    71  }
    72  
    73  func (self *LDBDatabase) LastKnownTD() []byte {
    74  	data, _ := self.Get([]byte("LTD"))
    75  
    76  	if len(data) == 0 {
    77  		data = []byte{0x0}
    78  	}
    79  
    80  	return data
    81  }
    82  
    83  func (self *LDBDatabase) NewIterator() iterator.Iterator {
    84  	return self.db.NewIterator(nil, nil)
    85  }
    86  
    87  func (self *LDBDatabase) Write(batch *leveldb.Batch) error {
    88  	return self.db.Write(batch, nil)
    89  }
    90  
    91  func (self *LDBDatabase) Close() {
    92  	// Close the leveldb database
    93  	self.db.Close()
    94  }