Esempio n. 1
0
 /**
  * Check if the server has a given key. This is used for TPC operations that need to check whether
  * or not a transaction can be performed but you don't want to modify the state of the cache by
  * calling get(). You are allowed to call dataStore.get() for this method.
  *
  * @param key key to check for membership in store
  */
 public boolean hasKey(String key) {
   try {
     dataStore.get(key);
   } catch (KVException e) { // an exception of key_not_found is caught
     return false;
   }
   return true;
 }
Esempio n. 2
0
  /**
   * Performs get request. Checks cache first. Updates cache if not in cache but located in store.
   *
   * @param key String key
   * @return String value associated with key
   * @throws KVException with ERROR_NO_SUCH_KEY if key does not exist in store
   */
  @Override
  public String get(String key) throws KVException {
    if (key.length() > MAX_KEY_SIZE) {
      KVMessage msg = new KVMessage(KVConstants.RESP, ERROR_NO_SUCH_KEY);
      throw new KVException(msg);
    }

    Lock lock = dataCache.getLock(key);
    String ret = null;
    try {
      lock.lock();
      ret = dataCache.get(key);
      if (ret == null) {
        ret = dataStore.get(key);
        if (ret != null) dataCache.put(key, ret);
      }
    } finally {
      lock.unlock();
    }
    return ret;
  }