All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.astyanax.cql.reads.model.DirectCqlResult Maven / Gradle / Ivy

package com.netflix.astyanax.cql.reads.model;

import java.util.ArrayList;
import java.util.List;

import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.netflix.astyanax.Serializer;
import com.netflix.astyanax.cql.util.CqlTypeMapping;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.CqlResult;
import com.netflix.astyanax.model.Rows;

/**
 * Impl for {@link CqlResult} that parses the {@link ResultSet} from java driver.
 * Note that the class does not interpret the column family definition but instead simply
 * parses each CQL column returned in the result set as an Astyanax column. 
 * Hence this class is applicable in instances where the table is considered to be a flat CQL table.  
 * 
 * @author poberai
 *
 * @param 
 * @param 
 */
public class DirectCqlResult implements CqlResult {

	private Long number = null;
	private CqlRowListImpl rows; 
	
	public DirectCqlResult(List rows, ColumnFamily cf) {
		
		List> rowList = new ArrayList>();
		
		for (Row row : rows) {
			rowList.add(getAstyanaxRow(row, cf));
		}
		this.rows = new CqlRowListImpl(rowList);
	}

	public DirectCqlResult(Long number) {
		this.number = number;
	}
	
	@Override
	public Rows getRows() {
		return this.rows;
	}

	@Override
	public int getNumber() {
		return number.intValue();
	}

	@Override
	public boolean hasRows() {
		return rows != null && rows.size() > 0;
	}

	@Override
	public boolean hasNumber() {
		return this.number != null;
	}

	private com.netflix.astyanax.model.Row getAstyanaxRow(Row row, ColumnFamily cf) {
		CqlRowImpl rowImpl = new CqlRowImpl(getAstyanaxRowKey(row, cf), getAstyanaxColumnList(row), cf);
		return rowImpl;
	}
	
	private K getAstyanaxRowKey(Row row, ColumnFamily cf) {
		
		Serializer keySerializer = cf.getKeySerializer();
		return (K) CqlTypeMapping.getDynamicColumn(row, keySerializer, 0, cf);
	}
	
	private CqlColumnListImpl getAstyanaxColumnList(Row row) {
		
		List> list = new ArrayList>();

		List colDefs  = row.getColumnDefinitions().asList();
		int index = 0;
		
		for (Definition colDef : colDefs) {
			C columnName = (C) colDef.getName();
			list.add(new CqlColumnImpl(columnName, row, index, colDef));
			index++;
		}
		
		return new CqlColumnListImpl(list);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy