ByteBuffer countBytes = ByteBufferUtil.bytes("count");
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
thriftColumns.add(new Column(metadata.getKeyName()).setValue(row.key.key).setTimestamp(-1));
result.schema.name_types.put(metadata.getKeyName(), TypeParser.getShortName(AsciiType.instance));
result.schema.value_types.put(metadata.getKeyName(), TypeParser.getShortName(metadata.getKeyValidator()));
}
// preserve comparator order
if (row.cf != null)
{
for (IColumn c : row.cf.getSortedColumns())
{
if (c.isMarkedForDelete())
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 = getKeyString(metadata);
// 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()));
IColumn c = row.cf.getColumn(name);
if (c == null || c.isMarkedForDelete())
thriftColumns.add(new Column().setName(name));
else
thriftColumns.add(thriftify(c));
}
}