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,72 +11,68 @@ 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 Map<ByteArrayWrapper, byte[]> changes;
private Map<ByteArrayWrapper, byte[]> deletes;
private boolean trackingChanges; public TrackTrie(TrieFacade trie) {
private Map<ByteArrayWrapper, byte[]> changes; this.trie = trie;
private Map<ByteArrayWrapper, byte[]> deletes; }
public TrackTrie(TrieFacade trie) { public void startTrack() {
this.trie = trie ; 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(){ public void rollbackTrack() {
changes = new HashMap<>(); changes = new HashMap<ByteArrayWrapper, byte[]>();
deletes = new HashMap<>(); deletes = new HashMap<ByteArrayWrapper, byte[]>();
trackingChanges = true; changes = null;
} trackingChanges = false;
}
public void commitTrack(){ @Override
for(ByteArrayWrapper key : changes.keySet()){ public void update(byte[] key, byte[] value) {
trie.update(key.getData(), changes.get(key)); if (trackingChanges) {
} changes.put(new ByteArrayWrapper(key), value);
changes = null; } else {
trackingChanges = false; trie.update(key, value);
} }
}
public void rollbackTrack(){ @Override
changes = new HashMap<>(); public byte[] get(byte[] key) {
deletes = new HashMap<>(); if (trackingChanges) {
changes = null; ByteArrayWrapper wKey = new ByteArrayWrapper(key);
trackingChanges = false; 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
@Override public void delete(byte[] key) {
public void update(byte[] key, byte[] value) { if (trackingChanges) {
if (trackingChanges){ ByteArrayWrapper wKey = new ByteArrayWrapper(key);
changes.put( new ByteArrayWrapper(key) , value); deletes.put(wKey, null);
} else { } else {
trie.update(key, value); trie.delete(key);
} }
} }
@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);
}
}
} }

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
* *