List of usage examples for org.apache.cassandra.utils ByteBufferUtil compare
@Inline public static int compare(ByteBuffer o1, byte[] o2)
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testReadBytes() throws IOException { final SequentialWriter w = createTempFile("brafReadBytes"); byte[] data = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE + 10]; for (int i = 0; i < data.length; i++) { data[i] = 'c'; }/*from ww w . j av a 2s.c o m*/ w.write(data); w.sync(); final RandomAccessReader r = RandomAccessReader.open(w, fs); ByteBuffer content = r.readBytes((int) r.length()); // after reading whole file we should be at EOF assertEquals(0, ByteBufferUtil.compare(content, data)); assert r.bytesRemaining() == 0 && r.isEOF(); r.seek(0); content = r.readBytes(10); // reading first 10 bytes assertEquals(ByteBufferUtil.compare(content, "cccccccccc".getBytes()), 0); assertEquals(r.bytesRemaining(), r.length() - content.limit()); // trying to read more than file has right now expectEOF(new Callable<Object>() { public Object call() throws IOException { return r.readBytes((int) r.length() + 10); } }); w.close(); r.close(); }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testClose() throws IOException { final SequentialWriter w = createTempFile("brafClose"); byte[] data = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE + 20); w.write(data);/*from w ww . ja va 2 s .co m*/ w.close(); // will flush final RandomAccessReader r = RandomAccessReader.open(new Path(new File(w.getPath()).getPath()), fs); r.close(); // closing to test read after close expectException(new Callable<Object>() { public Object call() { return r.read(); } }, AssertionError.class); expectException(new Callable<Object>() { public Object call() throws IOException { w.write(generateByteArray(1)); return null; } }, ClosedChannelException.class); RandomAccessReader copy = RandomAccessReader.open(new Path(new File(r.getPath()).getPath()), fs); ByteBuffer contents = copy.readBytes((int) copy.length()); assertEquals(contents.limit(), data.length); assertEquals(ByteBufferUtil.compare(contents, data), 0); }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testReadOnly() throws IOException { SequentialWriter file = createTempFile("brafReadOnlyTest"); byte[] data = new byte[20]; for (int i = 0; i < data.length; i++) data[i] = 'c'; file.write(data);// ww w . j ava 2 s. c om file.sync(); // flushing file to the disk // read-only copy of the file, with fixed file length final RandomAccessReader copy = RandomAccessReader.open(new Path(file.getPath()), fs); copy.seek(copy.length()); assertTrue(copy.bytesRemaining() == 0 && copy.isEOF()); // can't seek past the end of the file for read-only files expectException(new Callable<Object>() { public Object call() { copy.seek(copy.length() + 1); return null; } }, IllegalArgumentException.class); // Any write() call should fail expectException(new Callable<Object>() { public Object call() throws IOException { copy.write(1); return null; } }, UnsupportedOperationException.class); expectException(new Callable<Object>() { public Object call() throws IOException { copy.write(new byte[1]); return null; } }, UnsupportedOperationException.class); expectException(new Callable<Object>() { public Object call() throws IOException { copy.write(new byte[3], 0, 2); return null; } }, UnsupportedOperationException.class); copy.seek(0); copy.skipBytes(5); assertEquals(copy.bytesRemaining(), 15); assertEquals(copy.getFilePointer(), 5); assertTrue(!copy.isEOF()); copy.seek(0); ByteBuffer contents = copy.readBytes((int) copy.length()); assertEquals(contents.limit(), copy.length()); assertTrue(ByteBufferUtil.compare(contents, data) == 0); copy.seek(0); int count = 0; while (!copy.isEOF()) { assertEquals((byte) copy.read(), 'c'); count++; } assertEquals(count, copy.length()); copy.seek(0); byte[] content = new byte[10]; copy.read(content); assertEquals(new String(content), "cccccccccc"); file.close(); copy.close(); }
From source file:com.perpetumobile.bit.orm.cassandra.CliMain.java
License:Apache License
/** * 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 *///from w w w . j a va 2s . c o m 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); }
From source file:com.perpetumobile.bit.orm.cassandra.CliMain.java
License:Apache License
/** * Get specific ColumnDef in column family meta data by column name * @param columnFamily - CfDef record// w w w . j a v a 2 s. co m * @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; }