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

com.netflix.astyanax.cql.writes.CqlStyleMutationQuery Maven / Gradle / Ivy

package com.netflix.astyanax.cql.writes;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.netflix.astyanax.cql.CqlKeyspaceImpl.KeyspaceContext;
import com.netflix.astyanax.cql.schema.CqlColumnFamilyDefinitionImpl;
import com.netflix.astyanax.cql.util.CFQueryContext;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.ConsistencyLevel;

public class CqlStyleMutationQuery {

	protected final KeyspaceContext ksContext;
	protected final CFQueryContext cfContext;
	protected final List> mutationList;
	
	protected AtomicReference deleteRow;
	
	protected final AtomicReference defaultTimestamp;
	protected final AtomicReference defaultTTL; 
	protected final ConsistencyLevel consistencyLevel;

	private static final String USING = " USING ";
	private static final String TTL = " TTL ";
	private static final String AND = " AND";
	private static final String TIMESTAMP = " TIMESTAMP ";
	
	public CqlStyleMutationQuery(KeyspaceContext ksCtx, CFQueryContext cfCtx, 
								 List> mutationList, AtomicReference deleteRow, 
								 AtomicReference ttl, AtomicReference timestamp, ConsistencyLevel consistencyLevel) {
		
		this.ksContext = ksCtx;
		this.cfContext = cfCtx;
		
		this.mutationList = mutationList;
		this.deleteRow = deleteRow;
		this.defaultTTL = ttl;
		this.defaultTimestamp = timestamp;
		this.consistencyLevel = consistencyLevel;
		
		if (this.consistencyLevel != null) {
			cfContext.setConsistencyLevel(consistencyLevel);
		}
	}
	
	public String getDeleteEntireRowQuery() {
		ColumnFamily cf = cfContext.getColumnFamily();
		CqlColumnFamilyDefinitionImpl cfDef = (CqlColumnFamilyDefinitionImpl)cf.getColumnFamilyDefinition();
		return "DELETE FROM " + ksContext.getKeyspace() + "." + cf.getName() + 
				" WHERE " + cfDef.getPartitionKeyColumnDefinition().getName() + " = ?;";
	}

	public void appendWriteOptions(StringBuilder sb, Integer overrideTTL, Long overrideTimestamp) {
		
		Integer ttl = overrideTTL != null ? overrideTTL : defaultTTL.get();
		Long timestamp = overrideTimestamp != null ? overrideTimestamp : defaultTimestamp.get();
				
		if (ttl != null || timestamp != null) {
			sb.append(USING);
		}
		
		if (ttl != null) {
			sb.append(TTL + ttl);
		}
		
		if (timestamp != null) {
			if (ttl != null) {
				sb.append(AND);
			}
			sb.append(TIMESTAMP + timestamp);
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy