Skip to content

Commit

Permalink
repo-sqale: added modify support for Shadow.pendingOperations (count)
Browse files Browse the repository at this point in the history
Implemented in CountItemDeltaProcessor which was the third class using
the same code in process(), so FinalValueDeltaProcessor was added and
JsonbPolysItemDeltaProcessor and ArrayItemDeltaProcessor are all
subclasses implementing only setRealValues() and delete().
SimpleItemDeltaProcessor was removed, adds nothing to its superclass
SinglePathItemDeltaProcessor which works just as well.
  • Loading branch information
virgo47 committed Jul 22, 2021
1 parent bd8c934 commit 0797cd3
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 86 deletions.
4 changes: 1 addition & 3 deletions repo/repo-sqale/sql/pgnew-repo.sql
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ CREATE TABLE m_shadow (
dead BOOLEAN,
exist BOOLEAN,
fullSynchronizationTimestamp TIMESTAMPTZ,
pendingOperationCount INTEGER,
pendingOperationCount INTEGER NOT NULL,
primaryIdentifierValue TEXT,
-- status INTEGER, TODO how is this mapped? See RUtil.copyResultFromJAXB called from RTask and OperationResultMapper
synchronizationSituation SynchronizationSituationType,
Expand All @@ -810,8 +810,6 @@ CREATE TABLE m_shadow (
)
INHERITS (m_object);

-- TODO not partitioned yet, discriminator columns probably can't be NULL

CREATE TRIGGER m_shadow_oid_insert_tr BEFORE INSERT ON m_shadow
FOR EACH ROW EXECUTE PROCEDURE insert_object_oid();
CREATE TRIGGER m_shadow_update_tr BEFORE UPDATE ON m_shadow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@
import com.querydsl.core.types.dsl.ArrayPath;
import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.repo.sqale.delta.ItemDeltaValueProcessor;
import com.evolveum.midpoint.repo.sqale.update.SqaleUpdateContext;
import com.evolveum.midpoint.repo.sqlbase.RepositoryException;
import com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase;
import com.evolveum.midpoint.util.exception.SchemaException;

/**
* Filter processor for multi-value property represented by single array column.
* Delta processor for multi-value property represented by single array column.
*
* @param <V> type of value in schema
* @param <E> type of element in DB (can be the same like `V`)
*/
public class ArrayItemDeltaProcessor<V, E> extends ItemDeltaValueProcessor<E> {
public class ArrayItemDeltaProcessor<V, E> extends FinalValueDeltaProcessor<E> {

private final ArrayPath<E[], E> path;
private final Class<E> elementType;
Expand All @@ -52,19 +46,6 @@ public <Q extends FlexibleRelationalPathBase<R>, R> ArrayItemDeltaProcessor(
this.conversionFunction = conversionFunction;
}

@Override
public void process(ItemDelta<?, ?> modification) throws RepositoryException, SchemaException {
Item<PrismValue, ?> item = context.findItem(modification.getPath());
Collection<?> realValues = item != null ? item.getRealValues() : null;

if (realValues == null || realValues.isEmpty()) {
delete();
} else {
// Whatever the operation is, we just set the new value here.
setRealValues(realValues);
}
}

@Override
public void setRealValues(Collection<?> values) {
//noinspection unchecked
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.repo.sqale.delta.item;

import java.util.Collection;
import java.util.function.Function;

import com.querydsl.core.types.dsl.NumberPath;

import com.evolveum.midpoint.repo.sqale.update.SqaleUpdateContext;
import com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase;

/**
* Delta processor for columns storing size of multi-value item.
*
* @param <T> expected type of the real value, but we don't care in this class
*/
public class CountItemDeltaProcessor<T> extends FinalValueDeltaProcessor<T> {

private final NumberPath<Integer> path;

/**
* @param <Q> entity query type from which the attribute is resolved
* @param <R> row type related to {@link Q}
*/
public <Q extends FlexibleRelationalPathBase<R>, R> CountItemDeltaProcessor(
SqaleUpdateContext<?, Q, R> context,
Function<Q, NumberPath<Integer>> rootToQueryItem) {
super(context);
this.path = rootToQueryItem.apply(context.entityPath());
}

@Override
public void setRealValues(Collection<?> values) {
context.set(path, values.size());
}

@Override
public void delete() {
context.set(path, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.repo.sqale.delta.item;

import java.util.Collection;

import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.repo.sqale.delta.ItemDeltaValueProcessor;
import com.evolveum.midpoint.repo.sqale.update.SqaleUpdateContext;
import com.evolveum.midpoint.repo.sqlbase.RepositoryException;
import com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase;
import com.evolveum.midpoint.util.exception.SchemaException;

/**
* Common delta processor logic for storing based on the final value of the property.
* When the delta processor is run, the modification has been already applied to the prism object.
* This is typically used for storing multi-value item in a single column.
* This may not be efficient when the amount of values is too big, but it has to be really big
* to be of consequence (my wild guess is 100-1000 can be still OK).
*
* Subclasses have to implement {@link #setRealValues(Collection)} and {@link #delete()} methods.
*
* @param <T> expected type of the real value for the modification (after optional conversion)
*/
public abstract class FinalValueDeltaProcessor<T> extends ItemDeltaValueProcessor<T> {

/**
* @param <Q> entity query type from which the attribute is resolved
* @param <R> row type related to {@link Q}
*/
public <Q extends FlexibleRelationalPathBase<R>, R> FinalValueDeltaProcessor(
SqaleUpdateContext<?, Q, R> context) {
super(context);
}

@Override
public void process(ItemDelta<?, ?> modification) throws RepositoryException, SchemaException {
Item<PrismValue, ?> item = context.findItem(modification.getPath());
Collection<?> realValues = item != null ? item.getRealValues() : null;

if (realValues == null || realValues.isEmpty()) {
delete();
} else {
// Whatever the operation is, we just set the new value here.
setRealValues(realValues);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@
import java.util.Collection;
import java.util.function.Function;

import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.sqale.delta.ItemDeltaValueProcessor;
import com.evolveum.midpoint.repo.sqale.jsonb.JsonbPath;
import com.evolveum.midpoint.repo.sqale.jsonb.JsonbUtils;
import com.evolveum.midpoint.repo.sqale.update.SqaleUpdateContext;
import com.evolveum.midpoint.repo.sqlbase.RepositoryException;
import com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase;
import com.evolveum.midpoint.util.exception.SchemaException;

/**
* Filter processor for multi-value poly-strings represented as array in JSONB column.
* Delta processor for multi-value poly-strings represented as array in JSONB column.
*/
public class JsonbPolysItemDeltaProcessor extends ItemDeltaValueProcessor<PolyString> {
public class JsonbPolysItemDeltaProcessor extends FinalValueDeltaProcessor<PolyString> {

private final JsonbPath path;

Expand All @@ -39,19 +33,6 @@ public <Q extends FlexibleRelationalPathBase<R>, R> JsonbPolysItemDeltaProcessor
this.path = rootToQueryItem.apply(context.entityPath());
}

@Override
public void process(ItemDelta<?, ?> modification) throws RepositoryException, SchemaException {
Item<PrismValue, ?> item = context.findItem(modification.getPath());
Collection<?> realValues = item != null ? item.getRealValues() : null;

if (realValues == null || realValues.isEmpty()) {
delete();
} else {
// Whatever the operation is, we just set the new value here.
setRealValues(realValues);
}
}

@Override
public void setRealValues(Collection<?> values) {
//noinspection unchecked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public <Q extends FlexibleRelationalPathBase<R>, R> RefItemDeltaProcessor(
* @param <Q> entity query type from which the attribute is resolved
* @param <R> row type related to {@link Q}
*/
// exposed mainly for RefTableItemFilterProcessor
<Q extends FlexibleRelationalPathBase<R>, R> RefItemDeltaProcessor(
private <Q extends FlexibleRelationalPathBase<R>, R> RefItemDeltaProcessor(
SqaleUpdateContext<?, Q, R> context,
UuidPath oidPath, EnumPath<MObjectType> typePath, NumberPath<Integer> relationIdPath) {
super(context);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import java.util.function.Function;

import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.prism.query.ValueFilter;
import com.evolveum.midpoint.repo.sqale.delta.ItemDeltaProcessor;
import com.evolveum.midpoint.repo.sqale.delta.ItemDeltaValueProcessor;
import com.evolveum.midpoint.repo.sqale.update.SqaleUpdateContext;
import com.evolveum.midpoint.repo.sqlbase.QueryException;
import com.evolveum.midpoint.repo.sqlbase.RepositoryException;
import com.evolveum.midpoint.repo.sqlbase.SqlQueryContext;
import com.evolveum.midpoint.repo.sqlbase.filtering.item.ItemValueFilterProcessor;
import com.evolveum.midpoint.repo.sqlbase.mapping.DefaultItemSqlMapper;
Expand Down Expand Up @@ -62,7 +66,14 @@ public SqaleItemSqlMapper(
*/
public SqaleItemSqlMapper(
@NotNull Function<SqaleUpdateContext<S, Q, R>, ItemDeltaValueProcessor<?>> deltaProcessorFactory) {
super(ctx -> null);
super(ctx -> new ItemValueFilterProcessor<>(ctx) {
@Override
public Predicate process(ValueFilter<?, ?> filter) throws RepositoryException {
throw new QueryException(
"Value filter processor not supported for filter: " + filter);
}
});

this.deltaProcessorFactory = Objects.requireNonNull(deltaProcessorFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected ItemSqlMapper<Q, R> stringMapper(
Function<Q, StringPath> rootToQueryItem) {
return new SqaleItemSqlMapper<>(
ctx -> new SimpleItemFilterProcessor<>(ctx, rootToQueryItem),
ctx -> new SimpleItemDeltaProcessor<>(ctx, rootToQueryItem),
ctx -> new SinglePathItemDeltaProcessor<>(ctx, rootToQueryItem),
rootToQueryItem);
}

Expand All @@ -132,7 +132,7 @@ public ItemSqlMapper<Q, R> integerMapper(
Function<Q, NumberPath<Integer>> rootToQueryItem) {
return new SqaleItemSqlMapper<>(
ctx -> new SimpleItemFilterProcessor<>(ctx, rootToQueryItem),
ctx -> new SimpleItemDeltaProcessor<>(ctx, rootToQueryItem),
ctx -> new SinglePathItemDeltaProcessor<>(ctx, rootToQueryItem),
rootToQueryItem);
}

Expand All @@ -144,7 +144,7 @@ protected ItemSqlMapper<Q, R> booleanMapper(
Function<Q, BooleanPath> rootToQueryItem) {
return new SqaleItemSqlMapper<>(
ctx -> new SimpleItemFilterProcessor<>(ctx, rootToQueryItem),
ctx -> new SimpleItemDeltaProcessor<>(ctx, rootToQueryItem),
ctx -> new SinglePathItemDeltaProcessor<>(ctx, rootToQueryItem),
rootToQueryItem);
}

Expand All @@ -155,7 +155,7 @@ protected ItemSqlMapper<Q, R> booleanMapper(
protected ItemSqlMapper<Q, R> uuidMapper(Function<Q, UuidPath> rootToQueryItem) {
return new SqaleItemSqlMapper<>(
ctx -> new SimpleItemFilterProcessor<>(ctx, rootToQueryItem),
ctx -> new SimpleItemDeltaProcessor<>(ctx, rootToQueryItem),
ctx -> new SinglePathItemDeltaProcessor<>(ctx, rootToQueryItem),
rootToQueryItem);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.repo.sqale.SqaleRepoContext;
import com.evolveum.midpoint.repo.sqale.delta.item.CountItemDeltaProcessor;
import com.evolveum.midpoint.repo.sqale.mapping.CountMappingResolver;
import com.evolveum.midpoint.repo.sqale.mapping.SqaleItemSqlMapper;
import com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItemHolderType;
import com.evolveum.midpoint.repo.sqale.qmodel.object.QObjectMapping;
import com.evolveum.midpoint.repo.sqale.qmodel.resource.QResourceMapping;
Expand Down Expand Up @@ -68,6 +70,9 @@ private QShadowMapping(@NotNull SqaleRepoContext repositoryContext) {
timestampMapper(q -> q.synchronizationTimestamp));
addExtensionMapping(F_ATTRIBUTES, MExtItemHolderType.ATTRIBUTES, q -> q.attributes);

// Item mapping to update the count, relation resolver for query with EXISTS filter.
addItemMapping(F_PENDING_OPERATION, new SqaleItemSqlMapper<>(
ctx -> new CountItemDeltaProcessor<>(ctx, q -> q.pendingOperationCount)));
addRelationResolver(F_PENDING_OPERATION,
new CountMappingResolver<>(q -> q.pendingOperationCount));
}
Expand Down

0 comments on commit 0797cd3

Please sign in to comment.