Esempio n. 1
0
  /**
   * Convert Titan internal Mutation representation into HBase native commands.
   *
   * @param mutations Mutations to convert into HBase commands.
   * @param putTimestamp The timestamp to use for Put commands.
   * @param delTimestamp The timestamp to use for Delete commands.
   * @return Commands sorted by key converted from Titan internal representation.
   */
  private static Map<ByteBuffer, Pair<Put, Delete>> convertToCommands(
      Map<String, Map<ByteBuffer, KCVMutation>> mutations,
      final long putTimestamp,
      final long delTimestamp) {
    Map<ByteBuffer, Pair<Put, Delete>> commandsPerKey =
        new HashMap<ByteBuffer, Pair<Put, Delete>>();

    for (Map.Entry<String, Map<ByteBuffer, KCVMutation>> entry : mutations.entrySet()) {
      byte[] cfName = entry.getKey().getBytes();

      for (Map.Entry<ByteBuffer, KCVMutation> m : entry.getValue().entrySet()) {
        ByteBuffer key = m.getKey();
        KCVMutation mutation = m.getValue();

        Pair<Put, Delete> commands = commandsPerKey.get(key);

        if (commands == null) {
          commands = new Pair<Put, Delete>();
          commandsPerKey.put(key, commands);
        }

        if (mutation.hasDeletions()) {
          if (commands.getSecond() == null)
            commands.setSecond(new Delete(ByteBufferUtil.getArray(key), delTimestamp, null));

          for (ByteBuffer b : mutation.getDeletions()) {
            commands.getSecond().deleteColumns(cfName, ByteBufferUtil.getArray(b), delTimestamp);
          }
        }

        if (mutation.hasAdditions()) {
          if (commands.getFirst() == null)
            commands.setFirst(new Put(ByteBufferUtil.getArray(key), putTimestamp));

          for (Entry e : mutation.getAdditions()) {
            commands
                .getFirst()
                .add(
                    cfName,
                    ByteBufferUtil.getArray(e.getColumn()),
                    putTimestamp,
                    ByteBufferUtil.getArray(e.getValue()));
          }
        }
      }
    }

    return commandsPerKey;
  }
Esempio n. 2
0
 private void handleScvf(SingleColumnValueFilter scvf) {
   ValuePartition vp = null;
   if (scvf instanceof SingleColumnValuePartitionFilter) {
     vp = ((SingleColumnValuePartitionFilter) scvf).getValuePartition();
   }
   Column column = new Column(scvf.getFamily(), scvf.getQualifier(), vp);
   Pair<Value, Value> pair = colWithOperators.get(column);
   if (pair == null) {
     pair = new Pair<Value, Value>();
     // The first operator should be set here
     pair.setFirst(new Value(scvf.getOperator(), scvf.getComparator().getValue(), scvf));
     colWithOperators.put(column, pair);
   } else {
     if (pair.getFirst() != null && pair.getSecond() == null) {
       // TODO As Anoop said we may have to check the Value type also..
       // We can not compare and validate this way. btw "a" and "K".
       // Only in case of Numeric col type we can have this check.
       byte[] curBoundValue = scvf.getComparator().getValue();
       byte[] prevBoundValue = pair.getFirst().getValue();
       int result = Bytes.compareTo(prevBoundValue, curBoundValue);
       CompareOp curBoundOperator = scvf.getOperator();
       CompareOp prevBoundOperator = pair.getFirst().getOperator();
       switch (curBoundOperator) {
         case GREATER:
         case GREATER_OR_EQUAL:
           if (prevBoundOperator == CompareOp.GREATER
               || prevBoundOperator == CompareOp.GREATER_OR_EQUAL) {
             LOG.warn("Wrong usage.  It should be < > || > <.  Cannot be > >");
             if (result > 1) {
               pair.setFirst(new Value(curBoundOperator, curBoundValue, scvf));
             }
             pair.setSecond(null);
           } else if (prevBoundOperator == CompareOp.LESS
               || prevBoundOperator == CompareOp.LESS_OR_EQUAL) {
             if (result < 1) {
               LOG.warn("Possible wrong usage as there cannot be a value < 10 and  > 20");
               pair.setFirst(null);
               pair.setSecond(null);
             } else {
               pair.setSecond(new Value(curBoundOperator, curBoundValue, scvf));
             }
           } else if (prevBoundOperator == CompareOp.EQUAL) {
             LOG.warn("Use the equal operator and ignore the current one");
             pair.setSecond(null);
           }
           break;
         case LESS:
         case LESS_OR_EQUAL:
           if (prevBoundOperator == CompareOp.LESS
               || prevBoundOperator == CompareOp.LESS_OR_EQUAL) {
             LOG.warn("Wrong usage.  It should be < > || > <.  Cannot be > >");
             if (result < 1) {
               pair.setFirst(new Value(curBoundOperator, curBoundValue, scvf));
             }
             pair.setSecond(null);
           } else if (prevBoundOperator == CompareOp.GREATER
               || prevBoundOperator == CompareOp.GREATER_OR_EQUAL) {
             if (result > 1) {
               LOG.warn("Possible wrong usage as there cannot be a value < 10 and  > 20");
               pair.setFirst(null);
               pair.setSecond(null);
             } else {
               pair.setSecond(new Value(curBoundOperator, curBoundValue, scvf));
             }
           } else if (prevBoundOperator == CompareOp.EQUAL) {
             LOG.warn("Use the EQUAL operator only  and ignore the current one.");
             pair.setSecond(null);
           }
           break;
         case EQUAL:
           // For equal condition give priority to equals only..
           // If the prevOperator is also == and the current is also ==
           // take the second one.(Currently)
           if (prevBoundOperator == CompareOp.LESS
               || prevBoundOperator == CompareOp.LESS_OR_EQUAL
               || prevBoundOperator == CompareOp.EQUAL
               || prevBoundOperator == CompareOp.GREATER
               || prevBoundOperator == CompareOp.GREATER_OR_EQUAL) {
             pair.setFirst(new Value(curBoundOperator, curBoundValue, scvf));
             pair.setSecond(null);
           }
           break;
         case NOT_EQUAL:
         case NO_OP:
           // Need to check this
           break;
       }
     } else {
       LOG.warn(
           "Am getting an extra comparison coming for the same col family."
               + "I cannot have 3 conditions on the same column");
       pair.setFirst(null);
       pair.setSecond(null);
     }
   }
 }
Esempio n. 3
0
 @Override
 public void asSubByteBuffer(int offset, int length, Pair<ByteBuffer, Integer> pair) {
   // Just return the single BB that is available
   pair.setFirst(this.buf);
   pair.setSecond(offset);
 }