All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ehcache.clustered.client.internal.service.ClusteredStateHolder Maven / Gradle / Ivy

Go to download

Ehcache 3 Clustered: Defines the client jar and the kit containing the Terracotta server

The newest version!
/*
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ehcache.clustered.client.internal.service;

import org.ehcache.clustered.client.internal.store.ClusterTierClientEntity;
import org.ehcache.clustered.common.internal.exceptions.ClusterException;
import org.ehcache.clustered.common.internal.messages.EhcacheEntityResponse;
import org.ehcache.clustered.common.internal.messages.StateRepositoryMessageFactory;
import org.ehcache.clustered.common.internal.messages.StateRepositoryOpMessage;
import org.ehcache.spi.persistence.StateHolder;

import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;

import static org.ehcache.clustered.client.internal.service.ValueCodecFactory.getCodecForClass;

public class ClusteredStateHolder implements StateHolder {

  private final StateRepositoryMessageFactory messageFactory;
  private final ClusterTierClientEntity entity;
  private final Class keyClass;
  private final ValueCodec keyCodec;
  private final ValueCodec valueCodec;

  public ClusteredStateHolder(final String cacheId, final String mapId, final ClusterTierClientEntity entity,
                              Class keyClass, Class valueClass,
                              Predicate> isClassPermittted, ClassLoader classLoader) {
    this.keyClass = keyClass;
    this.keyCodec = getCodecForClass(keyClass, isClassPermittted, classLoader);
    this.valueCodec = getCodecForClass(valueClass, isClassPermittted, classLoader);
    this.messageFactory = new StateRepositoryMessageFactory(cacheId, mapId);
    this.entity = entity;
  }

  @Override
  @SuppressWarnings("unchecked")
  public V get(final Object key) {
    if (!keyClass.isAssignableFrom(key.getClass())) {
      return null;
    }
    @SuppressWarnings("unchecked")
    Object response = getResponse(messageFactory.getMessage(keyCodec.encode((K) key)), false);
    return valueCodec.decode(response);
  }

  private Object getResponse(StateRepositoryOpMessage message, boolean track) {
    try {
      EhcacheEntityResponse response = entity.invokeStateRepositoryOperation(message, track);
      return ((EhcacheEntityResponse.MapValue)response).getValue();
    } catch (ClusterException | TimeoutException ce) {
      throw new ClusteredMapException(ce);
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public Set> entrySet() {
    @SuppressWarnings("unchecked")
    Set> response = (Set>) getResponse(messageFactory.entrySetMessage(), true);
    Set> entries = new HashSet<>();
    for (Map.Entry objectEntry : response) {
      entries.add(new AbstractMap.SimpleEntry<>(keyCodec.decode(objectEntry.getKey()),
        valueCodec.decode(objectEntry.getValue())));
    }
    return entries;
  }

  @Override
  @SuppressWarnings("unchecked")
  public V putIfAbsent(final K key, final V value) {
    Object response = getResponse(messageFactory.putIfAbsentMessage(keyCodec.encode(key), valueCodec.encode(value)), true);
    return valueCodec.decode(response);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy