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),
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.key).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.Column c : row.cf.getSortedColumns())
{
if (c.isMarkedForDelete(now))
continue;
ColumnDefinition cd = metadata.getColumnDefinitionFromColumnName(c.name());
if (cd != null)
result.schema.value_types.put(c.name(), TypeParser.getShortName(cd.getValidator()));
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.key).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 name;
try
{
name = term.getByteBuffer(metadata.comparator, variables);
}
catch (InvalidRequestException e)
{
throw new AssertionError(e);
}
ColumnDefinition cd = metadata.getColumnDefinitionFromColumnName(name);
if (cd != null)
result.schema.value_types.put(name, TypeParser.getShortName(cd.getValidator()));
org.apache.cassandra.db.Column c = row.cf.getColumn(name);
if (c == null || c.isMarkedForDelete(now))
thriftColumns.add(new Column().setName(name));
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.key;
cqlRow.columns = thriftColumns;
if (select.isColumnsReversed())
Collections.reverse(cqlRow.columns);
cqlRows.add(cqlRow);