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

com.scalar.database.api.Operation 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.api;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;
import com.scalar.database.io.Key;
import com.scalar.database.storage.cassandra.OperationVisitor;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * An abstraction for storage operations.
 *
 * @author Hiroyuki Yamada
 */
@NotThreadSafe
public abstract class Operation {
  private final Key partitionKey;
  private final Optional clusteringKey;
  private Optional namespace;
  private Optional tableName;
  private Consistency consistency;

  public Operation(Key partitionKey, Key clusteringKey) {
    this.partitionKey = checkNotNull(partitionKey);
    this.clusteringKey = Optional.ofNullable(clusteringKey);
    namespace = Optional.empty();
    tableName = Optional.empty();
    consistency = Consistency.SEQUENTIAL;
  }

  /**
   * Returns the namespace for this operation
   *
   * @return an {@code Optional} with the returned namespace
   */
  @Nonnull
  public Optional forNamespace() {
    return namespace;
  }

  /**
   * Returns the table name for this operation
   *
   * @return an {@code Optional} with the returned table name
   */
  @Nonnull
  public Optional forTable() {
    return tableName;
  }

  /**
   * Sets the specified target namespace for this operation
   *
   * @param namespace target namespace for this operation
   * @return this object
   */
  public Operation forNamespace(String namespace) {
    this.namespace = Optional.ofNullable(namespace);
    return this;
  }

  /**
   * Sets the specified target namespace for this operation
   *
   * @param tableName target table name for this operation
   * @return this object
   */
  public Operation forTable(String tableName) {
    this.tableName = Optional.ofNullable(tableName);
    return this;
  }

  /**
   * Returns the partition key
   *
   * @return the partition {@code Key}
   */
  @Nonnull
  public Key getPartitionKey() {
    return partitionKey;
  }

  /**
   * Returns the clustering key
   *
   * @return the clustering {@code Key}
   */
  @Nonnull
  public Optional getClusteringKey() {
    return clusteringKey;
  }

  /**
   * Returns the consistency level for this operation
   *
   * @return the consistency level
   */
  public Consistency getConsistency() {
    return consistency;
  }

  /**
   * Sets the specified consistency level for this operation
   *
   * @param consistency consistency level to set
   * @return this object
   */
  public Operation withConsistency(Consistency consistency) {
    this.consistency = consistency;
    return this;
  }

  /**
   * Indicates whether some other object is "equal to" this object. The other object is considered
   * equal if:
   *
   * 
    *
  • it is also an {@code Operation} and *
  • both instances have the same partition key, clustering key, namespace, table name and * consistency *
* * @param o an object to be tested for equality * @return {@code true} if the other object is "equal to" this object otherwise {@code false} */ @Override public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof Operation)) { return false; } Operation other = (Operation) o; return ComparisonChain.start() .compare(partitionKey, other.partitionKey) .compare( clusteringKey.orElse(null), other.clusteringKey.orElse(null), Ordering.natural().nullsFirst()) .compare( namespace.orElse(null), other.namespace.orElse(null), Ordering.natural().nullsFirst()) .compare( tableName.orElse(null), other.tableName.orElse(null), Ordering.natural().nullsFirst()) .compare(consistency, other.consistency) .result() == 0; } /** * Access the specified visitor * * @param v a visitor object to access */ public abstract void accept(OperationVisitor v); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy