text: /*
   *                   GridGain Community Edition Licensing
   *                   Copyright 2019 GridGain Systems, Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License") modified with Commons Clause
   * Restriction; 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.
   *
   * Commons Clause Restriction
   *
   * The Software is provided to you by the Licensor under the License, as defined below, subject to
   * the following condition.
   *
   * Without limiting other conditions in the License, the grant of rights under the License will not
   * include, and the License does not grant to you, the right to Sell the Software.
   * For purposes of the foregoing, “Sell” means practicing any or all of the rights granted to you
   * under the License to provide to third parties, for a fee or other consideration (including without
   * limitation fees for hosting or consulting/ support services related to the Software), a product or
   * service whose value derives, entirely or substantially, from the functionality of the Software.
   * Any license notice or attribution required by the License must also include this Commons Clause
   * License Condition notice.
   *
   * For purposes of the clause above, the “Licensor” is Copyright 2019 GridGain Systems, Inc.,
   * the “License” is the Apache License, Version 2.0, and the Software is the GridGain Community
   * Edition software provided with this notice.
   */
  
  #ifndef _IGNITE_IMPL_THIN_CACHE_CACHE_CLIENT_IMPL
  #define _IGNITE_IMPL_THIN_CACHE_CACHE_CLIENT_IMPL
  
  #include <stdint.h>
  #include <string>
  
  #include "impl/data_router.h"
  
  namespace ignite
  {
      namespace impl
      {
          namespace thin
          {
              /* Forward declaration. */
              class Readable;
  
              /* Forward declaration. */
              class Writable;
  
              /* Forward declaration. */
              class WritableKey;
  
              namespace cache
              {
                  /**
                   * Ignite client class implementation.
                   *
                   * This is an entry point for Thin C++ Ignite client. Its main
                   * purpose is to establish connection to the remote server node.
                   */
                  class CacheClientImpl
                  {
                  public:
                      /**
                       * Constructor.
                       *
                       * @param router Data router instance.
                       * @param name Cache name.
                       * @param id Cache ID.
                       */
                      CacheClientImpl(
                          const SP_DataRouter& router,
                          const std::string& name,
                          int32_t id);
  
                      /**
                       * Destructor.
                       */
                      ~CacheClientImpl();
  
                      /**
                       * Put value to cache.
                       *
                       * @param key Key.
                       * @param value Value.
                       */
                      void Put(const WritableKey& key, const Writable& value);
  
                      /**
                       * Stores given key-value pairs in cache.
                       * If write-through is enabled, the stored values will be persisted to store.
                       *
                       * @param pairs Writable key-value pair sequence.
                       */
                      void PutAll(const Writable& pairs);
  
                      /**
                       * Get value from cache.
                       *
                       * @param key Key.
                       * @param value Value.
                       */
                      void Get(const WritableKey& key, Readable& value);
  
                      /**
                       * Retrieves values mapped to the specified keys from cache.
                       * If some value is not present in cache, then it will be looked up from swap storage. If
                       * it's not present in swap, or if swap is disabled, and if read-through is allowed, value
                       * will be loaded from persistent store.
                       *
                       * @param keys Writable key sequence.
                       * @param pairs Readable key-value pair sequence.
                       */
                      void GetAll(const Writable& keys, Readable& pairs);
  
                      /**
                       * Stores given key-value pair in cache only if there is a previous mapping for it.
                       * If cache previously contained value for the given key, then this value is returned.
                       * In case of PARTITIONED or REPLICATED caches, the value will be loaded from the primary node,
                       * which in its turn may load the value from the swap storage, and consecutively, if it's not
                       * in swap, rom the underlying persistent storage.
                       * If write-through is enabled, the stored value will be persisted to store.
                       *
                       * @param key Key to store in cache.
                       * @param value Value to be associated with the given key.
                       * @return True if the value was replaced.
                       */
                      bool Replace(const WritableKey& key, const Writable& value);
  
                      /**
                       * Check if the cache contains a value for the specified key.
                       *
                       * @param key Key whose presence in this cache is to be tested.
                       * @return @c true if the cache contains specified key.
                       */
                      bool ContainsKey(const WritableKey& key);
  
                      /**
                       * Check if cache contains mapping for these keys.
                       *
                       * @param keys Keys.
                       * @return True if cache contains mapping for all these keys.
                       */
                      bool ContainsKeys(const Writable& keys);
  
                      /**
                       * Gets the number of all entries cached across all nodes.
                       * @note This operation is distributed and will query all
                       * participating nodes for their cache sizes.
                       *
                       * @param peekModes Peek modes mask.
                       * @return Cache size across all nodes.
                       */
                      int64_t GetSize(int32_t peekModes);
  
                      /**
                       * Removes given key mapping from cache. If cache previously contained value for the given key,
                       * then this value is returned. In case of PARTITIONED or REPLICATED caches, the value will be
                       * loaded from the primary node, which in its turn may load the value from the disk-based swap
                       * storage, and consecutively, if it's not in swap, from the underlying persistent storage.
                       * If the returned value is not needed, method removex() should always be used instead of this
                       * one to avoid the overhead associated with returning of the previous value.
                       * If write-through is enabled, the value will be removed from store.
                       *
                       * @param key Key whose mapping is to be removed from cache.
                       * @return False if there was no matching key.
                       */
                      bool Remove(const WritableKey& key);
  
                      /**
                       * Removes given key mappings from cache.
                       * If write-through is enabled, the value will be removed from store.
                       *
                       * @param keys Keys whose mappings are to be removed from cache.
                       */
                      void RemoveAll(const Writable& keys);
  
                      /**
                       * Removes all mappings from cache.
                       * If write-through is enabled, the value will be removed from store.
                       */
                      void RemoveAll();
  
                      /**
                       * Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
                       * Entry is cleared only if it is not currently locked, and is not participating in a transaction.
                       *
                       * @param key Key to clear.
                       */
                      void Clear(const WritableKey& key);
  
                      /**
                       * Clear cache.
                       */
                      void Clear();
  
                      /**
                       * Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
                       * Entry is cleared only if it is not currently locked, and is not participating in a transaction.
                       *
                       * @param keys Keys to clear.
                       */
                      void ClearAll(const Writable& keys);
  
                      /**
                       * Peeks at in-memory cached value using default optional
                       * peek mode. This method will not load value from any
                       * persistent store or from a remote node.
                       *
                       * Use for testing purposes only.
                       *
                       * @param key Key whose presence in this cache is to be tested.
                       * @param value Value.
                       */
                      void LocalPeek(const WritableKey& key, Readable& value);
  
                      /**
                       * Update cache partitions info.
                       */
                      void RefreshAffinityMapping();
  
                  private:
                      /**
                       * Synchronously send request message and receive response.
                       *
                       * @param key Key.
                       * @param req Request message.
                       * @param rsp Response message.
                       * @throw IgniteError on error.
                       */
                      template<typename ReqT, typename RspT>
                      void SyncCacheKeyMessage(const WritableKey& key, const ReqT& req, RspT& rsp);
  
                      /**
                       * Synchronously send message and receive response.
                       *
                       * @param req Request message.
                       * @param rsp Response message.
                       * @throw IgniteError on error.
                       */
                      template<typename ReqT, typename RspT>
                      void SyncMessage(const ReqT& req, RspT& rsp);
  
                      /** Data router. */
                      SP_DataRouter router;
  
                      /** Cache name. */
                      std::string name;
  
                      /** Cache ID. */
                      int32_t id;
  
                      /** Binary flag. */
                      bool binary;
                  };
  
                  typedef common::concurrent::SharedPointer<CacheClientImpl> SP_CacheClientImpl;
              }
          }
      }
  }
  #endif // _IGNITE_IMPL_THIN_CACHE_CACHE_CLIENT_IMPL
  