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 del request.
   *
   * @param key String key
   * @throws KVException with ERROR_NO_SUCH_KEY if key does not exist in store
   */
  @Override
  public void del(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);
    try {
      lock.lock();
      dataCache.del(key);
      dataStore.del(key);
    } finally {
      lock.unlock();
    }
  }
Esempio n. 3
0
 /**
  * Performs put request on cache and store.
  *
  * @param key String key
  * @param value String value
  * @throws KVException if key or value is too long
  */
 @Override
 public void put(String key, String value) throws KVException {
   if (key.length() > MAX_KEY_SIZE) {
     KVMessage msg = new KVMessage(KVConstants.RESP, ERROR_OVERSIZED_KEY);
     throw new KVException(msg);
   }
   if (value.length() > MAX_VAL_SIZE) {
     KVMessage msg = new KVMessage(KVConstants.RESP, ERROR_OVERSIZED_VALUE);
     throw new KVException(msg);
   }
   Lock lock = dataCache.getLock(key);
   try {
     // In case of some unexpected exception thrown here
     lock.lock();
     dataCache.put(key, value);
     dataStore.put(key, value);
   } finally {
     lock.unlock();
   }
 }
Esempio n. 4
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;
  }
Esempio n. 5
0
 /** This method is purely for convenience and will not be tested. */
 @Override
 public String toString() {
   return dataStore.toString() + dataCache.toString();
 }