예제 #1
0
  public static Long getAsLongValue(ColumnSlice<String, String> columnSlice, String columnName) {
    LongSerializer ls = LongSerializer.get();
    if (StringUtils.isEmpty(columnName)) {
      return null;
    }

    HColumn<String, String> hColumn = columnSlice.getColumnByName(columnName);
    return hColumn == null ? null : ls.fromByteBuffer(hColumn.getValueBytes());
  }
예제 #2
0
 public HecubaClientManager<Long> getHecubaClientManagerWithLongKeys(
     CassandraParamsBean parameters,
     HecubaConstants.CassandraClientImplementation cassandraManagerType) {
   switch (cassandraManagerType) {
     case ASTYANAX:
       return new AstyanaxBasedHecubaClientManager<>(
           parameters, com.netflix.astyanax.serializers.LongSerializer.get());
     case HECTOR:
       return new HectorBasedHecubaClientManager<>(
           parameters, me.prettyprint.cassandra.serializers.LongSerializer.get());
     case DATASTAX:
       return new DataStaxBasedHecubaClientManager<>(parameters, DataType.bigint());
     case DATASTAX_SHARED:
       return new DataStaxBasedSharedHecubaClientManager<>(parameters, DataType.bigint());
     default:
       throw new RuntimeException("Unhandled CassandraManagerType: " + cassandraManagerType);
   }
 }
예제 #3
0
/**
 * Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * <p>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
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>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.
 */
public class LongInserter implements TypeInserter {

  private static final LongSerializer longSerializer = LongSerializer.get();
  private static final StringSerializer stringSerializer = StringSerializer.get();

  @Override
  public Mutator addDataToBatchInsertion(
      Object data,
      String streamColumnFamily,
      String columnName,
      String rowKey,
      Mutator<String> mutator) {
    Long longVal = ((data) instanceof Double) ? ((Double) data).longValue() : (Long) data;
    if (longVal != null) {
      mutator.addInsertion(
          rowKey,
          streamColumnFamily,
          HFactory.createColumn(columnName, longVal, stringSerializer, longSerializer));
    }
    return mutator;
  }
}
예제 #4
0
 public void setLong(N columnName, Long value, int ttl) {
   addInsertion(columnName, value, LongSerializer.get(), ttl);
 }
예제 #5
0
 public void setLong(N columnName, Long value) {
   addInsertion(columnName, value, LongSerializer.get(), globalTtl);
 }
public class AbstractOnRamp implements IOnRamp {
  // Configurable properties
  protected String darkStarNode;
  protected int darkStarPort;
  protected String clusterName;
  protected String keyspace;
  protected String eventName;

  // Client interface vars
  protected Mutator<ByteBuffer> mutator;
  protected Cluster cluster;
  protected Keyspace keySpace;

  protected StringSerializer se = StringSerializer.get();
  protected LongSerializer ls = LongSerializer.get();
  protected ByteBufferSerializer bfs = ByteBufferSerializer.get();
  protected BytesArraySerializer bas = BytesArraySerializer.get();
  protected final ConsistencyLevelPolicy policy = new DefaultConsistencyLevel();

  protected static final String timestamp_field = "_onRampSent";

  protected void setProperties(BaseConfig info) {
    darkStarNode = info.getDarkStarNode();
    darkStarPort = info.getDarkStarPort();
    clusterName = info.getClusterName();
    keyspace = info.getKeyspace();
    eventName = info.getEventName();
  }

  public void setProperties(
      String darkStarNode,
      int darkStarPort,
      String clusterName,
      String keyspace,
      String eventName) {
    this.darkStarNode = darkStarNode;
    this.darkStarPort = darkStarPort;
    this.clusterName = clusterName;
    this.keyspace = keyspace;
    this.eventName = eventName;
  }

  public void init() {
    CassandraHostConfigurator hostConfig =
        new CassandraHostConfigurator(darkStarNode + ":" + String.valueOf(darkStarPort));
    cluster = HFactory.createCluster(clusterName, hostConfig);
    keySpace = HFactory.createKeyspace(keyspace, cluster, policy);
    mutator = HFactory.createMutator(keySpace, bfs);
  }

  public void send(EventObject event, String key) throws JSONException {
    ByteBuffer rowKey = se.toByteBuffer(key);
    event.put(timestamp_field, System.currentTimeMillis());
    mutator.addInsertion(rowKey, "system", createColumn(eventName, event.toString(), se, se));
    mutator.execute();
  }

  public void send(EventObject event) throws JSONException {
    ByteBuffer rowKey = se.toByteBuffer(event.getString("partition_on"));
    event.put(timestamp_field, System.currentTimeMillis());
    mutator.addInsertion(
        rowKey, keyspace, createColumn(event.getEventName(), event.toString(), se, se));
    mutator.execute();
  }
}
예제 #7
0
/**
 * Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * <p>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
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>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.
 */
public class CassandraSDSUtils {
  public static String convertStreamNameToCFName(String streamName) {
    if (streamName == null) {
      return null;
    }
    int keySpaceLength = KeySpaceUtils.getKeySpaceName().length();
    if ((streamName.length() + keySpaceLength) > 48) {
      throw new RuntimeException(
          "The stream name you provided is too long. This has caused the"
              + " generated key (\""
              + streamName
              + "\") to go "
              + "beyond the allowed characters. of "
              + (48 - keySpaceLength));
    }
    return streamName.replace(":", "_").replace(".", "_");
  }

  public static long getLong(ByteBuffer byteBuffer) throws IOException {
    return longSerializer.fromByteBuffer(byteBuffer);
  }

  public static String getString(ByteBuffer byteBuffer) throws IOException {
    return stringSerializer.fromByteBuffer(byteBuffer);
  }

  private static final StringSerializer stringSerializer = StringSerializer.get();
  private static final IntegerSerializer integerSerializer = IntegerSerializer.get();
  private static final LongSerializer longSerializer = LongSerializer.get();
  private static final BooleanSerializer booleanSerializer = BooleanSerializer.get();
  private static final FloatSerializer floatSerializer = FloatSerializer.get();
  private static final DoubleSerializer doubleSerializer = DoubleSerializer.get();

  public static Object getOriginalValueFromColumnValue(
      ByteBuffer byteBuffer, AttributeType attributeType) throws IOException {
    switch (attributeType) {
      case BOOL:
        {
          return booleanSerializer.fromByteBuffer(byteBuffer);
        }
      case INT:
        {
          return integerSerializer.fromByteBuffer(byteBuffer);
        }
      case DOUBLE:
        {
          return doubleSerializer.fromByteBuffer(byteBuffer);
        }
      case FLOAT:
        {
          return floatSerializer.fromByteBuffer(byteBuffer);
        }
      case LONG:
        {
          return longSerializer.fromByteBuffer(byteBuffer);
        }
      case STRING:
        {
          return stringSerializer.fromByteBuffer(byteBuffer);
        }
    }
    return null;
  }

  public static String getColumnName(DataType dataType, Attribute attribute) {
    return dataType.name() + "_" + attribute.getName();
  }

  public static String createRowKey(long timestamp, String ip, int port, int count) {
    return timestamp + "::" + ip + "::" + port + "::" + count;
  }
}
예제 #8
0
/**
 * Parent class of Composite and DynamicComposite. Acts as a list of objects that get serialized
 * into a composite column name. Unless setAutoDeserialize(true) is called, it's going to try to
 * match serializers to Cassandra comparator types.
 *
 * @author edanuff
 */
