Skip to content

Commit

Permalink
refactor: add method that apply all changes to the index engine
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Jan 11, 2025
1 parent 5b4efd0 commit 93e1372
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.orientechnologies.common.util.ORawPair;
import com.orientechnologies.orient.core.config.IndexEngineData;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.index.OIndexInternal;
import com.orientechnologies.orient.core.index.OIndexMetadata;
import com.orientechnologies.orient.core.index.OIndexOneValue;
import com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation;
import com.orientechnologies.orient.core.tx.OTransactionIndexChanges;
import com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey;
import java.io.IOException;
import java.util.stream.Stream;

Expand Down Expand Up @@ -95,4 +99,47 @@ boolean validatedPut(
Object key,
ORID value,
IndexEngineValidator<Object, ORID> validator);

default void applyTxChanges(OAtomicOperation atomicOperation, OTransactionIndexChanges changes) {
for (final OTransactionIndexChangesPerKey changesPerKey : changes.changesPerKey.values()) {
applyKeyTxChanges(atomicOperation, changesPerKey, this, changes.getAssociatedIndex());
}
applyKeyTxChanges(atomicOperation, changes.nullKeyChanges, this, changes.getAssociatedIndex());
}

private static void applyKeyTxChanges(
OAtomicOperation atomicOperation,
OTransactionIndexChangesPerKey changes,
OBaseIndexEngine engine,
OIndexInternal index) {

IndexEngineValidator<Object, ORID> uniqueValidator = null;
if (index.isUnique()) {
uniqueValidator = ((OIndexOneValue) index).getUniqueValidator();
}
for (OTransactionIndexChangesPerKey.OTransactionIndexEntry op :
index.interpretTxKeyChanges(changes)) {
switch (op.getOperation()) {
case PUT:
if (uniqueValidator != null) {
engine.validatedPut(
atomicOperation, changes.key, op.getValue().getIdentity(), uniqueValidator);
} else {
engine.put(atomicOperation, changes.key, op.getValue().getIdentity());
}
break;
case REMOVE:
if (op.getValue() != null) {
engine.remove(atomicOperation, changes.key, op.getValue().getIdentity());
} else {
engine.remove(atomicOperation, changes.key);
}
break;
case CLEAR:
// SHOULD NEVER BE THE CASE HANDLE BY cleared FLAG
break;
}
engine.updateUniqueIndexVersion(changes.key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@
import com.orientechnologies.orient.core.index.OIndexInternal;
import com.orientechnologies.orient.core.index.OIndexManagerAbstract;
import com.orientechnologies.orient.core.index.OIndexMetadata;
import com.orientechnologies.orient.core.index.OIndexOneValue;
import com.orientechnologies.orient.core.index.OIndexes;
import com.orientechnologies.orient.core.index.ORuntimeKeyIndexDefinition;
import com.orientechnologies.orient.core.index.engine.IndexEngineValidator;
import com.orientechnologies.orient.core.index.engine.IndexEngineValuesTransformer;
import com.orientechnologies.orient.core.index.engine.OBaseIndexEngine;
import com.orientechnologies.orient.core.index.engine.OIndexEngine;
Expand Down Expand Up @@ -171,7 +169,6 @@
import com.orientechnologies.orient.core.tx.OTransactionData;
import com.orientechnologies.orient.core.tx.OTransactionId;
import com.orientechnologies.orient.core.tx.OTransactionIndexChanges;
import com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey;
import com.orientechnologies.orient.core.tx.OTransactionInternal;
import com.orientechnologies.orient.core.tx.OTxMetadataHolder;
import com.orientechnologies.orient.core.tx.OTxMetadataHolderImpl;
Expand Down Expand Up @@ -2625,55 +2622,14 @@ private void commitIndexes(
final OBaseIndexEngine engine = indexEngines.get(indexId);
assert engine.getId() == indexId;

for (final OTransactionIndexChangesPerKey changesPerKey : changes.changesPerKey.values()) {
applyTxChanges(atomicOperation, changesPerKey, engine, index);
}
applyTxChanges(atomicOperation, changes.nullKeyChanges, engine, index);
engine.applyTxChanges(atomicOperation, changes);

} catch (final OInvalidIndexEngineIdException e) {
throw OException.wrapException(new OStorageException("Error during index commit"), e);
}
}
}

private void applyTxChanges(
OAtomicOperation atomicOperation,
OTransactionIndexChangesPerKey changes,
OBaseIndexEngine engine,
OIndexInternal index)
throws OInvalidIndexEngineIdException {

IndexEngineValidator<Object, ORID> uniqueValidator = null;
if (index.isUnique()) {
uniqueValidator = ((OIndexOneValue) index).getUniqueValidator();
}
for (OTransactionIndexChangesPerKey.OTransactionIndexEntry op :
index.interpretTxKeyChanges(changes)) {
switch (op.getOperation()) {
case PUT:
if (uniqueValidator != null) {
engine.validatedPut(
atomicOperation, changes.key, op.getValue().getIdentity(), uniqueValidator);
} else {
engine.put(atomicOperation, changes.key, op.getValue().getIdentity());
}
break;
case REMOVE:
if (op.getValue() != null) {
engine.remove(atomicOperation, changes.key, op.getValue().getIdentity());
} else {
engine.remove(atomicOperation, changes.key);
}
break;
case CLEAR:
// SHOULD NEVER BE THE CASE HANDLE BY cleared FLAG
break;
}
if (!isDistributedMode(lastMetadata)) {
engine.updateUniqueIndexVersion(changes.key);
}
}
}

public int loadIndexEngine(final String name) {
try {
stateLock.readLock().lock();
Expand Down

0 comments on commit 93e1372

Please sign in to comment.