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

com.scalar.database.storage.cassandra.InsertStatementHandler Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

There is a newer version: 3.14.0-alpha.1
Show newest version
package com.scalar.database.storage.cassandra;

import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.Insert;
import com.scalar.database.api.Operation;
import com.scalar.database.api.Put;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;

/**
 * A handler class for insert statement
 *
 * @author Hiroyuki Yamada
 */
@ThreadSafe
public class InsertStatementHandler extends MutateStatementHandler {

  /**
   * Constructs {@code InsertStatementHandler} with the specified {@code Session}
   *
   * @param session session to be used with this statement
   */
  public InsertStatementHandler(Session session) {
    super(session);
  }

  @Override
  @Nonnull
  protected PreparedStatement prepare(Operation operation) {
    checkArgument(operation, Put.class);

    Put put = (Put) operation;
    Insert insert = prepare(put);
    String query = insert.getQueryString();

    return prepare(query);
  }

  @Override
  @Nonnull
  protected BoundStatement bind(PreparedStatement prepared, Operation operation) {
    checkArgument(operation, Put.class);

    BoundStatement bound = prepared.bind();
    bound = bind(bound, (Put) operation);

    return bound;
  }

  @Override
  @Nonnull
  protected ResultSet execute(BoundStatement bound, Operation operation) {
    return session.execute(bound);
  }

  private Insert prepare(Put put) {
    Insert insert = insertInto(put.forNamespace().get(), put.forTable().get());

    put.getPartitionKey().forEach(v -> insert.value(v.getName(), bindMarker()));
    put.getClusteringKey()
        .ifPresent(
            k -> {
              k.forEach(v -> insert.value(v.getName(), bindMarker()));
            });
    put.getValues().forEach((k, v) -> insert.value(v.getName(), bindMarker()));

    setCondition(insert, put);

    return insert;
  }

  private BoundStatement bind(BoundStatement bound, Put put) {
    ValueBinder binder = new ValueBinder(bound);

    // bind in the prepared order
    put.getPartitionKey().forEach(v -> v.accept(binder));
    put.getClusteringKey().ifPresent(k -> k.forEach(v -> v.accept(binder)));
    put.getValues().forEach((k, v) -> v.accept(binder));

    // it calls for consistency, but actually nothing to bind here
    bindCondition(binder, put);

    return bound;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy