Search in sources :

Example 1 with QUri

use of com.evolveum.midpoint.repo.sqale.qmodel.common.QUri 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 2 with QUri

use of com.evolveum.midpoint.repo.sqale.qmodel.common.QUri 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

QUri (com.evolveum.midpoint.repo.sqale.qmodel.common.QUri)2 JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)2 MUri (com.evolveum.midpoint.repo.sqale.qmodel.common.MUri)1 QName (javax.xml.namespace.QName)1 Nullable (org.jetbrains.annotations.Nullable)1