Use List for deletes in Track databases

This commit is contained in:
nicksavers 2014-08-06 01:05:16 +02:00
parent 7f44fb49a2
commit 0299d42f39
2 changed files with 21 additions and 26 deletions

View File

@ -1,10 +1,10 @@
package org.ethereum.db;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ethereum.util.LRUMap;
/**
* www.ethereumJ.com
*
@ -13,14 +13,11 @@ import org.ethereum.util.LRUMap;
*/
public class TrackDatabase implements Database {
// private static final int MAX_ENTRIES = 1000; // Should contain most commonly hashed values
// private static LRUMap<ByteArrayWrapper, byte[]> valueCache = new LRUMap<>(0, MAX_ENTRIES);
private Database db;
private boolean trackingChanges;
private Map<ByteArrayWrapper, byte[]> changes;
private Map<ByteArrayWrapper, byte[]> deletes;
private List<ByteArrayWrapper> deletes;
public TrackDatabase(Database db) {
this.db = db;
@ -28,7 +25,7 @@ public class TrackDatabase implements Database {
public void startTrack() {
changes = new HashMap<>();
deletes = new HashMap<>();
deletes = new ArrayList<>();
trackingChanges = true;
}
@ -36,19 +33,21 @@ public class TrackDatabase implements Database {
for(ByteArrayWrapper key : changes.keySet()) {
db.put(key.getData(), changes.get(key));
}
for(ByteArrayWrapper key : deletes) {
db.delete(key.getData());
}
changes = null;
trackingChanges = false;
}
public void rollbackTrack() {
changes = new HashMap<>();
deletes = new HashMap<>();
deletes = new ArrayList<>();
changes = null;
trackingChanges = false;
}
public void put(byte[] key, byte[] value) {
// valueCache.put(wKey, value);
if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
changes.put(wKey, value);
@ -60,12 +59,9 @@ public class TrackDatabase implements Database {
public byte[] get(byte[] key) {
if(trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
if (deletes.get(wKey) != null) return null;
if (deletes.contains(wKey)) return null;
if (changes.get(wKey) != null) return changes.get(wKey);
}
// byte[] result = valueCache.get(wKey);
// if(result != null)
// return result;
return db.get(key);
}
@ -73,7 +69,7 @@ public class TrackDatabase implements Database {
public void delete(byte[] key) {
if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
deletes.put(wKey, null);
deletes.add(wKey);
} else {
db.delete(key);
}

View File

@ -1,8 +1,11 @@
package org.ethereum.trie;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.util.ByteUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -17,7 +20,7 @@ public class TrackTrie implements TrieFacade {
private boolean trackingChanges = false;
private Map<ByteArrayWrapper, byte[]> changes;
private Map<ByteArrayWrapper, byte[]> deletes;
private List<ByteArrayWrapper> deletes;
public TrackTrie(TrieFacade trie) {
this.trie = trie;
@ -25,7 +28,7 @@ public class TrackTrie implements TrieFacade {
public void startTrack() {
changes = new HashMap<>();
deletes = new HashMap<>();
deletes = new ArrayList<>();
trackingChanges = true;
}
@ -33,14 +36,16 @@ public class TrackTrie implements TrieFacade {
for (ByteArrayWrapper key : changes.keySet()) {
trie.update(key.getData(), changes.get(key));
}
// trie.sync();
for (ByteArrayWrapper key : deletes) {
trie.update(key.getData(), ByteUtil.EMPTY_BYTE_ARRAY);
}
changes = null;
trackingChanges = false;
}
public void rollbackTrack() {
changes = new HashMap<>();
deletes = new HashMap<>();
deletes = new ArrayList<>();
changes = null;
trackingChanges = false;
}
@ -58,7 +63,7 @@ public class TrackTrie implements TrieFacade {
public byte[] get(byte[] key) {
if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
if (deletes.get(wKey) != null)
if (deletes.contains(wKey))
return null;
if (changes.get(wKey) != null)
return changes.get(wKey);
@ -71,15 +76,9 @@ public class TrackTrie implements TrieFacade {
public void delete(byte[] key) {
if (trackingChanges) {
ByteArrayWrapper wKey = new ByteArrayWrapper(key);
deletes.put(wKey, null);
deletes.add(wKey);
} else {
trie.delete(key);
}
}
@Override
public void sync() {
// TODO Auto-generated method stub
}
}