me.prettyprint.hector.api.query.SliceQuery Java Examples

The following examples show how to use me.prettyprint.hector.api.query.SliceQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpsCiStateDao.java    From oneops with Apache License 2.0 6 votes vote down vote up
public List<CiChangeStateEvent> getCiStateHistory(long ciId, Long startTime, Long endTime, Integer count) {

        if (count == null) count = 1000;
        List<CiChangeStateEvent> states = new ArrayList<CiChangeStateEvent>();
        SliceQuery<Long, Long, String> sliceQuery = HFactory.createSliceQuery(keyspace, longSerializer, longSerializer, stringSerializer);
        sliceQuery.setColumnFamily(SchemaBuilder.CI_STATE_HIST_CF);
        sliceQuery.setRange(startTime, endTime, false, count);
        sliceQuery.setKey(ciId);
        QueryResult<ColumnSlice<Long, String>> result = sliceQuery.execute();
        ColumnSlice<Long, String> resultCols = result.get();
        for (HColumn<Long, String> col : resultCols.getColumns()) {
            CiChangeStateEvent event = gson.fromJson(col.getValue(), CiChangeStateEvent.class);
            states.add(event);
        }
        return states;
    }
 
Example #2
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Get the list of users mapped to a role.
 */
@Override
public String[] doGetUserListOfRole(String roleName, String filter) throws UserStoreException {

    List<String> usersList = new ArrayList<String>();
    Composite key = new Composite();
    key.addComponent(roleName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);
    SliceQuery<Composite, String, String> query = HFactory
            .createSliceQuery(keyspace, CompositeSerializer.get(), StringSerializer.get(), StringSerializer.get())
            .setKey(key).setColumnFamily(CFConstants.UM_ROLE_USER_INDEX);

    ColumnSliceIterator<Composite, String, String> iterator = new ColumnSliceIterator<Composite, String, String>(
            query, null, "\uFFFF", false);

    while (iterator.hasNext()) {
        HColumn<String, String> column = iterator.next();
        usersList.add(column.getValue());
    }
    return usersList.toArray(new String[usersList.size()]);
}
 
Example #3
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the external role list of a user.
 */
@Override
public String[] doGetExternalRoleListOfUser(String userName, String filter) throws UserStoreException {

    List<String> roles = new ArrayList<String>();
    int arrayLength = 0;
    Composite key = new Composite();
    key.addComponent(userName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);
    SliceQuery<Composite, String, String> query = HFactory
            .createSliceQuery(keyspace, CompositeSerializer.get(), StringSerializer.get(), StringSerializer.get())
            .setKey(key).setColumnFamily(CFConstants.UM_USER_ROLE);

    ColumnSliceIterator<Composite, String, String> iterator = new ColumnSliceIterator<Composite, String, String>(
            query, null, "\uFFFF", false);

    while (iterator.hasNext()) {
        HColumn<String, String> column = iterator.next();
        roles.add(column.getValue());
    }
    return roles.toArray(new String[arrayLength]);
}
 
Example #4
Source File: AbstractSearch.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/**
 * This method intentionally swallows ordered execution issues.  For some reason, our Time UUID ordering does
 * not agree with the cassandra comparator as our micros get very close
 * @param query
 * @param <K>
 * @param <UUID>
 * @param <V>
 * @return
 */
protected static <K, UUID, V> List<HColumn<UUID, V>> swallowOrderedExecution( final SliceQuery<K, UUID, V> query ) {
    try {

        return query.execute().get().getColumns();
    }
    catch ( HInvalidRequestException e ) {
        //invalid request.  Occasionally we get order issues when there shouldn't be, disregard them.

        final Throwable invalidRequestException = e.getCause();

        if ( invalidRequestException instanceof InvalidRequestException
                //we had a range error
                && ( ( InvalidRequestException ) invalidRequestException ).getWhy().contains(
                "range finish must come after start in the order of traversal" )) {
            return Collections.emptyList();
        }

        throw e;
    }
}
 
Example #5
Source File: QueueManagerImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public Message getMessage( UUID messageId ) {
    SliceQuery<UUID, String, ByteBuffer> q =
            createSliceQuery( cass.getApplicationKeyspace( applicationId ), ue, se, be );
    q.setColumnFamily( MESSAGE_PROPERTIES.getColumnFamily() );
    q.setKey( messageId );
    q.setRange( null, null, false, ALL_COUNT );
    QueryResult<ColumnSlice<String, ByteBuffer>> r = q.execute();
    ColumnSlice<String, ByteBuffer> slice = r.get();
    List<HColumn<String, ByteBuffer>> results = slice.getColumns();
    return deserializeMessage( results );
}
 
Example #6
Source File: QueueManagerImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public Set<String> getQueueCounterNames( String queuePath ) throws Exception {
    Set<String> names = new HashSet<String>();
    Keyspace ko = cass.getApplicationKeyspace( applicationId );
    SliceQuery<String, String, ByteBuffer> q = createSliceQuery( ko, se, se, be );
    q.setColumnFamily( QueuesCF.QUEUE_DICTIONARIES.toString() );
    q.setKey( CassandraPersistenceUtils.key( getQueueId( queuePath ), DICTIONARY_COUNTERS ).toString() );
    q.setRange( null, null, false, ALL_COUNT );

    List<HColumn<String, ByteBuffer>> columns = q.execute().get().getColumns();
    for ( HColumn<String, ByteBuffer> column : columns ) {
        names.add( column.getName() );
    }
    return names;
}
 
Example #7
Source File: QueueManagerImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public Queue getQueue( String queuePath, UUID queueId ) {
    SliceQuery<UUID, String, ByteBuffer> q =
            createSliceQuery( cass.getApplicationKeyspace( applicationId ), ue, se, be );
    q.setColumnFamily( QUEUE_PROPERTIES.getColumnFamily() );
    q.setKey( queueId );
    q.setRange( null, null, false, ALL_COUNT );
    QueryResult<ColumnSlice<String, ByteBuffer>> r = q.execute();
    ColumnSlice<String, ByteBuffer> slice = r.get();
    List<HColumn<String, ByteBuffer>> results = slice.getColumns();
    return deserializeQueue( results );
}
 
Example #8
Source File: ConsumerTransaction.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/**
 * Get all pending transactions that have timed out
 *
 * @param queueId The queue id
 * @param consumerId The consumer id
 * @param params The server params
 * @param startTimeUUID The start time
 */
protected List<TransactionPointer> getConsumerIds( UUID queueId, UUID consumerId, SearchParam params,
                                                   UUID startTimeUUID )
{

    SliceQuery<ByteBuffer, UUID, UUID> q = createSliceQuery( ko, be, ue, ue );
    q.setColumnFamily( CONSUMER_QUEUE_TIMEOUTS.getColumnFamily() );
    q.setKey( getQueueClientTransactionKey( queueId, consumerId ) );
    q.setRange( params.startId, startTimeUUID, false, params.limit + 1 );

    List<HColumn<UUID, UUID>> cassResults = swallowOrderedExecution(q);

    List<TransactionPointer> results = new ArrayList<TransactionPointer>( params.limit );

    for ( HColumn<UUID, UUID> column : cassResults )
    {

        if ( logger.isTraceEnabled() )
        {
            logger.trace( "Adding uuid '{}' for original message '{}' to results for queue '{}' and consumer '{}'",
                    column.getName(), column.getValue(), queueId, consumerId );
            logger.trace( "Max timeuuid : '{}', Current timeuuid : '{}', comparison '{}'",
                    startTimeUUID, column.getName(), UUIDUtils.compare( startTimeUUID, column.getName() )
            );
        }

        results.add( new TransactionPointer( column.getName(), column.getValue() ) );
    }

    return results;
}
 
Example #9
Source File: ConsumerTransaction.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public boolean hasOutstandingTransactions( UUID queueId, UUID consumerId )
{
    SliceQuery<ByteBuffer, UUID, UUID> q = createSliceQuery( ko, be, ue, ue );
    q.setColumnFamily( CONSUMER_QUEUE_TIMEOUTS.getColumnFamily() );
    q.setKey( getQueueClientTransactionKey( queueId, consumerId ) );
    q.setRange( null, null, false, 1 );
    return q.execute().get().getColumns().size() > 0;
}
 
Example #10
Source File: CassandraService.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the columns.
 *
 * @param ko the keyspace
 * @param columnFamily the column family
 * @param key the key
 *
 * @return columns
 *
 * @throws Exception the exception
 */
