Skip to content

Commit

Permalink
fix(#677): dropped storage parts were taken into an account
Browse files Browse the repository at this point in the history
  • Loading branch information
novoj committed Nov 19, 2024
1 parent ed1b9a1 commit ff59da5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.evitadb.api.requestResponse.data.structure.Entity;
import io.evitadb.api.requestResponse.schema.AttributeSchemaContract;
import io.evitadb.dataType.EvitaDataTypes;
import io.evitadb.exception.GenericEvitaInternalError;
import io.evitadb.utils.Assert;
import io.evitadb.utils.ComparatorUtils;
import io.evitadb.utils.MemoryMeasuringConstants;
Expand Down Expand Up @@ -415,6 +416,23 @@ public AttributeValue(int version, @Nonnull AttributeKey attributeKey, @Nonnull
this(version, attributeKey, value, false);
}

/**
* Returns the value of the attribute if it is not null. Throws an exception if the value is null,
* indicating that the attribute value is unexpectedly missing.
*
* @return the non-null value of the attribute
* @throws GenericEvitaInternalError if the attribute value is null
*/
@Nonnull
public Serializable valueOrThrowException() {
final Serializable theValue = this.value;
Assert.isPremiseValid(
theValue != null,
"Attribute value " + key.attributeName() + " is unexpectedly null!"
);
return theValue;
}

@Override
public int compareTo(AttributeValue o) {
return key.compareTo(o.key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,6 @@ EntitySchema getEntitySchema() {
return this.schemaAccessor.get();
}

/**
* Returns current entity schema.
*/
@Nullable
EntitySchema getEntitySchema(@Nonnull String entityType) {
return this.otherEntitiesSchemaAccessor.apply(entityType);
}

/*
FRIENDLY METHODS
*/
Expand Down Expand Up @@ -606,7 +598,7 @@ private void removeEntity(int primaryKey) {
*/
private void removeEntityFromIndexes(@Nonnull Entity entity, @Nonnull Scope scope) {
final GlobalEntityIndex globalIndex = (GlobalEntityIndex) getOrCreateIndex(new EntityIndexKey(EntityIndexType.GLOBAL, scope));
final Integer entityPrimaryKey = entity.getPrimaryKey();
final int entityPrimaryKey = entity.getPrimaryKeyOrThrowException();
final EntitySchemaContract entitySchema = entity.getSchema();
final EntityExistingDataFactory existingDataSupplierFactory = new EntityExistingDataFactory(entity);
// un-index attributes
Expand All @@ -617,7 +609,7 @@ private void removeEntityFromIndexes(@Nonnull Entity entity, @Nonnull Scope scop
unindexAllPrices(entity, scope, globalIndex, existingDataSupplierFactory);
// un-index references (and their attributes)
unindexReferences(entity, scope, entitySchema, globalIndex, existingDataSupplierFactory);
// remove all languages from the global indexe
// remove all languages from the global indexes
unindexLocales(entity, entitySchema, globalIndex, existingDataSupplierFactory);
// finally, remove entity from the global index
unindexPrimaryKey(entityPrimaryKey, scope, globalIndex);
Expand Down Expand Up @@ -660,7 +652,7 @@ private void unindexLocales(
@Nonnull GlobalEntityIndex globalIndex,
@Nonnull EntityExistingDataFactory existingDataSupplierFactory
) {
final Integer epk = entity.getPrimaryKey();
final int epk = entity.getPrimaryKeyOrThrowException();
for (Locale locale : entity.getLocales()) {
removeEntityLanguageInTargetIndex(
epk, locale, globalIndex, entitySchema, existingDataSupplierFactory, this.undoActionsAppender
Expand Down Expand Up @@ -717,6 +709,8 @@ private void unindexAllGlobalAttributes(
final Function<String, AttributeSchema> attributeSchemaRetriever = attributeName -> entitySchema.getAttribute(attributeName).map(AttributeSchema.class::cast).orElse(null);
final Function<String, Stream<SortableAttributeCompoundSchema>> compoundSchemaRetriever = attributeName -> entitySchema.getSortableAttributeCompoundsForAttribute(attributeName).stream().map(SortableAttributeCompoundSchema.class::cast);
entity.getAttributeValues()
.stream()
.filter(Droppable::exists)
.forEach(
attributeValue -> {
final AttributeKey key = attributeValue.key();
Expand Down Expand Up @@ -763,19 +757,21 @@ private void unindexReferences(
@Nonnull GlobalEntityIndex globalIndex,
@Nonnull EntityExistingDataFactory existingDataSupplierFactory
) {
final Integer epk = entity.getPrimaryKey();
final int epk = entity.getPrimaryKeyOrThrowException();
for (ReferenceContract reference : entity.getReferences()) {
final ReferenceKey referenceKey = reference.getReferenceKey();
if (ReferenceIndexMutator.isIndexedReference(reference, scope)) {
final EntityIndexKey referencedTypeIndexKey = getReferencedTypeIndexKey(referenceKey.referenceName(), scope);
final ReferencedTypeEntityIndex referenceTypeIndex = (ReferencedTypeEntityIndex) getOrCreateIndex(referencedTypeIndexKey);
final EntityIndex mainReferenceIndex = ReferenceIndexMutator.getOrCreateReferencedEntityIndex(this, referenceKey, scope);
ReferenceIndexMutator.referenceRemoval(
epk, entitySchema, this,
globalIndex, referenceTypeIndex, mainReferenceIndex, referenceKey,
existingDataSupplierFactory,
this.undoActionsAppender
);
if (reference.exists()) {
final ReferenceKey referenceKey = reference.getReferenceKey();
if (ReferenceIndexMutator.isIndexedReference(reference, scope)) {
final EntityIndexKey referencedTypeIndexKey = getReferencedTypeIndexKey(referenceKey.referenceName(), scope);
final ReferencedTypeEntityIndex referenceTypeIndex = (ReferencedTypeEntityIndex) getOrCreateIndex(referencedTypeIndexKey);
final EntityIndex mainReferenceIndex = ReferenceIndexMutator.getOrCreateReferencedEntityIndex(this, referenceKey, scope);
ReferenceIndexMutator.referenceRemoval(
epk, entitySchema, this,
globalIndex, referenceTypeIndex, mainReferenceIndex, referenceKey,
existingDataSupplierFactory,
this.undoActionsAppender
);
}
}
}
}
Expand Down Expand Up @@ -805,6 +801,7 @@ private void unindexAllPrices(
this.undoActionsAppender
);
priceSupplier.getExistingPrices()
.filter(Droppable::exists)
.forEach(
price -> {
// first remove from reduced indexes, because they consult the super index
Expand All @@ -828,7 +825,7 @@ private void unindexAllPrices(
*/
private void addEntityToIndexes(@Nonnull Entity entity, @Nonnull Scope scope) {
final GlobalEntityIndex globalIndex = (GlobalEntityIndex) getOrCreateIndex(new EntityIndexKey(EntityIndexType.GLOBAL, scope));
final Integer entityPrimaryKey = entity.getPrimaryKey();
final int entityPrimaryKey = entity.getPrimaryKeyOrThrowException();
final EntitySchemaContract entitySchema = entity.getSchema();
final EntityExistingDataFactory existingDataSupplierFactory = new EntityExistingDataFactory(entity);
// add entity from to the global index
Expand Down Expand Up @@ -874,7 +871,7 @@ private void indexAllLocales(
@Nonnull GlobalEntityIndex globalIndex,
@Nonnull EntityExistingDataFactory existingDataSupplierFactory
) {
final int epk = entity.getPrimaryKey();
final int epk = entity.getPrimaryKeyOrThrowException();
for (Locale locale : entity.getLocales()) {
upsertEntityLanguageInTargetIndex(
epk, locale, globalIndex, entitySchema, existingDataSupplierFactory, this.undoActionsAppender
Expand Down Expand Up @@ -904,6 +901,8 @@ private void indexAllGlobalAttributes(
final Function<String, AttributeSchema> attributeSchemaRetriever = attributeName -> entitySchema.getAttribute(attributeName).map(AttributeSchema.class::cast).orElse(null);
final Function<String, Stream<SortableAttributeCompoundSchema>> compoundSchemaRetriever = attributeName -> entitySchema.getSortableAttributeCompoundsForAttribute(attributeName).stream().map(SortableAttributeCompoundSchema.class::cast);
entity.getAttributeValues()
.stream()
.filter(Droppable::exists)
.forEach(
attributeValue -> {
final AttributeKey key = attributeValue.key();
Expand All @@ -914,7 +913,7 @@ private void indexAllGlobalAttributes(
ExistingAttributeValueSupplier.NO_EXISTING_VALUE_SUPPLIER,
index,
key,
attributeValue.value(),
attributeValue.valueOrThrowException(),
updateGlobalIndex,
true,
this.undoActionsAppender
Expand Down Expand Up @@ -945,7 +944,7 @@ private void indexHierarchyPlacement(
setParent(
this,
globalIndex,
entity.getPrimaryKey(),
entity.getPrimaryKeyOrThrowException(),
entity.getParent().getAsInt(),
this.undoActionsAppender
);
Expand Down Expand Up @@ -982,6 +981,7 @@ private void indexAllPrices(
this.undoActionsAppender
);
priceSupplier.getExistingPrices()
.filter(Droppable::exists)
.forEach(
price -> {
priceUpsertOperation.accept(globalIndex, price);
Expand Down Expand Up @@ -1014,31 +1014,33 @@ private void indexAllReferences(
@Nonnull GlobalEntityIndex globalIndex,
@Nonnull EntityExistingDataFactory existingDataSupplierFactory
) {
final int epk = entity.getPrimaryKey();
final int epk = entity.getPrimaryKeyOrThrowException();
for (ReferenceContract reference : entity.getReferences()) {
final ReferenceKey referenceKey = reference.getReferenceKey();
if (ReferenceIndexMutator.isIndexedReference(reference, scope)) {
final EntityIndexKey referencedTypeIndexKey = getReferencedTypeIndexKey(referenceKey.referenceName(), scope);
final ReferencedTypeEntityIndex referenceTypeIndex = (ReferencedTypeEntityIndex) getOrCreateIndex(referencedTypeIndexKey);
final EntityIndex mainReferenceIndex = ReferenceIndexMutator.getOrCreateReferencedEntityIndex(this, referenceKey, scope);
ReferenceIndexMutator.referenceInsert(
epk, entitySchema, this,
globalIndex, referenceTypeIndex, mainReferenceIndex, referenceKey,
existingDataSupplierFactory,
this.undoActionsAppender
);
if (ReferenceIndexMutator.isFacetedReference(scope, referenceKey, this)) {
for (ReferenceContract otherReferences : entity.getReferences()) {
if (!referenceKey.equals(otherReferences.getReferenceKey())) {
final EntityIndex referenceIndex = ReferenceIndexMutator.getOrCreateReferencedEntityIndex(this, referenceKey, scope);
ReferenceIndexMutator.addFacetToIndex(
referenceIndex,
referenceKey,
reference.getGroup().filter(Droppable::exists).map(GroupEntityReference::getPrimaryKey).orElse(null),
this,
epk,
this.undoActionsAppender
);
if (reference.exists()) {
final ReferenceKey referenceKey = reference.getReferenceKey();
if (ReferenceIndexMutator.isIndexedReference(reference, scope)) {
final EntityIndexKey referencedTypeIndexKey = getReferencedTypeIndexKey(referenceKey.referenceName(), scope);
final ReferencedTypeEntityIndex referenceTypeIndex = (ReferencedTypeEntityIndex) getOrCreateIndex(referencedTypeIndexKey);
final EntityIndex mainReferenceIndex = ReferenceIndexMutator.getOrCreateReferencedEntityIndex(this, referenceKey, scope);
ReferenceIndexMutator.referenceInsert(
epk, entitySchema, this,
globalIndex, referenceTypeIndex, mainReferenceIndex, referenceKey,
existingDataSupplierFactory,
this.undoActionsAppender
);
if (ReferenceIndexMutator.isFacetedReference(scope, referenceKey, this)) {
for (ReferenceContract otherReferences : entity.getReferences()) {
if (!referenceKey.equals(otherReferences.getReferenceKey())) {
final EntityIndex referenceIndex = ReferenceIndexMutator.getOrCreateReferencedEntityIndex(this, referenceKey, scope);
ReferenceIndexMutator.addFacetToIndex(
referenceIndex,
referenceKey,
reference.getGroup().filter(Droppable::exists).map(GroupEntityReference::getPrimaryKey).orElse(null),
this,
epk,
this.undoActionsAppender
);
}
}
}
}
Expand Down

0 comments on commit ff59da5

Please sign in to comment.