/** * Returns the wrapped value as {@code GradoopId}. * * @return {@code GradoopId} value */ public GradoopId getGradoopId() { return GradoopId.fromByteArray( Arrays.copyOfRange(rawBytes, OFFSET, GradoopId.ID_SIZE + OFFSET)); }
@Override public GradoopId readTargetId(Result res) { return GradoopId.fromByteArray(res.getValue(CF_META_BYTES, COL_TARGET_BYTES)); }
@Override public GradoopId readSourceId(Result res) { return GradoopId.fromByteArray(res.getValue(CF_META_BYTES, COL_SOURCE_BYTES)); }
/** * Since we will need to do operations on individual ids we need * to reconstruct the {@link GradoopId} instances. * @param bytes serialized sequence of {@link GradoopId}s * @return a set representation */ private Set<GradoopId> readIds(byte[] bytes) { ids = new HashSet<>(); for (int i = 0; i < bytes.length / GradoopId.ID_SIZE; i++) { byte[] idBytes = new byte[GradoopId.ID_SIZE]; System.arraycopy(bytes, i * GradoopId.ID_SIZE, idBytes, 0, GradoopId.ID_SIZE); ids.add(GradoopId.fromByteArray(idBytes)); } return ids; }
/** * Returns the Id of the entry stored at the specified position * @param column the position the entry is stored at * @return ID of the entry */ public GradoopId getId(int column) { return GradoopId.fromByteArray(getRawId(column)); }
@Override public GradoopId readId(@Nonnull final Result res) { if (useSpreadingByte) { return GradoopId.fromByteArray(RowKeyDistributor.getInstance().getOriginalKey(res.getRow())); } else { return GradoopId.fromByteArray(res.getRow()); } }
/** * Returns the Id of the entry stored at the specified position * @param column the position the entry is stored at * @return ID of the entry */ public GradoopId getId(int column) { return GradoopId.fromByteArray(getRawId(column)); }
/** * Returns the ID-List stored at the specified column * @param column the entries index * @return ID-List stored at the specified column */ public List<GradoopId> getIdList(int column) { int offset = getIdListOffset(column); int listSize = Ints.fromByteArray(ArrayUtils.subarray(idListData, offset, offset + Integer.BYTES)); offset += Integer.BYTES; List<GradoopId> idList = new ArrayList<>(listSize); for (int i = 0; i < listSize; i++) { idList.add(GradoopId.fromByteArray( ArrayUtils.subarray(idListData, offset, offset + GradoopId.ID_SIZE) )); offset += GradoopId.ID_SIZE; } return idList; }
/** * Returns the ID-List stored at the specified column * @param column the entries index * @return ID-List stored at the specified column */ public List<GradoopId> getIdList(int column) { int offset = getIdListOffset(column); int listSize = Ints.fromByteArray(ArrayUtils.subarray(idListData, offset, offset + Integer.BYTES)); offset += Integer.BYTES; List<GradoopId> idList = new ArrayList<>(listSize); for (int i = 0; i < listSize; i++) { idList.add(GradoopId.fromByteArray( ArrayUtils.subarray(idListData, offset, offset + GradoopId.ID_SIZE) )); offset += GradoopId.ID_SIZE; } return idList; }
@Test public void testFromBytes() { int randomTime = ThreadLocalRandom.current().nextInt(); int randomMachineId = ThreadLocalRandom.current().nextInt(0, 16777215); short randomProcessId = (short) ThreadLocalRandom.current().nextInt(0, Short.MAX_VALUE); int randomCounter = ThreadLocalRandom.current().nextInt(0, 16777215); GradoopId expectedId = new GradoopId(randomTime, randomMachineId, randomProcessId, randomCounter); byte[] bytes = new byte[GradoopId.ID_SIZE]; ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.putInt(randomTime); byte b1; byte b2; byte b3; b3 = (byte) (randomMachineId & 0xFF); b2 = (byte) ((randomMachineId >> 8) & 0xFF); b1 = (byte) ((randomMachineId >> 16) & 0xFF); buffer.put(b1); buffer.put(b2); buffer.put(b3); buffer.putShort(randomProcessId); b3 = (byte) (randomCounter & 0xFF); b2 = (byte) ((randomCounter >> 8) & 0xFF); b1 = (byte) ((randomCounter >> 16) & 0xFF); buffer.put(b1); buffer.put(b2); buffer.put(b3); GradoopId newId = GradoopId.fromByteArray(bytes); assertEquals(expectedId, newId); }
@Test public void testGetRawBytes() { GradoopId originalId = GradoopId.get(); assertEquals(GradoopId.ID_SIZE, originalId.toByteArray().length); assertEquals( "Reconstruction failed", originalId, GradoopId.fromByteArray(originalId.toByteArray()) ); }