public <N, V> List<HColumn<N, V>> getAllColumns( Keyspace ko, Object columnFamily, Object key,
                                                 Serializer<N> nameSerializer, Serializer<V> valueSerializer )
        throws Exception {

    if ( db_logger.isTraceEnabled() ) {
        db_logger.trace( "getColumns cf={} key={}", columnFamily, key );
    }

    SliceQuery<ByteBuffer, N, V> q = createSliceQuery( ko, be, nameSerializer, valueSerializer );
    q.setColumnFamily( columnFamily.toString() );
    q.setKey( bytebuffer( key ) );
    q.setRange( null, null, false, ALL_COUNT );
    QueryResult<ColumnSlice<N, V>> r = q.execute();
    ColumnSlice<N, V> slice = r.get();
    List<HColumn<N, V>> results = slice.getColumns();

    if ( db_logger.isTraceEnabled() ) {
        if ( results == null ) {
            db_logger.trace( "getColumns returned null" );
        }
        else {
            db_logger.trace( "getColumns returned {} columns", results.size() );
        }
    }

    return results;
}
 
Example #11
Source File: CassandraService.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the columns.
 *
 * @param ko the keyspace
 * @param columnFamily the column family
 * @param key the key
 * @param columnNames the column names
 *
 * @return columns
 *
 * @throws Exception the exception
 */
@SuppressWarnings("unchecked")
public <N, V> List<HColumn<N, V>> getColumns( Keyspace ko, Object columnFamily, Object key, Set<String> columnNames,
                                              Serializer<N> nameSerializer, Serializer<V> valueSerializer )
        throws Exception {

    if ( db_logger.isTraceEnabled() ) {
        db_logger.trace( "getColumns cf={} key={} names={}", columnFamily, key, columnNames );
    }

    SliceQuery<ByteBuffer, N, V> q = createSliceQuery( ko, be, nameSerializer, valueSerializer );
    q.setColumnFamily( columnFamily.toString() );
    q.setKey( bytebuffer( key ) );
    // q.setColumnNames(columnNames.toArray(new String[0]));
    q.setColumnNames( ( N[] ) nameSerializer.fromBytesSet( se.toBytesSet( new ArrayList<String>( columnNames ) ) )
                                            .toArray() );

    QueryResult<ColumnSlice<N, V>> r = q.execute();
    ColumnSlice<N, V> slice = r.get();
    List<HColumn<N, V>> results = slice.getColumns();

    if ( db_logger.isTraceEnabled() ) {
        if ( results == null ) {
            db_logger.trace( "getColumns returned null" );
        }
        else {
            db_logger.trace( "getColumns returned {} columns", results.size());
        }
    }

    return results;
}
 
Example #12
Source File: CassandraService.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public <N, V> ColumnSlice<N, V> getColumns( Keyspace ko, Object columnFamily, Object key, N[] columns,
                                            Serializer<N> nameSerializer, Serializer<V> valueSerializer )
        throws Exception {

    if ( db_logger.isTraceEnabled() ) {
        db_logger.trace( "getColumn cf={} key={} column={}", columnFamily, key, columns );
    }

/*
 * ByteBuffer column_bytes = null; if (column instanceof List) {
 * column_bytes = Composite.serializeToByteBuffer((List<?>) column); } else
 * { column_bytes = bytebuffer(column); }
 */

    SliceQuery<ByteBuffer, N, V> q = HFactory.createSliceQuery( ko, be, nameSerializer, valueSerializer );
    QueryResult<ColumnSlice<N, V>> r =
            q.setKey( bytebuffer( key ) ).setColumnNames( columns ).setColumnFamily( columnFamily.toString() )
             .execute();
    ColumnSlice<N, V> result = r.get();

    if ( db_logger.isTraceEnabled() ) {
        if ( result == null ) {
            db_logger.trace( "getColumn returned null" );
        }
    }

    return result;
}
 
Example #13
Source File: ConsumerTransaction.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Renew the existing transaction. Does so by deleting the exiting timeout, and replacing it with a new value
 *
 * @param queuePath The queue path
 * @param transactionId The transaction id
 * @param query The query params
 *
 * @return The new transaction uuid
 */
