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

com.netflix.astyanax.ThreadLocalMutationBatchManager Maven / Gradle / Ivy

There is a newer version: 3.10.2
Show newest version
package com.netflix.astyanax;

import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.model.ConsistencyLevel;
import com.netflix.astyanax.retry.RetryPolicy;

/**
 * Simple mutation batch using thread local storage to keeps track of one 
 * mutation per thread
 * 
 * @author elandau
 *
 */
public class ThreadLocalMutationBatchManager implements MutationBatchManager {
    private ThreadLocal batches = new ThreadLocal();
    
    private final Keyspace          keyspace;
    private final ConsistencyLevel  cl;
    private final RetryPolicy       retryPolicy;
    
    public ThreadLocalMutationBatchManager(Keyspace keyspace, ConsistencyLevel cl) {
        this(keyspace, cl, null);
    }
    
    public ThreadLocalMutationBatchManager(Keyspace keyspace, ConsistencyLevel cl, RetryPolicy retryPolicy) {
        this.keyspace    = keyspace;
        this.cl          = cl;
        this.retryPolicy = retryPolicy;
    }
    
    @Override
    public MutationBatch getSharedMutationBatch() {
        MutationBatch mb = batches.get();
        if (mb == null) {
            mb = keyspace.prepareMutationBatch().setConsistencyLevel(cl);
            if (retryPolicy != null) 
                mb.withRetryPolicy(retryPolicy);
            batches.set(mb);
        }
        return mb;
    }
    
    @Override
    public MutationBatch getNewMutationBatch() {
        return keyspace.prepareMutationBatch().setConsistencyLevel(cl);
    }

    @Override
    public void commitSharedMutationBatch() throws ConnectionException {
        MutationBatch mb = batches.get();
        if (mb != null) {
            mb.execute();
            batches.remove();
        }
    }

    @Override
    public void discard() {
        batches.remove();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy