Search in sources :

Example 1 with QueryParameters

use of org.keycloak.models.map.storage.QueryParameters in project keycloak by keycloak.

the class JpaMapKeycloakTransaction method read.

@Override
@SuppressWarnings("unchecked")
public Stream<E> read(QueryParameters<M> queryParameters) {
    JpaModelCriteriaBuilder mcb = queryParameters.getModelCriteriaBuilder().flashToModelCriteriaBuilder(createJpaModelCriteriaBuilder());
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<RE> query = cb.createQuery(entityType);
    Root<RE> root = query.from(entityType);
    query.select(selectCbConstruct(cb, root));
    // ordering
    if (!queryParameters.getOrderBy().isEmpty()) {
        List<Order> orderByList = new LinkedList<>();
        for (QueryParameters.OrderBy<M> order : queryParameters.getOrderBy()) {
            switch(order.getOrder()) {
                case ASCENDING:
                    orderByList.add(cb.asc(root.get(order.getModelField().getName())));
                    break;
                case DESCENDING:
                    orderByList.add(cb.desc(root.get(order.getModelField().getName())));
                    break;
                default:
                    throw new UnsupportedOperationException("Unknown ordering.");
            }
        }
        query.orderBy(orderByList);
    }
    BiFunction<CriteriaBuilder, Root<RE>, Predicate> predicateFunc = mcb.getPredicateFunc();
    if (predicateFunc != null)
        query.where(predicateFunc.apply(cb, root));
    return closing(paginateQuery(em.createQuery(query), queryParameters.getOffset(), queryParameters.getLimit()).getResultStream()).map(this::mapToEntityDelegate);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) Root(javax.persistence.criteria.Root) QueryParameters(org.keycloak.models.map.storage.QueryParameters) LinkedList(java.util.LinkedList) Predicate(javax.persistence.criteria.Predicate)

Example 2 with QueryParameters

use of org.keycloak.models.map.storage.QueryParameters in project keycloak by keycloak.

the class HotRodMapStorage method delete.

