result.schema = new CqlMetadata(Collections.<ByteBuffer, String>emptyMap(),
Collections.<ByteBuffer, String>emptyMap(),
"AsciiType",
"LongType");
List<Column> columns = Collections.singletonList(new Column(countBytes).setValue(ByteBufferUtil.bytes((long) rows.size())));
result.rows = Collections.singletonList(new CqlRow(countBytes, columns));
return result;
}
// otherwise create resultset from query results
result.schema = new CqlMetadata(new HashMap<ByteBuffer, String>(),
new HashMap<ByteBuffer, String>(),
TypeParser.getShortName(metadata.comparator.asAbstractType()),
TypeParser.getShortName(metadata.getDefaultValidator()));
List<CqlRow> cqlRows = new ArrayList<CqlRow>(rows.size());
for (org.apache.cassandra.db.Row row : rows)
{
List<Column> thriftColumns = new ArrayList<Column>();
if (select.isColumnRange())
{
if (select.isFullWildcard())
{
// prepend key
ByteBuffer keyName = ByteBufferUtil.bytes(metadata.getCQL2KeyName());
thriftColumns.add(new Column(keyName).setValue(row.key.getKey()).setTimestamp(-1));
result.schema.name_types.put(keyName, TypeParser.getShortName(AsciiType.instance));
result.schema.value_types.put(keyName, TypeParser.getShortName(metadata.getKeyValidator()));
}
// preserve comparator order
if (row.cf != null)
{
for (org.apache.cassandra.db.Cell c : row.cf.getSortedColumns())
{
if (!c.isLive(now))
continue;
ColumnDefinition cd = metadata.getColumnDefinition(c.name());
if (cd != null)
result.schema.value_types.put(c.name().toByteBuffer(), TypeParser.getShortName(cd.type));
thriftColumns.add(thriftify(c));
}
}
}
else
{
String keyString = metadata.getCQL2KeyName();
// order columns in the order they were asked for
for (Term term : select.getColumnNames())
{
if (term.getText().equalsIgnoreCase(keyString))
{
// preserve case of key as it was requested
ByteBuffer requestedKey = ByteBufferUtil.bytes(term.getText());
thriftColumns.add(new Column(requestedKey).setValue(row.key.getKey()).setTimestamp(-1));
result.schema.name_types.put(requestedKey, TypeParser.getShortName(AsciiType.instance));
result.schema.value_types.put(requestedKey, TypeParser.getShortName(metadata.getKeyValidator()));
continue;
}
if (row.cf == null)
continue;
ByteBuffer nameBytes;
try
{
nameBytes = term.getByteBuffer(metadata.comparator.asAbstractType(), variables);
}
catch (InvalidRequestException e)
{
throw new AssertionError(e);
}
CellName name = metadata.comparator.cellFromByteBuffer(nameBytes);
ColumnDefinition cd = metadata.getColumnDefinition(name);
if (cd != null)
result.schema.value_types.put(nameBytes, TypeParser.getShortName(cd.type));
org.apache.cassandra.db.Cell c = row.cf.getColumn(name);
if (c == null || !c.isLive())
thriftColumns.add(new Column().setName(nameBytes));
else
thriftColumns.add(thriftify(c));
}
}
// Create a new row, add the columns to it, and then add it to the list of rows
CqlRow cqlRow = new CqlRow();
cqlRow.key = row.key.getKey();
cqlRow.columns = thriftColumns;
if (select.isColumnsReversed())
Collections.reverse(cqlRow.columns);
cqlRows.add(cqlRow);