public static void insertPost(Post post, String type) {
    try {
      String postId = post.getId();
      IdNameEntity idNameEntity = post.getFrom();
      String userId = idNameEntity.getId();
      String userName = idNameEntity.getName();
      String commentsCount = post.getComments() == null ? "0" : post.getComments().size() + "";
      String likeCount = post.getLikes() == null ? "0" : post.getLikes().size() + "";

      String columnFamily = COLUMN_FAMILY_FACEBOOK_POST;
      String rowKey = type;
      String superColumn = postId + "-" + userId + "-" + userName;

      Clock clock = new Clock(System.nanoTime());
      Column column = new Column();
      String columnName = commentsCount + "-" + likeCount;
      column.setName(columnName.getBytes(UTF8));
      String columnValue = post.getMessage();
      if (!StringUtils.isBlank(columnValue)) {
        column.setValue(columnValue.getBytes(UTF8));
      } else {
        column.setValue("".getBytes(UTF8));
      }
      column.setTimestamp(clock.timestamp);

      cassandra.insertSuperColumn(columnFamily, rowKey, superColumn, column);
    } catch (UnsupportedEncodingException e) {
      logger.error("[Info: encoding invalid] - [Error: {}]", e.toString());
    }
  }
Exemple #2
0
  /** Test insertion of a supercolumn using insert */
  @Test
  public void testInsertSuper()
      throws IllegalArgumentException, NoSuchElementException, IllegalStateException,
          HNotFoundException, Exception {

    // insert value
    ColumnParent columnParent = new ColumnParent("Super1");
    columnParent.setSuper_column(StringSerializer.get().toByteBuffer("testInsertSuper_super"));
    Column column =
        new Column(
            StringSerializer.get().toByteBuffer("testInsertSuper_column"),
            StringSerializer.get().toByteBuffer("testInsertSuper_value"),
            connectionManager.createClock());

    keyspace.insert(
        StringSerializer.get().toByteBuffer("testInsertSuper_key"), columnParent, column);
    column.setName(StringSerializer.get().toByteBuffer("testInsertSuper_column2"));
    keyspace.insert(
        StringSerializer.get().toByteBuffer("testInsertSuper_key"), columnParent, column);

    // get value and assert
    ColumnPath cp2 = new ColumnPath("Super1");
    cp2.setSuper_column(bytes("testInsertSuper_super"));
    SuperColumn sc = keyspace.getSuperColumn("testInsertSuper_key", cp2);
    assertNotNull(sc);
    assertEquals("testInsertSuper_super", string(sc.getName()));
    assertEquals(2, sc.getColumns().size());
    assertEquals("testInsertSuper_value", string(sc.getColumns().get(0).getValue()));

    // remove value
    keyspace.remove("testInsertSuper_super", cp2);
  }
  @PooledConnection
  public void setColumn(
      String keyspace,
      String column_family,
      String key,
      JSONObject json,
      ConsistencyLevel consistency_level,
      boolean index,
      long timestamp)
      throws InvalidRequestException, UnavailableException, TimedOutException, TException,
          HttpException, IOException {
    List<Mutation> slice = new ArrayList<Mutation>();
    for (Object field : json.keySet()) {
      String name = (String) field;
      String value = (String) json.get(name);
      Column c = new Column();
      c.setName(ByteBufferUtil.bytes(name));
      c.setValue(ByteBufferUtil.bytes(value));
      c.setTimestamp(timestamp);

      Mutation m = new Mutation();
      ColumnOrSuperColumn cc = new ColumnOrSuperColumn();
      cc.setColumn(c);
      m.setColumn_or_supercolumn(cc);
      slice.add(m);
    }
    Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap =
        new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
    Map<String, List<Mutation>> cfMutations = new HashMap<String, List<Mutation>>();
    cfMutations.put(column_family, slice);
    mutationMap.put(ByteBufferUtil.bytes(key), cfMutations);
    getConnection(keyspace).batch_mutate(mutationMap, consistency_level);

    if (config.isIndexingEnabled() && index) indexer.index(column_family, key, json);
  }
 public List<NotificationEntry> getNotifications(String username, String startId) {
   try {
     byte[] timeUUIDAsBytes = EMPTY_BYTE_ARRAY;
     if (startId != null && startId.length() > 0) {
       UUID uuid = UUIDUtil.fromString(startId);
       final byte[] idAsBytes = UUIDUtil.toBytes(uuid);
       timeUUIDAsBytes = getTimeUUIDFromID(username, idAsBytes);
       if (timeUUIDAsBytes == null) {
         timeUUIDAsBytes = EMPTY_BYTE_ARRAY;
       }
     }
     Selector selector = Pelops.createSelector(TRENDOCEAN_POOL, TRENDOCEAN_KEYSPACE);
     SlicePredicate columnPredicate =
         Selector.newColumnsPredicate(timeUUIDAsBytes, EMPTY_BYTE_ARRAY, true, PAGE_SIZE);
     List<SuperColumn> columns =
         selector.getSuperColumnsFromRow(username, WALL_CF, columnPredicate, ConsistencyLevel.ONE);
     List<NotificationEntry> notificationList = new ArrayList<NotificationEntry>(columns.size());
     for (SuperColumn superColumn : columns) {
       List<Column> wallEntryColumns = superColumn.getColumns();
       String notificationId = null;
       NotificationType notificationType = null;
       String questionId = null;
       String relatedUser = null;
       long answerCount = -1L;
       long time = 0;
       for (Column notificationColumn : wallEntryColumns) {
         String notificationColumnName = toString(notificationColumn.getName());
         if (notificationColumnName.equals("_id")) {
           notificationId = UUIDUtil.toUUID(notificationColumn.getValue()).toString();
           long timeInMicroSeconds = notificationColumn.getTimestamp();
           time = timeInMicroSeconds / 1000L;
         } else if (notificationColumnName.equals("_type")) {
           notificationType = NotificationType.fromId(toByte(notificationColumn));
         } else if (notificationColumnName.equals("questionId")) {
           questionId = toString(notificationColumn);
         } else if (notificationColumnName.equals("relatedUser")) {
           relatedUser = toString(notificationColumn);
         } else if (notificationColumnName.equals("answerCount")) {
           answerCount = toLong(notificationColumn);
         } else {
           logger.warn("Invalid notification column:{}", notificationColumnName);
         }
       }
       NotificationEntry notificationEntry = new NotificationEntry();
       notificationEntry.setId(notificationId);
       notificationEntry.setTime(time);
       notificationEntry.setNotificationType(notificationType);
       notificationEntry.setQuestionId(questionId);
       notificationList.add(notificationEntry);
       notificationEntry.setUsername(relatedUser);
       notificationEntry.setAnswerCount(answerCount);
     }
     return notificationList;
   } catch (NotFoundException ex) {
     return Collections.EMPTY_LIST;
   } catch (Exception ex) {
     logger.error("Can not get time uuid", ex);
     throw new TrendOceanException(ex);
   }
 }
  @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;
    }
  }
Exemple #6
0
  public static void main(String[] args) throws HectorException {
    CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
    CassandraClient client = pool.borrowClient("tush", 9160);
    // A load balanced version would look like this:
    // CassandraClient client = pool.borrowClient(new String[] {"cas1:9160", "cas2:9160",
    // "cas3:9160"});

    try {
      Keyspace keyspace = client.getKeyspace("Keyspace1");
      ColumnPath columnPath = new ColumnPath("Standard1");
      ColumnParent columnParent = new ColumnParent("Standard1");
      columnPath.setColumn(bytes("column-name"));

      // insert
      keyspace.insert(
          bytes("key"),
          columnParent,
          new Column(bytes("column-name"), bytes("value"), keyspace.createClock()));

      // read
      Column col = keyspace.getColumn(bytes("key"), columnPath);

      System.out.println("Read from cassandra: " + string(col.getValue()));

      // This line makes sure that even if the client had failures and recovered, a correct
      // releaseClient is called, on the up to date client.
      client = keyspace.getClient();
    } finally {
      // return client to pool. do it in a finally block to make sure it's executed
      pool.releaseClient(client);
    }
  }
Exemple #7
0
  @Test
  public void testBatchMutateBatchMutation() throws HectorException {
    BatchMutation<String> batchMutation = new BatchMutation<String>(StringSerializer.get());
    List<String> columnFamilies = Arrays.asList("Standard1");
    for (int i = 0; i < 10; i++) {

      for (int j = 0; j < 10; j++) {
        Column col =
            new Column(
                StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j),
                StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j),
                connectionManager.createClock());
        batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col);
      }
    }
    keyspace.batchMutate(batchMutation);

    // get value
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        ColumnPath cp = new ColumnPath("Standard1");
        cp.setColumn(bytes("testBatchMutateColumn_" + j));

        Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp);
        assertNotNull(col);
        String value = string(col.getValue());
        assertEquals("testBatchMutateColumn_value_" + j, value);
      }
    }
    batchMutation = new BatchMutation<String>(StringSerializer.get());
    // batch_mutate delete by key
    for (int i = 0; i < 10; i++) {
      SlicePredicate slicePredicate = new SlicePredicate();
      for (int j = 0; j < 10; j++) {
        slicePredicate.addToColumn_names(
            StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j));
      }
      Deletion deletion = new Deletion(connectionManager.createClock());
      deletion.setPredicate(slicePredicate);
      batchMutation.addDeletion("testBatchMutateColumn_" + i, columnFamilies, deletion);
    }
    keyspace.batchMutate(batchMutation);
    // make sure the values are gone
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        ColumnPath cp = new ColumnPath("Standard1");
        cp.setColumn(bytes("testBatchMutateColumn_" + j));
        try {
          keyspace.getColumn("testBatchMutateColumn_" + i, cp);
          fail();
        } catch (HNotFoundException e) {
          // good, we want this to throw.
        }
      }
    }
  }
Exemple #8
0
  /**
   * Insert a record in the database. Any field/value pairs in the specified values HashMap will be
   * written into the record with the specified record key.
   *
   * @param table The name of the table
   * @param key The record key of the record to insert.
   * @param values A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error
   */
  public int insert(String table, String key, HashMap<String, ByteIterator> values) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

    for (int i = 0; i < OperationRetries; i++) {
      if (_debug) {
        System.out.println("Inserting key: " + key);
      }

      try {
        ByteBuffer wrappedKey = ByteBuffer.wrap(key.getBytes("UTF-8"));

        ColumnOrSuperColumn column;
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
          column = new ColumnOrSuperColumn();
          Column subColumn = new Column(ByteBuffer.wrap(entry.getKey().getBytes("UTF-8")));
          subColumn.setValue(ByteBuffer.wrap(entry.getValue().toArray()));
          subColumn.setTimestamp(System.currentTimeMillis());
          column.setColumn(subColumn);

          mutations.add(new Mutation().setColumn_or_supercolumn(column));
        }

        mutationMap.put(column_family, mutations);
        record.put(wrappedKey, mutationMap);

        client.batch_mutate(record, ConsistencyLevel.ONE);

        mutations.clear();
        mutationMap.clear();
        record.clear();

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }

    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }
