ei.ne.ke.cassandra.cql3.template.BatchStatementBuilder.java Source code

Java tutorial

Introduction

Here is the source code for ei.ne.ke.cassandra.cql3.template.BatchStatementBuilder.java

Source

/*
 * Copyright 2013 EK3 Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ei.ne.ke.cassandra.cql3.template;

import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;

import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

/**
 * Builder for CQL3 BATCH statements. CQL3 batches can only contain
 * data-mutating statements.
 */
public class BatchStatementBuilder implements Mutating {

    private boolean unlogged;
    private boolean usingTimestamp;
    private Collection<Option> options;
    private Collection<Mutating> statements;

    /**
     * Constructor.
     */
    BatchStatementBuilder() {
        this.unlogged = false;
        this.usingTimestamp = false;
        this.options = Lists.newArrayList();
        this.statements = Lists.newArrayList();
    }

    /**
     * Whether or not this is an unlogged (non-atomic) batch. By default, all
     * batches are logged (false). Logged batches have a 30% performance hit. In
     * summary, if you need performance use an unlogged batch. If you need
     * atomicity, use a logged batch.
     * @see http://www.datastax.com/dev/blog/atomic-batches-in-cassandra-1-2
     * @param value whether or not this is an unlogged batch.
     * @return this builder.
     */
    public BatchStatementBuilder unlogged(boolean value) {
        this.unlogged = value;
        return this;
    }

    /**
     * Sets the timestamp for this batch. If you set this, you mustn't set a
     * timestamp in any enclosed statement.
     * @param value the timestamp.
     * @return this builder.
     */
    public BatchStatementBuilder usingTimestamp(long value) {
        options.add(Option.timestamp(value));
        usingTimestamp = true;
        return this;
    }

    /**
     *
     *
     * @param stmts
     * @return this builder.
     */
    public BatchStatementBuilder statements(Mutating... stmts) {
        this.statements.addAll(Arrays.asList(stmts));
        return this;
    }

    /**
     *
     *
     * @param stmts
     * @return this builder.
     */
    public BatchStatementBuilder statement(Collection<Mutating> stmts) {
        statements.addAll(stmts);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String build() {
        Preconditions.checkArgument(!statements.isEmpty(), "You must provide at least one statement");
        StringBuilder sb = new StringBuilder();
        sb.append("BEGIN ");
        if (unlogged) {
            sb.append("UNLOGGED ");
        }
        sb.append("BATCH ");
        if (options.size() > 0) {
            sb.append("USING ");
            sb.append(StringUtils.join(Collections2.transform(options, Functions.toStringFunction()), " AND "));
        }
        for (Mutating statement : statements) {
            if (statement.usingTimestamp()) {
                throw new IllegalArgumentException(
                        "When batching, you cannot set a timestamp on individual statements.");
            }
            sb.append(" ");
            sb.append(statement.build());
        }
        sb.append(" APPLY BATCH;");
        return sb.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean usingTimestamp() {
        return usingTimestamp;
    }

}