Skip to content

Commit

Permalink
Optimise OTxIndexChangesList and enable it for index tx management
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila committed May 19, 2021
1 parent 4b98092 commit 17fc010
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
Expand Up @@ -35,8 +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;
private final OTxIndexChangesList entries;

public boolean clientTrackOnly;

Expand Down Expand Up @@ -79,43 +78,35 @@ public OIdentifiable getValue() {

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);
Optional<OTxIndexChangesList.Node> node = entries.getNode(this);

this.value = newValue;
node.ifPresent(x -> x.onRidChange(oldValueId, newValueId));
}
}

public OTransactionIndexChangesPerKey(final Object iKey) {
this.key = iKey;
entries = new ArrayList<OTransactionIndexEntry>();
ridToNEntries = new HashMap<>();
entries = new OTxIndexChangesList();
}

public void add(OIdentifiable iValue, final OPERATION iOperation) {
synchronized (this) {
ORID valueIdentity = iValue == null ? null : iValue.getIdentity();
Iterator<OTransactionIndexEntry> iter = entries.iterator();
Integer count = ridToNEntries.get(valueIdentity);
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(valueIdentity, count - 1);
return;
}
}

Optional<OTxIndexChangesList.Node> nodeToRemove =
entries.getFirstNode(
valueIdentity, iOperation == OPERATION.PUT ? OPERATION.REMOVE : OPERATION.PUT);
if (nodeToRemove.isPresent()) {
nodeToRemove.get().remove();
return;
}

OTransactionIndexEntry item = new OTransactionIndexEntry(valueIdentity, iOperation);

entries.add(item);
ridToNEntries.put(valueIdentity, count == null ? 1 : count + 1);
}
}

Expand Down Expand Up @@ -143,7 +134,6 @@ public Iterable<OTransactionIndexEntry> interpret(Interpretation interpretation)
public void clear() {
synchronized (this) {
this.entries.clear();
this.ridToNEntries.clear();
}
}

Expand Down
Expand Up @@ -46,6 +46,16 @@ public void remove() {
// update size
size--;
}

public void onRidChange(ORID oldRid, ORID newRid) {
ridToNodes.get(oldRid).remove(this);
List<Node> newMapList = ridToNodes.get(newRid);
if (newMapList == null) {
newMapList = new ArrayList<>();
ridToNodes.put(newRid, newMapList);
}
newMapList.add(this);
}
}

private Node first;
Expand Down Expand Up @@ -417,4 +427,21 @@ public List<OTransactionIndexChangesPerKey.OTransactionIndexEntry> subList(
// TODO implement this
throw new UnsupportedOperationException();
}

public Optional<Node> getFirstNode(ORID rid, OTransactionIndexChanges.OPERATION op) {
List<Node> list = ridToNodes.get(rid);
if (list != null) {
return list.stream().filter(x -> x.entry.getOperation() == op).findFirst();
}
return Optional.empty();
}

public Optional<Node> getNode(OTransactionIndexChangesPerKey.OTransactionIndexEntry entry) {
ORID rid = entry.getValue() == null ? null : entry.getValue().getIdentity();
List<Node> list = ridToNodes.get(rid);
if (list != null) {
return list.stream().filter(x -> x.entry == entry).findFirst();
}
return Optional.empty();
}
}

0 comments on commit 17fc010

Please sign in to comment.