Search in sources :

Example 16 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class ExtItemCache method resolveExtensionItem.

@NotNull
public synchronized MExtItem resolveExtensionItem(@NotNull MExtItem.Key extItemKey) {
    if (jdbcSessionSupplier == null) {
        throw new IllegalStateException("Ext item cache was not initialized yet!");
    }
    MExtItem extItem = keyToExtItem.get(extItemKey);
    if (extItem != null) {
        return extItem;
    }
    QExtItem ei = QExtItem.DEFAULT;
    try (JdbcSession jdbcSession = jdbcSessionSupplier.get().startTransaction()) {
        Integer id = jdbcSession.newInsert(ei).set(ei.itemName, extItemKey.itemName).set(ei.valueType, extItemKey.valueType).set(ei.holderType, extItemKey.holderType).set(ei.cardinality, extItemKey.cardinality).executeWithKey(ei.id);
        jdbcSession.commit();
        extItem = MExtItem.of(id, extItemKey);
        updateMaps(extItem);
    } catch (RuntimeException e) {
        if (SqaleUtils.isUniqueConstraintViolation(e)) {
            extItem = retrieveFromDb(extItemKey);
        } else {
            throw e;
        }
    }
    LOGGER.debug("Ext item cache row inserted: {}", extItem);
    return extItem;
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) MExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem) QExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class ExtItemCache method retrieveFromDb.

private MExtItem retrieveFromDb(@NotNull MExtItem.Key key) {
    QExtItem ei = QExtItem.DEFAULT;
    MExtItem row;
    try (JdbcSession jdbcSession = jdbcSessionSupplier.get().startReadOnlyTransaction()) {
        row = jdbcSession.newQuery().select(ei).from(ei).where(ei.itemName.eq(key.itemName)).where(ei.valueType.eq(key.valueType)).where(ei.holderType.eq(key.holderType)).where(ei.cardinality.eq(key.cardinality)).fetchOne();
    }
    if (row != null) {
        updateMaps(row);
    }
    return row;
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) MExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem) QExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem)

Example 18 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class ExtItemCache method initialize.

/**
 * Initializes the ext-item cache.
 * Provided {@link JdbcSession} supplier will be used for later writes as well.
 */
public synchronized void initialize(Supplier<JdbcSession> jdbcSessionSupplier) {
    this.jdbcSessionSupplier = jdbcSessionSupplier;
    // this can be called repeatedly in tests, so the clear may be necessary
    idToExtItem.clear();
    keyToExtItem.clear();
    QExtItem uri = QExtItem.DEFAULT;
    List<MExtItem> result;
    try (JdbcSession jdbcSession = jdbcSessionSupplier.get().startReadOnlyTransaction()) {
        result = jdbcSession.newQuery().select(uri).from(uri).fetch();
        jdbcSession.commit();
    }
    for (MExtItem row : result) {
        updateMaps(row);
    }
    LOGGER.info("Ext item cache initialized with {} items.", result.size());
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) MExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem) QExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem)

Example 19 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class UriCache method initialize.

/**
 * Initializes the URI cache.
 * Provided {@link JdbcSession} supplier will be used for later writes as well.
 */
public synchronized void initialize(Supplier<JdbcSession> jdbcSessionSupplier) {
    this.jdbcSessionSupplier = jdbcSessionSupplier;
    // this can be called repeatedly in tests, so the clear may be necessary
    idToUri.clear();
    uriToId.clear();
    QUri uri = QUri.DEFAULT;
    List<MUri> result;
    try (JdbcSession jdbcSession = jdbcSessionSupplier.get().startReadOnlyTransaction()) {
        result = jdbcSession.newQuery().select(uri).from(uri).fetch();
        jdbcSession.commit();
    }
    for (MUri row : result) {
        updateMaps(row);
    }
    LOGGER.info("URI cache initialized with {} items.", result.size());
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) MUri(com.evolveum.midpoint.repo.sqale.qmodel.common.MUri) QUri(com.evolveum.midpoint.repo.sqale.qmodel.common.QUri)

Example 20 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession in project midpoint by Evolveum.

the class UriCache method processCacheableUri.

/**
 * Returns ID for URI creating new cache row in DB as needed.
 * Returns null for null URI parameter.
 */
@Nullable
public synchronized Integer processCacheableUri(@Nullable Object uri) {
    if (uri == null) {
        return null;
    }
    if (jdbcSessionSupplier == null) {
        throw new IllegalStateException("URI cache was not initialized yet!");
    }
    String uriString = uri instanceof QName ? QNameUtil.qNameToUri((QName) uri) : uri.toString();
    Integer id = getId(uriString);
    if (id != null) {
        return id;
    }
    QUri qu = QUri.DEFAULT;
    try (JdbcSession jdbcSession = jdbcSessionSupplier.get().startTransaction()) {
        id = jdbcSession.newInsert(qu).set(qu.uri, uriString).executeWithKey(qu.id);
        jdbcSession.commit();
        updateMaps(MUri.of(id, uriString));
    } catch (RuntimeException e) {
        if (SqaleUtils.isUniqueConstraintViolation(e)) {
            // Insert failed, record exists, so lets try to retrieve it
            Integer retId = retrieveIdFromDb(uriString);
            if (retId == null) {
                throw new IllegalStateException("Couldn't insert uri to cache and uri was not present in cache.", e);
            }
            return retId;
        }
        throw e;
    }
    LOGGER.debug("URI cache inserted URI={} under ID={}", uri, id);
    return id;
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) QName(javax.xml.namespace.QName) QUri(com.evolveum.midpoint.repo.sqale.qmodel.common.QUri) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)27 SqaleRepoBaseTest (com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)4 MExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem)4 QExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 NotNull (org.jetbrains.annotations.NotNull)4 MUri (com.evolveum.midpoint.repo.sqale.qmodel.common.MUri)3 QUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser)3 Test (org.testng.annotations.Test)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)2 ItemName (com.evolveum.midpoint.prism.path.ItemName)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2 RepoModifyOptions (com.evolveum.midpoint.repo.api.RepoModifyOptions)2 Jsonb (com.evolveum.midpoint.repo.sqale.jsonb.Jsonb)2 QUri (com.evolveum.midpoint.repo.sqale.qmodel.common.QUri)2 MUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser)2 QUserMapping (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUserMapping)2 Instant (java.time.Instant)2 QName (javax.xml.namespace.QName)2