Example usage for org.apache.cassandra.utils ByteBufferUtil string

List of usage examples for org.apache.cassandra.utils ByteBufferUtil string

Introduction

In this page you can find the example usage for org.apache.cassandra.utils ByteBufferUtil string.

Prototype

public static String string(ByteBuffer buffer, Charset charset) throws CharacterCodingException 

Source Link

Document

Decode a String representation.

Usage

From source file:com.impetus.client.cassandra.index.InvertedIndexHandlerBase.java

License:Apache License

/**
 * Searches into inverted index based on <code>expression</code> and adds
 * search result to <code>searchResults</code>
 *//*w w w . j  a va  2  s. c o  m*/
private void searchAndAddToResults(EntityMetadata m, String persistenceUnit, ConsistencyLevel consistencyLevel,
        String columnFamilyName, List<SearchResult> searchResults, IndexExpression expression,
        boolean isRowKeyQuery) {
    SearchResult searchResult = new SearchResult();

    byte[] superColumnName = expression.getValue();
    String superColumnNameStr = null;
    String rowKey = null;
    try {
        rowKey = ByteBufferUtil.string(ByteBuffer.wrap(expression.getColumn_name()),
                Charset.forName(Constants.CHARSET_UTF8));
        superColumnNameStr = new String(expression.getValue(), Charset.forName(Constants.CHARSET_UTF8));
    } catch (CharacterCodingException e) {
        log.error("Error while retrieving records {}, Caused by:", e);
        throw new PersistenceException(e);
    }
    Object pk = PropertyAccessorHelper.getObject(m.getIdAttribute().getJavaType(), superColumnName);
    IndexOperator condition = expression.getOp();

    if (log.isInfoEnabled()) {
        log.info("RowKey: {} ; Super column Name: {} on condition.", rowKey, superColumnNameStr, condition);
    }

    // TODO: Second check unnecessary but unavoidable as filter clause
    // property is incorrectly passed as column name

    // Search based on Primary key
    if (isRowKeyQuery && (rowKey.equals(m.getIdAttribute().getName())
            || rowKey.equals(((DefaultSingularAttribute) m.getIdAttribute()).getJPAColumnName()))) {
        if (searchResults.isEmpty()) {
            searchResult.setPrimaryKey(pk);
            searchResults.add(searchResult);
        } else {
            SearchResult existing = searchResults.get(0);
            if (existing.getPrimaryKey() != null && existing.getPrimaryKey().equals(superColumnNameStr)) {
                searchResults.add(searchResult);
            } else {
                searchResults.remove(0);
            }
        }
    } else {
        // Search results in the form of thrift super columns
        List<SuperColumn> thriftSuperColumns = new ArrayList<SuperColumn>();

        switch (condition) {
        // EQUAL Operator
        case EQ:
            SuperColumn thriftSuperColumn = getSuperColumnForRow(consistencyLevel, columnFamilyName, rowKey,
                    superColumnName, persistenceUnit);

            if (thriftSuperColumn != null)
                thriftSuperColumns.add(thriftSuperColumn);
            break;

        // LIKE operation not available
        // Greater than operator
        case GT:
            searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey,
                    superColumnName, thriftSuperColumns, superColumnName, new byte[0]);
            break;
        // Less than Operator
        case LT:
            searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey,
                    superColumnName, thriftSuperColumns, new byte[0], superColumnName);
            break;
        // Greater than-equals to operator
        case GTE:
            searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey,
                    superColumnName, thriftSuperColumns, superColumnName, new byte[0]);
            break;
        // Less than equal to operator
        case LTE:
            searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey,
                    superColumnName, thriftSuperColumns, new byte[0], superColumnName);
            break;

        default:
            throw new QueryHandlerException(
                    condition + " comparison operator not supported currently for Cassandra Inverted Index.");
        }

        // Construct search results out of these thrift columns
        for (SuperColumn thriftSuperColumn : thriftSuperColumns) {

            for (Column column : thriftSuperColumn.getColumns()) {
                byte[] columnName = column.getName();
                searchResult.setPrimaryKey(
                        PropertyAccessorHelper.getObject(m.getIdAttribute().getJavaType(), columnName));
                byte[] columnValue = column.getValue();
                String ecValue = UTF8Type.instance.compose(ByteBuffer.wrap(columnValue));

                if (ecValue != null && !"".equals(ecValue.trim())) {
                    searchResult.setEmbeddedColumnName(
                            rowKey.substring(0, rowKey.indexOf(Constants.INDEX_TABLE_ROW_KEY_DELIMITER)));
                    searchResult.addEmbeddedColumnValue(ecValue);
                }
            }

            if (searchResults.isEmpty()) {
                searchResults.add(searchResult);
            } else {
                SearchResult existing = searchResults.get(0);
                if (existing.getPrimaryKey() != null
                        && existing.getPrimaryKey().equals(searchResult.getPrimaryKey())) {
                    searchResults.add(searchResult);
                } else {
                    searchResults.remove(0);
                }
            }

        }
    }
}