public UUID renewTransaction( String queuePath, UUID transactionId, QueueQuery query )
        throws TransactionNotFoundException
{
    long now = System.currentTimeMillis();

    if ( query == null )
    {
        query = new QueueQuery();
    }

    UUID queueId = getQueueId( queuePath );
    UUID consumerId = getConsumerId( queueId, query );
    ByteBuffer key = getQueueClientTransactionKey( queueId, consumerId );

    // read the original transaction, if it's not there, then we can't possibly
    // extend it
    SliceQuery<ByteBuffer, UUID, UUID> q = createSliceQuery( ko, be, ue, ue );
    q.setColumnFamily( CONSUMER_QUEUE_TIMEOUTS.getColumnFamily() );
    q.setKey( key );
    q.setColumnNames( transactionId );

    HColumn<UUID, UUID> col = q.execute().get().getColumnByName( transactionId );

    if ( col == null )
    {
        throw new TransactionNotFoundException(
                String.format( "No transaction with id %s exists", transactionId ) );
    }

    UUID origTrans = col.getName();
    UUID messageId = col.getValue();

    // Generate a new expiration and insert it
    UUID expirationId = UUIDUtils.newTimeUUID( now + query.getTimeout() );

    if (logger.isTraceEnabled()) {
        logger.trace("Writing new timeout at '{}' for message '{}'", expirationId, messageId);
    }


    Mutator<ByteBuffer> mutator = CountingMutator.createFlushingMutator( ko, be );

    mutator.addInsertion( key, CONSUMER_QUEUE_TIMEOUTS.getColumnFamily(),
            createColumn( expirationId, messageId, cass.createTimestamp(), ue, ue ) );

    mutator.execute();

    // now delete the old value
    deleteTransaction( queueId, consumerId, origTrans );

    return expirationId;
}
 
Example #14
Source File: CassandraService.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the columns.
 *
 * @param ko the keyspace
 * @param columnFamily the column family
 * @param key the key
 * @param start the start
 * @param finish the finish
 * @param count the count
 * @param reversed the reversed
 *
 * @return columns
 *
 * @throws Exception the exception
 */
public List<HColumn<ByteBuffer, ByteBuffer>> getColumns( Keyspace ko, Object columnFamily, Object key, Object start,
                                                         Object finish, int count, boolean reversed )
        throws Exception {

    if ( db_logger.isTraceEnabled() ) {
        db_logger.debug( "getColumns cf=" + columnFamily + " key=" + key + " start=" + start + " finish=" + finish
                + " count=" + count + " reversed=" + reversed );
    }

    SliceQuery<ByteBuffer, ByteBuffer, ByteBuffer> q = createSliceQuery( ko, be, be, be );
    q.setColumnFamily( columnFamily.toString() );
    q.setKey( bytebuffer( key ) );

    ByteBuffer start_bytes = null;
    if ( start instanceof DynamicComposite ) {
        start_bytes = ( ( DynamicComposite ) start ).serialize();
    }
    else if ( start instanceof List ) {
        start_bytes = DynamicComposite.toByteBuffer( ( List<?> ) start );
    }
    else {
        start_bytes = bytebuffer( start );
    }

    ByteBuffer finish_bytes = null;
    if ( finish instanceof DynamicComposite ) {
        finish_bytes = ( ( DynamicComposite ) finish ).serialize();
    }
    else if ( finish instanceof List ) {
        finish_bytes = DynamicComposite.toByteBuffer( ( List<?> ) finish );
    }
    else {
        finish_bytes = bytebuffer( finish );
    }

/*
 * if (reversed) { q.setRange(finish_bytes, start_bytes, reversed, count); }
 * else { q.setRange(start_bytes, finish_bytes, reversed, count); }
 */
    q.setRange( start_bytes, finish_bytes, reversed, count );
    QueryResult<ColumnSlice<ByteBuffer, ByteBuffer>> r = q.execute();
    ColumnSlice<ByteBuffer, ByteBuffer> slice = r.get();
    List<HColumn<ByteBuffer, ByteBuffer>> results = slice.getColumns();

    if ( db_logger.isTraceEnabled() ) {
        if ( results == null ) {
            db_logger.trace("getColumns returned null");
        }
        else {
            db_logger.trace("getColumns returned {} columns", results.size());
        }
    }

    return results;
}