github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/wtcdb/memory_database.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-wtc 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-wtc 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 wtcdb
    18  
    19  import (
    20  	"errors"
    21  	"sync"
    22  
    23  	"github.com/wtc/go-wtc/common"
    24  )
    25  
    26  /*
    27   * This is a test memory database. Do not use for any production it does not get persisted
    28   */
    29  type MemDatabase struct {
    30  	db   map[string][]byte
    31  	lock sync.RWMutex
    32  }
    33  
    34  func NewMemDatabase() (*MemDatabase, error) {
    35  	return &MemDatabase{
    36  		db: make(map[string][]byte),
    37  	}, nil
    38  }
    39  
    40  func (db *MemDatabase) Put(key []byte, value []byte) error {
    41  	db.lock.Lock()
    42  	defer db.lock.Unlock()
    43  
    44  	db.db[string(key)] = common.CopyBytes(value)
    45  	return nil
    46  }
    47  
    48  func (db *MemDatabase) Has(key []byte) (bool, error) {
    49  	db.lock.RLock()
    50  	defer db.lock.RUnlock()
    51  
    52  	_, ok := db.db[string(key)]
    53  	return ok, nil
    54  }
    55  
    56  func (db *MemDatabase) Get(key []byte) ([]byte, error) {
    57  	db.lock.RLock()
    58  	defer db.lock.RUnlock()
    59  
    60  	if entry, ok := db.db[string(key)]; ok {
    61  		return common.CopyBytes(entry), nil
    62  	}
    63  	return nil, errors.New("not found")
    64  }
    65  
    66  func (db *MemDatabase) Keys() [][]byte {
    67  	db.lock.RLock()
    68  	defer db.lock.RUnlock()
    69  
    70  	keys := [][]byte{}
    71  	for key := range db.db {
    72  		keys = append(keys, []byte(key))
    73  	}
    74  	return keys
    75  }
    76  
    77  /*
    78  func (db *MemDatabase) GetKeys() []*common.Key {
    79  	data, _ := db.Get([]byte("KeyRing"))
    80  
    81  	return []*common.Key{common.NewKeyFromBytes(data)}
    82  }
    83  */
    84  
    85  func (db *MemDatabase) Delete(key []byte) error {
    86  	db.lock.Lock()
    87  	defer db.lock.Unlock()
    88  
    89  	delete(db.db, string(key))
    90  	return nil
    91  }
    92  
    93  func (db *MemDatabase) Close() {}
    94  
    95  func (db *MemDatabase) NewBatch() Batch {
    96  	return &memBatch{db: db}
    97  }
    98  
    99  type kv struct{ k, v []byte }
   100  
   101  type memBatch struct {
   102  	db     *MemDatabase
   103  	writes []kv
   104  	size   int
   105  }
   106  
   107  func (b *memBatch) Put(key, value []byte) error {
   108  	b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)})
   109  	b.size += len(value)
   110  	return nil
   111  }
   112  
   113  func (b *memBatch) Write() error {
   114  	b.db.lock.Lock()
   115  	defer b.db.lock.Unlock()
   116  
   117  	for _, kv := range b.writes {
   118  		b.db.db[string(kv.k)] = kv.v
   119  	}
   120  	return nil
   121  }
   122  
   123  func (b *memBatch) ValueSize() int {
   124  	return b.size
   125  }