com.jetbrains.teamsys.dnq.database.TransientChangesTrackerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dnq-transient-store Show documentation
Show all versions of dnq-transient-store Show documentation
Data definition and queries Kotlin DSL over Xodus
/**
* Copyright 2006 - 2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.teamsys.dnq.database;
import jetbrains.exodus.core.dataStructures.Pair;
import jetbrains.exodus.core.dataStructures.decorators.HashMapDecorator;
import jetbrains.exodus.core.dataStructures.decorators.LinkedHashSetDecorator;
import jetbrains.exodus.core.dataStructures.hash.HashMap;
import jetbrains.exodus.core.dataStructures.hash.HashSet;
import jetbrains.exodus.core.dataStructures.hash.LinkedHashSet;
import jetbrains.exodus.database.*;
import jetbrains.exodus.entitystore.Entity;
import jetbrains.exodus.entitystore.PersistentStoreTransaction;
import jetbrains.exodus.entitystore.ReadOnlyPersistentEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* @author Vadim.Gurov
*/
public final class TransientChangesTrackerImpl implements TransientChangesTracker {
private final Set changedEntities = new LinkedHashSet();
private final Set addedEntities = new LinkedHashSet();
private final Set removedEntities = new LinkedHashSetDecorator();
private final Set affectedEntityTypes = new HashSet();
private final Map> removedFrom = new HashMapDecorator>();
private final Map> entityToChangedLinksDetailed = new HashMapDecorator>();
private final Map> entityToChangedProperties = new HashMapDecorator>();
private PersistentStoreTransaction snapshot;
TransientChangesTrackerImpl(PersistentStoreTransaction snapshot) {
this.snapshot = snapshot;
}
@NotNull
public Set getChangedEntities() {
return changedEntities;
}
@NotNull
public Set getAffectedEntityTypes() {
return Collections.unmodifiableSet(affectedEntityTypes);
}
public PersistentStoreTransaction getSnapshot() {
return snapshot;
}
@Override
public TransientEntityImpl getSnapshotEntity(TransientEntity e) {
final ReadOnlyPersistentEntity ro = e.getPersistentEntity().getSnapshot(snapshot);
return new ReadonlyTransientEntityImpl(getChangeDescription(e), ro, (TransientEntityStore) e.getStore());
}
@NotNull
public Set getChangesDescription() {
Set changesDescription = new LinkedHashSetDecorator();
for (TransientEntity e : getChangedEntities()) {
// do not notify about RemovedNew entities - such entities was created and removed during same transaction
if (wasCreatedAndRemovedInSameTransaction(e)) continue;
changesDescription.add(new TransientEntityChange(this, e, getChangedProperties(e), getChangedLinksDetailed(e), getEntityChangeType(e)));
}
return changesDescription;
}
@Override
public int getChangesDescriptionCount() {
int addedAndRemovedCount = 0;
for (TransientEntity removed: removedEntities) {
if (addedEntities.contains(removed)) {
addedAndRemovedCount++;
}
}
return changedEntities.size() - addedAndRemovedCount;
}
private EntityChangeType getEntityChangeType(TransientEntity e) {
if (addedEntities.contains(e)) return EntityChangeType.ADD;
if (removedEntities.contains(e)) return EntityChangeType.REMOVE;
return EntityChangeType.UPDATE;
}
public TransientEntityChange getChangeDescription(TransientEntity e) {
return new TransientEntityChange(this, e, getChangedProperties(e), getChangedLinksDetailed(e), getEntityChangeType(e));
}
@Nullable
public Map getChangedLinksDetailed(@NotNull TransientEntity e) {
return entityToChangedLinksDetailed.get(e);
}
@Nullable
public Set getChangedProperties(@NotNull TransientEntity e) {
return entityToChangedProperties.get(e);
}
public Set getRemovedEntities() {
return removedEntities;
}
public Set getAddedEntities() {
return addedEntities;
}
public boolean isNew(@NotNull TransientEntity e) {
return addedEntities.contains(e);
}
public boolean isRemoved(@NotNull TransientEntity e) {
return removedEntities.contains(e);
}
public boolean isSaved(@NotNull TransientEntity e) {
return !addedEntities.contains(e) && !removedEntities.contains(e);
}
boolean wasCreatedAndRemovedInSameTransaction(@NotNull TransientEntity e) {
return addedEntities.contains(e) && removedEntities.contains(e);
}
public void linksRemoved(@NotNull TransientEntity source, @NotNull String linkName, Iterable links) {
entityChanged(source);
final Pair