Use generics in TrackTrie

This commit is contained in:
nicksavers 2014-06-23 18:40:54 +02:00
parent 5d4ac105d3
commit c1cc8e0a86
2 changed files with 54 additions and 62 deletions

View File

@ -1,7 +1,6 @@
package org.ethereum.trie;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.db.Database;
import java.util.HashMap;
import java.util.Map;
@ -12,72 +11,68 @@ import java.util.Map;
* @author: Roman Mandeleil
* Created on: 11/06/2014 19:47
*/
public class TrackTrie implements TrieFacade {
private TrieFacade trie;
private TrieFacade trie;
private boolean trackingChanges;
private Map<ByteArrayWrapper, byte[]> changes;
private Map<ByteArrayWrapper, byte[]> deletes;
private boolean trackingChanges;
private Map<ByteArrayWrapper, byte[]> changes;
private Map<ByteArrayWrapper, byte[]> deletes;
public TrackTrie(TrieFacade trie) {
this.trie = trie;
}
public TrackTrie(TrieFacade trie) {
this.trie = trie ;
}
public void startTrack() {
changes = new HashMap<ByteArrayWrapper, byte[]>();
deletes = new HashMap<ByteArrayWrapper, byte[]>();
trackingChanges = true;
}
public void commitTrack() {
for (ByteArrayWrapper key : changes.keySet()) {
trie.update(key.getData(), changes.get(key));
}
changes = null;
trackingChanges = false;
}
public void startTrack(){
changes = new HashMap<>();
deletes = new HashMap<>();
trackingChanges = true;
}
public void rollbackTrack() {
changes = new HashMap<ByteArrayWrapper, byte[]>();
deletes = new HashMap<ByteArrayWrapper, byte[]>();
changes = null;
trackingChanges = false;
}
public void commitTrack(){
for(ByteArrayWrapper key : changes.keySet()){
trie.update(key.getData(), changes.get(key));
}
changes = null;
trackingChanges = false;
}
@Override
public void update(byte[] key, byte[] value) {
if (trackingChanges) {
changes.put(new ByteArrayWrapper(key), value);
} else {
trie.update(key, value);
}
}
public void rollbackTrack(){
changes = new HashMap<>();
deletes = new HashMap<>();
changes = null;
trackingChanges = false;
}
@Override
public byte[] get(byte[] key) {
if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
if (deletes.get(wKey) != null)
return null;
if (changes.get(wKey) != null)
return changes.get(wKey);
return trie.get(key);
}
return trie.get(key);
}
@Override
public void update(byte[] key, byte[] value) {
if (trackingChanges){
changes.put( new ByteArrayWrapper(key) , value);
} else {
trie.update(key, value);
}
}
@Override
public byte[] get(byte[] key) {
if(trackingChanges){
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
if (deletes.get(wKey) != null) return null;
if (changes.get(wKey) != null) return changes.get(wKey);
return trie.get(key);
}
return trie.get(key);
}
@Override
public void delete(byte[] key) {
if (trackingChanges){
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
deletes.put(wKey, null);
} else {
trie.delete(key);
}
}
@Override
public void delete(byte[] key) {
if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
deletes.put(wKey, null);
} else {
trie.delete(key);
}
}
}

View File

@ -7,7 +7,6 @@ import static org.ethereum.util.CompactEncoder.*;
import java.util.Arrays;
import org.ethereum.crypto.HashUtil;
import org.ethereum.db.Database;
import org.ethereum.util.Value;
import org.iq80.leveldb.DB;
import org.spongycastle.util.encoders.Hex;
@ -125,7 +124,6 @@ public class Trie implements TrieFacade{
return c.asBytes();
}
/**
* Delete a key/value pair from the trie
*
@ -135,7 +133,6 @@ public class Trie implements TrieFacade{
delete(Hex.encode(key));
}
/**
* Delete a key/value pair from the trie
*