<commit_before>/*
 * 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.loaderwriter.writebehind;

import org.ehcache.clustered.client.internal.loaderwriter.ClusteredLoaderWriterStore;
import org.ehcache.clustered.client.internal.store.ClusteredStore;
import org.ehcache.clustered.client.internal.store.ClusteredValueHolder;
import org.ehcache.clustered.client.internal.store.ResolvedChain;
import org.ehcache.clustered.client.internal.store.ServerStoreProxy;
import org.ehcache.clustered.client.internal.store.lock.LockManager;
import org.ehcache.clustered.client.internal.store.operations.ChainResolver;
import org.ehcache.clustered.client.internal.store.operations.ConditionalRemoveOperation;
import org.ehcache.clustered.client.internal.store.operations.ConditionalReplaceOperation;
import org.ehcache.clustered.client.internal.store.operations.PutIfAbsentOperation;
import org.ehcache.clustered.client.internal.store.operations.PutOperation;
import org.ehcache.clustered.client.internal.store.operations.PutWithWriterOperation;
import org.ehcache.clustered.client.internal.store.operations.RemoveOperation;
import org.ehcache.clustered.client.internal.store.operations.ReplaceOperation;
import org.ehcache.clustered.client.internal.store.operations.Result;
import org.ehcache.clustered.client.internal.store.operations.codecs.OperationsCodec;
import org.ehcache.clustered.client.service.ClusteringService;
import org.ehcache.clustered.common.internal.store.Chain;
import org.ehcache.config.ResourceType;
import org.ehcache.core.spi.store.tiering.AuthoritativeTier;
import org.ehcache.core.spi.time.TimeSource;
import org.ehcache.core.spi.time.TimeSourceService;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
import org.ehcache.spi.loaderwriter.WriteBehindConfiguration;
import org.ehcache.spi.resilience.StoreAccessException;
import org.ehcache.spi.service.ServiceConfiguration;
import org.ehcache.spi.service.ServiceDependencies;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeoutException;

import static org.ehcache.core.exceptions.StorePassThroughException.handleException;
import static org.ehcache.core.spi.service.ServiceUtils.findSingletonAmongst;

public class ClusteredWriteBehindStore<K, V> extends ClusteredStore<K, V> implements AuthoritativeTier<K, V> {

  private final CacheLoaderWriter<? super K, V> cacheLoaderWriter;
  private final ClusteredWriteBehind<K, V> clusteredWriteBehind;

  private ClusteredWriteBehindStore(Configuration<K, V> config,
                                    OperationsCodec<K, V> codec,
                                    ChainResolver<K, V> resolver,
                                    TimeSource timeSource,
                                    CacheLoaderWriter<? super K, V> loaderWriter,
                                    ExecutorService executorService) {
    super(config, codec, resolver, timeSource);
    this.cacheLoaderWriter = loaderWriter;
    this.clusteredWriteBehind = new ClusteredWriteBehind<>(this, executorService,
                                                         timeSource,
                                                         resolver,
                                                         this.cacheLoaderWriter,
                                                         codec);
  }


  Chain lock(long hash) throws TimeoutException {
    return ((LockManager) storeProxy).lock(hash);
  }

  void unlock(long hash) throws TimeoutException {
    ((LockManager) storeProxy).unlock(hash);
  }

  void replaceAtHead(long key, Chain expected, Chain replacement) {
    storeProxy.replaceAtHead(key, expected, replacement);
  }

  @Override
  protected ValueHolder<V> getInternal(K key) throws StoreAccessException, TimeoutException {
    try {
      Chain chain = storeProxy.get(extractLongKey(key));
      if (!chain.isEmpty()) {
        ClusteredValueHolder<V> holder = null;
        ResolvedChain<K, V> resolvedChain = resolver.resolve(chain, key, timeSource.getTimeMillis());
        Result<K, V> resolvedResult = resolvedChain.getResolvedResult(key);
        if (resolvedResult != null) {
          V value = resolvedResult.getValue();
          long expirationTime = resolvedChain.getExpirationTime();
          if (expirationTime == Long.MAX_VALUE) {
            holder = new ClusteredValueHolder<>(value);
          } else {<commit_msg>Log a warning when using clustered caches with write-behind<commit_after>/*
 * 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.loaderwriter.writebehind;

import org.ehcache.clustered.client.internal.loaderwriter.ClusteredLoaderWriterStore;
import org.ehcache.clustered.client.internal.store.ClusteredStore;
import org.ehcache.clustered.client.internal.store.ClusteredValueHolder;
import org.ehcache.clustered.client.internal.store.ResolvedChain;
import org.ehcache.clustered.client.internal.store.ServerStoreProxy;
import org.ehcache.clustered.client.internal.store.lock.LockManager;
import org.ehcache.clustered.client.internal.store.operations.ChainResolver;
import org.ehcache.clustered.client.internal.store.operations.ConditionalRemoveOperation;
import org.ehcache.clustered.client.internal.store.operations.ConditionalReplaceOperation;
import org.ehcache.clustered.client.internal.store.operations.PutIfAbsentOperation;
import org.ehcache.clustered.client.internal.store.operations.PutOperation;
import org.ehcache.clustered.client.internal.store.operations.PutWithWriterOperation;
import org.ehcache.clustered.client.internal.store.operations.RemoveOperation;
import org.ehcache.clustered.client.internal.store.operations.ReplaceOperation;
import org.ehcache.clustered.client.internal.store.operations.Result;
import org.ehcache.clustered.client.internal.store.operations.codecs.OperationsCodec;
import org.ehcache.clustered.client.service.ClusteringService;
import org.ehcache.clustered.common.internal.store.Chain;
import org.ehcache.config.ResourceType;
import org.ehcache.core.spi.store.tiering.AuthoritativeTier;
import org.ehcache.core.spi.time.TimeSource;
import org.ehcache.core.spi.time.TimeSourceService;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
import org.ehcache.spi.loaderwriter.WriteBehindConfiguration;
import org.ehcache.spi.resilience.StoreAccessException;
import org.ehcache.spi.service.ServiceConfiguration;
import org.ehcache.spi.service.ServiceDependencies;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeoutException;

import static org.ehcache.core.exceptions.StorePassThroughException.handleException;
import static org.ehcache.core.spi.service.ServiceUtils.findSingletonAmongst;

public class ClusteredWriteBehindStore<K, V> extends ClusteredStore<K, V> implements AuthoritativeTier<K, V> {

  private static final Logger LOGGER = LoggerFactory.getLogger(ClusteredWriteBehindStore.class);

  private final CacheLoaderWriter<? super K, V> cacheLoaderWriter;
  private final ClusteredWriteBehind<K, V> clusteredWriteBehind;

  private ClusteredWriteBehindStore(Configuration<K, V> config,
                                    OperationsCodec<K, V> codec,
                                    ChainResolver<K, V> resolver,
                                    TimeSource timeSource,
                                    CacheLoaderWriter<? super K, V> loaderWriter,
                                    ExecutorService executorService) {
    super(config, codec, resolver, timeSource);
    this.cacheLoaderWriter = loaderWriter;
    this.clusteredWriteBehind = new ClusteredWriteBehind<>(this, executorService,
                                                         timeSource,
                                                         resolver,
                                                         this.cacheLoaderWriter,
                                                         codec);
    LOGGER.warn("Write-behind caches using clustered resources have unresolved correctness issues. This can lead to dropped writes due to either client failure or heavy eviction rates.");
  }


  Chain lock(long hash) throws TimeoutException {
    return ((LockManager) storeProxy).lock(hash);
  }

  void unlock(long hash) throws TimeoutException {
    ((LockManager) storeProxy).unlock(hash);
  }

  void replaceAtHead(long key, Chain expected, Chain replacement) {
    storeProxy.replaceAtHead(key, expected, replacement);
  }

  @Override
  protected ValueHolder<V> getInternal(K key) throws StoreAccessException, TimeoutException {
    try {
      Chain chain = storeProxy.get(extractLongKey(key));
      if (!chain.isEmpty()) {
        ClusteredValueHolder<V> holder = null;
        ResolvedChain<K, V> resolvedChain = resolver.resolve(chain, key, timeSource.getTimeMillis());
        Result<K, V> resolvedResult = resolvedChain.getResolvedResult(key);
        if (resolvedResult != null) {
          V value = resolvedResult.getValue();
          long expirationTime = resolvedChain.getExpirationTime();
          if (expirationTime == Long.MAX_VALUE) {
            holder = new ClusteredValueHolder<>(value);
          } else {
{'fraction_non_alphanumeric': 0.08680131517144199, 'fraction_numerical': 0.0009394081728511038, 'mean_word_length': 3.5927523727351165, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7065751', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2796, 'n_words': 604}