@SuppressWarnings("rawtypes")
public abstract class AbstractComposite extends AbstractList<Object>
    implements Comparable<AbstractComposite> {

  public static Logger log = LoggerFactory.getLogger(AbstractComposite.class);

  public enum ComponentEquality {
    LESS_THAN_EQUAL((byte) -1),
    EQUAL((byte) 0),
    GREATER_THAN_EQUAL((byte) 1);

    private final byte equality;

    ComponentEquality(byte equality) {
      this.equality = equality;
    }

    public byte toByte() {
      return equality;
    }

    public static ComponentEquality fromByte(byte equality) {
      if (equality > 0) {
        return GREATER_THAN_EQUAL;
      }
      if (equality < 0) {
        return LESS_THAN_EQUAL;
      }
      return EQUAL;
    }
  }

  public static final BiMap<Class<? extends Serializer>, String>
      DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING =
          new ImmutableBiMap.Builder<Class<? extends Serializer>, String>()
              .put(AsciiSerializer.class, AsciiSerializer.get().getComparatorType().getTypeName())
              .put(
                  BigIntegerSerializer.class,
                  BigIntegerSerializer.get().getComparatorType().getTypeName())
              .put(
                  ByteBufferSerializer.class,
                  ByteBufferSerializer.get().getComparatorType().getTypeName())
              .put(LongSerializer.class, LongSerializer.get().getComparatorType().getTypeName())
              .put(StringSerializer.class, StringSerializer.get().getComparatorType().getTypeName())
              .put(FloatSerializer.class, FloatSerializer.get().getComparatorType().getTypeName())
              .put(UUIDSerializer.class, UUIDSerializer.get().getComparatorType().getTypeName())
              .build();

  static final ImmutableClassToInstanceMap<Serializer> SERIALIZERS =
      new ImmutableClassToInstanceMap.Builder<Serializer>()
          .put(AsciiSerializer.class, AsciiSerializer.get())
          .put(BigIntegerSerializer.class, BigIntegerSerializer.get())
          .put(ByteBufferSerializer.class, ByteBufferSerializer.get())
          .put(FloatSerializer.class, FloatSerializer.get())
          .put(LongSerializer.class, LongSerializer.get())
          .put(StringSerializer.class, StringSerializer.get())
          .put(UUIDSerializer.class, UUIDSerializer.get())
          .build();

  public static final BiMap<Byte, String> DEFAULT_ALIAS_TO_COMPARATOR_MAPPING =
      new ImmutableBiMap.Builder<Byte, String>()
          .put((byte) 'a', ASCIITYPE.getTypeName())
          .put((byte) 'b', BYTESTYPE.getTypeName())
          .put((byte) 'f', FLOATTYPE.getTypeName())
          .put((byte) 'i', INTEGERTYPE.getTypeName())
          .put((byte) 'x', LEXICALUUIDTYPE.getTypeName())
          .put((byte) 'l', LONGTYPE.getTypeName())
          .put((byte) 't', TIMEUUIDTYPE.getTypeName())
          .put((byte) 's', UTF8TYPE.getTypeName())
          .put((byte) 'u', UUIDTYPE.getTypeName())
          .build();

  BiMap<Class<? extends Serializer>, String> serializerToComparatorMapping =
      DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING;

  BiMap<Byte, String> aliasToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING;

  final boolean dynamic;

  List<Serializer<?>> serializersByPosition = null;
  List<String> comparatorsByPosition = null;

  public class Component<T> {
    final Serializer<T> serializer;
    final T value;
    final ByteBuffer bytes;
    final String comparator;
    final ComponentEquality equality;

    public Component(
        T value,
        ByteBuffer bytes,
        Serializer<T> serializer,
        String comparator,
        ComponentEquality equality) {
      this.serializer = serializer;
      this.value = value;
      this.bytes = bytes;
      this.comparator = comparator;
      this.equality = equality;
    }

    public Serializer<T> getSerializer() {
      return serializer;
    }

    @SuppressWarnings("unchecked")
    public <A> A getValue(Serializer<A> s) {
      if (s == null) {
        s = (Serializer<A>) serializer;
      }
      if ((value == null) && (bytes != null) && (s != null)) {
        ByteBuffer cb = bytes.duplicate();
        return s.fromByteBuffer(cb);
      }
      if (value instanceof ByteBuffer) {
        return (A) ((ByteBuffer) value).duplicate();
      }
      return (A) value;
    }

    public T getValue() {
      return getValue(serializer);
    }

    @SuppressWarnings("unchecked")
    public <A> ByteBuffer getBytes(Serializer<A> s) {
      if (bytes == null) {
        if (value instanceof ByteBuffer) {
          return ((ByteBuffer) value).duplicate();
        }

        if (value == null) {
          return null;
        }

        if (s == null) {
          s = (Serializer<A>) serializer;
        }
        if (s != null) {
          return s.toByteBuffer((A) value).duplicate();
        }
      }

      return bytes.duplicate();
    }

    public ByteBuffer getBytes() {
      return getBytes(serializer);
    }

    public String getComparator() {
      return comparator;
    }

    public ComponentEquality getEquality() {
      return equality;
    }

    @Override
    public String toString() {
      return "Component [" + getValue() + "]";
    }
  }

  List<Component<?>> components = new ArrayList<Component<?>>();
  ComponentEquality equality = ComponentEquality.EQUAL;

  ByteBuffer serialized = null;

  public AbstractComposite(boolean dynamic) {
    this.dynamic = dynamic;
  }

  public AbstractComposite(boolean dynamic, Object... o) {
    this.dynamic = dynamic;
    this.addAll(Arrays.asList(o));
  }

  public AbstractComposite(boolean dynamic, List<?> l) {
    this.dynamic = dynamic;
    this.addAll(l);
  }

  public List<Component<?>> getComponents() {
    return components;
  }

  public void setComponents(List<Component<?>> components) {
    serialized = null;
    this.components = components;
  }

  public Map<Class<? extends Serializer>, String> getSerializerToComparatorMapping() {
    return serializerToComparatorMapping;
  }

  public void setSerializerToComparatorMapping(
      Map<Class<? extends Serializer>, String> serializerToComparatorMapping) {
    serialized = null;
    this.serializerToComparatorMapping =
        new ImmutableBiMap.Builder<Class<? extends Serializer>, String>()
            .putAll(serializerToComparatorMapping)
            .build();
  }

  public Map<Byte, String> getAliasesToComparatorMapping() {
    return aliasToComparatorMapping;
  }

  public void setAliasesToComparatorMapping(Map<Byte, String> aliasesToComparatorMapping) {
    serialized = null;
    aliasToComparatorMapping =
        new ImmutableBiMap.Builder<Byte, String>().putAll(aliasesToComparatorMapping).build();
  }

  public boolean isDynamic() {
    return dynamic;
  }

  public List<Serializer<?>> getSerializersByPosition() {
    return serializersByPosition;
  }

  public void setSerializersByPosition(List<Serializer<?>> serializersByPosition) {
    this.serializersByPosition = serializersByPosition;
  }

  public void setSerializersByPosition(Serializer<?>... serializers) {
    serializersByPosition = Arrays.asList(serializers);
  }

  public void setSerializerByPosition(int index, Serializer<?> s) {
    if (serializersByPosition == null) {
      serializersByPosition = new ArrayList<Serializer<?>>();
    }
    while (serializersByPosition.size() <= index) {
      serializersByPosition.add(null);
    }
    serializersByPosition.set(index, s);
  }

  public List<String> getComparatorsByPosition() {
    return comparatorsByPosition;
  }

  public void setComparatorsByPosition(List<String> comparatorsByPosition) {
    this.comparatorsByPosition = comparatorsByPosition;
  }

  public void setComparatorsByPosition(String... comparators) {
    comparatorsByPosition = Arrays.asList(comparators);
  }

  public void setComparatorByPosition(int index, String c) {
    if (comparatorsByPosition == null) {
      comparatorsByPosition = new ArrayList<String>();
    }
    while (comparatorsByPosition.size() <= index) {
      comparatorsByPosition.add(null);
    }
    comparatorsByPosition.set(index, c);
  }

  @Override
  public int compareTo(AbstractComposite o) {
    return serialize().compareTo(o.serialize());
  }

  private String comparatorForSerializer(Serializer<?> s) {
    String comparator = serializerToComparatorMapping.get(s.getClass());
    if (comparator != null) {
      return comparator;
    }
    return BYTESTYPE.getTypeName();
  }

  private Serializer<?> serializerForComparator(String c) {
    int p = c.indexOf('(');
    if (p >= 0) {
      c = c.substring(0, p);
    }
    if (LEXICALUUIDTYPE.getTypeName().equals(c) || TIMEUUIDTYPE.getTypeName().equals(c)) {
      return UUIDSerializer.get();
    }

    Serializer<?> s = SERIALIZERS.getInstance(serializerToComparatorMapping.inverse().get(c));
    if (s != null) {
      return s;
    }
    return ByteBufferSerializer.get();
  }

  private Serializer<?> serializerForPosition(int i) {
    if (serializersByPosition == null) {
      return null;
    }
    if (i >= serializersByPosition.size()) {
      return null;
    }
    return serializersByPosition.get(i);
  }

  private Serializer<?> getSerializer(int i, String c) {
    Serializer<?> s = serializerForPosition(i);
    if (s != null) {
      return s;
    }
    return serializerForComparator(c);
  }

  private String comparatorForPosition(int i) {
    if (comparatorsByPosition == null) {
      return null;
    }
    if (i >= comparatorsByPosition.size()) {
      return null;
    }
    return comparatorsByPosition.get(i);
  }

  private String getComparator(int i, ByteBuffer bb) {
    String name = comparatorForPosition(i);
    if (name != null) {
      return name;
    }
    if (!dynamic) {
      if (bb.hasRemaining()) {
        return BYTESTYPE.getTypeName();
      } else {
        return null;
      }
    }
    if (bb.hasRemaining()) {
      try {
        int header = getShortLength(bb);
        if ((header & 0x8000) == 0) {

          name = Charsets.UTF_8.newDecoder().decode(getBytes(bb, header).duplicate()).toString();
        } else {
          byte a = (byte) (header & 0xFF);
          name = aliasToComparatorMapping.get(a);
          if (name == null) {
            a = (byte) Character.toLowerCase((char) a);
            name = aliasToComparatorMapping.get(a);
            if (name != null) {
              name += "(reversed=true)";
            }
          }
        }
      } catch (CharacterCodingException e) {
        throw new RuntimeException(e);
      }
    }
    if ((name != null) && (name.length() == 0)) {
      name = null;
    }
    return name;
  }

  @Override
  public void clear() {
    serialized = null;
    components = new ArrayList<Component<?>>();
  }

  @Override
  public int size() {
    return components.size();
  }

  @SuppressWarnings("unchecked")
  public <T> AbstractComposite addComponent(int index, T element, ComponentEquality equality) {
    serialized = null;

    if (element instanceof Component) {
      components.add(index, (Component<?>) element);
      return this;
    }

    Serializer s = serializerForPosition(index);
    if (s == null) {
      s = SerializerTypeInferer.getSerializer(element);
    }
    String c = comparatorForPosition(index);
    if (c == null) {
      c = comparatorForSerializer(s);
    }
    components.add(index, new Component(element, null, s, c, equality));
    setSerializerByPosition(index, s);
    return this;
  }

  public <T> AbstractComposite addComponent(T value, Serializer<T> s) {

    addComponent(value, s, comparatorForSerializer(s));

    return this;
  }

  public <T> AbstractComposite addComponent(T value, Serializer<T> s, String comparator) {

    addComponent(value, s, comparator, ComponentEquality.EQUAL);

    return this;
  }

  public <T> AbstractComposite addComponent(
      T value, Serializer<T> s, String comparator, ComponentEquality equality) {

    addComponent(-1, value, s, comparator, equality);

    return this;
  }

  @SuppressWarnings("unchecked")
  public <T> AbstractComposite addComponent(
      int index, T value, Serializer<T> s, String comparator, ComponentEquality equality) {
    serialized = null;

    if (value == null) {
      throw new NullPointerException("Unable able to add null component");
    }

    if (index < 0) {
      index = components.size();
    }

    while (components.size() < index) {
      components.add(null);
    }
    components.add(index, new Component(value, null, s, comparator, equality));

    return this;
  }

  private static Object mapIfNumber(Object o) {
    if ((o instanceof Byte) || (o instanceof Integer) || (o instanceof Short)) {
      return BigInteger.valueOf(((Number) o).longValue());
    }
    return o;
  }

  @SuppressWarnings({"unchecked"})
  private static Collection<?> flatten(Collection<?> c) {
    if (c instanceof AbstractComposite) {
      return ((AbstractComposite) c).getComponents();
    }
    boolean hasCollection = false;
    for (Object o : c) {
      if (o instanceof Collection) {
        hasCollection = true;
        break;
      }
    }
    if (!hasCollection) {
      return c;
    }
    List newList = new ArrayList();
    for (Object o : c) {
      if (o instanceof Collection) {
        newList.addAll(flatten((Collection) o));
      } else {
        newList.add(o);
      }
    }
    return newList;
  }

  /**
   * For education purposes, when addAll is called, it ultimately ends up calling add(int,Object).
   * The call stack using JDK7 is the following. 1. addAll 2.
   * AbstractCollection<E>.addAll(Collection<Object>) 3. AbstractList<E>.add(E) 4.
   * AbstractComposite.add(int,Object)
   *
   * <p>What happens is AbstractList<E>.add(E) does this add(size(),E). Neither
   * java.util.AbstractList or java.util.AbstractCollection provide storage in the base class. It
   * expects the subclass to provide the storage.
   */
  @Override
  public boolean addAll(Collection<? extends Object> c) {
    return super.addAll(flatten(c));
  }

  @Override
  public boolean containsAll(Collection<?> c) {
    return super.containsAll(flatten(c));
  }

  @Override
  public boolean removeAll(Collection<?> c) {
    return super.removeAll(flatten(c));
  }

  @Override
  public boolean retainAll(Collection<?> c) {
    return super.retainAll(flatten(c));
  }

  /**
   * Method is similar to addAll(Collection<? extends Object>) in that it ends up calling
   * AbstractComposite.add(int,Object).
   */
  @Override
  public boolean addAll(int i, Collection<? extends Object> c) {
    return super.addAll(i, flatten(c));
  }

  /**
   * add(int,Object) is the primary method that adds elements to the list. All other add methods end
   * up here.
   */
  @Override
  public void add(int index, Object element) {
    element = mapIfNumber(element);
    addComponent(index, element, ComponentEquality.EQUAL);
  }

  /**
   * remove(int) is the primary method that removes elements from the list. All other remove methods
   * end up calling AbstractComposite.remove(int).
   */
  @Override
  public Object remove(int index) {
    serialized = null;
    Component prev = components.remove(index);
    if (prev != null) {
      return prev.getValue();
    }
    return null;
  }

  public <T> AbstractComposite setComponent(int index, T value, Serializer<T> s) {

    setComponent(index, value, s, comparatorForSerializer(s));

    return this;
  }

  public <T> AbstractComposite setComponent(
      int index, T value, Serializer<T> s, String comparator) {

    setComponent(index, value, s, comparator, ComponentEquality.EQUAL);

    return this;
  }

  @SuppressWarnings("unchecked")
  public <T> AbstractComposite setComponent(
      int index, T value, Serializer<T> s, String comparator, ComponentEquality equality) {
    serialized = null;

    while (components.size() <= index) {
      components.add(null);
    }
    components.set(index, new Component(value, null, s, comparator, equality));

    return this;
  }

  @SuppressWarnings("unchecked")
  @Override
  public Object set(int index, Object element) {
    serialized = null;

    if (element instanceof Component) {
      Component prev = components.set(index, (Component<?>) element);
      if (prev != null) {
        return prev.getValue();
      }
      return null;
    }

    element = mapIfNumber(element);
    Serializer s = serializerForPosition(index);
    if (s == null) {
      s = SerializerTypeInferer.getSerializer(element);
    }
    String c = comparatorForPosition(index);
    if (c == null) {
      c = comparatorForSerializer(s);
    }
    Component prev =
        components.set(index, new Component(element, null, s, c, ComponentEquality.EQUAL));
    if (prev != null) {
      return prev.getValue();
    }
    return null;
  }

  @Override
  public Object get(int i) {
    Component c = components.get(i);
    if (c != null) {
      return c.getValue();
    }
    return null;
  }

  public <T> T get(int i, Serializer<T> s) throws ClassCastException {
    T value = null;
    Component<?> c = components.get(i);
    if (c != null) {
      value = c.getValue(s);
    }
    return value;
  }

  public Component getComponent(int i) {
    if (i >= components.size()) {
      return null;
    }
    Component c = components.get(i);
    return c;
  }

  public Iterator<Component<?>> componentsIterator() {
    return components.iterator();
  }

  @SuppressWarnings("unchecked")
  public ByteBuffer serialize() {
    if (serialized != null) {
      return serialized.duplicate();
    }

    ByteBufferOutputStream out = new ByteBufferOutputStream();

    int i = 0;
    for (Component c : components) {
      Serializer<?> s = serializerForPosition(i);
      ByteBuffer cb = c.getBytes(s);
      if (cb == null) {
        cb = ByteBuffer.allocate(0);
      }

      if (dynamic) {
        String comparator = comparatorForPosition(i);
        if (comparator == null) {
          comparator = c.getComparator();
        }
        if (comparator == null) {
          comparator = BYTESTYPE.getTypeName();
        }
        int p = comparator.indexOf("(reversed=true)");
        boolean desc = false;
        if (p >= 0) {
          comparator = comparator.substring(0, p);
          desc = true;
        }
        if (aliasToComparatorMapping.inverse().containsKey(comparator)) {
          byte a = aliasToComparatorMapping.inverse().get(comparator);
          if (desc) {
            a = (byte) Character.toUpperCase((char) a);
          }
          out.writeShort((short) (0x8000 | a));
        } else {
          out.writeShort((short) comparator.length());
          out.write(comparator.getBytes(Charsets.UTF_8));
        }
        // if (comparator.equals(BYTESTYPE.getTypeName()) && (cb.remaining() ==
        // 0)) {
        // log.warn("Writing zero-length BytesType, probably an error");
        // }
      }
      out.writeShort((short) cb.remaining());
      out.write(cb.slice());
      if ((i == (components.size() - 1)) && (equality != ComponentEquality.EQUAL)) {
        out.write(equality.toByte());
      } else {
        out.write(c.getEquality().toByte());
      }
      i++;
    }

    serialized = out.getByteBuffer();
    return serialized.duplicate();
  }

  @SuppressWarnings("unchecked")
  public void deserialize(ByteBuffer b) {
    serialized = b.duplicate();
    components = new ArrayList<Component<?>>();

    String comparator = null;
    int i = 0;
    while ((comparator = getComparator(i, b)) != null) {
      ByteBuffer data = getWithShortLength(b);
      if (data != null) {
        Serializer<?> s = getSerializer(i, comparator);
        ComponentEquality equality = ComponentEquality.fromByte(b.get());
        components.add(new Component(null, data.slice(), s, comparator, equality));
      } else {
        throw new RuntimeException("Missing component data in composite type");
      }
      i++;
    }
  }

  protected static int getShortLength(ByteBuffer bb) {
    int length = (bb.get() & 0xFF) << 8;
    return length | (bb.get() & 0xFF);
  }

  protected static ByteBuffer getBytes(ByteBuffer bb, int length) {
    ByteBuffer copy = bb.duplicate();
    copy.limit(copy.position() + length);
    bb.position(bb.position() + length);
    return copy;
  }

  protected static ByteBuffer getWithShortLength(ByteBuffer bb) {
    int length = getShortLength(bb);
    return getBytes(bb, length);
  }

  public ComponentEquality getEquality() {
    return equality;
  }

  public void setEquality(ComponentEquality equality) {
    serialized = null;
    this.equality = equality;
  }
}
예제 #9
0
/**
 * Class <code>CassandraDataAccessHelper</code> Encapsulate the Cassandra DataAccessLogic used in
 * CassandraMessageStore
 */
