private static void addIndexesEntries(ODatabaseDocumentInternal database, ODocument document, final Collection<OIndex<?>> indexes, List<IndexChange> changes) { // STORE THE RECORD IF NEW, OTHERWISE ITS RID final OIdentifiable rid = document.getIdentity(); for (final OIndex<?> index : indexes) { addIndexEntry(document, rid, getTransactionalIndex(database, index), changes); } }
private static void deleteIndexKey(final OIndex<?> index, final ODocument iRecord, final Object origValue, List<IndexChange> changes) { final OIndexDefinition indexDefinition = index.getDefinition(); if (origValue instanceof Collection) { for (final Object valueItem : (Collection<?>) origValue) { if (!indexDefinition.isNullValuesIgnored() || valueItem != null) addRemove(changes, index, valueItem, iRecord); } } else if (!indexDefinition.isNullValuesIgnored() || origValue != null) { addRemove(changes, index, origValue, iRecord); } }
public static void checkIndexesAfterUpdate(ODocument iDocument, ODatabaseDocumentInternal database) { iDocument = checkForLoading(iDocument); List<IndexChange> changes = new ArrayList<>(); processIndexOnUpdate(database, iDocument, changes); applyChanges(changes); }
public static void checkIndexesAfterCreate(ODocument document, ODatabaseDocumentInternal database) { document = checkForLoading(document); List<IndexChange> ops = new ArrayList<>(); processIndexOnCreate(database, document, ops); applyChanges(ops); }
public static void checkIndexesAfterDelete(ODocument iDocument, ODatabaseDocumentInternal database) { List<IndexChange> changes = new ArrayList<>(); processIndexOnDelete(database, iDocument, changes); applyChanges(changes); }
indexes.add(getTransactionalIndex(database, index)); result = processCompositeIndexDelete(index, dirtyFields, iDocument, changes); else result = processSingleIndexDelete(index, dirtyFields, iDocument, changes); deleteIndexKey(index, iDocument, key, changes);
private static void processSingleIndexUpdate(final OIndex<?> index, final Set<String> dirtyFields, final ODocument iRecord, List<IndexChange> changes) { final OIndexDefinition indexDefinition = index.getDefinition(); final List<String> indexFields = indexDefinition.getFields(); if (indexFields.isEmpty()) return; final String indexField = indexFields.get(0); if (!dirtyFields.contains(indexField)) return; final OMultiValueChangeTimeLine<?, ?> multiValueChangeTimeLine = iRecord.getCollectionTimeLine(indexField); if (multiValueChangeTimeLine != null) { final OIndexDefinitionMultiValue indexDefinitionMultiValue = (OIndexDefinitionMultiValue) indexDefinition; final Map<Object, Integer> keysToAdd = new HashMap<>(); final Map<Object, Integer> keysToRemove = new HashMap<>(); for (OMultiValueChangeEvent<?, ?> changeEvent : multiValueChangeTimeLine.getMultiValueChangeEvents()) { indexDefinitionMultiValue.processChangeEvent(changeEvent, keysToAdd, keysToRemove); } for (final Object keyToRemove : keysToRemove.keySet()) addRemove(changes, index, keyToRemove, iRecord); for (final Object keyToAdd : keysToAdd.keySet()) addPut(changes, index, keyToAdd, iRecord.getIdentity()); } else { final Object origValue = indexDefinition.createValue(iRecord.getOriginalValue(indexField)); final Object newValue = indexDefinition.getDocumentValueToIndex(iRecord); processIndexUpdateFieldAssignment(index, iRecord, origValue, newValue, changes); } }
private static void processIndexUpdateFieldAssignment(OIndex<?> index, ODocument iRecord, final Object origValue, final Object newValue, List<IndexChange> changes) { final OIndexDefinition indexDefinition = index.getDefinition(); if ((origValue instanceof Collection) && (newValue instanceof Collection)) { final Set<Object> valuesToRemove = new HashSet<>((Collection<?>) origValue); final Set<Object> valuesToAdd = new HashSet<>((Collection<?>) newValue); valuesToRemove.removeAll((Collection<?>) newValue); valuesToAdd.removeAll((Collection<?>) origValue); for (final Object valueToRemove : valuesToRemove) { if (!indexDefinition.isNullValuesIgnored() || valueToRemove != null) { addRemove(changes, index, valueToRemove, iRecord); } } for (final Object valueToAdd : valuesToAdd) { if (!indexDefinition.isNullValuesIgnored() || valueToAdd != null) { addPut(changes, index, valueToAdd, iRecord); } } } else { deleteIndexKey(index, iRecord, origValue, changes); if (newValue instanceof Collection) { for (final Object newValueItem : (Collection<?>) newValue) { addPut(changes, index, newValueItem, iRecord.getIdentity()); } } else if (!indexDefinition.isNullValuesIgnored() || newValue != null) { addPut(changes, index, newValue, iRecord.getIdentity()); } } }
deleteIndexKey(index, iRecord, origValue, changes); return true;
public static void processIndexOnCreate(ODatabaseDocumentInternal database, ODocument document, List<IndexChange> ops) { final OImmutableClass cls = ODocumentInternal.getImmutableSchemaClass(database, document); if (cls != null) { final Collection<OIndex<?>> indexes = cls.getRawIndexes(); addIndexesEntries(database, document, indexes, ops); } }
private static void addIndexEntry(ODocument document, OIdentifiable rid, OIndex<?> index, List<IndexChange> changes) { 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) addPut(changes, index, keyItem, rid); } else if (!indexDefinition.isNullValuesIgnored() || key != null) addPut(changes, index, key, rid); }
public void afterUpdateOperations(final OIdentifiable id) { if (id instanceof ODocument) { ODocument doc = (ODocument) id; OImmutableClass clazz = ODocumentInternal.getImmutableSchemaClass(this, doc); if (clazz != null) { OClassIndexManager.checkIndexesAfterUpdate((ODocument) id, this); if (clazz.isFunction()) { this.getSharedContext().getFunctionLibrary().updatedFunction(doc); Orient.instance().getScriptManager().close(this.getName()); } if (clazz.isOuser() || clazz.isOrole()) { getMetadata().getSecurity().incrementVersion(); } if (clazz.isSequence()) { ((OSequenceLibraryProxy) getMetadata().getSequenceLibrary()).getDelegate().onSequenceUpdated(this, doc); } if (clazz.isTriggered()) { OClassTrigger.onRecordAfterUpdate(doc, this); } } OLiveQueryHook.addOp(doc, ORecordOperation.UPDATED, this); OLiveQueryHookV2.addOp(doc, ORecordOperation.UPDATED, this); } callbackHooks(ORecordHook.TYPE.AFTER_UPDATE, id); }
public void afterDeleteOperations(final OIdentifiable id) { if (id instanceof ODocument) { ODocument doc = (ODocument) id; OImmutableClass clazz = ODocumentInternal.getImmutableSchemaClass(this, doc); if (clazz != null) { OClassIndexManager.checkIndexesAfterDelete(doc, this); if (clazz.isFunction()) { this.getSharedContext().getFunctionLibrary().droppedFunction(doc); Orient.instance().getScriptManager().close(this.getName()); } if (clazz.isOuser() || clazz.isOrole()) { getMetadata().getSecurity().incrementVersion(); } if (clazz.isSequence()) { ((OSequenceLibraryProxy) getMetadata().getSequenceLibrary()).getDelegate().onSequenceDropped(this, doc); } if (clazz.isScheduler()) { final String eventName = doc.field(OScheduledEvent.PROP_NAME); getSharedContext().getScheduler().removeEventInternal(eventName); } if (clazz.isTriggered()) { OClassTrigger.onRecordAfterDelete(doc, this); } } OLiveQueryHook.addOp(doc, ORecordOperation.DELETED, this); OLiveQueryHookV2.addOp(doc, ORecordOperation.DELETED, this); } callbackHooks(ORecordHook.TYPE.AFTER_DELETE, id); }
public void afterCreateOperations(final OIdentifiable id) { if (id instanceof ODocument) { ODocument doc = (ODocument) id; OImmutableClass clazz = ODocumentInternal.getImmutableSchemaClass(this, doc); if (clazz != null) { OClassIndexManager.checkIndexesAfterCreate(doc, this); if (clazz.isFunction()) { this.getSharedContext().getFunctionLibrary().createdFunction(doc); Orient.instance().getScriptManager().close(this.getName()); } if (clazz.isOuser() || clazz.isOrole()) { getMetadata().getSecurity().incrementVersion(); } if (clazz.isSequence()) { ((OSequenceLibraryProxy) getMetadata().getSequenceLibrary()).getDelegate().onSequenceCreated(this, doc); } if (clazz.isScheduler()) { getMetadata().getScheduler().scheduleEvent(new OScheduledEvent(doc)); } if (clazz.isTriggered()) { OClassTrigger.onRecordAfterCreate(doc, this); } } OLiveQueryHook.addOp(doc, ORecordOperation.CREATED, this); OLiveQueryHookV2.addOp(doc, ORecordOperation.CREATED, this); } callbackHooks(ORecordHook.TYPE.AFTER_CREATE, id); }
addRemove(changes, index, origValue, iRecord); addPut(changes, index, newValue, iRecord.getIdentity()); } else { final OMultiValueChangeTimeLine<?, ?> multiValueChangeTimeLine = iRecord.getCollectionTimeLine(multiValueField); final Object newValue = indexDefinition.getDocumentValueToIndex(iRecord); processIndexUpdateFieldAssignment(index, iRecord, origValue, newValue, changes); } else { addRemove(changes, index, keyToRemove, iRecord); addPut(changes, index, keyToAdd, iRecord.getIdentity()); } else { final OTrackedMultiValue fieldValue = iRecord.field(multiValueField); final Object newValue = indexDefinition.getDocumentValueToIndex(iRecord); processIndexUpdateFieldAssignment(index, iRecord, origValue, newValue, changes);
@SuppressWarnings({ "rawtypes", "unchecked" }) private static boolean processSingleIndexDelete(final OIndex<?> index, final Set<String> dirtyFields, final ODocument iRecord, List<IndexChange> changes) { final OIndexDefinition indexDefinition = index.getDefinition(); final List<String> indexFields = indexDefinition.getFields(); if (indexFields.isEmpty()) { return false; } final String indexField = indexFields.iterator().next(); if (dirtyFields.contains(indexField)) { final OMultiValueChangeTimeLine<?, ?> multiValueChangeTimeLine = iRecord.getCollectionTimeLine(indexField); final Object origValue; if (multiValueChangeTimeLine != null) { final OTrackedMultiValue fieldValue = iRecord.field(indexField); final Object restoredMultiValue = fieldValue.returnOriginalState(multiValueChangeTimeLine.getMultiValueChangeEvents()); origValue = indexDefinition.createValue(restoredMultiValue); } else origValue = indexDefinition.createValue(iRecord.getOriginalValue(indexField)); deleteIndexKey(index, iRecord, origValue, changes); return true; } return false; }