Skip to content

Commit

Permalink
repo-sqale: ExtItemCache/raw mode code cleanup, index added
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Aug 24, 2021
1 parent b580077 commit 0ad826c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 49 deletions.
4 changes: 4 additions & 0 deletions repo/repo-sqale/sql/pgnew-repo.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,10 @@ CREATE TABLE m_ext_item (
-- information about storage mechanism (JSON common/separate, column, table separate/common, etc.)
-- storageType JSONB NOT NULL default '{"type": "EXT_JSON"}', -- currently only JSONB is used
);

-- This works fine for itemName+holderType search used in raw processing
ALTER TABLE m_ext_item ADD CONSTRAINT m_ext_item_key
UNIQUE (itemName, holderType, valueType, cardinality);
-- endregion

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem;
import com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem;
Expand All @@ -27,12 +28,11 @@ public class ExtItemCache {

private static final Trace LOGGER = TraceManager.getTrace(ExtItemCache.class);

// TODO: id->ext item will be used for index only
// TODO: id->ext item will be used for index-only extension attributes (when reading them we know only ID)
private final Map<Integer, MExtItem> idToExtItem = new ConcurrentHashMap<>();
private final Map<MExtItem.Key, MExtItem> keyToExtItem = new ConcurrentHashMap<>();
private final Map<MExtItem.ItemNameKey, MExtItem> itemNameToExtItem = new ConcurrentHashMap<>();


private Supplier<JdbcSession> jdbcSessionSupplier;

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ private void updateMaps(MExtItem row) {
return extItem;
}

public @NotNull MExtItem resolveExtensionItem(@NotNull MExtItem.ItemNameKey extItemKey) {
public @Nullable MExtItem getExtensionItem(@NotNull MExtItem.ItemNameKey extItemKey) {
if (jdbcSessionSupplier == null) {
throw new IllegalStateException("Ext item cache was not initialized yet!");
}
Expand All @@ -111,14 +111,14 @@ private void updateMaps(MExtItem row) {
.newQuery()
.from(QExtItem.DEFAULT)
.select(QExtItem.DEFAULT)
.where(QExtItem.DEFAULT.itemName.eq(extItemKey.itemName).and(QExtItem.DEFAULT.holderType.eq(extItemKey.holderType)))
.where(QExtItem.DEFAULT.itemName.eq(extItemKey.itemName)
.and(QExtItem.DEFAULT.holderType.eq(extItemKey.holderType)))
// TODO let's consider fetchOne that throws if count > 1, right now we're not confident enough to do so.
.fetchFirst();

if (extItem != null) {
updateMaps(extItem);
}
return extItem;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.querydsl.sql.types.EnumAsObjectType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.repo.sqale.jsonb.QuerydslJsonbType;
import com.evolveum.midpoint.repo.sqale.qmodel.common.MContainerType;
Expand Down Expand Up @@ -134,7 +135,7 @@ public QName resolveUriIdToQName(Integer uriId) {
return extItemCache.resolveExtensionItem(extItemKey);
}

public @NotNull MExtItem resolveExtensionItem(@NotNull MExtItem.ItemNameKey extItemKey) {
return extItemCache.resolveExtensionItem(extItemKey);
public @Nullable MExtItem getExtensionItem(@NotNull MExtItem.ItemNameKey extItemKey) {
return extItemCache.getExtensionItem(extItemKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,24 @@ public int hashCode() {
public static class ItemNameKey {
public String itemName;
public MExtItemHolderType holderType;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((holderType == null) ? 0 : holderType.hashCode());
result = prime * result + ((itemName == null) ? 0 : itemName.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
public boolean equals(Object o) {
if (this == o) {
return true;
if (!(obj instanceof ItemNameKey))
return false;
ItemNameKey other = (ItemNameKey) obj;
if (holderType != other.holderType)
return false;
if (itemName == null) {
if (other.itemName != null)
return false;
} else if (!itemName.equals(other.itemName))
}
if (o == null || getClass() != o.getClass()) {
return false;
return true;
}
ItemNameKey key = (ItemNameKey) o;

return Objects.equals(itemName, key.itemName)
&& holderType == key.holderType;
}

@Override
public int hashCode() {
return Objects.hash(itemName, holderType);
}
}

Expand All @@ -121,6 +115,13 @@ public static Key keyFrom(ItemDefinition<?> definition, MExtItemHolderType holde
return key;
}

public static @NotNull ItemNameKey itemNameKey(ItemName elementName, MExtItemHolderType type) {
ItemNameKey ret = new ItemNameKey();
ret.itemName = QNameUtil.qNameToUri(elementName);
ret.holderType = type;
return ret;
}

@Override
public String toString() {
return "MExtItem{" +
Expand All @@ -131,11 +132,4 @@ public String toString() {
", cardinality=" + cardinality +
'}';
}

public static @NotNull MExtItem.ItemNameKey itemNameKey(ItemName elementName, MExtItemHolderType type) {
ItemNameKey ret = new ItemNameKey();
ret.itemName = QNameUtil.qNameToUri(elementName);
ret.holderType = type;
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@

import java.util.Collection;
import java.util.Objects;

import javax.xml.namespace.QName;

import com.querydsl.core.Tuple;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.MutableItemDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.repo.sqale.ExtUtils;
import com.evolveum.midpoint.repo.sqale.SqaleRepoContext;
Expand All @@ -37,7 +33,6 @@
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.querydsl.core.Tuple;

/**
* Mapping between {@link QShadow} and {@link ShadowType}.
Expand Down Expand Up @@ -134,18 +129,16 @@ public ShadowType toSchemaObject(Tuple row, QShadow entityPath,
// FIXME: we store it because provisioning now sends it to repo, but it should be transient
shadowType.asPrismObject().removeContainer(ShadowType.F_ASSOCIATION);


GetOperationOptions rootOptions = SelectorOptions.findRootOptions(options);
if (GetOperationOptions.isRaw(rootOptions)) {
// If raw=true, we populate attributes with types cached in repository
applyShadowAttributesDefinitions(shadowType);
}


return shadowType;
}

@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
private void applyShadowAttributesDefinitions(ShadowType shadowType) throws SchemaException {
if (shadowType.getAttributes() == null) {
return;
Expand All @@ -154,12 +147,12 @@ private void applyShadowAttributesDefinitions(ShadowType shadowType) throws Sche

for (Item<?, ?> attribute : attributesOld.getItems()) {
ItemName itemName = attribute.getElementName();
MExtItem itemInfo = repositoryContext().resolveExtensionItem(MExtItem.itemNameKey(attribute.getElementName(), MExtItemHolderType.ATTRIBUTES));
MExtItem itemInfo = repositoryContext().getExtensionItem(
MExtItem.itemNameKey(attribute.getElementName(), MExtItemHolderType.ATTRIBUTES));
if (itemInfo != null && attribute.getDefinition() == null) {
((Item) attribute).applyDefinition(definitionFrom(itemName, itemInfo), true);
}
}

}

private ItemDefinition<?> definitionFrom(QName name, MExtItem itemInfo) {
Expand All @@ -176,6 +169,4 @@ private ItemDefinition<?> definitionFrom(QName name, MExtItem itemInfo) {
def.setDynamic(true);
return def;
}


}

0 comments on commit 0ad826c

Please sign in to comment.