public class CassandraDataAccessHelper {

  private static final String USERNAME_KEY = "username";
  private static final String PASSWORD_KEY = "password";

  /** Serializes used for Cassandra data operations */
  private static StringSerializer stringSerializer = StringSerializer.get();

  private static LongSerializer longSerializer = LongSerializer.get();

  private static BytesArraySerializer bytesArraySerializer = BytesArraySerializer.get();

  private static IntegerSerializer integerSerializer = IntegerSerializer.get();

  private static ByteBufferSerializer byteBufferSerializer = ByteBufferSerializer.get();

  /**
   * Create a Cassandra Cluster instance given the connection details
   *
   * @param userName userName to connect to the cassandra
   * @param password password to connect to cassandra
   * @param clusterName Cluster name
   * @param connectionString cassandra connection string
   * @return Cluster instance
   * @throws CassandraDataAccessException In case of and error in accessing database or data error
   */
  public static Cluster createCluster(
      String userName, String password, String clusterName, String connectionString)
      throws CassandraDataAccessException {

    if (userName == null || password == null) {
      throw new CassandraDataAccessException(
          "Can't create cluster with empty userName or Password");
    }

    if (clusterName == null) {
      throw new CassandraDataAccessException("Can't create cluster with empty cluster name");
    }

    if (connectionString == null) {
      throw new CassandraDataAccessException("Can't create cluster with empty connection string");
    }

    Map<String, String> credentials = new HashMap<String, String>();
    credentials.put(USERNAME_KEY, userName);
    credentials.put(PASSWORD_KEY, password);

    CassandraHostConfigurator hostConfigurator = new CassandraHostConfigurator(connectionString);
    hostConfigurator.setMaxActive(2000);
    Cluster cluster = HFactory.getCluster(clusterName);

    if (cluster == null) {
      cluster = HFactory.createCluster(clusterName, hostConfigurator, credentials);
    }
    return cluster;
  }

  /**
   * Create a Column family in a Given Cluster instance
   *
   * @param name ColumnFamily Name
   * @param keySpace KeySpace name
   * @param cluster Cluster instance
   * @param comparatorType Comparator
   * @throws CassandraDataAccessException In case of an Error accessing database or data error
   */
  public static void createColumnFamily(
      String name, String keySpace, Cluster cluster, String comparatorType)
      throws CassandraDataAccessException {

    KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace);

    if (ksDef == null) {
      throw new CassandraDataAccessException(
          "Can't create Column family, keyspace " + keySpace + " does not exist");
    }

