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

com.netflix.astyanax.recipes.UniquenessConstraint Maven / Gradle / Ivy

There is a newer version: 3.8.0-bv14
Show newest version
package com.netflix.astyanax.recipes;

import com.google.common.base.Supplier;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.MutationBatch;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.ColumnList;
import com.netflix.astyanax.model.ConsistencyLevel;

@Deprecated
public class UniquenessConstraint {
    private final ColumnFamily columnFamily;
    private final Keyspace keyspace;
    private final Supplier uniqueColumnSupplier;
    private Integer ttl;
    private ConsistencyLevel consistencyLevel = ConsistencyLevel.CL_QUORUM;
    private UniquenessConstraintViolationMonitor monitor;

    public UniquenessConstraint(Keyspace keyspace, ColumnFamily columnFamily, Supplier uniqueColumnSupplier) {
        this.keyspace = keyspace;
        this.columnFamily = columnFamily;
        this.uniqueColumnSupplier = uniqueColumnSupplier;
    }

    public UniquenessConstraint setTtl(Integer ttl) {
        this.ttl = ttl;
        return this;
    }

    public UniquenessConstraint setMonitor(UniquenessConstraintViolationMonitor monitor) {
        this.monitor = monitor;
        return this;
    }

    public C isUnique(K key) throws ConnectionException {
        C unique = uniqueColumnSupplier.get();

        // Phase 1: Write a unique column
        MutationBatch m = keyspace.prepareMutationBatch().setConsistencyLevel(consistencyLevel);
        m.withRow(columnFamily, key).putEmptyColumn(unique, ttl);
        m.execute();

        // Phase 2: Read back all columns. There should be only 1
        ColumnList result = keyspace.prepareQuery(columnFamily).setConsistencyLevel(consistencyLevel).getKey(key)
                .execute().getResult();

        if (result.size() == 1) {
            return unique;
        }

        if (this.monitor != null)
            this.monitor.onViolation(key, unique);

        // Rollback
        m = keyspace.prepareMutationBatch().setConsistencyLevel(consistencyLevel);
        m.withRow(columnFamily, key).deleteColumn(unique);
        m.execute();
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy