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

com.netflix.astyanax.cql.util.CFQueryContext Maven / Gradle / Ivy

package com.netflix.astyanax.cql.util;

import java.nio.ByteBuffer;

import com.netflix.astyanax.cql.schema.CqlColumnFamilyDefinitionImpl;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.ConsistencyLevel;
import com.netflix.astyanax.retry.RetryPolicy;

public class CFQueryContext {

	private final ColumnFamily columnFamily;
	private final Object rowKey;
	private RetryPolicy retryPolicy;
	private ConsistencyLevel clLevel; 

	public CFQueryContext(ColumnFamily cf) {
		this(cf, null, null, null);
	}

	public CFQueryContext(ColumnFamily cf, K rKey) {
		this(cf, rKey, null, null);
	}

	public CFQueryContext(ColumnFamily cf, K rKey, RetryPolicy retry, ConsistencyLevel cl) {
		this.columnFamily = cf;
		this.rowKey = checkRowKey(rKey);
		this.retryPolicy = retry;
		this.clLevel = cl;
	}

	public ColumnFamily getColumnFamily() {
		return columnFamily;
	}

	public Object getRowKey() {
		return rowKey;
	}

	public void setRetryPolicy(RetryPolicy retry) {
		this.retryPolicy = retry;
	}

	public RetryPolicy getRetryPolicy() {
		return retryPolicy;
	}

	public void setConsistencyLevel(ConsistencyLevel cl) {
		this.clLevel = cl;
	}

	public ConsistencyLevel getConsistencyLevel() {
		return clLevel;
	}
	
	public Object checkRowKey(K rKey) {
		
		if (rKey == null) {
			return null;
		}
		
		CqlColumnFamilyDefinitionImpl cfDef = (CqlColumnFamilyDefinitionImpl) columnFamily.getColumnFamilyDefinition();
		
		if (cfDef.getKeyValidationClass().contains("BytesType")) {
			
			// Row key is of type bytes. Convert row key to bytebuffer if needed
			if (rKey instanceof ByteBuffer) {
				return rKey;
			}
			
			return columnFamily.getKeySerializer().toByteBuffer(rKey);
		}
		
		// else just return the row key as is
		return rKey;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("CF=").append(columnFamily.getName());
		sb.append(" RowKey: ").append(rowKey);
		sb.append(" RetryPolicy: ").append(retryPolicy);
		sb.append(" ConsistencyLevel: ").append(clLevel);
		return sb.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy