Java Code Examples for org.apache.hadoop.hbase.util.Bytes.SIZEOF_SHORT

The following are Jave code examples for showing how to use SIZEOF_SHORT of the org.apache.hadoop.hbase.util.Bytes class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ditb   File: HFileReaderV3.java   View Source Code Vote up 7 votes
protected final void readKeyValueLen() {
  // TODO: METHOD (mostly) DUPLICATED IN V2!!!! FIXED in master branch by collapsing v3 and v2.
  // This is a hot method. We go out of our way to make this method short so it can be
  // inlined and is not too big to compile. We also manage position in ByteBuffer ourselves
  // because it is faster than going via range-checked ByteBuffer methods or going through a
  // byte buffer array a byte at a time.
  int p = blockBuffer.position() + blockBuffer.arrayOffset();
  // Get a long at a time rather than read two individual ints. In micro-benchmarking, even
  // with the extra bit-fiddling, this is order-of-magnitude faster than getting two ints.
  long ll = Bytes.toLong(blockBuffer.array(), p);
  // Read top half as an int of key length and bottom int as value length
  this.currKeyLen = (int)(ll >> Integer.SIZE);
  this.currValueLen = (int)(Bytes.MASK_FOR_LOWER_INT_IN_LONG ^ ll);
  checkKeyValueLen();
  // Move position past the key and value lengths and then beyond the key and value
  p += (Bytes.SIZEOF_LONG + currKeyLen + currValueLen);
  if (reader.hfileContext.isIncludesTags()) {
    // Tags length is a short.
    this.currTagsLen = Bytes.toShort(blockBuffer.array(), p);
    checkTagsLen();
    p += (Bytes.SIZEOF_SHORT + currTagsLen);
  }
  readMvccVersion(p);
}
 
Example 2
Project: ditb   File: KeyValue.java   View Source Code Vote up 7 votes
/**
 * Use for logging.
 * @param b Key portion of a KeyValue.
 * @param o Offset to start of key
 * @param l Length of key.
 * @return Key as a String.
 */
public static String keyToString(final byte [] b, final int o, final int l) {
  if (b == null) return "";
  int rowlength = Bytes.toShort(b, o);
  String row = Bytes.toStringBinary(b, o + Bytes.SIZEOF_SHORT, rowlength);
  int columnoffset = o + Bytes.SIZEOF_SHORT + 1 + rowlength;
  int familylength = b[columnoffset - 1];
  int columnlength = l - ((columnoffset - o) + TIMESTAMP_TYPE_SIZE);
  String family = familylength == 0? "":
    Bytes.toStringBinary(b, columnoffset, familylength);
  String qualifier = columnlength == 0? "":
    Bytes.toStringBinary(b, columnoffset + familylength,
    columnlength - familylength);
  long timestamp = Bytes.toLong(b, o + (l - TIMESTAMP_TYPE_SIZE));
  String timestampStr = humanReadableTimestamp(timestamp);
  byte type = b[o + l - 1];
  return row + "/" + family +
    (family != null && family.length() > 0? ":" :"") +
    qualifier + "/" + timestampStr + "/" + Type.codeToType(type);
}
 
Example 3
Project: ditb   File: HFileReaderV3.java   View Source Code Vote up 5 votes
@Override
protected int getCellBufSize() {
  int kvBufSize = super.getCellBufSize();
  if (reader.hfileContext.isIncludesTags()) {
    kvBufSize += Bytes.SIZEOF_SHORT + currTagsLen;
  }
  return kvBufSize;
}
 
Example 4
Project: ditb   File: HFileReaderV3.java   View Source Code Vote up 5 votes
@Override
protected int getNextCellStartPosition() {
  int nextKvPos = super.getNextCellStartPosition();
  if (reader.hfileContext.isIncludesTags()) {
    nextKvPos += Bytes.SIZEOF_SHORT + currTagsLen;
  }
  return nextKvPos;
}
 
Example 5
Project: ditb   File: IntegrationTestBigLinkedList.java   View Source Code Vote up 5 votes
/**
 * @param ordinal
 * @param r
 * @return Return new byte array that has <code>ordinal</code> as prefix on front taking up
 * Bytes.SIZEOF_SHORT bytes followed by <code>r</code>
 */
public static byte[] addPrefixFlag(final int ordinal, final byte [] r) {
  byte[] prefix = Bytes.toBytes((short)ordinal);
  if (prefix.length != Bytes.SIZEOF_SHORT) {
    throw new RuntimeException("Unexpected size: " + prefix.length);
  }
  byte[] result = new byte[prefix.length + r.length];
  System.arraycopy(prefix, 0, result, 0, prefix.length);
  System.arraycopy(r, 0, result, prefix.length, r.length);
  return result;
}
 
Example 6
Project: ditb   File: ServerName.java   View Source Code Vote up 5 votes
/**
 * Use this method instantiating a {@link ServerName} from bytes
 * gotten from a call to {@link #getVersionedBytes()}.  Will take care of the
 * case where bytes were written by an earlier version of hbase.
 * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
 * @return A ServerName instance.
 * @see #getVersionedBytes()
 */
public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
  // Version is a short.
  short version = Bytes.toShort(versionedBytes);
  if (version == VERSION) {
    int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
    return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
  }
  // Presume the bytes were written with an old version of hbase and that the
  // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
  return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE);
}
 
Example 7
Project: ditb   File: KeyValue.java   View Source Code Vote up 5 votes
/**
 * @return Family offset
 */
private int getFamilyOffset(int rlength) {
  return this.offset + ROW_OFFSET + Bytes.SIZEOF_SHORT + rlength + Bytes.SIZEOF_BYTE;
}
 