Exemple #9
0
  @Test
  public void testBatchUpdateInsertAndDelOnSame() throws HectorException {

    ColumnPath sta1 = new ColumnPath("Standard1");
    sta1.setColumn(bytes("deleteThroughInserBatch_col"));

    keyspace.insert(
        "deleteThroughInserBatch_key",
        sta1,
        StringSerializer.get().toByteBuffer("deleteThroughInserBatch_val"));

    Column found = keyspace.getColumn("deleteThroughInserBatch_key", sta1);
    assertNotNull(found);

    BatchMutation<String> batchMutation = new BatchMutation<String>(StringSerializer.get());
    List<String> columnFamilies = Arrays.asList("Standard1");
    for (int i = 0; i < 10; i++) {

      for (int j = 0; j < 10; j++) {
        Column col =
            new Column(
                StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j),
                StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j),
                connectionManager.createClock());
        batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col);
      }
    }
    SlicePredicate slicePredicate = new SlicePredicate();
    slicePredicate.addToColumn_names(
        StringSerializer.get().toByteBuffer("deleteThroughInserBatch_col"));

    Deletion deletion = new Deletion(connectionManager.createClock());
    deletion.setPredicate(slicePredicate);

    batchMutation.addDeletion("deleteThroughInserBatch_key", columnFamilies, deletion);
    keyspace.batchMutate(batchMutation);
    try {
      keyspace.getColumn("deleteThroughInserBatch_key", sta1);
      fail("Should not have found a value here");
    } catch (Exception e) {
    }
    // get value
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        ColumnPath cp = new ColumnPath("Standard1");
        cp.setColumn(bytes("testBatchMutateColumn_" + j));

        Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp);
        assertNotNull(col);
        String value = string(col.getValue());
        assertEquals("testBatchMutateColumn_value_" + j, value);
      }
    }
  }
