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

com.scalar.database.transaction.consensuscommit.PartitionedMutations 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.transaction.consensuscommit;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.scalar.database.api.Mutation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

/** */
@Immutable
public class PartitionedMutations {
  private final ImmutableListMultimap partitions;

  public PartitionedMutations(Collection... collections) {
    ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder();
    for (Collection collection : collections) {
      collection.forEach(m -> builder.put(new Key(m), m));
    }
    partitions = builder.build();
  }

  @Nonnull
  public ImmutableList getOrderedKeys() {
    List keys = new ArrayList<>(partitions.keySet());
    Collections.sort(keys);
    return ImmutableList.copyOf(keys);
  }

  @Nonnull
  public ImmutableList get(Key key) {
    return partitions.get(key);
  }

  @Immutable
  public static final class Key implements Comparable {
    private final String namespace;
    private final String table;
    private final com.scalar.database.io.Key partitionKey;

    public Key(Mutation mutation) {
      namespace = mutation.forNamespace().get();
      table = mutation.forTable().get();
      partitionKey = mutation.getPartitionKey();
    }

    @Override
    public int hashCode() {
      return Objects.hash(namespace, table, partitionKey);
    }

    @Override
    public boolean equals(Object o) {
      if (o == this) {
        return true;
      }
      if (!(o instanceof Key)) {
        return false;
      }
      Key another = (Key) o;
      return this.namespace.equals(another.namespace)
          && this.table.equals(another.table)
          && this.partitionKey.equals(another.partitionKey);
    }

    @Override
    public int compareTo(Key o) {
      return ComparisonChain.start()
          .compare(this.namespace, o.namespace)
          .compare(this.table, o.table)
          .compare(this.partitionKey, o.partitionKey)
          .result();
    }
  }

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (!(o instanceof PartitionedMutations)) {
      return false;
    }
    PartitionedMutations other = (PartitionedMutations) o;
    return partitions.equals(other.partitions);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy