Skip to content

Commit

Permalink
Issue #4206 is fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
laa committed Jun 9, 2015
1 parent f64fd86 commit 1065069
Show file tree
Hide file tree
Showing 3 changed files with 445 additions and 87 deletions.
Expand Up @@ -49,7 +49,6 @@
import com.orientechnologies.orient.core.exception.OFastConcurrentModificationException;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.hook.ODocumentHookAbstract;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentInternal;
Expand Down Expand Up @@ -303,24 +302,12 @@ private ODocument checkIndexedPropertiesOnCreation(final ODocument record, final
final TreeMap<OIndex<?>, List<Object>> indexKeysMap = new TreeMap<OIndex<?>, List<Object>>();

for (final OIndex<?> index : indexes) {
final List<Object> keys = new ArrayList<Object>();
if (index.getInternal() instanceof OIndexUnique) {
OIndexRecorder indexRecorder = new OIndexRecorder((OIndexUnique) index.getInternal());

final OIndexDefinition indexDefinition = index.getDefinition();
final Object key = index.getDefinition().getDocumentValueToIndex(record);

if (key instanceof Collection) {
for (final Object keyItem : (Collection<?>) key) {
if (!indexDefinition.isNullValuesIgnored() || keyItem != null) {
keys.add(copyKeyIfNeeded(index.getInternal().getCollatingValue(keyItem)));
}
}
} else {
if (!indexDefinition.isNullValuesIgnored() || key != null) {
keys.add(copyKeyIfNeeded(index.getInternal().getCollatingValue(key)));
}
addIndexEntry(record, record.getIdentity(), indexRecorder);
indexKeysMap.put(index, indexRecorder.getAffectedKeys());
}

indexKeysMap.put(index, keys);
}

for (Map.Entry<OIndex<?>, List<Object>> entry : indexKeysMap.entrySet()) {
Expand Down Expand Up @@ -361,30 +348,13 @@ private void checkIndexedPropertiesOnUpdate(final ODocument record, final Collec
return;

for (final OIndex<?> index : indexes) {
final List<Object> keys = new ArrayList<Object>();

final OIndexDefinition indexDefinition = index.getDefinition();
final List<String> indexFields = indexDefinition.getFields();
for (final String indexField : indexFields) {
if (dirtyFields.contains(indexField)) {
final Object key = index.getDefinition().getDocumentValueToIndex(record);
if (key instanceof Collection) {
for (final Object keyItem : (Collection<?>) key) {
if (!indexDefinition.isNullValuesIgnored() || keyItem != null) {
keys.add(copyKeyIfNeeded(index.getInternal().getCollatingValue(keyItem)));
}
}
} else {
if (!indexDefinition.isNullValuesIgnored() || key != null) {
keys.add(copyKeyIfNeeded(index.getInternal().getCollatingValue(key)));
}

}
break;
}
}
if (index instanceof OIndexUnique) {
final OIndexRecorder indexRecorder = new OIndexRecorder((OIndexUnique) index);
processIndexUpdate(record, dirtyFields, indexRecorder);

indexKeysMap.put(index, keys);
indexKeysMap.put(index, indexRecorder.getAffectedKeys());
}
}

for (Map.Entry<OIndex<?>, List<Object>> entry : indexKeysMap.entrySet()) {
Expand All @@ -403,21 +373,6 @@ private void checkIndexedPropertiesOnUpdate(final ODocument record, final Collec
}
}

private Object copyKeyIfNeeded(Object object) {
if (object instanceof ORecordId)
return new ORecordId((ORecordId) object);
else if (object instanceof OCompositeKey) {
final OCompositeKey copy = new OCompositeKey();
for (Object key : ((OCompositeKey) object).getKeys()) {
copy.addKey(copyKeyIfNeeded(key));
}

return copy;
}

return object;
}

private static ODocument checkForLoading(final ODocument iRecord) {
if (iRecord.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED) {
try {
Expand All @@ -444,8 +399,17 @@ public RESULT onRecordBeforeCreate(final ODocument document) {
}

@Override
public void onRecordAfterCreate(final ODocument iDocument) {
addIndexesEntries(iDocument);
public void onRecordAfterCreate(ODocument document) {
document = checkForLoading(document);

// STORE THE RECORD IF NEW, OTHERWISE ITS RID
final OIdentifiable rid = document.getIdentity();

final OClass cls = ODocumentInternal.getImmutableSchemaClass(document);
if (cls != null) {
final Collection<OIndex<?>> indexes = cls.getIndexes();
addIndexesEntries(document, indexes);
}
}

@Override
Expand Down Expand Up @@ -477,15 +441,19 @@ public void onRecordAfterUpdate(ODocument iDocument) {

if (!dirtyFields.isEmpty()) {
for (final OIndex<?> index : indexes) {
if (index.getDefinition() instanceof OCompositeIndexDefinition)
processCompositeIndexUpdate(index, dirtyFields, iDocument);
else
processSingleIndexUpdate(index, dirtyFields, iDocument);
processIndexUpdate(iDocument, dirtyFields, index);
}
}
}
}

private void processIndexUpdate(ODocument iDocument, Set<String> dirtyFields, OIndex<?> index) {
if (index.getDefinition() instanceof OCompositeIndexDefinition)
processCompositeIndexUpdate(index, dirtyFields, iDocument);
else
processSingleIndexUpdate(index, dirtyFields, iDocument);
}

@Override
public void onRecordUpdateFailed(final ODocument iDocument) {
}
Expand Down Expand Up @@ -524,36 +492,33 @@ public void onRecordDeleteFailed(final ODocument iDocument) {
public void onRecordDeleteReplicated(final ODocument iDocument) {
}

private void addIndexesEntries(ODocument document) {
document = checkForLoading(document);

private void addIndexesEntries(ODocument document, final Collection<OIndex<?>> indexes) {
// STORE THE RECORD IF NEW, OTHERWISE ITS RID
final OIdentifiable rid = document.getIdentity();

final OClass cls = ODocumentInternal.getImmutableSchemaClass(document);
if (cls != null) {
final Collection<OIndex<?>> indexes = cls.getIndexes();
for (final OIndex<?> index : indexes) {
final OIndexDefinition indexDefinition = index.getDefinition();
final Object key = indexDefinition.getDocumentValueToIndex(document);
if (key instanceof Collection) {
for (final Object keyItem : (Collection<?>) key)
if (!indexDefinition.isNullValuesIgnored() || keyItem != null)
index.put(keyItem, rid);
} else if (!indexDefinition.isNullValuesIgnored() || key != null)
try {
index.put(key, rid);
} catch (ORecordDuplicatedException e) {
if (!database.getTransaction().isActive()) {
database.delete(document);
}
throw e;
}
}

for (final OIndex<?> index : indexes) {
addIndexEntry(document, rid, index);
}
}

private void addIndexEntry(ODocument document, OIdentifiable rid, OIndex<?> index) {
final OIndexDefinition indexDefinition = index.getDefinition();
final Object key = indexDefinition.getDocumentValueToIndex(document);
if (key instanceof Collection) {
for (final Object keyItem : (Collection<?>) key)
if (!indexDefinition.isNullValuesIgnored() || keyItem != null)
index.put(keyItem, rid);
} else if (!indexDefinition.isNullValuesIgnored() || key != null)
try {
index.put(key, rid);
} catch (ORecordDuplicatedException e) {
if (!database.getTransaction().isActive()) {
database.delete(document);
}
throw e;
}
}

private void deleteIndexEntries(ODocument iDocument) {
final OClass cls = ODocumentInternal.getImmutableSchemaClass(iDocument);
if (cls == null)
Expand Down

0 comments on commit 1065069

Please sign in to comment.