Exemple #10
0
  private static Mutation getMutation(String colName, String value) {

    Column c = new Column();

    c.setName(ByteBufferUtil.bytes(colName));

    c.setValue(ByteBufferUtil.bytes(value));
    c.setTimestamp(System.currentTimeMillis());

    Mutation m = new Mutation();
    m.setColumn_or_supercolumn(new ColumnOrSuperColumn());
    m.column_or_supercolumn.setColumn(c);
    return m;
  }
  private RayoNode buildNode(List<Column> columns) {

    if (columns != null && columns.size() > 0) {
      RayoNode node = new RayoNode();
      for (Column column : columns) {
        String name = Bytes.toUTF8(column.getName());
        if (name.equals("ip")) {
          node.setIpAddress(Bytes.toUTF8(column.getValue()));
        }
        if (name.equals("weight")) {
          node.setWeight(Integer.parseInt(Bytes.toUTF8(column.getValue())));
        }
        if (name.equals("priority")) {
          node.setPriority(Integer.parseInt(Bytes.toUTF8(column.getValue())));
        }
        if (name.equals("consecutive-errors")) {
          node.setConsecutiveErrors(Integer.parseInt(Bytes.toUTF8(column.getValue())));
        }
        if (name.equals("blacklisted")) {
          node.setBlackListed(Boolean.valueOf(Bytes.toUTF8(column.getValue())));
        }
        if (name.equals("platforms")) {
          node.setPlatforms(
              new HashSet<String>(
                  Arrays.asList(StringUtils.split(Bytes.toUTF8(column.getValue()), ","))));
        }
      }
      return node;
    }
    return null;
  }
 private byte[] getTimeUUIDFromID(String username, byte[] idAsBytes) {
   try {
     Selector selector = Pelops.createSelector(TRENDOCEAN_POOL, TRENDOCEAN_KEYSPACE);
     Column column =
         selector.getColumnFromRow(username, ID_TO_TIMEUUID_CF, idAsBytes, ConsistencyLevel.ONE);
     byte[] timeUUIDAsBytes = column.getValue();
     return timeUUIDAsBytes;
   } catch (NotFoundException ex) {
     return null;
   } catch (Exception ex) {
     logger.error("Can not get time uuid", ex);
     throw new TrendOceanException(ex);
   }
 }
  @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;
  }
 /**
  * Purge expired entries. Expiration entries are stored in a single key (expirationKey) within a
  * specific ColumnFamily (set by configuration). The entries are grouped by expiration timestamp
  * in SuperColumns within which each entry's key is mapped to a column
  */
 @Override
 protected void purgeInternal() throws CacheLoaderException {
   if (trace) log.trace("purgeInternal");
   Cassandra.Client cassandraClient = null;
   try {
     cassandraClient = dataSource.getConnection();
     // We need to get all supercolumns from the beginning of time until
     // now, in SLICE_SIZE chunks
     SlicePredicate predicate = new SlicePredicate();
     predicate.setSlice_range(
         new SliceRange(
             ByteBufferUtil.EMPTY_BYTE_BUFFER,
             ByteBufferUtil.bytes(System.currentTimeMillis()),
             false,
             SLICE_SIZE));
     Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap =
         new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
     for (boolean complete = false; !complete; ) {
       // Get all columns
       List<ColumnOrSuperColumn> slice =
           cassandraClient.get_slice(
               expirationKey, expirationColumnParent, predicate, readConsistencyLevel);
       complete = slice.size() < SLICE_SIZE;
       // Delete all keys returned by the slice
       for (ColumnOrSuperColumn crumb : slice) {
         SuperColumn scol = crumb.getSuper_column();
         for (Iterator<Column> i = scol.getColumnsIterator(); i.hasNext(); ) {
           Column col = i.next();
           // Remove the entry row
           remove0(ByteBuffer.wrap(col.getName()), mutationMap);
         }
         // Remove the expiration supercolumn
         addMutation(
             mutationMap,
             expirationKey,
             config.expirationColumnFamily,
             ByteBuffer.wrap(scol.getName()),
             null,
             null);
       }
     }
     cassandraClient.batch_mutate(mutationMap, writeConsistencyLevel);
   } catch (Exception e) {
     throw new CacheLoaderException(e);
   } finally {
     dataSource.releaseConnection(cassandraClient);
   }
 }
  @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 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 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;
  }
  private GatewayCall buildCall(List<Column> columns, String id) {

    if (columns != null && columns.size() > 0) {
      GatewayCall call = new GatewayCall();
      call.setCallId(id);
      for (Column column : columns) {
        String name = Bytes.toUTF8(column.getName());
        if (name.equals("node")) {
          call.setNodeJid(Bytes.toUTF8(column.getValue()));
        }
        if (name.equals("jid")) {
          call.setClientJid(Bytes.toUTF8(column.getValue()));
        }
      }
      return call;
    }
    return null;
  }
  private GatewayMixer buildMixer(List<Column> columns, String name) {

    String nodeJid = null;
    List<String> participants = new ArrayList<String>();
    if (columns != null && columns.size() > 0) {
      for (Column column : columns) {
        String columnName = Bytes.toUTF8(column.getName());
        if (columnName.equals("node")) {
          nodeJid = Bytes.toUTF8(column.getValue());
        } else {
          participants.add(Bytes.toUTF8(column.getName()));
        }
      }
      if (nodeJid != null) {
        GatewayMixer mixer = new GatewayMixer(name, nodeJid);
        mixer.addCalls(participants);
        return mixer;
      }
    }
    return null;
  }
  @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;
    }
  }
  @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;
  }
