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; package org.ethereum.trie;
import org.ethereum.db.ByteArrayWrapper; import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.db.Database;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -12,10 +11,8 @@ import java.util.Map;
* @author: Roman Mandeleil * @author: Roman Mandeleil
* Created on: 11/06/2014 19:47 * Created on: 11/06/2014 19:47
*/ */
public class TrackTrie implements TrieFacade { public class TrackTrie implements TrieFacade {
private TrieFacade trie; private TrieFacade trie;
private boolean trackingChanges; private boolean trackingChanges;
@ -23,36 +20,34 @@ public class TrackTrie implements TrieFacade {
private Map<ByteArrayWrapper, byte[]> deletes; private Map<ByteArrayWrapper, byte[]> deletes;
public TrackTrie(TrieFacade trie) { public TrackTrie(TrieFacade trie) {
this.trie = trie ; this.trie = trie;
} }
public void startTrack() {
public void startTrack(){ changes = new HashMap<ByteArrayWrapper, byte[]>();
changes = new HashMap<>(); deletes = new HashMap<ByteArrayWrapper, byte[]>();
deletes = new HashMap<>();
trackingChanges = true; trackingChanges = true;
} }
public void commitTrack(){ public void commitTrack() {
for(ByteArrayWrapper key : changes.keySet()){ for (ByteArrayWrapper key : changes.keySet()) {
trie.update(key.getData(), changes.get(key)); trie.update(key.getData(), changes.get(key));
} }
changes = null; changes = null;
trackingChanges = false; trackingChanges = false;
} }
public void rollbackTrack(){ public void rollbackTrack() {
changes = new HashMap<>(); changes = new HashMap<ByteArrayWrapper, byte[]>();
deletes = new HashMap<>(); deletes = new HashMap<ByteArrayWrapper, byte[]>();
changes = null; changes = null;
trackingChanges = false; trackingChanges = false;
} }
@Override @Override
public void update(byte[] key, byte[] value) { public void update(byte[] key, byte[] value) {
if (trackingChanges){ if (trackingChanges) {
changes.put( new ByteArrayWrapper(key) , value); changes.put(new ByteArrayWrapper(key), value);
} else { } else {
trie.update(key, value); trie.update(key, value);
} }
@ -60,10 +55,12 @@ public class TrackTrie implements TrieFacade {
@Override @Override
public byte[] get(byte[] key) { public byte[] get(byte[] key) {
if(trackingChanges){ if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key); ByteArrayWrapper wKey = new ByteArrayWrapper(key);
if (deletes.get(wKey) != null) return null; if (deletes.get(wKey) != null)
if (changes.get(wKey) != null) return changes.get(wKey); return null;
if (changes.get(wKey) != null)
return changes.get(wKey);
return trie.get(key); return trie.get(key);
} }
return trie.get(key); return trie.get(key);
@ -71,7 +68,7 @@ public class TrackTrie implements TrieFacade {
@Override @Override
public void delete(byte[] key) { public void delete(byte[] key) {
if (trackingChanges){ if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key); ByteArrayWrapper wKey = new ByteArrayWrapper(key);
deletes.put(wKey, null); deletes.put(wKey, null);
} else { } else {
@ -79,5 +76,3 @@ public class TrackTrie implements TrieFacade {
} }
} }
} }

View File

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