Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed May 3, 2021
2 parents 67b20d2 + 1b2c07c commit 6b48bef
Show file tree
Hide file tree
Showing 109 changed files with 1,753 additions and 2,545 deletions.
Expand Up @@ -18,14 +18,13 @@
import com.evolveum.midpoint.repo.sqlbase.SqlRepoContext;
import com.evolveum.midpoint.repo.sqlbase.filtering.FilterProcessor;
import com.evolveum.midpoint.repo.sqlbase.mapping.QueryTableMapping;
import com.evolveum.midpoint.repo.sqlbase.mapping.SqlTransformer;
import com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase;

public class SqaleQueryContext<S, Q extends FlexibleRelationalPathBase<R>, R>
extends SqlQueryContext<S, Q, R> {

public static <S, Q extends FlexibleRelationalPathBase<R>, R> SqaleQueryContext<S, Q, R> from(
Class<S> schemaType, SqaleTransformerSupport transformerSupport,
Class<S> schemaType, SqaleSupportService sqaleSupportService,
SqlRepoContext sqlRepoContext) {

SqaleTableMapping<S, Q, R> rootMapping = sqlRepoContext.getMappingBySchemaType(schemaType);
Expand All @@ -36,21 +35,16 @@ public static <S, Q extends FlexibleRelationalPathBase<R>, R> SqaleQueryContext<
query.getMetadata().setValidate(true);

return new SqaleQueryContext<>(
rootPath, rootMapping, transformerSupport, sqlRepoContext, query);
rootPath, rootMapping, sqaleSupportService, sqlRepoContext, query);
}

private SqaleQueryContext(
Q entityPath,
SqaleTableMapping<S, Q, R> mapping,
SqaleTransformerSupport transformerSupport,
SqaleSupportService sqaleSupportService,
SqlRepoContext sqlRepoContext,
SQLQuery<?> query) {
super(entityPath, mapping, sqlRepoContext, transformerSupport, query);
}

@Override
protected SqlTransformer<S, Q, R> createTransformer() {
return entityPathMapping.createTransformer(transformerSupport);
super(entityPath, mapping, sqaleSupportService, query);
}

@Override
Expand All @@ -59,7 +53,7 @@ public FilterProcessor<InOidFilter> createInOidFilter(SqlQueryContext<?, ?, ?> c
}

public @NotNull Integer searchCachedRelationId(QName qName) {
return transformerSupport().searchCachedRelationId(qName);
return supportService().searchCachedRelationId(qName);
}