@Override
public long delete(QueryParameters<M> queryParameters) {
    IckleQueryMapModelCriteriaBuilder<E, M> iqmcb = queryParameters.getModelCriteriaBuilder().flashToModelCriteriaBuilder(createCriteriaBuilder());
    String queryString = "SELECT id " + iqmcb.getIckleQuery();
    if (!queryParameters.getOrderBy().isEmpty()) {
        queryString += " ORDER BY " + queryParameters.getOrderBy().stream().map(HotRodMapStorage::toOrderString).collect(Collectors.joining(", "));
    }
    LOG.tracef("Executing delete Ickle query: %s", queryString);
    QueryFactory queryFactory = Search.getQueryFactory(remoteCache);
    Query<Object[]> query = paginateQuery(queryFactory.create(queryString), queryParameters.getOffset(), queryParameters.getLimit());
    query.setParameters(iqmcb.getParameters());
    AtomicLong result = new AtomicLong();
    CloseableIterator<Object[]> iterator = query.iterator();
    StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false).peek(e -> result.incrementAndGet()).map(a -> a[0]).map(String.class::cast).forEach(this::delete);
    iterator.close();
    return result.get();
}
Also used : Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) Logger(org.jboss.logging.Logger) AbstractHotRodEntity(org.keycloak.models.map.storage.hotRod.common.AbstractHotRodEntity) MapKeycloakTransaction(org.keycloak.models.map.storage.MapKeycloakTransaction) QueryFactory(org.infinispan.query.dsl.QueryFactory) RemoteCache(org.infinispan.client.hotrod.RemoteCache) SearchableModelField(org.keycloak.storage.SearchableModelField) Function(java.util.function.Function) CloseableIterator(org.infinispan.commons.util.CloseableIterator) QueryParameters(org.keycloak.models.map.storage.QueryParameters) MapModelCriteriaBuilder(org.keycloak.models.map.storage.chm.MapModelCriteriaBuilder) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) ConcurrentHashMapCrudOperations(org.keycloak.models.map.storage.chm.ConcurrentHashMapCrudOperations) HotRodUtils.paginateQuery(org.keycloak.models.map.storage.hotRod.common.HotRodUtils.paginateQuery) StreamsUtil.closing(org.keycloak.utils.StreamsUtil.closing) KeycloakSession(org.keycloak.models.KeycloakSession) MapStorage(org.keycloak.models.map.storage.MapStorage) MapFieldPredicates(org.keycloak.models.map.storage.chm.MapFieldPredicates) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractEntity(org.keycloak.models.map.common.AbstractEntity) StringKeyConvertor(org.keycloak.models.map.common.StringKeyConvertor) Stream(java.util.stream.Stream) Search(org.infinispan.client.hotrod.Search) DeepCloner(org.keycloak.models.map.common.DeepCloner) Query(org.infinispan.query.dsl.Query) HotRodEntityDelegate(org.keycloak.models.map.storage.hotRod.common.HotRodEntityDelegate) ConcurrentHashMapKeycloakTransaction(org.keycloak.models.map.storage.chm.ConcurrentHashMapKeycloakTransaction) HotRodEntityDescriptor(org.keycloak.models.map.storage.hotRod.common.HotRodEntityDescriptor) QueryFactory(org.infinispan.query.dsl.QueryFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 3 with QueryParameters

use of org.keycloak.models.map.storage.QueryParameters in project keycloak by keycloak.

the class ConcurrentHashMapStorage method delete.

@Override
public long delete(QueryParameters<M> queryParameters) {
    DefaultModelCriteria<M> criteria = queryParameters.getModelCriteriaBuilder();
    if (criteria == null) {
        long res = store.size();
        store.clear();
        return res;
    }
    @SuppressWarnings("unchecked") MapModelCriteriaBuilder<K, V, M> mcb = criteria.flashToModelCriteriaBuilder(createCriteriaBuilder());
    Predicate<? super K> keyFilter = mcb.getKeyFilter();
    Predicate<? super V> entityFilter = mcb.getEntityFilter();
    Stream<Entry<K, V>> storeStream = store.entrySet().stream();
    final AtomicLong res = new AtomicLong(0);
    if (!queryParameters.getOrderBy().isEmpty()) {
        Comparator<V> comparator = MapFieldPredicates.getComparator(queryParameters.getOrderBy().stream());
        storeStream = storeStream.sorted((entry1, entry2) -> comparator.compare(entry1.getValue(), entry2.getValue()));
    }
    paginatedStream(storeStream.filter(next -> keyFilter.test(next.getKey()) && entityFilter.test(next.getValue())), queryParameters.getOffset(), queryParameters.getLimit()).peek(item -> {
        res.incrementAndGet();
    }).map(Entry::getKey).forEach(store::remove);
    return res.get();
}
Also used : DefaultModelCriteria(org.keycloak.models.map.storage.criteria.DefaultModelCriteria) Predicate(java.util.function.Predicate) UpdatableEntity(org.keycloak.models.map.common.UpdatableEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeycloakSession(org.keycloak.models.KeycloakSession) MapKeycloakTransaction(org.keycloak.models.map.storage.MapKeycloakTransaction) MapStorage(org.keycloak.models.map.storage.MapStorage) SearchableModelField(org.keycloak.storage.SearchableModelField) ConcurrentMap(java.util.concurrent.ConcurrentMap) Objects(java.util.Objects) AtomicLong(java.util.concurrent.atomic.AtomicLong) UpdatePredicatesFunc(org.keycloak.models.map.storage.chm.MapModelCriteriaBuilder.UpdatePredicatesFunc) StringKeyConvertor(org.keycloak.models.map.common.StringKeyConvertor) AbstractEntity(org.keycloak.models.map.common.AbstractEntity) Stream(java.util.stream.Stream) QueryParameters(org.keycloak.models.map.storage.QueryParameters) Map(java.util.Map) DeepCloner(org.keycloak.models.map.common.DeepCloner) Entry(java.util.Map.Entry) StreamsUtil.paginatedStream(org.keycloak.utils.StreamsUtil.paginatedStream) Comparator(java.util.Comparator) Entry(java.util.Map.Entry) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

QueryParameters (org.keycloak.models.map.storage.QueryParameters)3 Map (java.util.Map)2 Objects (java.util.Objects)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Stream (java.util.stream.Stream)2 KeycloakSession (org.keycloak.models.KeycloakSession)2 AbstractEntity (org.keycloak.models.map.common.AbstractEntity)2 DeepCloner (org.keycloak.models.map.common.DeepCloner)2 StringKeyConvertor (org.keycloak.models.map.common.StringKeyConvertor)2 MapKeycloakTransaction (org.keycloak.models.map.storage.MapKeycloakTransaction)2 MapStorage (org.keycloak.models.map.storage.MapStorage)2 SearchableModelField (org.keycloak.storage.SearchableModelField)2 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 LinkedList (java.util.LinkedList)1 Entry (java.util.Map.Entry)1 Spliterators (java.util.Spliterators)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Function (java.util.function.Function)1