@SuppressWarnings("unchecked")
  @Override
  public List<Application> getApplications() {

    try {
      log.debug("Finding all applications");

      List<Application> applications = new ArrayList<Application>();
      Selector selector = Pelops.createSelector(schemaName);
      LinkedHashMap<Bytes, List<Column>> rows =
          selector.getColumnsFromRows(
              "applications",
              Selector.newKeyRange("", "", 10000), // 10000 applications limite,
              false,
              ConsistencyLevel.ONE);

      for (Map.Entry<Bytes, List<Column>> row : rows.entrySet()) {
        if (row.getValue().size() > 0) {
          Application application = new Application(row.getKey().toUTF8());
          populateApplicationData(application, row.getValue());
          applications.add(application);
        }
      }

      return applications;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return Collections.EMPTY_LIST;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<GatewayVerb> getVerbs() {

    log.debug("Getting list with all active verb");
    try {

      List<GatewayVerb> verbs = new ArrayList<GatewayVerb>();
      Selector selector = Pelops.createSelector(schemaName);
      LinkedHashMap<Bytes, List<Column>> rows =
          selector.getColumnsFromRows(
              "verbs",
              Selector.newKeyRange("", "", 10000), // 10000 mixers limit,
              false,
              ConsistencyLevel.ONE);

      for (Map.Entry<Bytes, List<Column>> row : rows.entrySet()) {
        if (row.getValue().size() > 0) {
          for (Column column : row.getValue()) {
            GatewayVerb verb =
                new GatewayVerb(
                    row.getKey().toUTF8(),
                    Bytes.toUTF8(column.getName()),
                    Bytes.toUTF8(column.getValue()));
            verbs.add(verb);
          }
        }
      }

      return verbs;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return Collections.EMPTY_LIST;
    }
  }
  @Override
  public RayoNode getNode(String rayoNode) {

    log.debug("Getting node with id: [%s]", rayoNode);
    RayoNode node = null;
    try {
      Selector selector = Pelops.createSelector(schemaName);
      Map<String, List<SuperColumn>> rows =
          selector.getSuperColumnsFromRowsUtf8Keys(
              "nodes",
              Selector.newKeyRange("", "", 100), // 100 platforms limit should be enough :)
              Selector.newColumnsPredicate(rayoNode),
              ConsistencyLevel.ONE);

      Iterator<Entry<String, List<SuperColumn>>> it = rows.entrySet().iterator();
      while (it.hasNext()) {
        Entry<String, List<SuperColumn>> element = it.next();
        String currPlatform = element.getKey();
        List<SuperColumn> platformOccurrences = element.getValue();
        for (SuperColumn column : platformOccurrences) {
          if (node == null) {
            node = new RayoNode();
            node = buildNode(column.getColumns());
            node.setHostname(rayoNode);
          }
          node.addPlatform(currPlatform);
        }
      }
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return null;
    }
    return node;
  }
  @SuppressWarnings("unchecked")
  public List<RayoNode> getRayoNodesForPlatform(String platformId) {

    try {
      log.debug("Finding rayo nodes for platform: [%s]", platformId);
      Set<String> platforms = new HashSet<String>();
      platforms.add(platformId);

      List<RayoNode> nodes = new ArrayList<RayoNode>();
      Selector selector = Pelops.createSelector(schemaName);
      List<SuperColumn> columns =
          selector.getSuperColumnsFromRow("nodes", platformId, false, ConsistencyLevel.ONE);
      for (SuperColumn column : columns) {
        String id = Bytes.toUTF8(column.getName());
        RayoNode rayoNode = buildNode(column.getColumns());
        rayoNode.setHostname(id);
        rayoNode.setPlatforms(platforms);
        nodes.add(rayoNode);
      }

      return nodes;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return Collections.EMPTY_LIST;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public Collection<GatewayMixer> getMixers() {

    log.debug("Getting list with all active mixers");
    try {

      List<GatewayMixer> mixers = new ArrayList<GatewayMixer>();
      Selector selector = Pelops.createSelector(schemaName);
      LinkedHashMap<Bytes, List<Column>> rows =
          selector.getColumnsFromRows(
              "mixers",
              Selector.newKeyRange("", "", 10000), // 10000 mixers limit,
              false,
              ConsistencyLevel.ONE);

      for (Map.Entry<Bytes, List<Column>> row : rows.entrySet()) {
        if (row.getValue().size() > 0) {
          mixers.add(buildMixer(row.getValue(), row.getKey().toUTF8()));
        }
      }

      return mixers;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return Collections.EMPTY_LIST;
    }
  }
  @Override
  public Application getApplicationForAddress(String address) {

    log.debug("Finding application for address: [%s]", address);
    Selector selector = Pelops.createSelector(schemaName);
    List<Column> columns =
        selector.getColumnsFromRow("addresses", address, false, ConsistencyLevel.ONE);
    if (columns != null && columns.size() > 0) {
      Column column = columns.get(0);
      return getApplication(Bytes.toUTF8(column.getValue()));
    }
    log.debug("No application found for address: [%s]", address);
    return null;
  }
  @Override
  public GatewayCall getCall(String id) {

    log.debug("Getting call with id [%s]", id);
    Selector selector = Pelops.createSelector(schemaName);
    try {
      List<Column> columns = selector.getColumnsFromRow("calls", id, false, ConsistencyLevel.ONE);

      return buildCall(columns, id);
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return null;
    }
  }
  @Override
  public GatewayMixer getMixer(String name) {

    log.debug("Getting mixer with name [%s]", name);
    Selector selector = Pelops.createSelector(schemaName);
    try {
      List<Column> columns =
          selector.getColumnsFromRow("mixers", name, false, ConsistencyLevel.ONE);

      return buildMixer(columns, name);
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return null;
    }
  }
  @Override
  public Application getApplication(String jid) {

    if (jid == null) return null;
    log.debug("Finding application with jid: [%s]", jid);
    Application application = null;

    Selector selector = Pelops.createSelector(schemaName);
    List<Column> columns =
        selector.getColumnsFromRow("applications", jid, false, ConsistencyLevel.ONE);
    if (columns.size() > 0) {
      application = new Application(jid);
      populateApplicationData(application, columns);
    }
    return application;
  }
  @SuppressWarnings("unchecked")
  private Collection<String> getCalls(String jid, String type) {

    try {
      Selector selector = Pelops.createSelector(schemaName);
      List<Column> columns =
          selector.getSubColumnsFromRow("jids", type, jid, false, ConsistencyLevel.ONE);
      List<String> calls = new ArrayList<String>();
      for (Column column : columns) {
        calls.add(Bytes.toUTF8(column.getValue()));
      }
      return calls;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return Collections.EMPTY_LIST;
    }
  }
  @Override
  public List<String> getFilteredApplications(String id) throws DatastoreException {

    log.debug("Getting the list of filtered jids for id [%s]", id);
    List<String> ids = new ArrayList<String>();
    Selector selector = Pelops.createSelector(schemaName);
    try {
      List<Column> columns = selector.getColumnsFromRow("filters", id, false, ConsistencyLevel.ONE);
      for (Column column : columns) {
        ids.add(Bytes.toUTF8(column.getName()));
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return Collections.EMPTY_LIST;
    }
    return ids;
  }
  @Override
  public String getNodeForIpAddress(String ip) {

    try {
      log.debug("Finding node for IP address: [%s]", ip);
      Selector selector = Pelops.createSelector(schemaName);
      Column column = selector.getColumnFromRow("ips", ip, "node", ConsistencyLevel.ONE);
      if (column != null) {
        return Bytes.toUTF8(column.getValue());
      }
    } catch (NotFoundException nfe) {
      log.debug("No node found for ip address: [%s]", ip);
      return null;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
    }
    return null;
  }
  @Override
  public GatewayVerb getVerb(String mixerName, String id) {

    log.debug("Getting the verb [%s] from mixer [%s]", id, mixerName);
    Selector selector = Pelops.createSelector(schemaName);
    try {
      List<Column> columns =
          selector.getColumnsFromRow("verbs", mixerName, false, ConsistencyLevel.ONE);
      for (Column column : columns) {
        if (Bytes.toUTF8(column.getName()).equals(id)) {
          return new GatewayVerb(mixerName, id, Bytes.toUTF8(column.getValue()));
        }
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
    return null;
  }
  @Override
  public final UserAccount findUserAccountByUsername(final String username) {

    if (username == null) {
      throw new IllegalArgumentException("The passed username was null.");
    }

    final Selector selector = connectionPool.createSelector();
    final List<Column> columns =
        selector.getColumnsFromRow(COLUMN_FAMILY_USERNAME, username, false, CONSISTENCY_LEVEL_READ);

    if (!columns.isEmpty()) {
      final String userid = Selector.getColumnStringValue(columns, "userid");
      return findUserAccountByPrimaryKey(userid);
    }

    return null;
  }
  private List<String> getAllRowNames(
      String columnFamily, int numColumns, boolean excludeIfLessColumns, String colName) {

    List<String> result = new ArrayList<String>();
    try {
      Selector selector = Pelops.createSelector(schemaName);
      final int PAGE_SIZE = 100;
      String currRow = "";
      while (true) {
        SlicePredicate predicate = null;
        if (colName == null) {
          predicate = Selector.newColumnsPredicateAll(false, numColumns);
        } else {
          predicate = Selector.newColumnsPredicate(colName, colName, false, numColumns);
        }

        Map<String, List<Column>> rows =
            selector.getColumnsFromRowsUtf8Keys(
                columnFamily,
                Selector.newKeyRange(currRow, "", PAGE_SIZE),
                predicate,
                ConsistencyLevel.ONE);

        Iterator<Entry<String, List<Column>>> it = rows.entrySet().iterator();
        while (it.hasNext()) {
          Entry<String, List<Column>> element = it.next();
          if (excludeIfLessColumns && element.getValue().size() < numColumns) {
            continue;
          }
          currRow = element.getKey();
          if (!result.contains(currRow)) {
            result.add(currRow);
          }
        }

        if (rows.keySet().size() < PAGE_SIZE) break;
      }

    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
    }

    return result;
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<GatewayVerb> getVerbs(String mixerName) {

    log.debug("Getting the list of active verbs for mixer [%s]", mixerName);
    List<GatewayVerb> ids = new ArrayList<GatewayVerb>();
    Selector selector = Pelops.createSelector(schemaName);
    try {
      List<Column> columns =
          selector.getColumnsFromRow("verbs", mixerName, false, ConsistencyLevel.ONE);
      for (Column column : columns) {
        GatewayVerb verb =
            new GatewayVerb(
                mixerName, Bytes.toUTF8(column.getName()), Bytes.toUTF8(column.getValue()));
        ids.add(verb);
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return Collections.EMPTY_LIST;
    }
    return ids;
  }
  @Override
  public Collection<String> getCalls() {

    log.debug("Getting list with all active calls");

    try {
      Selector selector = Pelops.createSelector(schemaName);
      List<String> calls = new ArrayList<String>();
      List<SuperColumn> cols =
          selector.getSuperColumnsFromRow("jids", "nodes", false, ConsistencyLevel.ONE);
      for (SuperColumn col : cols) {
        List<Column> columns = col.getColumns();
        for (Column column : columns) {
          calls.add(Bytes.toUTF8(column.getValue()));
        }
      }
      return calls;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return null;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<String> getClientResources(String bareJid) {

    try {
      log.debug("Finding resources for clients with jid: [%s]", bareJid);
      Selector selector = Pelops.createSelector(schemaName);
      List<Column> resourceColumn =
          selector.getColumnsFromRow("clients", bareJid, false, ConsistencyLevel.ONE);
      List<String> resources = new ArrayList<String>();
      for (Column column : resourceColumn) {
        String name = Bytes.toUTF8(column.getName());
        if (!name.equals("appId")) {
          resources.add(Bytes.toUTF8(column.getName()));
        }
      }
      return resources;
    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
      return Collections.EMPTY_LIST;
    }
  }
  @Override
  public GatewayClient getClient(String jid) {

    log.debug("Finding client with jid: [%s]", jid);
    GatewayClient client = null;
    try {
      String bareJid = JIDUtils.getBareJid(jid);
      String resource = JIDUtils.getResource(jid);
      boolean resourceFound = false;

      Selector selector = Pelops.createSelector(schemaName);
      List<Column> columns =
          selector.getColumnsFromRow("clients", bareJid, false, ConsistencyLevel.ONE);
      if (columns != null && columns.size() > 0) {
        for (Column column : columns) {
          String name = Bytes.toUTF8(column.getName());
          if (name.equals(resource)) {
            resourceFound = true;
          }
        }
      }

      if (resourceFound) {
        Application application = getApplication(JIDUtils.getBareJid(jid));
        if (application != null) {
          client = new GatewayClient();
          client.setJid(jid);
          client.setPlatform(application.getPlatform());
        }
      }

    } catch (PelopsException pe) {
      log.error(pe.getMessage(), pe);
    }
    return client;
  }