@Override public String toString() { return Objects.toStringHelper(this.getClass()) .add("Prefix", String.format("%02X", PREFIX)) .add("TableId", tableId) .add("UUID", uuid == null ? "" : Util.generateHexString(Util.UUIDToBytes(uuid))) .toString(); }
public void closeTable() { tableName = null; store = null; Util.closeQuietly(table); table = null; }
public static QueryKey deserialize(byte[] serializedIndexKey) { checkNotNull(serializedIndexKey); return new QueryKey(Util.deserializeAvroObject(serializedIndexKey, reader)); }
/** * Set UUID to a new random UUID */ public void setRandomUUID() { row.setUuid(new UUIDContainer(Util.UUIDToBytes(UUID.randomUUID()))); }
@Test public void testUUIDBytes() throws Exception { for (UUID uuid : Iterables.toIterable(new UUIDGenerator())) { Assert.assertEquals(uuid, Util.bytesToUUID(Util.UUIDToBytes(uuid))); } } }
/** * Returns the {@link UUID} of this Row. * * @return UUID of this Row. */ public UUID getUUID() { return Util.bytesToUUID(row.getUuid().bytes()); }
private List<String> recordValueStrings() { final List<String> strings = Lists.newArrayList(); for (final byte[] bytes : records) { strings.add((bytes == null) ? "null" : Util.generateHexString(bytes)); } return strings; }
/** * Serialize the index schema out to a byte array * * @return Serialized form of the index schema */ public byte[] serialize() { return Util.serializeAvroObject(avroIndexSchema, writer); }
/** * Deserialize the provided serialized data into an instance of the specified class type * * @param serializedData a buffer containing the serialized data * @param reader the datum reader for the class * @return A new instance of the specified class representing the deserialized data */ public static <T> T deserializeAvroObject(byte[] serializedData, DatumReader<T> reader) { checkNotNull(serializedData); checkNotNull(reader); Decoder binaryDecoder = DecoderFactory.get().binaryDecoder(serializedData, null); try { return reader.read(null, binaryDecoder); } catch (IOException e) { throw deserializationError(serializedData, e, null); } }
/** * Serialize an object to a byte array * * @param obj The object to serialize * @param writer The datum writer for the class * @return Serialized row */ public static <T> byte[] serializeAvroObject(T obj, DatumWriter<T> writer) { ByteArrayOutputStream out = new ByteArrayOutputStream(); Encoder encoder = EncoderFactory.get().binaryEncoder(out, null); try { writer.write(obj, encoder); encoder.flush(); } catch (IOException e) { throw serializationError(obj, e); } return out.toByteArray(); }
public void updateRow(byte[] oldRowBytes, byte[] rowBytes) { checkTableOpen(); checkNotNull(rowBytes); Row updatedRow = Row.deserialize(rowBytes); TableSchema schema = store.getSchema(tableName); Row oldRow = Row.deserialize(oldRowBytes); oldRow.setUUID(updatedRow.getUUID()); ImmutableList<IndexSchema> changedIndices = Util.getChangedIndices(schema.getIndices(), oldRow.getRecords(), updatedRow.getRecords()); table.updateRow(oldRow, updatedRow, changedIndices); if (schema.hasUniqueIndices()) { table.flush(); } }
@Override public byte[] encode() { final byte[] prefixBytes = {prefix}; final List<byte[]> encodedParts = Lists.newArrayList(); encodedParts.add(prefixBytes); encodedParts.add(VarEncoder.encodeULong(tableId)); encodedParts.add(VarEncoder.encodeULong(indexId)); if (records != null) { for (final byte[] record : records) { if (record == null) { encodedParts.add(nullBytes); } else { encodedParts.add(notNullBytes); encodedParts.add(record); } } if (uuid != null) { encodedParts.add(Util.UUIDToBytes(uuid)); } } return VarEncoder.appendByteArrays(encodedParts); }
public byte[] getRow(byte[] uuid) { checkTableOpen(); checkNotNull(uuid, "Get row cannot have a null UUID."); return table.getRow(Util.bytesToUUID(uuid)).serialize(); }
private static <T> RuntimeException deserializationError(byte[] serializedData, IOException e, Class<T> clazz) { String clazzMessage = clazz == null ? "" : "of class type " + clazz.getName(); String format = String.format("Deserialization failed for data (%s) " + clazzMessage, Util.generateHexString(serializedData)); logger.error(format, e); return new RuntimeException(format, e); }
/** * Serialize this {@link Row} instance to a byte array. * * @return Serialized row */ public byte[] serialize() { return Util.serializeAvroObject(row, writer); }
@Override public int compareTo(RowKey o) { int typeCompare = getPrefix() - o.getPrefix(); if (typeCompare != 0) { return typeCompare; } DataRowKey row2 = (DataRowKey) o; return ComparisonChain.start() .compare(getTableId(), row2.getTableId()) .compare(Util.UUIDToBytes(getUuid()), Util.UUIDToBytes(row2.getUuid()), new Bytes.ByteArrayComparator()) .result(); } }
@Override public String toString() { return Objects.toStringHelper(this.getClass()) .add("Prefix", String.format("%02X", prefix)) .add("TableId", tableId) .add("IndexId", indexId) .add("Records", records == null ? "" : recordValueStrings()) .add("UUID", uuid == null ? "" : Util.generateHexString(Util.UUIDToBytes(uuid))) .toString(); }
@Override public void close() { Util.closeQuietly(hTable); }
/** * Deserialize the provided serialized row buffer to a new {@link Row} instance * * @param serializedRow byte buffer containing serialized Row * @return new Row instance from serializedRow */ public static Row deserialize(byte[] serializedRow) { checkNotNull(serializedRow); checkArgument(serializedRow.length > 0); SchemaVersionUtils.processSchemaVersion(serializedRow[0], RowSchemaInfo.VER_CURRENT); return new Row(Util.deserializeAvroObject(serializedRow, reader)); }
/** * Serialize the table schema out to a byte array * * @return Serialized form of the table schema */ public byte[] serialize() { return Util.serializeAvroObject(avroTableSchema, writer); }