Java Code Examples for org.apache.hadoop.hbase.util.Bytes.toInt()

The following are Jave code examples for showing how to use toInt() 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: LMDTester.java   View Source Code Vote up 10 votes
private void printResult(Result result) {
  long rowkey = Bytes.toInt(result.getRow());
  //    int a = Bytes.toInt(result.getValue(familyName, Bytes.toBytes("a")));
  //    int b = Bytes.toInt(result.getValue(familyName, Bytes.toBytes("b")));
  //    int c = Bytes.toInt(result.getValue(familyName, Bytes.toBytes("c")));
  //    int info = Bytes.toInt(result.getValue(familyName, Bytes.toBytes("info")));
  StringBuilder sb = new StringBuilder();
  sb.append("{");
  sb.append("rowkey=").append(rowkey).append(",");

  for (Cell cell : result.listCells()) {
    sb.append(Bytes.toString(cell.getQualifier())).append("=")
        .append(Bytes.toInt(cell.getValue())).append(",");
  }

  //    sb.append("a=").append(a).append(",");
  //    sb.append("b=").append(b).append(",");
  //    sb.append("c=").append(c).append(",");
  //    sb.append("info=").append(info).append(",");
  sb.append("}");
  System.out.println(sb.toString());
}
 
Example 2
Project: ditb   File: LMDIndexDirectStoreFileScanner.java   View Source Code Vote up 6 votes
private MDRange[] getRanges(TreeMap<byte[], TreeSet<byte[]>> indexFamilyMap,
    ScanRange.ScanRangeList rangeList) {
  List<MDRange> mdRangeList = new ArrayList<>();
  for (Map.Entry<byte[], TreeSet<byte[]>> entry : indexFamilyMap.entrySet()) {
    for (byte[] qualifier : entry.getValue()) {
      boolean match = false;
      for (ScanRange range : rangeList.getRanges()) {
        if (Bytes.compareTo(entry.getKey(), range.getFamily()) == 0
            && Bytes.compareTo(qualifier, range.getQualifier()) == 0) {
          int min = (range.getStart() == null) ? 0 : Bytes.toInt(range.getStart());
          int max =
              range.getRawStop() == null ? Integer.MAX_VALUE : Bytes.toInt(range.getRawStop());
          mdRangeList.add(new MDRange(min, max));
          match = true;
        }
      }
      if (!match) {
        mdRangeList.add(new MDRange(0, Integer.MAX_VALUE));
      }
    }
  }
  return mdRangeList.toArray(new MDRange[mdRangeList.size()]);
}
 
Example 3
Project: ditb   File: RecoverableZooKeeper.java   View Source Code Vote up 6 votes
public byte[] removeMetaData(byte[] data) {
  if(data == null || data.length == 0) {
    return data;
  }
  // check the magic data; to be backward compatible
  byte magic = data[0];
  if(magic != MAGIC) {
    return data;
  }

  int idLength = Bytes.toInt(data, ID_LENGTH_OFFSET);
  int dataLength = data.length-MAGIC_SIZE-ID_LENGTH_SIZE-idLength;
  int dataOffset = MAGIC_SIZE+ID_LENGTH_SIZE+idLength;

  byte[] newData = new byte[dataLength];
  System.arraycopy(data, dataOffset, newData, 0, dataLength);
  return newData;
}
 
Example 4
Project: ditb   File: LMDIndexWriter.java   View Source Code Vote up 6 votes
/**
 * parse put, add index put into mdRecordList
 */
private void processPut(List<KeyValue> mdRecordList, Put put) throws IOException {
  if (put == null) return;
  byte[] rawRowkey = put.getRow();
  int[] arr = new int[dimensions];
  int i = 0;
  for (Map.Entry<byte[], TreeSet<byte[]>> entry : tableRelation.getIndexFamilyMap().entrySet()) {
    for (byte[] qualifier : entry.getValue()) {
      arr[i] = Bytes.toInt(put.get(entry.getKey(), qualifier).get(0).getValue());
      ++i;
    }
  }
  byte[] mdKey = MDUtils.bitwiseZip(arr, dimensions);
  KeyValue keyValue =
      new KeyValue(mdKey, LMDIndexConstants.FAMILY, rawRowkey, put.getTimeStamp(), Type.Put,
          LMDIndexConstants.VALUE);
  mdRecordList.add(keyValue);
}
 
Example 5
Project: stroom-stats   File: ValueCellValue.java   View Source Code Vote up 5 votes
public ValueCellValue(final byte[] bytes, final int cellValueOffset, final int cellValueLength) {
    if (bytes == null || bytes.length == 0) {
        // cell doesn't exist or contains null so treat as zero value and
        // zero count
        this.count = 0;
        this.aggregatedValue = 0;
        this.minValue = 0;
        this.maxValue = 0;

    } else {
        // check the cell is the right length
        if (cellValueLength != CELL_BYTE_ARRAY_LENGTH) {
            throw new RuntimeException("Cell value is not a valid format.  Expecting " + CELL_BYTE_ARRAY_LENGTH
                    + ", got " + cellValueLength + ". Cell value is ["
                    + ByteArrayUtils.byteArrayToHex(Arrays.copyOfRange(bytes, cellValueOffset, cellValueLength)));
        }
        int position = cellValueOffset;

        // extract the four parts of the cell value (count, value, min and
        // max) from the byte[]
        this.count = Bytes.toInt(bytes, position, COUNT_BYTES_LENGTH);
        this.aggregatedValue = Bytes
                .toDouble(Arrays.copyOfRange(bytes, position += COUNT_BYTES_LENGTH, position + VALUE_BYTES_LENGTH));
        this.minValue = Bytes
                .toDouble(Arrays.copyOfRange(bytes, position += VALUE_BYTES_LENGTH, position + VALUE_BYTES_LENGTH));
        this.maxValue = Bytes
                .toDouble(Arrays.copyOfRange(bytes, position += VALUE_BYTES_LENGTH, position + VALUE_BYTES_LENGTH));
    }
}
 
Example 6
Project: ditb   File: MDRangeFilter.java   View Source Code Vote up 5 votes
@Override public ReturnCode filterKeyValue(Cell cell) {
  byte[] value = cell.getValue();
  for (int i = 0; i < ranges.length; i++) {
    int v = Bytes.toInt(value, i * 4);
    if (!ranges[i].include(v)) {
      return ReturnCode.NEXT_ROW;
    }
  }
  return ReturnCode.INCLUDE;
}
 
Example 7
Project: ditb   File: IndexFile.java   View Source Code Vote up 5 votes
public boolean next() throws IOException {
  // LOG.deug("rem:" + block.remaining() + " p:" + block.position() +
  // " kl: " + currKeyLen + " kv: " + currValueLen);
  // winter where scanning index happens
  if (block == null) {
    throw new IOException("Next called on non-seeked scanner");
  }
  block.position(block.position() + currKeyLen + currValueLen);
  if (block.remaining() <= 0) {
    // LOG.debug("Fetch next block");
    currBlock++;
    if (currBlock >= reader.blockIndex.count) {
      // damn we are at the end
      currBlock = 0;
      block = null;
      return false;
    }
    block = reader.readBlock(this.currBlock, this.cacheBlocks, this.pread);
    currKeyLen = Bytes.toInt(block.array(), block.arrayOffset()+block.position(), 4);
    currValueLen = Bytes.toInt(block.array(), block.arrayOffset()+block.position()+4, 4);
    block.position(block.position()+8);
    blockFetches++;
    return true;
  }
  // LOG.debug("rem:" + block.remaining() + " p:" + block.position() +
  // " kl: " + currKeyLen + " kv: " + currValueLen);
  currKeyLen = Bytes.toInt(block.array(), block.arrayOffset()+block.position(), 4);
  currValueLen = Bytes.toInt(block.array(), block.arrayOffset()+block.position()+4, 4);
  block.position(block.position()+8);
  return true;
}
 
Example 8
Project: ditb   File: DefaultVisibilityLabelServiceImpl.java   View Source Code Vote up 5 votes
@Override
public List<String> getGroupAuths(String[] groups, boolean systemCall)
    throws IOException {
  assert (labelsRegion != null || systemCall);
  if (systemCall || labelsRegion == null) {
    return this.labelsCache.getGroupAuths(groups);
  }
  Scan s = new Scan();
  if (groups != null && groups.length > 0) {
    for (String group : groups) {
      s.addColumn(LABELS_TABLE_FAMILY, Bytes.toBytes(AuthUtil.toGroupEntry(group)));
    }
  }
  Filter filter = VisibilityUtils.createVisibilityLabelFilter(this.labelsRegion,
      new Authorizations(SYSTEM_LABEL));
  s.setFilter(filter);
  Set<String> auths = new HashSet<String>();
  RegionScanner scanner = this.labelsRegion.getScanner(s);
  try {
    List<Cell> results = new ArrayList<Cell>(1);
    while (true) {
      scanner.next(results);
      if (results.isEmpty()) break;
      Cell cell = results.get(0);
      int ordinal = Bytes.toInt(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
      String label = this.labelsCache.getLabel(ordinal);
      if (label != null) {
        auths.add(label);
      }
      results.clear();
    }
  } finally {
    scanner.close();
  }
  return new ArrayList<String>(auths);
}
 
Example 9
Project: ditb   File: TestRowProcessorEndpoint.java   View Source Code Vote up 5 votes
@Override
public void process(long now, HRegion region,
    List<Mutation> mutations, WALEdit walEdit) throws IOException {
  // Scan current counter
  List<Cell> kvs = new ArrayList<Cell>();
  Scan scan = new Scan(row, row);
  scan.addColumn(FAM, COUNTER);
  doScan(region, scan, kvs);
  counter = kvs.size() == 0 ? 0 :
    Bytes.toInt(CellUtil.cloneValue(kvs.iterator().next()));

  // Assert counter value
  assertEquals(expectedCounter, counter);

  // Increment counter and send it to both memstore and wal edit
  counter += 1;
  expectedCounter += 1;


  Put p = new Put(row);
  KeyValue kv =
      new KeyValue(row, FAM, COUNTER, now, Bytes.toBytes(counter));
  p.add(kv);
  mutations.add(p);
  walEdit.add(kv);

  // We can also inject some meta data to the walEdit
  KeyValue metaKv = new KeyValue(
      row, WALEdit.METAFAMILY,
      Bytes.toBytes("I just increment counter"),
      Bytes.toBytes(counter));
  walEdit.add(metaKv);
}
 
Example 10
Project: ditb   File: MDIndex.java   View Source Code Vote up 5 votes
/**
 * fetches a bucket which holds the queried row.
 *
 * @param row a queried row key
 * @return a bucket which holds the queried row.
 * @throws IOException
 */
public MDBucket fetchBucket(byte[] row) throws IOException {
  Result bucketEntry = getRowOrBefore(bucketTable, row, MDHBaseAdmin.BUCKET_FAMILY);
  byte[] bucketKey = bucketEntry.getRow();
  // prefix length has been written while creating table, defined to #dimension
  int prefixLength = Bytes.toInt(
      bucketEntry.getValue(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_PREFIX_LEN_QUALIFIER));
  MDRange[] ranges = toRanges(bucketKey, prefixLength);
  return createBucket(ranges);
}
 
Example 11
Project: ditb   File: IndexKeyValue.java   View Source Code Vote up 5 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 qualifierlength = Bytes.toInt(b, o);
  String qualifier = Bytes.toStringBinary(b, o + Bytes.SIZEOF_INT, qualifierlength);
  String qualifierValue =
      Bytes.toStringBinary(b, o + Bytes.SIZEOF_INT + qualifierlength, l - Bytes.SIZEOF_INT
          - qualifierlength);

  return qualifier + "/" + qualifierValue;
}
 
Example 12
Project: ditb   File: MDIndex.java   View Source Code Vote up 5 votes
private void splitBucket(byte[] splitKey) throws IOException {
  Result bucketEntry = getRowOrBefore(bucketTable, splitKey, MDHBaseAdmin.BUCKET_FAMILY);
  byte[] bucketKey = bucketEntry.getRow();
  int prefixLength = Bytes.toInt(
      bucketEntry.getValue(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_PREFIX_LEN_QUALIFIER));
  long bucketSize = Bytes.toLong(
      bucketEntry.getValue(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_SIZE_QUALIFIER));
  int newPrefixLength = prefixLength + 1;
  if (newPrefixLength > 32 * 2) {
    return; // exceeds the maximum prefix length.
  }
  byte[] newChildKey0 = bucketKey;
  byte[] newChildKey1 = MDUtils.makeBit(bucketKey, prefixLength, dimensions);
  Scan scan = new Scan(newChildKey0, newChildKey1);
  scan.addFamily(MDHBaseAdmin.SECONDARY_FAMILY);
  scan.setCaching(1000);
  ResultScanner scanner = secondaryTable.getScanner(scan);
  long newSize = 0L;
  for (Result result : scanner) {
    newSize += result.getFamilyMap(MDHBaseAdmin.SECONDARY_FAMILY).size();
  }
  splitTimes++;
  scanner.close();
  Put put0 = new Put(newChildKey0);
  put0.addColumn(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_PREFIX_LEN_QUALIFIER,
      Bytes.toBytes(newPrefixLength));
  put0.addColumn(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_SIZE_QUALIFIER,
      Bytes.toBytes(newSize));
  Put put1 = new Put(newChildKey1);
  put1.addColumn(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_PREFIX_LEN_QUALIFIER,
      Bytes.toBytes(newPrefixLength));
  put1.addColumn(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_SIZE_QUALIFIER,
      Bytes.toBytes(bucketSize - newSize));
  List<Put> puts = new ArrayList<>(2);
  puts.add(put0);
  puts.add(put1);
  bucketTable.put(puts);
  maySplit(newChildKey0, newSize);
  maySplit(newChildKey1, bucketSize - newSize);
}
 
Example 13
Project: stroom-stats   File: SimpleRowKeyBuilder.java   View Source Code Vote up 4 votes
public static long getPartialTimestamp(final RowKey rowKey, final EventStoreTimeIntervalEnum timeInterval) {
    final long IntervalNo = Bytes.toInt(rowKey.getPartialTimestamp());
    final long intervalSizeMillis = timeInterval.rowKeyInterval();
    final long partialTimestampMillis = IntervalNo * intervalSizeMillis;
    return partialTimestampMillis;
}
 
Example 14
Project: ditb   File: TestZKLeaderManager.java   View Source Code Vote up 4 votes
@Test
public void testLeaderSelection() throws Exception {
  MockLeader currentLeader = getCurrentLeader();
  // one leader should have been found
  assertNotNull("Leader should exist", currentLeader);
  LOG.debug("Current leader index is "+currentLeader.getIndex());

  byte[] znodeData = ZKUtil.getData(currentLeader.getWatcher(), LEADER_ZNODE);
  assertNotNull("Leader znode should contain leader index", znodeData);
  assertTrue("Leader znode should not be empty", znodeData.length > 0);
  int storedIndex = Bytes.toInt(znodeData);
  LOG.debug("Stored leader index in ZK is "+storedIndex);
  assertEquals("Leader znode should match leader index",
      currentLeader.getIndex(), storedIndex);

  // force a leader transition
  currentLeader.abdicate();
  assertFalse(currentLeader.isMaster());

  // check for new leader
  currentLeader = getCurrentLeader();
  // one leader should have been found
  assertNotNull("New leader should exist after abdication", currentLeader);
  LOG.debug("New leader index is "+currentLeader.getIndex());

  znodeData = ZKUtil.getData(currentLeader.getWatcher(), LEADER_ZNODE);
  assertNotNull("Leader znode should contain leader index", znodeData);
  assertTrue("Leader znode should not be empty", znodeData.length > 0);
  storedIndex = Bytes.toInt(znodeData);
  LOG.debug("Stored leader index in ZK is "+storedIndex);
  assertEquals("Leader znode should match leader index",
      currentLeader.getIndex(), storedIndex);

  // force another transition by stopping the current
  currentLeader.stop("Stopping for test");
  assertFalse(currentLeader.isMaster());

  // check for new leader
  currentLeader = getCurrentLeader();
  // one leader should have been found
  assertNotNull("New leader should exist after stop", currentLeader);
  LOG.debug("New leader index is "+currentLeader.getIndex());

  znodeData = ZKUtil.getData(currentLeader.getWatcher(), LEADER_ZNODE);
  assertNotNull("Leader znode should contain leader index", znodeData);
  assertTrue("Leader znode should not be empty", znodeData.length > 0);
  storedIndex = Bytes.toInt(znodeData);
  LOG.debug("Stored leader index in ZK is "+storedIndex);
  assertEquals("Leader znode should match leader index",
      currentLeader.getIndex(), storedIndex);

  // with a second stop we can guarantee that a previous leader has resumed leading
  currentLeader.stop("Stopping for test");
  assertFalse(currentLeader.isMaster());

  // check for new
  currentLeader = getCurrentLeader();
  assertNotNull("New leader should exist", currentLeader);
}
 
Example 15
Project: ditb   File: RawInteger.java   View Source Code Vote up 4 votes
@Override
public Integer decode(PositionedByteRange src) {
  int val = Bytes.toInt(src.getBytes(), src.getOffset() + src.getPosition());
  skip(src);
  return val;
}
 
Example 16
Project: ditb   File: MDIndex.java   View Source Code Vote up 4 votes
/**
 * finds buckets which intersect with the query region.
 *
 * @param ranges
 * @return
 * @throws IOException
 */
public Iterable<MDBucket> findBucketsInRange(MDRange[] ranges, int cacheSize) throws IOException {
  int[] mins = new int[ranges.length];
  int[] maxs = new int[ranges.length];
  for (int i = 0; i < ranges.length; i++) {
    mins[i] = ranges[i].min;
    maxs[i] = ranges[i].max;
  }
  byte[] probeKey = MDUtils.bitwiseZip(mins, dimensions);
  Result bucketEntry = getRowOrBefore(bucketTable, probeKey, MDHBaseAdmin.BUCKET_FAMILY);
  byte[] startKey = bucketEntry.getRow();
  //    byte[] stopKey = Bytes.incrementBytes(MDUtils.bitwiseZip(maxs), 1L);
  byte[] stopKey = MDUtils.increment(MDUtils.bitwiseZip(maxs, dimensions));
  Scan scan = new Scan(startKey, stopKey);
  scan.addFamily(MDHBaseAdmin.BUCKET_FAMILY);
  scan.setCaching(cacheSize);
  ResultScanner scanner = bucketTable.getScanner(scan);
  List<MDBucket> hitBuckets = new LinkedList<>();
  long startTime = System.currentTimeMillis();
  int counter = 0;
  for (Result result : scanner) {
    ++counter;
    byte[] row = result.getRow();
    int pl = Bytes.toInt(
        result.getValue(MDHBaseAdmin.BUCKET_FAMILY, MDHBaseAdmin.BUCKET_PREFIX_LEN_QUALIFIER));
    MDRange[] rs = toRanges(row, pl);
    boolean intersect = true;
    for (int i = 0; i < rs.length; i++) {
      if (!ranges[i].intersect(rs[i])) {
        intersect = false;
        break;
      }
    }
    if (intersect) {
      hitBuckets.add(createBucket(rs));
    }
  }
  lastScanTotalNumber = counter;
  lastScanTotalTime = System.currentTimeMillis() - startTime;
  scanner.close();
  return hitBuckets;
}
 
Example 17
Project: ditb   File: KeyValue.java   View Source Code Vote up 4 votes
/**
 * @return Value length
 */
@Override
public int getValueLength() {
  int vlength = Bytes.toInt(this.bytes, this.offset + Bytes.SIZEOF_INT);
  return vlength;
}
 
Example 18
Project: ditb   File: IndexKeyValue.java   View Source Code Vote up 4 votes
/**
 * @return Row length
 */
public int getRowLength() {
  return Bytes.toInt(this.bytes, this.offset + Bytes.SIZEOF_INT);
}
 
Example 19
Project: ditb   File: IndexKeyValue.java   View Source Code Vote up 4 votes
/**
 * @return Qualifier length
 */
public int getQualifierLength() {
  return Bytes.toInt(bytes, offset + QUALIFIER_OFFSET);
}
 
Example 20
Project: ditb   File: IndexKeyValue.java   View Source Code Vote up 2 votes
/**
 * Determines the total length of the KeyValue stored in the specified byte array and offset.
 * Includes all headers.
 * @param bytes byte array
 * @param offset offset to start of the KeyValue
 * @return length of entire KeyValue, in bytes
 */
private static int getLength(byte[] bytes, int offset) {
  return KEYVALUE_INFRASTRUCTURE_SIZE + Bytes.toInt(bytes, offset)
      + Bytes.toInt(bytes, offset + Bytes.SIZEOF_INT);
}