/**
Expand All @@ -72,10 +66,10 @@ public FilterProcessor<InOidFilter> createInOidFilter(SqlQueryContext<?, ?, ?> c
deriveNew(TQ newPath, QueryTableMapping<TS, TQ, TR> newMapping) {
return (SqlQueryContext<TS, TQ, TR>) new SqaleQueryContext(
newPath, (SqaleTableMapping<?, ?, ?>) newMapping,
transformerSupport(), sqlRepoContext, sqlQuery);
supportService(), sqlRepoContext, sqlQuery);
}

private SqaleTransformerSupport transformerSupport() {
return (SqaleTransformerSupport) transformerSupport;
private SqaleSupportService supportService() {
return (SqaleSupportService) sqlSupportService;
}
}
Expand Up @@ -20,6 +20,7 @@
import com.evolveum.midpoint.repo.sqlbase.SqlRepoContext;
import com.evolveum.midpoint.repo.sqlbase.mapping.QueryModelMappingRegistry;
import com.evolveum.midpoint.repo.sqlbase.querydsl.QuerydslJsonbType;
import com.evolveum.midpoint.schema.SchemaService;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

/**
Expand All @@ -32,8 +33,9 @@ public class SqaleRepoContext extends SqlRepoContext {
public SqaleRepoContext(
JdbcRepositoryConfiguration jdbcRepositoryConfiguration,
DataSource dataSource,
SchemaService schemaService,
QueryModelMappingRegistry mappingRegistry) {
super(jdbcRepositoryConfiguration, dataSource, mappingRegistry);
super(jdbcRepositoryConfiguration, dataSource, schemaService, mappingRegistry);

// each enum type must be registered if we want to map it as objects (to PG enum types)
querydslConfig.register(new EnumAsObjectType<>(ActivationStatusType.class));
Expand Down
Expand Up @@ -110,6 +110,7 @@ public DataSource dataSource(DataSourceFactory dataSourceFactory)
@Bean
public SqaleRepoContext sqlRepoContext(
SqaleRepositoryConfiguration repositoryConfiguration,
SchemaService schemaService,
DataSource dataSource) {
QueryModelMappingRegistry mappingRegistry = new QueryModelMappingRegistry()
// ordered alphabetically here, mappings without schema type at the end
Expand Down Expand Up @@ -152,7 +153,11 @@ public SqaleRepoContext sqlRepoContext(
.register(QReferenceMapping.INSTANCE)
.seal();

return new SqaleRepoContext(repositoryConfiguration, dataSource, mappingRegistry);
return new SqaleRepoContext(
repositoryConfiguration,
dataSource,
schemaService,
mappingRegistry);
}

@Bean
Expand Down
Expand Up @@ -54,9 +54,7 @@

/**
* Repository implementation based on SQL, JDBC and Querydsl without any ORM.
* WORK IN PROGRESS:
* It will be PostgreSQL only or at least PG optimized with generic SQL support for other unsupported DB.
* Possible Oracle support is in play.
* WORK IN PROGRESS.
*/
public class SqaleRepositoryService implements RepositoryService {

Expand All @@ -74,7 +72,7 @@ public class SqaleRepositoryService implements RepositoryService {
private final SqaleRepoContext sqlRepoContext;
private final SchemaService schemaService;
private final SqlQueryExecutor sqlQueryExecutor;
private final SqaleTransformerSupport transformerSupport;
private final SqaleSupportService supportService;
private final SqlPerformanceMonitorsCollection sqlPerformanceMonitorsCollection;

// TODO: see comment in the SystemConfigurationChangeDispatcherImpl for related issues
Expand All @@ -92,7 +90,7 @@ public SqaleRepositoryService(
this.sqlRepoContext = sqlRepoContext;
this.schemaService = schemaService;
this.sqlQueryExecutor = new SqlQueryExecutor(sqlRepoContext);
this.transformerSupport = new SqaleTransformerSupport(schemaService, sqlRepoContext);
this.supportService = new SqaleSupportService(schemaService, sqlRepoContext);
this.sqlPerformanceMonitorsCollection = sqlPerformanceMonitorsCollection;

// monitor initialization and registration
Expand Down Expand Up @@ -184,8 +182,7 @@ private <S extends ObjectType> S readByOid(
throw new ObjectNotFoundException(schemaType, oid.toString());
}

return rootMapping.createTransformer(transformerSupport)
.toSchemaObject(result, root, options);
return rootMapping.toSchemaObject(result, root, options);
}

@Override
Expand Down Expand Up @@ -258,7 +255,7 @@ public <T extends ObjectType> String addObject(
// TODO use executeAttempts

String oid = new AddObjectOperation<>(object, options, operationResult)
.execute(transformerSupport);
.execute(supportService);
invokeConflictWatchers((w) -> w.afterAddObject(oid, object));
return oid;
/*
Expand Down Expand Up @@ -416,8 +413,7 @@ RootUpdateContext<S, Q, R> prepareUpdateContext(
throw new ObjectNotFoundException(schemaType, oid.toString());
}

S object = rootMapping.createTransformer(transformerSupport)
.toSchemaObject(result, root, Collections.emptyList());
S object = rootMapping.toSchemaObject(result, root, Collections.emptyList());

R rootRow = rootMapping.newRowObject();
rootRow.oid = oid;
Expand All @@ -426,7 +422,7 @@ RootUpdateContext<S, Q, R> prepareUpdateContext(
rootRow.objectType = MObjectType.fromSchemaType(object.getClass());
// we don't care about full object in row

return new RootUpdateContext<>(transformerSupport, jdbcSession, object, rootRow);
return new RootUpdateContext<>(supportService, jdbcSession, object, rootRow);
}

private void logTraceModifications(@NotNull Collection<? extends ItemDelta<?, ?>> modifications) {
Expand Down Expand Up @@ -484,8 +480,7 @@ private void logTraceModifications(@NotNull Collection<? extends ItemDelta<?, ?>
DeleteObjectResult deleteObjectAttempt(Class<T> type, UUID oid, JdbcSession jdbcSession)
throws ObjectNotFoundException {

QueryTableMapping<T, Q, R> mapping =
transformerSupport.sqlRepoContext().getMappingBySchemaType(type);
QueryTableMapping<T, Q, R> mapping = sqlRepoContext.getMappingBySchemaType(type);
Q entityPath = mapping.defaultAlias();
byte[] fullObject = jdbcSession.newQuery()
.select(entityPath.fullObject)
Expand Down Expand Up @@ -522,7 +517,7 @@ public <T extends ObjectType> int countObjects(Class<T> type, ObjectQuery query,
.build();

try {
var queryContext = SqaleQueryContext.from(type, transformerSupport, sqlRepoContext);
var queryContext = SqaleQueryContext.from(type, supportService, sqlRepoContext);
return sqlQueryExecutor.count(queryContext, query, options);
} catch (RepositoryException | RuntimeException e) {
throw handledGeneralException(e, operationResult);
Expand Down Expand Up @@ -550,7 +545,7 @@ public <T extends ObjectType> int countObjects(Class<T> type, ObjectQuery query,
.build();

try {
var queryContext = SqaleQueryContext.from(type, transformerSupport, sqlRepoContext);
var queryContext = SqaleQueryContext.from(type, supportService, sqlRepoContext);
SearchResultList<T> result =
sqlQueryExecutor.list(queryContext, query, options);
// TODO see the commented code from old repo lower, problems for each object must be caught
Expand All @@ -569,7 +564,7 @@ public <T extends ObjectType> int countObjects(Class<T> type, ObjectQuery query,

/*
TODO from ObjectRetriever, how to do this per-object Throwable catch + record result?
should we smuggle the OperationResult all the way to the transformer call?
should we smuggle the OperationResult all the way to the mapping call?
@NotNull
private <T extends ObjectType> List<PrismObject<T>> queryResultToPrismObjects(
List<T> objects, Class<T> type,
Expand Down Expand Up @@ -633,7 +628,7 @@ public <T extends Containerable> SearchResultList<T> searchContainers(
.build();

try {
var queryContext = SqaleQueryContext.from(type, transformerSupport, sqlRepoContext);
var queryContext = SqaleQueryContext.from(type, supportService, sqlRepoContext);
SearchResultList<T> result =
sqlQueryExecutor.list(queryContext, query, options);
return result;
Expand Down
Expand Up @@ -11,17 +11,24 @@
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.repo.sqale.qmodel.common.QUri;
import com.evolveum.midpoint.repo.sqlbase.SqlTransformerSupport;
import com.evolveum.midpoint.repo.sqlbase.SqlSupportService;
import com.evolveum.midpoint.schema.SchemaService;
import com.evolveum.midpoint.util.QNameUtil;

/**
* Extension of {@link SqlTransformerSupport} adding Sqale features like {@link UriCache} support.
* Extension of {@link SqlSupportService} adding Sqale features like {@link UriCache} support.
*/
public class SqaleTransformerSupport extends SqlTransformerSupport {
public class SqaleSupportService extends SqlSupportService {

public SqaleTransformerSupport(SchemaService schemaService, SqaleRepoContext sqaleRepoContext) {
private static SqaleSupportService instance;

public SqaleSupportService(SchemaService schemaService, SqaleRepoContext sqaleRepoContext) {
super(schemaService, sqaleRepoContext);
instance = this;
}

public static SqaleSupportService getInstance() {
return instance;
}

private SqaleRepoContext sqaleRepoContext() {
Expand Down
Expand Up @@ -46,7 +46,7 @@ public void addValues(Collection<Referencable> values) {
public void deleteValues(Collection<Referencable> values) {
Q r = refTableMapping.defaultAlias();
for (Referencable ref : values) {
Integer relId = context.transformerSupport().searchCachedRelationId(ref.getRelation());
Integer relId = context.supportService().searchCachedRelationId(ref.getRelation());
context.jdbcSession().newDelete(r)
.where(r.isOwnedBy(context.row())
.and(r.targetOid.eq(UUID.fromString(ref.getOid())))
Expand Down
Expand Up @@ -18,12 +18,11 @@
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.repo.api.RepoAddOptions;
import com.evolveum.midpoint.repo.sqale.ContainerValueIdGenerator;
import com.evolveum.midpoint.repo.sqale.SqaleTransformerSupport;
import com.evolveum.midpoint.repo.sqale.qmodel.SqaleTableMapping;
import com.evolveum.midpoint.repo.sqale.SqaleSupportService;
import com.evolveum.midpoint.repo.sqale.qmodel.object.MObject;
import com.evolveum.midpoint.repo.sqale.qmodel.object.MObjectType;
import com.evolveum.midpoint.repo.sqale.qmodel.object.ObjectSqlTransformer;
import com.evolveum.midpoint.repo.sqale.qmodel.object.QObject;
import com.evolveum.midpoint.repo.sqale.qmodel.object.QObjectMapping;
import com.evolveum.midpoint.repo.sqlbase.JdbcSession;
import com.evolveum.midpoint.repo.sqlbase.SqlRepoContext;
import com.evolveum.midpoint.schema.result.OperationResult;
Expand All @@ -49,7 +48,7 @@ public class AddObjectOperation<S extends ObjectType, Q extends QObject<R>, R ex

private SqlRepoContext sqlRepoContext;
private Q root;
private ObjectSqlTransformer<S, Q, R> transformer;
private QObjectMapping<S, Q, R> rootMapping;
private MObjectType objectType;

public AddObjectOperation(@NotNull PrismObject<S> object,
Expand All @@ -60,18 +59,15 @@ public AddObjectOperation(@NotNull PrismObject<S> object,
}

/** Inserts the object provided to the constructor and returns its OID. */
public String execute(SqaleTransformerSupport transformerSupport)
public String execute(SqaleSupportService supportService)
throws SchemaException, ObjectAlreadyExistsException {
try {
// TODO utilize options and result
sqlRepoContext = transformerSupport.sqlRepoContext();
sqlRepoContext = supportService.sqlRepoContext();
Class<S> schemaObjectClass = object.getCompileTimeClass();
objectType = MObjectType.fromSchemaType(schemaObjectClass);
SqaleTableMapping<S, Q, R> rootMapping =
sqlRepoContext.getMappingBySchemaType(schemaObjectClass);
rootMapping = sqlRepoContext.getMappingBySchemaType(schemaObjectClass);
root = rootMapping.defaultAlias();
transformer = (ObjectSqlTransformer<S, Q, R>)
rootMapping.createTransformer(transformerSupport);

// we don't want CID generation here, because overwrite works different then normal add

Expand Down Expand Up @@ -100,17 +96,17 @@ private String addObjectWithOid() throws SchemaException {
long lastCid = new ContainerValueIdGenerator().generateForNewObject(object);
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
S schemaObject = object.asObjectable();
R row = transformer.toRowObjectWithoutFullObject(schemaObject, jdbcSession);
R row = rootMapping.toRowObjectWithoutFullObject(schemaObject, jdbcSession);
row.containerIdSeq = lastCid + 1;
transformer.setFullObject(row, schemaObject);
rootMapping.setFullObject(row, schemaObject);

UUID oid = jdbcSession.newInsert(root)
// default populate mapper ignores null, that's good, especially for objectType
.populate(row)
.executeWithKey(root.oid);

row.objectType = objectType; // sub-entities can use it, now it's safe to set it
transformer.storeRelatedEntities(row, schemaObject, jdbcSession);
rootMapping.storeRelatedEntities(row, schemaObject, jdbcSession);

jdbcSession.commit();
return Objects.requireNonNull(oid, "OID of inserted object can't be null")
Expand All @@ -121,7 +117,7 @@ private String addObjectWithOid() throws SchemaException {
private String addObjectWithoutOid() throws SchemaException {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
S schemaObject = object.asObjectable();
R row = transformer.toRowObjectWithoutFullObject(schemaObject, jdbcSession);
R row = rootMapping.toRowObjectWithoutFullObject(schemaObject, jdbcSession);

// first insert without full object, because we don't know the OID yet
UUID oid = jdbcSession.newInsert(root)
Expand All @@ -136,7 +132,7 @@ private String addObjectWithoutOid() throws SchemaException {
long lastCid = new ContainerValueIdGenerator().generateForNewObject(object);

// now to update full object with known OID
transformer.setFullObject(row, schemaObject);
rootMapping.setFullObject(row, schemaObject);
jdbcSession.newUpdate(root)
.set(root.fullObject, row.fullObject)
.set(root.containerIdSeq, lastCid + 1)
Expand All @@ -145,7 +141,7 @@ private String addObjectWithoutOid() throws SchemaException {

row.oid = oid;
row.objectType = objectType; // sub-entities can use it, now it's safe to set it
transformer.storeRelatedEntities(row, schemaObject, jdbcSession);
rootMapping.storeRelatedEntities(row, schemaObject, jdbcSession);

jdbcSession.commit();
return oidString;
Expand Down

This file was deleted.

0 comments on commit 6b48bef

Please sign in to comment.