Optimize addition of index entry
@@ -23,14 +23,7 @@ import com.orientechnologies.common.collection.OMultiCollectionIterator;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.tx.OTransactionIndexChanges.OPERATION;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Deque;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
/**
* Collects the changes to an index for a certain key
@@ -42,6 +35,7 @@ public class OTransactionIndexChangesPerKey {
/* internal */ static final int SET_ADD_THRESHOLD = 8;
public final Object key;
+ private final Map<ORID, Integer> ridToNEntries;
private final List<OTransactionIndexEntry> entries;
public boolean clientTrackOnly;
@@ -84,6 +78,14 @@ public class OTransactionIndexChangesPerKey {
}
public void setValue(OIdentifiable newValue) {
+ ORID oldValueId = value == null ? null : value.getIdentity();
+ Integer oldCount = ridToNEntries.get(oldValueId);
+ ridToNEntries.put(oldValueId, oldCount == null || oldCount < 1 ? 0 : oldCount - 1);
+
+ ORID newValueId = newValue == null ? null : newValue.getIdentity();
+ Integer newCount = ridToNEntries.get(newValueId);
+ ridToNEntries.put(newValueId, newCount == null ? 1 : newCount + 1);
+
this.value = newValue;
}
}
@@ -91,22 +93,30 @@ public class OTransactionIndexChangesPerKey {
public OTransactionIndexChangesPerKey(final Object iKey) {
this.key = iKey;
entries = new ArrayList<OTransactionIndexEntry>();
+ ridToNEntries = new HashMap<>();
}
public void add(OIdentifiable iValue, final OPERATION iOperation) {
synchronized (this) {
Iterator<OTransactionIndexEntry> iter = entries.iterator();
+ Integer count = ridToNEntries.get(iValue == null ? null : iValue.getIdentity());
+ if (count != null && count > 0) {
while (iter.hasNext()) {
OTransactionIndexEntry entry = iter.next();
if (((entry.value == iValue) || (entry.value != null && entry.value.equals(iValue)))
&& !entry.operation.equals(iOperation)) {
iter.remove();
+ ridToNEntries.put(iValue == null ? null : iValue.getIdentity(), count - 1);
return;
}
}
+ }
OTransactionIndexEntry item =
new OTransactionIndexEntry(iValue != null ? iValue.getIdentity() : null, iOperation);
+
entries.add(item);
+ ridToNEntries.put(
+ iValue == null ? null : iValue.getIdentity(), count == null ? 1 : count + 1);
}
}
@@ -133,7 +143,8 @@ public class OTransactionIndexChangesPerKey {
public void clear() {
synchronized (this) {
- entries.clear();
+ this.entries.clear();
+ this.ridToNEntries.clear();
}
}
