Coverage Summary for Class: HashMapDB (org.ethereum.datasource)

Class Class, % Method, % Line, %
HashMapDB 100% (1/1) 50% (6/12) 41.7% (15/36)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) 5  * 6  * This program is free software: you can redistribute it and/or modify 7  * it under the terms of the GNU Lesser General Public License as published by 8  * the Free Software Foundation, either version 3 of the License, or 9  * (at your option) any later version. 10  * 11  * This program is distributed in the hope that it will be useful, 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14  * GNU Lesser General Public License for more details. 15  * 16  * You should have received a copy of the GNU Lesser General Public License 17  * along with this program. If not, see <http://www.gnu.org/licenses/>. 18  */ 19  20 package org.ethereum.datasource; 21  22 import org.ethereum.db.ByteArrayWrapper; 23 import org.iq80.leveldb.DBException; 24  25 import java.util.*; 26 import java.util.concurrent.ConcurrentHashMap; 27 import java.util.stream.Collectors; 28  29 import static org.ethereum.util.ByteUtil.wrap; 30  31 public class HashMapDB implements KeyValueDataSource { 32  33  private final Map<ByteArrayWrapper, byte[]> storage = new ConcurrentHashMap<>(); 34  private boolean clearOnClose = true; 35  36  @Override 37  public void delete(byte[] arg0) throws DBException { 38  storage.remove(wrap(arg0)); 39  } 40  41  42  @Override 43  public byte[] get(byte[] arg0) throws DBException { 44  Objects.requireNonNull(arg0); 45  return storage.get(wrap(arg0)); 46  } 47  48  49  @Override 50  public byte[] put(byte[] key, byte[] value) throws DBException { 51  Objects.requireNonNull(key); 52  Objects.requireNonNull(value); 53  return storage.put(wrap(key), value); 54  } 55  56  @Override 57  public void init() { 58  this.storage.clear(); 59  } 60  61  @Override 62  public boolean isAlive() { 63  return true; 64  } 65  66  @Override 67  public String getName() { 68  return "in-memory"; 69  } 70  71  @Override 72  public synchronized Set<byte[]> keys() { 73  return storage.keySet().stream() 74  .map(ByteArrayWrapper::getData) 75  .collect(Collectors.toSet()); 76  } 77  78  @Override 79  public synchronized void updateBatch(Map<ByteArrayWrapper, byte[]> rows, Set<ByteArrayWrapper> keysToRemove) { 80  if (rows.containsKey(null) || rows.containsValue(null)) { 81  throw new IllegalArgumentException("Cannot update null values"); 82  } 83  rows.keySet().removeAll(keysToRemove); 84  for (Map.Entry<ByteArrayWrapper, byte[]> entry : rows.entrySet()) { 85  ByteArrayWrapper wrappedKey = entry.getKey(); 86  byte[] key = wrappedKey.getData(); 87  byte[] value = entry.getValue(); 88  put(key , value); 89  } 90  91  for (ByteArrayWrapper keyToRemove : keysToRemove) { 92  delete(keyToRemove.getData()); 93  } 94  } 95  96  public synchronized HashMapDB setClearOnClose(boolean clearOnClose) { 97  this.clearOnClose = clearOnClose; 98  return this; 99  } 100  101  @Override 102  public synchronized void close() { 103  if (clearOnClose) { 104  this.storage.clear(); 105  } 106  } 107  108  @Override 109  public void flush(){ 110  // HashMapDB has no flush: everything is kept in memory. 111  } 112 }