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

View File

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