/**
   * This method depends on addLiveContactInfo(...) and does two things. It either forwards or
   * removes a DHTValue it from the local Database. For details see Kademlia spec!
   */
  private void forwardOrRemoveValues(Contact node, Contact existing, DHTMessage message) {

    List<DHTValueEntity> valuesToForward = new ArrayList<DHTValueEntity>();

    Database database = context.getDatabase();
    synchronized (database) {
      for (KUID primaryKey : database.keySet()) {

        Operation op = getOperation(node, existing, primaryKey);

        if (LOG.isDebugEnabled())
          LOG.debug("node: " + node + "existing: " + existing + "operation: " + op);

        if (op.equals(Operation.FORWARD)) {
          Map<KUID, DHTValueEntity> bag = database.get(primaryKey);
          valuesToForward.addAll(bag.values());
          databaseStats.STORE_FORWARD_COUNT.incrementStat();

        } else if (op.equals(Operation.DELETE)
            && DatabaseSettings.DELETE_VALUE_IF_FURTHEST_NODE.getValue()) {
          Map<KUID, DHTValueEntity> bag = database.get(primaryKey);
          for (DHTValueEntity entity : bag.values()) {
            // System.out.println("REMOVING: " + entity + "\n");
            database.remove(entity.getPrimaryKey(), entity.getSecondaryKey());
          }
          databaseStats.STORE_FORWARD_REMOVALS.incrementStat();
        }
      }
    }

    if (!valuesToForward.isEmpty()) {
      SecurityToken securityToken = null;
      if (message instanceof SecurityTokenProvider) {
        securityToken = ((SecurityTokenProvider) message).getSecurityToken();

        if (securityToken == null && StoreSettings.STORE_REQUIRES_SECURITY_TOKEN.getValue()) {
          if (LOG.isInfoEnabled()) {
            LOG.info(node + " sent us a null SecurityToken");
          }
          return;
        }
      }

      context.store(node, securityToken, valuesToForward);
    }
  }