    ColumnFamilyDefinition cfDef =
        new ThriftCfDef(keySpace, /*"Queue"*/ name, ComparatorType.getByClassName(comparatorType));

    List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs();
    HashSet<String> cfNames = new HashSet<String>();
    for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) {
      cfNames.add(columnFamilyDefinition.getName());
    }
    if (!cfNames.contains(name)) {
      cluster.addColumnFamily(cfDef, true);
    }
  }

  /**
   * Create a Column family for cassandra counters in a given Cluster intance
   *
   * @param name ColumnFamily Name
   * @param keySpace KeySpace name
   * @param cluster Cluster instance
   * @throws CassandraDataAccessException In case of an Error accessing database or data error
   */
  public static void createCounterColumnFamily(String name, String keySpace, Cluster cluster)
      throws CassandraDataAccessException {
    KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace);

    if (ksDef == null) {
      throw new CassandraDataAccessException(
          "Can't create Column family, keyspace " + keySpace + " does not exist");
    }
    ColumnFamilyDefinition cfDef =
        HFactory.createColumnFamilyDefinition(keySpace, name, ComparatorType.COUNTERTYPE);
    cfDef.setComparatorType(ComparatorType.UTF8TYPE);
    cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE.getClassName());
    cfDef.setColumnType(ColumnType.STANDARD);

    List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs();
    HashSet<String> cfNames = new HashSet<String>();
    for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) {
      cfNames.add(columnFamilyDefinition.getName());
    }
    if (!cfNames.contains(name)) {
      cluster.addColumnFamily(cfDef, true);
    }
  }

  /**
   * Insert a raw-column for counting a property (row is property, column is item)
   *
   * @param cfName column family name
   * @param counterRowName name of row
   * @param queueColumn name of column
   * @param keyspace key space name
   * @throws CassandraDataAccessException
   */
  public static void insertCounterColumn(
      String cfName, String counterRowName, String queueColumn, Keyspace keyspace)
      throws CassandraDataAccessException {
    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get());
      // inserting counter column
      mutator.insertCounter(
          counterRowName,
          cfName,
          HFactory.createCounterColumn(queueColumn, 0L, StringSerializer.get()));
      mutator.execute();
      CounterQuery<String, String> counter =
          new ThriftCounterColumnQuery<String, String>(
              keyspace, stringSerializer, stringSerializer);

      counter.setColumnFamily(cfName).setKey(counterRowName).setName(queueColumn);
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while inserting data to:" + cfName, e);
    }
  }

  /**
   * remove allocated raw-column space for counter
   *
   * @param cfName column family name
   * @param counterRowName name of row
   * @param queueColumn name of column
   * @param keyspace key space name
   * @throws CassandraDataAccessException
   */
  public static void removeCounterColumn(
      String cfName, String counterRowName, String queueColumn, Keyspace keyspace)
      throws CassandraDataAccessException {
    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get());
      mutator.deleteCounter(counterRowName, cfName, queueColumn, stringSerializer);
      mutator.execute();
      CounterQuery<String, String> counter =
          new ThriftCounterColumnQuery<String, String>(
              keyspace, stringSerializer, stringSerializer);

      counter.setColumnFamily(cfName).setKey(counterRowName).setName(queueColumn);
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while accessing:" + cfName, e);
    }
  }

  /**
   * Increment counter by given value
   *
   * @param rawID raw name
   * @param columnFamily column family name
   * @param columnName name of column
   * @param keyspace keyspace
   * @param incrementBy value to increase by
   * @throws CassandraDataAccessException
   */
  public static void incrementCounter(
      String columnName, String columnFamily, String rawID, Keyspace keyspace, long incrementBy)
      throws CassandraDataAccessException {
    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get());
      mutator.incrementCounter(rawID, columnFamily, columnName, incrementBy);
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e);
    }
  }

  /**
   * Decrement counter by 1
   *
   * @param rawID raw name
   * @param columnFamily column family name
   * @param columnName name of column
   * @param keyspace keyspace
   * @throws CassandraDataAccessException
   */
  public static void decrementCounter(
      String columnName, String columnFamily, String rawID, Keyspace keyspace, long decrementBy)
      throws CassandraDataAccessException {
    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get());
      mutator.decrementCounter(rawID, columnFamily, columnName, decrementBy);
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e);
    }
  }

  /**
   * @param keyspace name of key space
   * @param columnFamily column family name
   * @param key key value
   * @param cloumnName column name
   * @return long count value
   * @throws CassandraDataAccessException
   */
  public static long getCountValue(
      Keyspace keyspace, String columnFamily, String cloumnName, String key)
      throws CassandraDataAccessException {
    try {
      CounterQuery<String, String> query =
          HFactory.createCounterColumnQuery(keyspace, stringSerializer, stringSerializer);
      query.setColumnFamily(columnFamily).setKey(key).setName(cloumnName);
      HCounterColumn<String> counter = query.execute().get();
      return counter.getValue();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e);
    }
  }

  public static Keyspace createKeySpace(Cluster cluster, String keySpace) {

    Keyspace keyspace;

    // Define the keySpace
    KeyspaceDefinition definition = new ThriftKsDef(keySpace);

    KeyspaceDefinition def = cluster.describeKeyspace(keySpace);
    if (def == null) {
      // Adding keySpace to the cluster
      cluster.addKeyspace(definition, true);
    }

    keyspace = HFactory.createKeyspace(keySpace, cluster);
    CassandraConsistencyLevelPolicy policy = new CassandraConsistencyLevelPolicy();
    keyspace.setConsistencyLevelPolicy(policy);
    return keyspace;
  }

  /**
   * Get List of Strings in a Given ROW in a Cassandra Column Family Here we assume that the columns
   * in a given row have string data and key and value in the given column in that row have same
   * values.
   *
   * @param columnFamilyName Name of the column Family
   * @param rowName Row name
   * @param keyspace keySpace
   * @return List of string in that given row.
   * @throws CassandraDataAccessException In case of database access error or data error
   */
  public static List<String> getRowList(String columnFamilyName, String rowName, Keyspace keyspace)
      throws CassandraDataAccessException {
    ArrayList<String> rowList = new ArrayList<String>();

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't access Data , no keyspace provided ");
    }

    if (columnFamilyName == null || rowName == null) {
      throw new CassandraDataAccessException(
          "Can't access data with columnFamily =" + columnFamilyName + " and rowName=" + rowName);
    }

    try {
      SliceQuery<String, String, String> sliceQuery =
          HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
      sliceQuery.setKey(rowName);
      sliceQuery.setColumnFamily(columnFamilyName);
      sliceQuery.setRange("", "", false, 10000);

      QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
      ColumnSlice<String, String> columnSlice = result.get();
      for (HColumn<String, String> column : columnSlice.getColumns()) {
        rowList.add(column.getName());
      }
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while accessing data from :" + columnFamilyName, e);
    }
    return rowList;
  }

  public static List<String> getDestinationQueueNamesFromCounterColumns(
      String columnFamilyName, String rowName, Keyspace keyspace)
      throws CassandraDataAccessException {
    ArrayList<String> rowList = new ArrayList<String>();

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't access Data , no keyspace provided ");
    }

    if (columnFamilyName == null || rowName == null) {
      throw new CassandraDataAccessException(
          "Can't access data with columnFamily =" + columnFamilyName + " and rowName=" + rowName);
    }

    try {

      SliceCounterQuery<String, String> sliceQuery =
          HFactory.createCounterSliceQuery(keyspace, stringSerializer, stringSerializer);
      sliceQuery.setKey(rowName);
      sliceQuery.setColumnFamily(columnFamilyName);
      sliceQuery.setRange("", "", false, Integer.MAX_VALUE);

      QueryResult<CounterSlice<String>> result = sliceQuery.execute();
      CounterSlice<String> columnSlice = result.get();
      for (HCounterColumn<String> column : columnSlice.getColumns()) {
        rowList.add(column.getName());
      }
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while accessing data from :" + columnFamilyName, e);
    }
    return rowList;
  }

  /**
   * Get the value of a given column in a Given ROW in a Cassandra Column Family Here we assume that
   * the columns in a given row have string data and key and value in the given column in that row
   * have same values.
   *
   * @param columnFamilyName Name of the column Family
   * @param rowName Row name
   * @param keyspace keySpace
   * @param columnName Name of the column
   * @return vlaue of a given column in that given row.
   * @throws CassandraDataAccessException In case of database access error or data error
   */
  public static String getColumnValue(
      String columnFamilyName, String rowName, Keyspace keyspace, String columnName)
      throws CassandraDataAccessException {
    ArrayList<String> rowList = new ArrayList<String>();
    String value = null;

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't access Data , no keyspace provided ");
    }

    if (columnFamilyName == null || rowName == null) {
      throw new CassandraDataAccessException(
          "Can't access data with columnFamily =" + columnFamilyName + " and rowName=" + rowName);
    }

    try {
      SliceQuery<String, String, String> sliceQuery =
          HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
      sliceQuery.setKey(rowName);
      sliceQuery.setColumnFamily(columnFamilyName);
      sliceQuery.setRange("", "", false, 10000);

      QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
      ColumnSlice<String, String> columnSlice = result.get();
      for (HColumn<String, String> column : columnSlice.getColumns()) {
        if (column.getName().equalsIgnoreCase(columnName)) {
          value = column.getValue();
          break;
        }
      }
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while accessing data from :" + columnFamilyName, e);
    }
    return value;
  }

  /**
   * Get set of messages in a column family
   *
   * @param queueName QueueName
   * @param columnFamilyName ColumnFamilyName
   * @param keyspace Cassandra KeySpace
   * @param lastProcessedId Last processed Message id to use as a off set
   * @param count max message count limit
   * @return ColumnSlice which contain the messages
   * @throws CassandraDataAccessException
   */
  public static ColumnSlice<Long, byte[]> getMessagesFromQueue(
      String queueName, String columnFamilyName, Keyspace keyspace, long lastProcessedId, int count)
      throws CassandraDataAccessException {
    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't access Data , no keyspace provided ");
    }

    if (columnFamilyName == null || queueName == null) {
      throw new CassandraDataAccessException(
          "Can't access data with columnFamily = "
              + columnFamilyName
              + " and queueName="
              + queueName);
    }

    try {
      SliceQuery<String, Long, byte[]> sliceQuery =
          HFactory.createSliceQuery(
              keyspace, stringSerializer, longSerializer, bytesArraySerializer);
      sliceQuery.setKey(queueName);
      sliceQuery.setRange(lastProcessedId + 1, Long.MAX_VALUE, false, count);
      sliceQuery.setColumnFamily(columnFamilyName);

      QueryResult<ColumnSlice<Long, byte[]>> result = sliceQuery.execute();
      ColumnSlice<Long, byte[]> columnSlice = result.get();

      return columnSlice;
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while getting data from " + columnFamilyName, e);
    }
  }

  /**
   * Get set of messages in a column family
   *
   * @param queueName QueueName
   * @param columnFamilyName ColumnFamilyName
   * @param keyspace Cassandra KeySpace
   * @param count max message count limit
   * @return ColumnSlice which contain the messages
   * @throws CassandraDataAccessException
   */
  public static ColumnSlice<Long, byte[]> getMessagesFromQueue(
      String queueName, String columnFamilyName, Keyspace keyspace, int count)
      throws CassandraDataAccessException {
    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't access Data , no keyspace provided ");
    }

    if (columnFamilyName == null || queueName == null) {
      throw new CassandraDataAccessException(
          "Can't access data with columnFamily = "
              + columnFamilyName
              + " and queueName="
              + queueName);
    }

    try {
      SliceQuery<String, Long, byte[]> sliceQuery =
          HFactory.createSliceQuery(
              keyspace, stringSerializer, longSerializer, bytesArraySerializer);
      sliceQuery.setKey(queueName);
      sliceQuery.setRange((long) 0, Long.MAX_VALUE, false, count);
      sliceQuery.setColumnFamily(columnFamilyName);

      QueryResult<ColumnSlice<Long, byte[]>> result = sliceQuery.execute();
      ColumnSlice<Long, byte[]> columnSlice = result.get();

      return columnSlice;
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while getting data from " + columnFamilyName, e);
    }
  }

  /**
   * Get Number of <String,String> type columns in a given row in a cassandra column family
   *
   * @param rowName row Name we are querying for
   * @param columnFamilyName columnFamilName
   * @param keyspace
   * @param count
   * @return
   */
  public static ColumnSlice<String, String> getStringTypeColumnsInARow(
      String rowName, String columnFamilyName, Keyspace keyspace, int count)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't access Data , no keyspace provided ");
    }

    if (columnFamilyName == null || rowName == null) {
      throw new CassandraDataAccessException(
          "Can't access data with columnFamily = " + columnFamilyName + " and rowName=" + rowName);
    }

    try {
      SliceQuery sliceQuery =
          HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
      sliceQuery.setKey(rowName);
      sliceQuery.setColumnFamily(columnFamilyName);
      sliceQuery.setRange("", "", false, count);

      QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
      ColumnSlice<String, String> columnSlice = result.get();

      return columnSlice;
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while getting data from : " + columnFamilyName, e);
    }
  }

  /**
   * Get a HColumn<Long, byte[]> with a given key in a given row in a given column Family
   *
   * @param rowName name of the row
   * @param columnFamily column Family name
   * @param key long type key of the column we are looking for
   * @param keyspace cassandra keySpace instance
   * @return query result as a cassandra column
   * @throws CassandraDataAccessException in case of an Error when accessing data
   */
  public static HColumn<Long, byte[]> getLongByteArrayColumnInARow(
      String rowName, String columnFamily, long key, Keyspace keyspace)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't access Data , no keyspace provided ");
    }

    if (columnFamily == null || rowName == null) {
      throw new CassandraDataAccessException(
          "Can't access data with columnFamily = " + columnFamily + " and rowName=" + rowName);
    }

    try {
      ColumnQuery columnQuery =
          HFactory.createColumnQuery(
              keyspace, stringSerializer, longSerializer, bytesArraySerializer);
      columnQuery.setColumnFamily(columnFamily);
      columnQuery.setKey(rowName);
      columnQuery.setName(key);

      QueryResult<HColumn<Long, byte[]>> result = columnQuery.execute();

      HColumn<Long, byte[]> column = result.get();
      return column;
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while executing quary for HColumn<Long, byte[]> with key ="
              + key
              + " in column Family = "
              + columnFamily,
          e);
    }
  }

  /**
   * Add Message to a Given Queue in Cassandra
   *
   * @param columnFamily ColumnFamily name
   * @param queue queue name
   * @param messageId Message id
   * @param message message in bytes
   * @param keyspace Cassandra KeySpace
   * @throws CassandraDataAccessException In case of database access error
   */
  public static void addMessageToQueue(
      String columnFamily, String queue, String messageId, byte[] message, Keyspace keyspace)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't add Data , no mutator provided ");
    }

    if (columnFamily == null || queue == null || messageId == null || message == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and queue="
              + queue
              + " message id  = "
              + messageId
              + " message = "
              + message);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.addInsertion(
          queue.trim(),
          columnFamily,
          HFactory.createColumn(messageId, message, stringSerializer, bytesArraySerializer));
      mutator.execute();

    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while adding message to Queue", e);
    }
  }

  /**
   * Add Message to a Given Queue in Cassandra
   *
   * @param columnFamily ColumnFamily name
   * @param queue queue name
   * @param messageId Message id
   * @param message message in bytes
   * @param keyspace Cassandra KeySpace
   * @throws CassandraDataAccessException In case of database access error
   */
  public static void addMessageToQueue(
      String columnFamily, String queue, long messageId, byte[] message, Keyspace keyspace)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't add Data , no mutator provided ");
    }

    if (columnFamily == null || queue == null || message == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and queue="
              + queue
              + " message id  = "
              + messageId
              + " message = "
              + message);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.addInsertion(
          queue.trim(),
          columnFamily,
          HFactory.createColumn(messageId, message, longSerializer, bytesArraySerializer));
      mutator.execute();

    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while adding message to Queue", e);
    }
  }

  /**
   * Add Message to a Given Queue in Cassandra
   *
   * @param columnFamily ColumnFamily name
   * @param queue queue name
   * @param messageId Message id
   * @param message message in bytes
   * @param mutator Cassandra KeySpace
   * @throws CassandraDataAccessException In case of database access error
   */
  public static void addMessageToQueue(
      String columnFamily,
      String queue,
      long messageId,
      byte[] message,
      Mutator<String> mutator,
      boolean execute)
      throws CassandraDataAccessException {

    if (mutator == null) {
      throw new CassandraDataAccessException("Can't add Data , no mutator provided ");
    }

    if (columnFamily == null || queue == null || message == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and queue="
              + queue
              + " message id  = "
              + messageId
              + " message = "
              + message);
    }

    try {
      mutator.addInsertion(
          queue.trim(),
          columnFamily,
          HFactory.createColumn(messageId, message, longSerializer, bytesArraySerializer));

      if (execute) {
        mutator.execute();
      }

    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while adding message to Queue", e);
    }
  }

  /**
   * Add a new Column <int,byte[]> to a given row in a given column family
   *
   * @param columnFamily column Family name
   * @param row row name
   * @param key key of the column
   * @param value value of the column
   * @param keyspace cassandra KeySpace
   * @throws CassandraDataAccessException
   */
  public static void addIntegerByteArrayContentToRaw(
      String columnFamily, String row, int key, byte[] value, Keyspace keyspace)
      throws CassandraDataAccessException {
    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't add Data , no keySpace provided ");
    }

    if (columnFamily == null || row == null || value == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and row="
              + row
              + " key  = "
              + key
              + " value = "
              + value);
    }

    try {
      Mutator<String> messageContentMutator = HFactory.createMutator(keyspace, stringSerializer);
      messageContentMutator.addInsertion(
          row,
          columnFamily,
          HFactory.createColumn(key, value, integerSerializer, bytesArraySerializer));
      messageContentMutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while adding new Column <int,byte[]> to cassandra store", e);
    }
  }

  public static void addIntegerByteArrayContentToRaw(
      String columnFamily,
      String row,
      int key,
      byte[] value,
      Mutator<String> mutator,
      boolean execute)
      throws CassandraDataAccessException {

    if (mutator == null) {
      throw new CassandraDataAccessException("Can't add Data , no Mutator provided ");
    }

    if (columnFamily == null || row == null || value == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and row="
              + row
              + " key  = "
              + key
              + " value = "
              + value);
    }

    mutator.addInsertion(
        row,
        columnFamily,
        HFactory.createColumn(key, value, integerSerializer, bytesArraySerializer));
    if (execute) {
      mutator.execute();
    }
  }

  /**
   * Add new Column<long,long> to a given row in a given cassandra column family
   *
   * @param columnFamily column family name
   * @param row row name
   * @param key long key value of the column
   * @param value long value of the column
   * @param keyspace Cassandra KeySpace
   * @throws CassandraDataAccessException
   */
  public static void addLongContentToRow(
      String columnFamily, String row, long key, long value, Keyspace keyspace)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't add Data , no keySpace provided ");
    }

    if (columnFamily == null || row == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.insert(
          row, columnFamily, HFactory.createColumn(key, value, longSerializer, longSerializer));
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while adding long content to a row", e);
    }
  }

  /**
   * Add a new Column <long,byte[]> to a given row in a given column family
   *
   * @param columnFamily column family name
   * @param row row name
   * @param key long key value
   * @param value value as a byte array
   * @param keyspace CassandraKeySpace
   * @throws CassandraDataAccessException
   */
  public static void addLongByteArrayColumnToRow(
      String columnFamily, String row, long key, byte[] value, Keyspace keyspace)
      throws CassandraDataAccessException {
    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't add Data , no keySpace provided ");
    }

    if (columnFamily == null || row == null || value == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and row="
              + row
              + " key  = "
              + key
              + " value = "
              + value);
    }

    try {
      Mutator<String> messageContentMutator = HFactory.createMutator(keyspace, stringSerializer);
      messageContentMutator.addInsertion(
          row,
          columnFamily,
          HFactory.createColumn(key, value, longSerializer, bytesArraySerializer));
      messageContentMutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while adding new Column <int,byte[]> to cassandra store", e);
    }
  }

  /**
   * Add a Mapping to a Given Row in cassandra column family. Mappings are used as search indexes
   *
   * @param columnFamily columnFamilyName
   * @param row row name
   * @param cKey key name for the adding column
   * @param cValue value for the adding column
   * @param keyspace Cassandra KeySpace
   * @throws CassandraDataAccessException In case of database access error or data error
   */
  public static void addMappingToRaw(
      String columnFamily, String row, String cKey, String cValue, Keyspace keyspace)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't add Data , no KeySpace provided ");
    }

    if (columnFamily == null || row == null || cKey == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + cKey);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.addInsertion(
          row,
          columnFamily,
          HFactory.createColumn(cKey, cValue.trim(), stringSerializer, stringSerializer));
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while adding a Mapping to row ", e);
    }
  }

  public static void addMappingToRow(
      String columnFamily,
      String row,
      String cKey,
      String cValue,
      Mutator<String> mutator,
      boolean execute)
      throws CassandraDataAccessException {

    if (mutator == null) {
      throw new CassandraDataAccessException("Can't add Data , no mutator provided ");
    }

    mutator.addInsertion(
        row,
        columnFamily,
        HFactory.createColumn(cKey, cValue.trim(), stringSerializer, stringSerializer));

    if (execute) {
      mutator.execute();
    }
  }

  /**
   * Add an Mapping Entry to a raw in a given column family
   *
   * @param columnFamily ColumnFamily name
   * @param row row name
   * @param cKey column key
   * @param cValue column value
   * @param mutator mutator
   * @param execute should we execute the insertion. if false it will just and the insertion to the
   *     mutator
   * @throws CassandraDataAccessException In case of database access error or data error
   */
  public static void addMappingToRaw(
      String columnFamily,
      String row,
      String cKey,
      String cValue,
      Mutator<String> mutator,
      boolean execute)
      throws CassandraDataAccessException {

    if (mutator == null) {
      throw new CassandraDataAccessException("Can't add Data , no mutator provided ");
    }

    if (columnFamily == null || row == null || cKey == null) {
      throw new CassandraDataAccessException(
          "Can't add data with columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + cKey);
    }

    try {
      mutator.addInsertion(
          row,
          columnFamily,
          HFactory.createColumn(cKey, cValue.trim(), stringSerializer, stringSerializer));

      if (execute) {
        mutator.execute();
      }
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while adding a Mapping to row ", e);
    }
  }

  /**
   * Delete a given string column in a raw in a column family
   *
   * @param columnFamily column family name
   * @param row row name
   * @param key key name
   * @param keyspace cassandra keySpace
   * @throws CassandraDataAccessException In case of database access error or data error
   */
  public static void deleteStringColumnFromRaw(
      String columnFamily, String row, String key, Keyspace keyspace)
      throws CassandraDataAccessException {
    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't delete Data , no keyspace provided ");
    }

    if (columnFamily == null || row == null || key == null) {
      throw new CassandraDataAccessException(
          "Can't delete data in columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.addDeletion(row, columnFamily, key, stringSerializer);
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while deleting " + key + " from " + columnFamily);
    }
  }

  /**
   * Delete a given string column in a raw in a column family
   *
   * @param columnFamily column family name
   * @param row row name
   * @param key key name
   * @param keyspace cassandra keySpace
   * @throws CassandraDataAccessException In case of database access error or data error
   */
  public static void deleteLongColumnFromRaw(
      String columnFamily, String row, long key, Keyspace keyspace)
      throws CassandraDataAccessException {
    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't delete Data , no keyspace provided ");
    }

    if (columnFamily == null || row == null) {
      throw new CassandraDataAccessException(
          "Can't delete data in columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.addDeletion(row, columnFamily, key, longSerializer);
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while deleting " + key + " from " + columnFamily);
    }
  }

  public static void deleteLongColumnFromRaw(
      String columnFamily, String row, long key, Mutator<String> mutator, boolean execute)
      throws CassandraDataAccessException {

    if (mutator == null) {
      throw new CassandraDataAccessException("Can't delete Data , no mutator provided ");
    }

    if (columnFamily == null || row == null) {
      throw new CassandraDataAccessException(
          "Can't delete data in columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      mutator.addDeletion(row, columnFamily, key, longSerializer);

      if (execute) {
        mutator.execute();
      }

    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while deleting " + key + " from " + columnFamily);
    }
  }

  /**
   * Delete a given string column in a raw in a column family
   *
   * @param columnFamily ColumnFamily Name
   * @param row row name
   * @param key string key to of the column
   * @param mutator Mutator reference
   * @param execute execute the deletion ?
   * @throws CassandraDataAccessException
   */
  public static void deleteStringColumnFromRaw(
      String columnFamily, String row, String key, Mutator<String> mutator, boolean execute)
      throws CassandraDataAccessException {

    if (mutator == null) {
      throw new CassandraDataAccessException("Can't delete Data , no mutator provided ");
    }

    if (columnFamily == null || row == null || key == null) {
      throw new CassandraDataAccessException(
          "Can't delete data in columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      mutator.addDeletion(row, columnFamily, key, stringSerializer);
      if (execute) {
        mutator.execute();
      }
    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while deleting " + key + " from " + columnFamily);
    }
  }

  public static void deleteIntegerColumnFromRow(
      String columnFamily, String row, int key, Mutator<String> mutator, boolean execute)
      throws CassandraDataAccessException {

    if (mutator == null) {
      throw new CassandraDataAccessException("Can't delete Data , no mutator provided ");
    }

    if (columnFamily == null || row == null) {
      throw new CassandraDataAccessException(
          "Can't delete data in columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      mutator.addDeletion(row, columnFamily, key, integerSerializer);

      if (execute) {
        mutator.execute();
      }

    } catch (Exception e) {
      throw new CassandraDataAccessException(
          "Error while deleting " + key + " from " + columnFamily);
    }
  }

  public static void deleteIntegerColumnFromRow(
      String columnFamily, String row, Integer key, Keyspace keyspace)
      throws CassandraDataAccessException {

    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't delete Data , no keyspace provided ");
    }

    if (columnFamily == null || row == null) {
      throw new CassandraDataAccessException(
          "Can't delete data in columnFamily = "
              + columnFamily
              + " and rowName="
              + row
              + " key = "
              + key);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      mutator.addDeletion(row, columnFamily, key, integerSerializer);
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while deleting data", e);
    }
  }

  public static void deleteIntegerColumnsFromRow(
      String columnFamily, List<String> rows, Keyspace keyspace)
      throws CassandraDataAccessException {
    if (keyspace == null) {
      throw new CassandraDataAccessException("Can't delete Data , no keyspace provided ");
    }

    if (columnFamily == null || rows == null) {
      throw new CassandraDataAccessException(
          "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + rows);
    }

    try {
      Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer);
      for (String row : rows) {
        mutator.addDeletion(row, columnFamily, null, integerSerializer);
      }
      mutator.execute();
    } catch (Exception e) {
      throw new CassandraDataAccessException("Error while deleting data", e);
    }
  }
}
예제 #10
0
public class Interface {

  private static StringSerializer stringSerializer = StringSerializer.get();
  private static LongSerializer longSerializer = LongSerializer.get();
  // private ArrayBlockingQueue<Query> causalQueue;
  public static long latency_read = 1; // All latencies given in ms
  public static long latency_insert = 1; // All latencies given in ms
  public static long latency_update = 2; // All latencies given in ms

  public static long latency_SLA = 15; // All latencies given in ms
  public static long curr_deadline = 15; // All latencies given in ms

  public void insert(
      String keyspace,
      String string,
      String string2,
      String string3,
      String string4,
      String string5,
      String string6,
      String string7,
      String string8,
      String string9,
      String string10,
      String string11,
      String string12,
      int index,
      long l,
      long latency_insert2,
      String cLevel,
      String CLID,
      long tInv,
      Tuple tuple) {
    // TODO Auto-generated method stub
    UUID idOne = UUID.randomUUID();
    // System.out.println("string8 value:---"+string8);
    // String cqlStr = "INSERT INTO " + keyspace + "." + string +"(key,"+ string3 +","+ string5
    // +","+ string7 +","+ string9 +","+ string11 + ") VALUES ('"+ string2 + "','"+ string4 +"','"+
    // string6 +"','"+ string8 +"','"+ string10 +"','"+ string12 + "')";
    String cqlStr =
        "INSERT INTO "
            + keyspace
            + "."
            + string
            + "("
            + string2
            + ","
            + string4
            + ","
            + string6
            + ","
            + string8
            + ") VALUES ('"
            + string3
            + "','"
            + string5
            + "','"
            + string7
            + "','"
            + string9
            + "')";
    Statement statement = new SimpleStatement(cqlStr);
    String operationtype = "insert";
    // System.out.println("***insert executed:="+cqlStr);
    scheduleAndQueue(
        operationtype,
        statement,
        tuple.getSession(),
        index,
        l,
        latency_insert2,
        cLevel,
        CLID,
        tInv,
        tuple);
  }

  public String read(
      String keyspace,
      String string,
      String string2,
      String string3,
      int index,
      long l,
      long latency_read2,
      String cLevel,
      String CLID,
      long tInv,
      Tuple tuple) {
    // TODO Auto-generated method stub
    String
        cqlStr =
            "SELECT * from "
                + keyspace
                + "."
                + string
                + " WHERE "
                + string2
                + " = '"
                + string3
                + "' ALLOW FILTERING",
        res = null;
    Statement statement = new SimpleStatement(cqlStr);
    String operationtype = "read";
    // System.out.println("***read executed:-"+cqlStr);
    ResultSet results =
        scheduleAndQueue(
            operationtype,
            statement,
            tuple.getSession(),
            index,
            l,
            latency_read2,
            cLevel,
            CLID,
            tInv,
            tuple);
    if (results != null) {
      for (Row aRow : results) {
        res = aRow.getString("price");
      }
    }

    return res;
  }

  public void update(
      String keyspace,
      String string,
      String string2,
      String string3,
      String string4,
      String string5,
      String string6,
      int index,
      long l,
      long latency_update2,
      String cLevel,
      String CLID,
      long tInv,
      Tuple tuple) {
    // TODO Auto-generated method stub
    String cqlStr =
        "update "
            + keyspace
            + "."
            + string
            + " set "
            + string2
            + " = '"
            + string3
            + "' WHERE "
            + string4
            + " = '"
            + string5
            + "'";
    Statement statement = new SimpleStatement(cqlStr);
    String operationtype = "update";
    // System.out.println("***update executed:="+cqlStr);
    // System.out.println("**statement:="+statement.toString());
    scheduleAndQueue(
        operationtype,
        statement,
        tuple.getSession(),
        index,
        l,
        latency_update2,
        cLevel,
        CLID,
        tInv,
        tuple);
    // QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute();

  }

  public ResultSet scheduleAndQueue(
      String operationtype,
      Statement statement,
      Session session,
      int index,
      long l,
      long latency_insert2,
      String cLevel,
      String clId,
      long tInv,
      Tuple tuple) {
    Query q = null;
    String key = null;
    // UUID idOne = UUID.randomUUID();
    String CLID = null; // String.valueOf(idOne) + String.valueOf(System.currentTimeMillis());
    long latencyDep = 0;
    CopyOnWriteArrayList<RegistryEntry> rList = null;
    String STATUS;
    ResultSet result = null;
    Scheduler scheduler = new Scheduler();

    if ((DependencyChecker.causalList.size() > index
            && ((Query) DependencyChecker.causalList.get(index))
                .getGuarantee()
                .equalsIgnoreCase("C"))
        || (DependencyChecker.serializeList.size() > index
            && ((Query) DependencyChecker.serializeList.get(index))
                .getGuarantee()
                .equalsIgnoreCase("S"))) {
      // System.out.println("***in iof constion causal:serialise");
      if (DependencyChecker.causalList.contains(index)
          && ((Query) DependencyChecker.causalList.get(index))
              .getGuarantee()
              .equalsIgnoreCase("C")) {
        while (!((Query) DependencyChecker.causalList.get(index))
            .getIndex()
            .equalsIgnoreCase(String.valueOf(index))) {
          // System.out.println("***Waiting in causal queue");
        }
        // if(((Query)DependencyChecker.causalList.get(index)).getIndex().equalsIgnoreCase(String.valu             eOf(index)))
        // {
        statement = updateConsistencyLevel(statement, cLevel, index, tuple);
        result = session.execute(statement); // unblock for deploy
        q = tuple.getCausalQueue().remove();
      } else {
        // System.out.println("***in else
        // constionL===="+((Query)tuple.getDependencyChecker().getSerializeList().get(index)).getIndex()+": index"+String.valueOf((int)tuple.getDependencyChecker().getQueryList().size()-index-1));
        if (((Query) DependencyChecker.serializeList.get(index))
            .getIndex()
            .equalsIgnoreCase(
                String.valueOf((int) DependencyChecker.queryList.size() - 1 - index))) {
          // System.out.println("1111in else
          // constionL===="+((Query)tuple.getDependencyChecker().getSerializeList().get(index)).getIndex());
          l = System.currentTimeMillis() - l;
          // key = (String)
          // ((HashMap<String,String>)DependencyChecker.serializeList.get(index)).keySet().toArray()[0];
          key = ((Query) DependencyChecker.serializeList.get(index)).getKey();
          for (int i = index; i < DependencyChecker.serializeList.size(); i++) {
            latencyDep = latencyDep + ((Query) DependencyChecker.serializeList.get(i)).getLatency();
          }
          curr_deadline = curr_deadline - (System.currentTimeMillis() - tInv);
          createRegistry(session);

          writeToRegister(
              ((Query) DependencyChecker.serializeList.get(index)).getKey(),
              index,
              clId,
              l,
              latencyDep,
              curr_deadline,
              "OFF",
              session); // unblock for deploy
          // System.out.println("***After write to register");//for thread
          // "+Thread.currentThread().getName());
          // CLID = readFromRegister(key, session);//unblock for deploy
          // System.out.println("****rlist size:="+ rList.size());
          // if(rList.size()>0)
          // if(CLID!=null)
          // {
          // CLID = scheduler.schedule(rList).split(":")[0];
          // STATUS = scheduler.schedule(rList).split(":")[1];

          while (readFromRegister(key, session) != null
              && readFromRegister(key, session).contains(":")
              && readFromRegister(key, session).split(":")[1].equalsIgnoreCase("OFF")
              && !clId.equalsIgnoreCase(
                  readFromRegister(key, session)
                      .split(":")[0])) // || !STATUS.equalsIgnoreCase("OFF"))
          {
            // System.out.println("***Client " + clId +"Waiting for the lock to the column "+key);//
            // for thread "+Thread.currentThread().getName());
            // System.out.println("***Waiting for the lock to the column "+key);// for thread
            // "+Thread.currentThread().getName());
          }
          scheduler.requestLock(session, key, clId, index); // unblock for deploy
          // System.out.println("***After requesting the lock to the columnfor the query index"+
          // index);
          statement = updateConsistencyLevel(statement, cLevel, index, tuple);
          result = session.execute(statement); // unblock for deploy
          // System.out.println("***After executing the query index"+ index);
          scheduler.releaseLock(session, key, clId, index); // unblock for deploy
          if (tuple.getCausalQueue().peek() != null) {
            q = tuple.getCausalQueue().remove();
            scheduler.removeregistry(session, key, clId, index); // unblock for deploy
          }
          // System.out.println("***After release lock and registry cleanup for the query index"+
          // index);
          // }
        } else {

        }
      }
    } else {
      // System.out.println("In eventual guarantee case:"+mutator);
      statement = updateConsistencyLevel(statement, cLevel, index, tuple);
      // System.out.println("***111 Executing eventual consistent queries statement:="+statement);
      // System.out.println("***222 Executing eventual consistent queries tuple
      // session:="+tuple.getSession());
      result = session.execute(statement); // unblock for deploy
      // if(causalQueue.peek()!=null)
      // q = causalQueue.remove();
    }
    return result;
  }

  public void scheduleAndQueueTPCC(int index, String key, long l, String clId, long tInv) {
    Scheduler scheduler = new Scheduler();
    // Session session = null;
    String cLevel = "ALL", condn = "price>20";
    ClientQueryExampleThread clientQueryExampleThread = new ClientQueryExampleThread(clId, tInv, 1);
    if (ClientQueryExampleThread.session == null || ClientQueryExampleThread.session.isClosed())
      ClientQueryExampleThread.session =
          ClientQueryExampleThread.callClient(cLevel, condn); // unblock for deploy
    Query q = null;
    // UUID idOne = UUID.randomUUID();
    String CLID = null; // String.valueOf(idOne) + String.valueOf(System.currentTimeMillis());
    long latencyDep = 0;
    l = System.currentTimeMillis() - l;
    // key = (String)
    // ((HashMap<String,String>)DependencyChecker.serializeList.get(index)).keySet().toArray()[0];

    curr_deadline = curr_deadline - (System.currentTimeMillis() - tInv);
    createRegistry(ClientQueryExampleThread.session);

    writeToRegister(
        key,
        index,
        clId,
        l,
        latencyDep,
        curr_deadline,
        "OFF",
        ClientQueryExampleThread.session); // unblock for deploy
    System.out.println(
        "***After write to register"); // for thread "+Thread.currentThread().getName());
    // CLID = readFromRegister(key, session);//unblock for deploy
    // System.out.println("****rlist size:="+ rList.size());
    // if(rList.size()>0)
    // if(CLID!=null)
    // {
    // CLID = scheduler.schedule(rList).split(":")[0];
    // STATUS = scheduler.schedule(rList).split(":")[1];

    while (readFromRegister(key, ClientQueryExampleThread.session) != null
        && readFromRegister(key, ClientQueryExampleThread.session).contains(":")
        && readFromRegister(key, ClientQueryExampleThread.session)
            .split(":")[1]
            .equalsIgnoreCase("OFF")
        && !clId.equalsIgnoreCase(
            readFromRegister(key, ClientQueryExampleThread.session)
                .split(":")[0])) // || !STATUS.equalsIgnoreCase("OFF"))
    {
      // System.out.println("***Client " + clId +"Waiting for the lock to the column "+key);// for
      // thread "+Thread.currentThread().getName());
      // System.out.println("***Waiting for the lock to the column "+key);// for thread
      // "+Thread.currentThread().getName());
    }
    scheduler.requestLock(ClientQueryExampleThread.session, key, clId, index); // unblock for deploy
    System.out.println("***After requesting the lock to the columnfor the query index" + index);
  }

  public void clearscheduleAndQueueTPCC(int index, String key, long l, String clId, long tInv) {
    Scheduler scheduler = new Scheduler();
    // Session session = null;
    String cLevel = "ALL", condn = "price>20";
    ClientQueryExampleThread clientQueryExampleThread = new ClientQueryExampleThread(clId, tInv, 1);
    if (ClientQueryExampleThread.session == null || ClientQueryExampleThread.session.isClosed())
      ClientQueryExampleThread.session =
          ClientQueryExampleThread.callClient(cLevel, condn); // unblock for deploy
    Query q = null;
    // UUID idOne = UUID.randomUUID();
    String CLID = null; // String.valueOf(idOne) + String.valueOf(System.currentTimeMillis());
    long latencyDep = 0;
    CopyOnWriteArrayList<RegistryEntry> rList = null;
    String STATUS;
    l = System.currentTimeMillis() - l;
    // key = (String)
    // ((HashMap<String,String>)DependencyChecker.serializeList.get(index)).keySet().toArray()[0];

    curr_deadline = curr_deadline - (System.currentTimeMillis() - tInv);

    System.out.println("***After executing the query index" + index);
    releaseLock(ClientQueryExampleThread.session, key, clId, index); // unblock for deploy
    removeregistry(ClientQueryExampleThread.session, key, clId, index); // unblock for deploy
    System.out.println("***After release lock and registry cleanup for the query index" + index);
  }

  public void requestLock(Session session, String key, String cLID, int index) {
    // TODO Auto-generated method stub
    setRegister(key, index, cLID, session);
  }

  private void setRegister(String key, int index, String clId, Session session) {
    // TODO Auto-generated method stub
    String cqlStr =
        "update consistify.registry set STATUS = 'ON' WHERE KEY = '"
            + key
            + "' and CLID = '"
            + clId
            + "' and indic = '"
            + index
            + "'";
    // System.out.println("****cqlStr:="+cqlStr);
    Statement statement = new SimpleStatement(cqlStr);
    statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
    ResultSet result = session.execute(statement);
  }

  public void releaseLock(Session session, String key, String cLID, int index) {
    // TODO Auto-generated method stub
    String cqlStr =
        "update consistify.registry SET STATUS = 'OFF' WHERE KEY = '"
            + key
            + "' and CLID = '"
            + cLID
            + "' and INDIC = '"
            + index
            + "'";
    Statement statement = new SimpleStatement(cqlStr);
    statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
    ResultSet result = session.execute(statement);
  }

  public void removeregistry(Session session, String key, String cLID, int index) {
    // TODO Auto-generated method stub
    String cqlStr =
        "delete from consistify.registry WHERE KEY =' "
            + key
            + "' and CLID = '"
            + cLID
            + "' and INDIC = '"
            + index
            + "'";
    Statement statement = new SimpleStatement(cqlStr);
    statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
    ResultSet result = session.execute(statement);
  }

  // private ArrayList<RegistryEntry> readFromRegister(String key, Session session) {
  private String readFromRegister(String key, Session session) {
    // TODO Auto-generated method stub
    // Map<String, String> resultMap = new HashMap<String, String>();
    String CLID = null, STATUS = null;
    String cqlStr = "SELECT * from consistify.registry WHERE key='" + key + "'";
    Statement statement = new SimpleStatement(cqlStr);
    ResultSet results = session.execute(statement);
    CopyOnWriteArrayList<RegistryEntry> rList = new CopyOnWriteArrayList<RegistryEntry>();
    RegistryEntry r = null;
    Scheduler scheduler = new Scheduler();
    for (Row aRow : results) {
      // if (!resultMap.containsKey(aRow.getKey())) {
      if (aRow.getString("CREATETIME") != null && aRow.getString("key").equalsIgnoreCase(key)) {
        r = new RegistryEntry();
        r.setKey(aRow.getString("key"));
        // System.out.println("**222*CLID:=="+aRow.getString("CLID"));
        r.setCLID(aRow.getString("CLID"));
        r.setDEADLINE(aRow.getString("DEADLINE"));
        r.setINDEX(aRow.getString("INDIC"));
        r.setLATENCYDEP(aRow.getString("LATENCYDEP"));
        r.setWAITINGTIME(aRow.getString("WAITINGTIME"));
        r.setSTATUS(aRow.getString("STATUS"));
        r.setCREATETIME(aRow.getString("CREATETIME"));
        rList.add(r);
      }
      // resultMap.put(aRow.getKey(), ++rowCnt);
      // System.out.println(aRow.getKey() + ":" + rowCnt);
      // }
    }
    // CLID = scheduler.schedule(rList).split(":")[0];
    CLID = scheduler.schedule(rList);
    // System.out.println("****CLID:="+CLID);
    return CLID;
  }

  private void createRegistry(Session session) {
    String cqlStr =
        "CREATE TABLE IF NOT EXISTS consistify.registry (   key text,   CLID text,   DEADLINE text,   INDIC text,   LATENCYDEP text,   WAITINGTIME text, STATUS text, CREATETIME text,  PRIMARY KEY(key,CLID,INDIC));";
    Statement statement = new SimpleStatement(cqlStr);
    // System.out.println("****consistify cqlStr:="+cqlStr);
    statement.setConsistencyLevel(ConsistencyLevel.ALL);
    session.execute(statement);
  }

  private void writeToRegister(
      String key,
      int index,
      String clId,
      long l,
      long latencyDep,
      long curr_deadline2,
      String string,
      Session session) {
    // TODO Auto-generated method stub
    String cqlStr =
        "INSERT INTO consistify.registry (KEY, CLID, DEADLINE, INDIC, LATENCYDEP,WAITINGTIME,STATUS, CREATETIME) VALUES ('"
            + key
            + "','"
            + clId
            + "','"
            + +curr_deadline2
            + "','"
            + index
            + " ','"
            + latencyDep
            + " ','"
            + l
            + " ','"
            + string
            + " ','"
            + (long) System.currentTimeMillis()
            + "') if not exists";
    Statement statement = new SimpleStatement(cqlStr);

    // System.out.println("****11consistify cqlStr:="+cqlStr);
    statement.setConsistencyLevel(ConsistencyLevel.ALL);
    session.execute(statement);
    // .executeAsync(arg0)
  }

  public static synchronized ArrayBlockingQueue<Query> pushQueues() {
    // TODO Auto-generated method stub
    String key = null;
    ArrayBlockingQueue<Query> causalQueue = null;
    if (DependencyChecker.serializeList.size() > 0)
      causalQueue = new ArrayBlockingQueue<Query>(DependencyChecker.serializeList.size());
    else if (DependencyChecker.causalList.size() > 0)
      causalQueue = new ArrayBlockingQueue<Query>(DependencyChecker.causalList.size());
    // System.out.println("**causalQueue:="+causalQueue.size());
    if (DependencyChecker.serializeList != null && DependencyChecker.serializeList.size() > 0) {
      for (int i = 0; i < DependencyChecker.serializeList.size(); i++) {
        // map = new HashMap<String,String>();
        // key = (String)
        // ((HashMap<String,String>)DependencyChecker.serializeList.get(i)).keySet().toArray()[0];
        // map.put(((HashMap<String,String>)DependencyChecker.serializeList.get(i)).get(key), "S");
        causalQueue.add(DependencyChecker.serializeList.get(i));
        // System.out.println("***causalQueue:="+causalQueue.size());
      }
    } else {
      for (int i = 0; i < DependencyChecker.causalList.size(); i++) {
        // map = new HashMap<String,String>();
        // map.put(DependencyChecker.causalList.get(i), "C");
        causalQueue.add(DependencyChecker.causalList.get(i));
      }
    }
    return causalQueue;
  }

  public Statement updateConsistencyLevel(
      Statement statement, String cLevel, int index, Tuple tuple) {
    // TODO Auto-generated method stub
    // Statement st = new SimpleStatement("CONSISTENCY all");
    for (int i = 0; i < DependencyChecker.serializeList.size(); i++) {
      if (i == index) cLevel = DependencyChecker.serializeList.get(i).getConsistencyLevel();
    }
    statement = setConsistencyLevel(statement, cLevel);
    return statement;
  }

  public Statement setConsistencyLevel(Statement statement, String cLevel) {
    // TODO Auto-generated method stub
    if (cLevel.equalsIgnoreCase("ALL")) statement.setConsistencyLevel(ConsistencyLevel.ALL);
    else if (cLevel.equalsIgnoreCase("QUORUM"))
      statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
    else if (cLevel.equalsIgnoreCase("ONE")) statement.setConsistencyLevel(ConsistencyLevel.ONE);
    else if (cLevel.equalsIgnoreCase("TWO")) statement.setConsistencyLevel(ConsistencyLevel.TWO);
    else if (cLevel.equalsIgnoreCase("ANY")) statement.setConsistencyLevel(ConsistencyLevel.ANY);
    return statement;
  }

  public synchronized Tuple callInterface(
      String condn, String className, String CLID, long tInv, int currthreadCount) {
    Scheduler scheduler = new Scheduler();
    ClientQueryExampleThread clientQueryExampleThread =
        new ClientQueryExampleThread(CLID, tInv, currthreadCount);
    // TODO Auto-generated method stub
    // Session session = null;
    // String cLevel= "ALL",condn = "QTY>20";
    String cLevel = "ONE";
    // Tuple tuple = new Tuple();

    if (!scheduler.checkschedulability()) {
      System.out.println(
          "Not Schedulable under given SLA."); // for thread "+Thread.currentThread().getName());
      System.exit(1);
    }

    if (ClientQueryExampleThread.tuple == null
        || ClientQueryExampleThread.tuple.getSession() == null
        || ClientQueryExampleThread.tuple.getSession().isClosed()) {
      if (DependencyChecker.queryList == null || DependencyChecker.queryList.size() <= 0) {
        DependencyChecker.getGuarantee(condn, className, CLID, tInv);
        // System.out.println("***dependencyChecker
        // tSerializeLis:="+dependencyChecker.getSerializeList().size());
        // tuple.seDependencyChecker();
      }
      ClientQueryExampleThread.tuple = new Tuple();
      Session session = ClientQueryExampleThread.callClient(cLevel, condn);
      ClientQueryExampleThread.tuple.setSession(session); // unblock for deploy
      // System.out.println("***After ClientQueryExampleThread.tuple
      // session:="+ClientQueryExampleThread.tuple.getSession());
      ArrayBlockingQueue<Query> causalQueue = Interface.pushQueues();
      System.out.println(
          "***DependencyChecker.serializeList.size():=" + DependencyChecker.serializeList.size());
      /*while(DependencyChecker.serializeList.size()<=1){

      }*/
      if (DependencyChecker.serializeList.size() > 1) Verifier.chooseConsistencyLevels();
      ClientQueryExampleThread.tuple.setCausalQueue(causalQueue);
    }
    // tuple.setSession(ClientQueryExampleThread.session);
    return ClientQueryExampleThread.tuple;
  }

  public static void main(String[] args) {
    Interface intrface = new Interface();
    // app is now the gateway.entry_point
    GatewayServer server = new GatewayServer(intrface, 9009);
    server.start();
  }
}
예제 #11
0
 public void setLong(N subColumnName, Long value) {
   subColumns.add(
       columnFactory.createColumn(
           subColumnName, value, clock, template.getSubSerializer(), LongSerializer.get()));
 }