Search in sources :

Example 1 with DefaultModelCriteria

use of org.keycloak.models.map.storage.criteria.DefaultModelCriteria 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)

Example 2 with DefaultModelCriteria

use of org.keycloak.models.map.storage.criteria.DefaultModelCriteria in project keycloak by keycloak.

the class ConcurrentHashMapStorageProviderFactory method storeMap.

@SuppressWarnings("unchecked")
private void storeMap(String mapName, ConcurrentHashMapStorage<?, ?, ?> store) {
    if (mapName != null) {
        File f = getFile(mapName);
        try {
            if (storageDirectory != null) {
                LOG.debugf("Storing contents to %s", f.getCanonicalPath());
                final DefaultModelCriteria readAllCriteria = criteria();
                Serialization.MAPPER.writeValue(f, store.read(withCriteria(readAllCriteria)));
            } else {
                LOG.debugf("Not storing contents of %s because directory not set", mapName);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : DefaultModelCriteria(org.keycloak.models.map.storage.criteria.DefaultModelCriteria) IOException(java.io.IOException) File(java.io.File)

Aggregations

DefaultModelCriteria (org.keycloak.models.map.storage.criteria.DefaultModelCriteria)2 File (java.io.File)1 IOException (java.io.IOException)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 KeycloakSession (org.keycloak.models.KeycloakSession)1 AbstractEntity (org.keycloak.models.map.common.AbstractEntity)1 DeepCloner (org.keycloak.models.map.common.DeepCloner)1 StringKeyConvertor (org.keycloak.models.map.common.StringKeyConvertor)1 UpdatableEntity (org.keycloak.models.map.common.UpdatableEntity)1 MapKeycloakTransaction (org.keycloak.models.map.storage.MapKeycloakTransaction)1 MapStorage (org.keycloak.models.map.storage.MapStorage)1 QueryParameters (org.keycloak.models.map.storage.QueryParameters)1