Example 8
Project: ditb   File: KeyValue.java   View Source Code Vote up 5 votes
@Override
public int getFamilyOffset() {
  return this.offset + Bytes.SIZEOF_SHORT + getRowLength() + Bytes.SIZEOF_BYTE;
}
 
Example 9
Project: ditb   File: CellUtil.java   View Source Code Vote up 5 votes
/**
 * Util method to iterate through the tags
 *
 * @param tags
 * @param offset
 * @param length
 * @return iterator for the tags
 */
public static Iterator<Tag> tagsIterator(final byte[] tags, final int offset, final int length) {
  return new Iterator<Tag>() {
    private int pos = offset;
    private int endOffset = offset + length - 1;

    @Override
    public boolean hasNext() {
      return this.pos < endOffset;
    }

    @Override
    public Tag next() {
      if (hasNext()) {
        int curTagLen = Bytes.readAsInt(tags, this.pos, Tag.TAG_LENGTH_SIZE);
        Tag tag = new Tag(tags, pos, curTagLen + Tag.TAG_LENGTH_SIZE);
        this.pos += Bytes.SIZEOF_SHORT + curTagLen;
        return tag;
      }
      return null;
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
  };
}
 
Example 10
Project: ditb   File: IntegrationTestBigLinkedList.java   View Source Code Vote up 4 votes
/**
 * @param bw
 * @return Row bytes minus the type flag.
 */
public static byte[] getRowOnly(BytesWritable bw) {
  byte[] bytes = new byte [bw.getLength() - Bytes.SIZEOF_SHORT];
  System.arraycopy(bw.getBytes(), Bytes.SIZEOF_SHORT, bytes, 0, bytes.length);
  return bytes;
}
 
Example 11
Project: ditb   File: KeyValue.java   View Source Code Vote up 4 votes
/**
 * @return Row offset
 */
@Override
public int getRowOffset() {
  return getKeyOffset() + Bytes.SIZEOF_SHORT;
}
 
Example 12
Project: ditb   File: KeyValue.java   View Source Code Vote up 4 votes
@Override
public int getRowOffset() {
  return getKeyOffset() + Bytes.SIZEOF_SHORT;
}
 
Example 13
Project: ditb   File: RawShort.java   View Source Code Vote up 4 votes
@Override
public int encodedLength(Short val) { return Bytes.SIZEOF_SHORT; }
 
Example 14
Project: ditb   File: RawShort.java   View Source Code Vote up 4 votes
@Override
public int skip(PositionedByteRange src) {
  src.setPosition(src.getPosition() + Bytes.SIZEOF_SHORT);
  return Bytes.SIZEOF_SHORT;
}
 
Example 15
Project: ditb   File: EncodedDataBlock.java   View Source Code Vote up 4 votes
/**
 * Provides access to compressed value.
 * @param headerSize header size of the block.
 * @return Forwards sequential iterator.
 */
public Iterator<Cell> getIterator(int headerSize) {
  final int rawSize = rawKVs.length;
  byte[] encodedDataWithHeader = getEncodedData();
  int bytesToSkip = headerSize + Bytes.SIZEOF_SHORT;
  ByteArrayInputStream bais = new ByteArrayInputStream(encodedDataWithHeader,
      bytesToSkip, encodedDataWithHeader.length - bytesToSkip);
  final DataInputStream dis = new DataInputStream(bais);

  return new Iterator<Cell>() {
    private ByteBuffer decompressedData = null;

    @Override
    public boolean hasNext() {
      if (decompressedData == null) {
        return rawSize > 0;
      }
      return decompressedData.hasRemaining();
    }

    @Override
    public Cell next() {
      if (decompressedData == null) {
        try {
          decompressedData = dataBlockEncoder.decodeKeyValues(dis, dataBlockEncoder
              .newDataBlockDecodingContext(meta));
        } catch (IOException e) {
          throw new RuntimeException("Problem with data block encoder, " +
              "most likely it requested more bytes than are available.", e);
        }
        decompressedData.rewind();
      }
      int offset = decompressedData.position();
      int klen = decompressedData.getInt();
      int vlen = decompressedData.getInt();
      int tagsLen = 0;
      ByteBufferUtils.skip(decompressedData, klen + vlen);
      // Read the tag length in case when steam contain tags
      if (meta.isIncludesTags()) {
        tagsLen = ((decompressedData.get() & 0xff) << 8) ^ (decompressedData.get() & 0xff);
        ByteBufferUtils.skip(decompressedData, tagsLen);
      }
      KeyValue kv = new KeyValue(decompressedData.array(), offset,
          (int) KeyValue.getKeyValueDataStructureSize(klen, vlen, tagsLen));
      if (meta.isIncludesMvcc()) {
        long mvccVersion = ByteBufferUtils.readVLong(decompressedData);
        kv.setSequenceId(mvccVersion);
      }
      return kv;
    }

    @Override
    public void remove() {
      throw new NotImplementedException("remove() is not supported!");
    }

    @Override
    public String toString() {
      return "Iterator of: " + dataBlockEncoder.getClass().getName();
    }

  };
}
 
Example 16
Project: ditb   File: BufferedDataBlockEncoder.java   View Source Code Vote up 4 votes
@Override
public int getRowOffset() {
  return Bytes.SIZEOF_SHORT;
}
 
Example 17
Project: ditb   File: BufferedDataBlockEncoder.java   View Source Code Vote up 4 votes
@Override
public int getRowOffset() {
  return Bytes.SIZEOF_SHORT;
}