Exemple #24
0
  @Test
  public void testInsertAndGetAndRemove()
      throws IllegalArgumentException, NoSuchElementException, IllegalStateException,
          HNotFoundException, Exception {

    // insert value
    ColumnPath cp = new ColumnPath("Standard1");
    cp.setColumn(bytes("testInsertAndGetAndRemove"));
    for (int i = 0; i < 100; i++) {
      keyspace.insert(
          "testInsertAndGetAndRemove_" + i,
          cp,
          StringSerializer.get().toByteBuffer("testInsertAndGetAndRemove_value_" + i));
    }

    // get value
    for (int i = 0; i < 100; i++) {
      Column col = keyspace.getColumn("testInsertAndGetAndRemove_" + i, cp);
      assertNotNull(col);
      String value = string(col.getValue());
      assertEquals("testInsertAndGetAndRemove_value_" + i, value);
    }

    // remove value
    for (int i = 0; i < 100; i++) {
      keyspace.remove("testInsertAndGetAndRemove_" + i, cp);
    }

    // get already removed value
    for (int i = 0; i < 100; i++) {
      try {
        keyspace.getColumn("testInsertAndGetAndRemove_" + i, cp);
        fail("the value should already being deleted");
      } catch (HNotFoundException e) {
        // good
      }
    }
  }
  public static Column createCol(String name, String value) {
    Column result = new Column();
    long time = System.currentTimeMillis();
    try {

      result = new Column(toByteBuffer(name));
      if ((name.equals(Status.colname_createdate)
              || name.equals(Tag.name_col_cdate)
              || name.equals(Status.colname_modifydate)
              || name.equals(Tag.name_col_mdate))
          && "".equals(value)) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        result.setValue(toByteBuffer(sdf.format(cal.getTime())));
      } else result.setValue(toByteBuffer(value));
      result.setTimestamp(time);

    } catch (UnsupportedEncodingException ex) {

      Logger.getLogger(GeneralHandling.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
  }
  public static void insertPostPopular(Post post, String type, Integer position) {
    try {
      String postId = post.getId();
      IdNameEntity idNameEntity = post.getFrom();
      String userId = idNameEntity.getId();
      String userName = idNameEntity.getName();

      String columnFamily = COLUMN_FAMILY_FACEBOOK_POST_POPULAR;
      String rowKey = (type + "-" + position);

      Clock clock = new Clock(System.nanoTime());
      Column column = new Column();
      String columnName = (postId + "-" + userId + "-" + userName);
      column.setName(columnName.getBytes(UTF8));
      String columnValue = post.getMessage();
      column.setValue(columnValue.getBytes(UTF8));
      column.setTimestamp(clock.timestamp);

      cassandra.insertColumn(columnFamily, rowKey, column);
    } catch (UnsupportedEncodingException e) {
      logger.error("[Info: encoding invalid] - [Error: {}]", e.toString());
    }
  }
  @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;
  }
  @Override
  public ColumnListMutation<C> putEmptyColumn(C columnName, Integer ttl) {
    Column column = new Column();
    column.setName(columnSerializer.toByteBuffer(columnName));
    column.setValue(ThriftUtils.EMPTY_BYTE_BUFFER);
    column.setTimestamp(timestamp);
    if (ttl != null) column.setTtl(ttl);
    else if (defaultTtl != null) column.setTtl(defaultTtl);

    // 2. Create a mutation and append to the mutation list.
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(column));
    mutationList.add(mutation);
    return this;
  }
  private void populateApplicationData(Application application, List<Column> columns) {

    for (Column column : columns) {
      String name = Bytes.toUTF8(column.getName());
      if (name.equals("appId")) {
        application.setAppId(Bytes.toUTF8((column.getValue())));
      }
      if (name.equals("platformId")) {
        application.setPlatform(Bytes.toUTF8((column.getValue())));
      }
      if (name.equals("name")) {
        application.setName(Bytes.toUTF8((column.getValue())));
      }
      if (name.equals("accountId")) {
        application.setAccountId(Bytes.toUTF8((column.getValue())));
      }
      if (name.equals("permissions")) {
        application.setPermissions(Bytes.toUTF8((column.getValue())));
      }
    }
  }
  @Override
  public <V> ColumnListMutation<C> putColumn(
      C columnName, V value, Serializer<V> valueSerializer, Integer ttl) {
    // 1. Set up the column with all the data
    Column column = new Column();
    column.setName(columnSerializer.toByteBuffer(columnName));
    column.setValue(valueSerializer.toByteBuffer(value));
    column.setTimestamp(timestamp);
    if (ttl != null) column.setTtl(ttl);
    else if (defaultTtl != null) column.setTtl(defaultTtl);

    // 2. Create a mutation and append to the mutation list.
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(column));
    mutationList.add(mutation);

    return this;
  }