Exemplo n.º 1
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();
    }
  }
Exemplo n.º 2
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();
   }
 }
Exemplo n.º 3
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;
  }
Exemplo n.º 4
0
 /** This method is purely for convenience and will not be tested. */
 @Override
 public String toString() {
   return dataStore.toString() + dataCache.toString();
 }