if (ByteBufferUtil.compare(col.name(), CassandraUtils.documentMetaFieldBytes.array()) == 0)
private void testWriteLByteArray(int bufferSize) throws IOException { ByteBufferOutputStream sink = new ByteBufferOutputStream(); byte[] written = new byte[bufferSize]; new Random().nextBytes(written); sink.write(written); assertEquals(0, ByteBufferUtil.compare(sink.getByteBuffer(), written)); }
@Test public void testWriteLargeByteBuffer() throws IOException { ByteBufferOutputStream sink = new ByteBufferOutputStream(); byte[] written = new byte[32 * 1000]; new Random().nextBytes(written); sink.write(ByteBuffer.wrap(written)); assertEquals(0, ByteBufferUtil.compare(sink.getByteBuffer(), written)); }
@Test public void testWriteByteArraySeveralSteps() throws IOException { ByteBufferOutputStream sink = new ByteBufferOutputStream(); byte[] written = new byte[32 * ByteBufferOutputStream.INITIAL_BUFFER_SIZE]; new Random().nextBytes(written); for(int i = 0; i < 32; i++) { sink.write(written, i * ByteBufferOutputStream.INITIAL_BUFFER_SIZE, ByteBufferOutputStream.INITIAL_BUFFER_SIZE); } assertEquals(0, ByteBufferUtil.compare(sink.getByteBuffer(), written)); }
/** * Get specific ColumnDef in column family meta data by column name * @param columnFamily - CfDef record * @param columnName - column name represented as byte[] * @return ColumnDef - found column definition */ private ColumnDef getColumnDefByName(CfDef columnFamily, ByteBuffer columnName) { for (ColumnDef columnDef : columnFamily.getColumn_metadata()) { byte[] currName = columnDef.getName(); if (ByteBufferUtil.compare(currName, columnName) == 0) { return columnDef; } } return null; }
/** * Converts column value into byte[] according to validation class * @param columnName - column name to which value belongs * @param columnFamilyName - column family name * @param columnValue - actual column value * @return value in byte array representation */ private ByteBuffer columnValueAsBytes(ByteBuffer columnName, String columnFamilyName, String columnValue) { CfDef columnFamilyDef = getCfDef(columnFamilyName); AbstractType<?> defaultValidator = getFormatType(columnFamilyDef.default_validation_class); for (ColumnDef columnDefinition : columnFamilyDef.getColumn_metadata()) { byte[] currentColumnName = columnDefinition.getName(); if (ByteBufferUtil.compare(currentColumnName, columnName) == 0) { try { String validationClass = columnDefinition.getValidation_class(); return getBytesAccordingToType(columnValue, getFormatType(validationClass)); } catch (Exception e) { throw new RuntimeException(e); } } } return defaultValidator.fromString(columnValue); }