com.netflix.astyanax.cql.reads.CqlRowCopier Maven / Gradle / Ivy
package com.netflix.astyanax.cql.reads;
import java.util.Iterator;
import com.google.common.util.concurrent.ListenableFuture;
import com.netflix.astyanax.MutationBatch;
import com.netflix.astyanax.RowCopier;
import com.netflix.astyanax.connectionpool.OperationResult;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.cql.CqlKeyspaceImpl;
import com.netflix.astyanax.cql.CqlKeyspaceImpl.KeyspaceContext;
import com.netflix.astyanax.cql.reads.model.CqlColumnImpl;
import com.netflix.astyanax.cql.writes.CqlColumnListMutationImpl;
import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.ColumnList;
import com.netflix.astyanax.query.RowQuery;
/**
* Impl for {@link RowCopier}
*
* @author poberai
*
* @param
* @param
*/
public class CqlRowCopier implements RowCopier {
private boolean useOriginalTimestamp = false;
private final RowQuery rowQuery;
private final ColumnFamily cf;
private final K rowKey;
private final KeyspaceContext ksContext;
public CqlRowCopier(ColumnFamily cf, K rowKey, RowQuery query, KeyspaceContext ksContext) {
this.cf = cf;
this.rowKey = rowKey;
this.rowQuery = query;
this.ksContext = ksContext;
}
@Override
public OperationResult execute() throws ConnectionException {
return getMutationBatch().execute();
}
@Override
public ListenableFuture> executeAsync() throws ConnectionException {
return getMutationBatch().executeAsync();
}
@Override
public RowCopier withOriginalTimestamp(boolean useTimestamp) {
this.useOriginalTimestamp = useTimestamp;
return this;
}
private MutationBatch getMutationBatch() throws ConnectionException {
ColumnList columnList = rowQuery.execute().getResult();
CqlKeyspaceImpl ksImpl = new CqlKeyspaceImpl(ksContext);
MutationBatch mBatch = ksImpl.prepareMutationBatch();
CqlColumnListMutationImpl colListMutation = (CqlColumnListMutationImpl)mBatch.withRow(cf, rowKey);
Iterator> iter = columnList.iterator();
boolean first = true;
while(iter.hasNext()) {
CqlColumnImpl col = (CqlColumnImpl) iter.next();
if (first && useOriginalTimestamp) {
colListMutation.setTimestamp(col.getTimestamp());
first = false;
}
colListMutation.putColumnWithGenericValue(col.getName(), col.getGenericValue(), null);
}
return mBatch;
}
}