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

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

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

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.Row;
import com.netflix.astyanax.Serializer;
import com.netflix.astyanax.cql.schema.CqlColumnFamilyDefinitionImpl;
import com.netflix.astyanax.cql.util.CqlTypeMapping;
import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.ColumnList;

/**
 * Class that implements the {@link ColumnList} interface. Note that this class handles the case where the table schema
 * could contain a clustering key or just regular columns for a flat table. 
 * 
 * In the case of a flat table, each row has a unique set of columns. In the case of a clustering key, each row is a unique column.
 * There are 2 separate constructors to this class in order to handle each of these cases. 
 * 
 * @author poberai
 *
 * @param 
 */
@SuppressWarnings("unchecked")
public class CqlColumnListImpl implements ColumnList {

	private List> columnList = new ArrayList>();
	private LinkedHashMap> map = new LinkedHashMap>();
	
	public CqlColumnListImpl() {

	}
	
	/**
	 * This constructor is meant to be called when we have a table with standard columns i.e no composites, just plain columns
	 * @param row
	 */
	public CqlColumnListImpl(Row row, ColumnFamily cf) {
		
		ColumnDefinitions cfDefinitions = row.getColumnDefinitions();
		
		int index = 1; // skip the key column
		while (index < cfDefinitions.size()) {
			String columnName = cfDefinitions.getName(index); 
			CqlColumnImpl cqlCol = new CqlColumnImpl((C) columnName, row, index);
			columnList.add(cqlCol);
			map.put((C) columnName, cqlCol);
			index+=3;  // skip past the ttl and the timestamp
		}
	}


	/**
	 * This constructor is meant to be used when we are using the CQL3 table but still in the legacy thrift mode
	 * @param rows
	 */
	public CqlColumnListImpl(List rows, ColumnFamily cf) {
		
		CqlColumnFamilyDefinitionImpl cfDef = (CqlColumnFamilyDefinitionImpl) cf.getColumnFamilyDefinition();
		
		int columnNameIndex = cfDef.getPartitionKeyColumnDefinitionList().size();  
		
		for (Row row : rows) {
			Object columnName = CqlTypeMapping.getDynamicColumn(row, cf.getColumnSerializer(), columnNameIndex, cf);
			int valueIndex = cfDef.getPartitionKeyColumnDefinitionList().size() + cfDef.getClusteringKeyColumnDefinitionList().size();
			
			CqlColumnImpl cqlCol = new CqlColumnImpl((C) columnName, row, valueIndex);
			columnList.add(cqlCol);
			map.put((C) columnName, cqlCol);
		}
	}
	
	public CqlColumnListImpl(List> newColumnList) {
		this.columnList.clear();
		for (Column column : newColumnList) {
			columnList.add(column);
			map.put(column.getName(), column);
		}
	}
	
	public void trimFirstColumn() {
		if (columnList.size() == 0) {
			return;
		}
		Column firstCol = this.columnList.remove(0);
		map.remove(firstCol.getName());
	}

	@Override
	public Iterator> iterator() {
		return columnList.iterator();
	}

	@Override
	public Collection getColumnNames() {
		return map.keySet();
	}

	@Override
	public Column getColumnByName(C columnName) {
		return map.get(columnName);
	}

	@Override
	public String getStringValue(C columnName, String defaultValue) {
		
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getStringValue();
		}
	}

	@Override
	public String getCompressedStringValue(C columnName, String defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getCompressedStringValue();
		}
	}

	@Override
	public Integer getIntegerValue(C columnName, Integer defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getIntegerValue();
		}
	}

	@Override
	public Double getDoubleValue(C columnName, Double defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getDoubleValue();
		}
	}

	@Override
	public Long getLongValue(C columnName, Long defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getLongValue();
		}
	}

	@Override
	public byte[] getByteArrayValue(C columnName, byte[] defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getByteArrayValue();
		}
	}

	@Override
	public Boolean getBooleanValue(C columnName, Boolean defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getBooleanValue();
		}
	}

	@Override
	public ByteBuffer getByteBufferValue(C columnName, ByteBuffer defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getByteBufferValue();
		}
	}

	@Override
	public  T getValue(C columnName, Serializer serializer, T defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getValue(serializer);
		}
	}

	@Override
	public Date getDateValue(C columnName, Date defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getDateValue();
		}
	}

	@Override
	public UUID getUUIDValue(C columnName, UUID defaultValue) {
		Column column = map.get(columnName);
		if (column == null) {
			return defaultValue;
		} else {
			return column.getUUIDValue();
		}
	}

	@Override
	public Column getColumnByIndex(int idx) {
		return columnList.get(idx);
	}

	@Override
	public  Column getSuperColumn(C columnName, Serializer colSer) {
		throw new UnsupportedOperationException("Operaiton not supported");
	}

	@Override
	public  Column getSuperColumn(int idx, Serializer colSer) {
		throw new UnsupportedOperationException("Operaiton not supported");
	}

	@Override
	public boolean isEmpty() {
		return columnList.size() == 0;
	}

	@Override
	public int size() {
		return columnList.size();
	}

	@Override
	public boolean isSuperColumn() {
		throw new UnsupportedOperationException("Operaiton not supported");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy