List of usage examples for org.apache.cassandra.io.util SequentialWriter write
@Override public void write(byte[] b) throws IOException
From source file:com.fullcontact.cassandra.io.compress.CompressedRandomAccessReaderTest.java
License:Apache License
private void testResetAndTruncate(File f, boolean compressed, int junkSize) throws IOException { final String filename = f.getAbsolutePath(); try {/*from w w w . j a v a2 s .c o m*/ SSTableMetadata.Collector sstableMetadataCollector = SSTableMetadata.createCollector() .replayPosition(null); SequentialWriter writer = compressed ? new CompressedSequentialWriter(f, filename + ".metadata", false, new CompressionParameters(SnappyCompressor.instance), sstableMetadataCollector) : new SequentialWriter(f, CompressionParameters.DEFAULT_CHUNK_LENGTH, false); writer.write("The quick ".getBytes()); FileMark mark = writer.mark(); writer.write("blue fox jumps over the lazy dog".getBytes()); // write enough to be sure to change chunk for (int i = 0; i < junkSize; ++i) { writer.write((byte) 1); } writer.resetAndTruncate(mark); writer.write("brown fox jumps over the lazy dog".getBytes()); writer.close(); assert f.exists(); RandomAccessReader reader = compressed ? CompressedRandomAccessReader.open(new Path(filename), new CompressionMetadata(filename + ".metadata", f.length(), fs), false, fs) : RandomAccessReader.open(new Path(f.getPath()), fs); String expected = "The quick brown fox jumps over the lazy dog"; assertEquals(expected.length(), reader.length()); byte[] b = new byte[expected.length()]; reader.readFully(b); assert new String(b).equals(expected) : "Expecting '" + expected + "', got '" + new String(b) + "'"; } finally { // cleanup if (f.exists()) f.delete(); File metadata = new File(filename + ".metadata"); if (compressed && metadata.exists()) metadata.delete(); } }
From source file:com.fullcontact.cassandra.io.compress.CompressedRandomAccessReaderTest.java
License:Apache License
@Test public void testDataCorruptionDetection() throws IOException { String CONTENT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae."; File file = new File("testDataCorruptionDetection"); file.deleteOnExit();//from ww w . j a v a 2s . c o m File metadata = new File(file.getPath() + ".meta"); metadata.deleteOnExit(); SSTableMetadata.Collector sstableMetadataCollector = SSTableMetadata.createCollector().replayPosition(null); SequentialWriter writer = new CompressedSequentialWriter(file, metadata.getPath(), false, new CompressionParameters(SnappyCompressor.instance), sstableMetadataCollector); writer.write(CONTENT.getBytes()); writer.close(); // open compression metadata and get chunk information CompressionMetadata meta = new CompressionMetadata(metadata.getPath(), file.length(), fs); CompressionMetadata.Chunk chunk = meta.chunkFor(0); RandomAccessReader reader = CompressedRandomAccessReader.open(new Path(file.getPath()), meta, false, fs); // read and verify compressed data assertEquals(CONTENT, reader.readLine()); // close reader reader.close(); Random random = new Random(); RandomAccessFile checksumModifier = null; try { checksumModifier = new RandomAccessFile(file, "rw"); byte[] checksum = new byte[4]; // seek to the end of the compressed chunk checksumModifier.seek(chunk.length); // read checksum bytes checksumModifier.read(checksum); // seek back to the chunk end checksumModifier.seek(chunk.length); // lets modify one byte of the checksum on each iteration for (int i = 0; i < checksum.length; i++) { checksumModifier.write(random.nextInt()); checksumModifier.getFD().sync(); // making sure that change was synced with disk final RandomAccessReader r = CompressedRandomAccessReader.open(new Path(file.getPath()), meta, false, fs); Throwable exception = null; try { r.readLine(); } catch (Throwable t) { exception = t; } assertNotNull(exception); assertEquals(exception.getClass(), CorruptSSTableException.class); assertEquals(exception.getCause().getClass(), CorruptBlockException.class); r.close(); } // lets write original checksum and check if we can read data updateChecksum(checksumModifier, chunk.length, checksum); reader = CompressedRandomAccessReader.open(new Path(file.getPath()), meta, false, fs); // read and verify compressed data assertEquals(CONTENT, reader.readLine()); // close reader reader.close(); } finally { if (checksumModifier != null) checksumModifier.close(); } }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testReadAndWrite() throws Exception { SequentialWriter w = createTempFile("braf"); // writting string of data to the file byte[] data = "Hello".getBytes(); w.write(data); assertEquals(data.length, w.length()); assertEquals(data.length, w.getFilePointer()); w.sync();// w w w . j av a 2s . com // reading small amount of data from file, this is handled by initial buffer RandomAccessReader r = RandomAccessReader.open(w, fs); byte[] buffer = new byte[data.length]; assertEquals(data.length, r.read(buffer)); assertTrue(Arrays.equals(buffer, data)); // we read exactly what we wrote assertEquals(r.read(), -1); // nothing more to read EOF assert r.bytesRemaining() == 0 && r.isEOF(); r.close(); // writing buffer bigger than page size, which will trigger reBuffer() byte[] bigData = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE + 10]; for (int i = 0; i < bigData.length; i++) bigData[i] = 'd'; long initialPosition = w.getFilePointer(); w.write(bigData); // writing data assertEquals(w.getFilePointer(), initialPosition + bigData.length); assertEquals(w.length(), initialPosition + bigData.length); // file size should equals to last position w.sync(); r = RandomAccessReader.open(w, fs); // re-opening file in read-only mode // reading written buffer r.seek(initialPosition); // back to initial (before write) position data = new byte[bigData.length]; long sizeRead = 0; for (int i = 0; i < data.length; i++) { data[i] = (byte) r.read(); sizeRead++; } assertEquals(sizeRead, data.length); // read exactly data.length bytes assertEquals(r.getFilePointer(), initialPosition + data.length); assertEquals(r.length(), initialPosition + bigData.length); assertTrue(Arrays.equals(bigData, data)); assertTrue(r.bytesRemaining() == 0 && r.isEOF()); // we are at the of the file // test readBytes(int) method r.seek(0); ByteBuffer fileContent = r.readBytes((int) w.length()); assertEquals(fileContent.limit(), w.length()); assert ByteBufferUtil.string(fileContent).equals("Hello" + new String(bigData)); // read the same buffer but using readFully(int) data = new byte[bigData.length]; r.seek(initialPosition); r.readFully(data); assert r.bytesRemaining() == 0 && r.isEOF(); // we should be at EOF assertTrue(Arrays.equals(bigData, data)); // try to read past mark (all methods should return -1) data = new byte[10]; assertEquals(r.read(), -1); assertEquals(r.read(data), -1); assertEquals(r.read(data, 0, data.length), -1); // test read(byte[], int, int) r.seek(0); data = new byte[20]; assertEquals(15, r.read(data, 0, 15)); assertTrue(new String(data).contains("Hellodddddddddd")); for (int i = 16; i < data.length; i++) { assert data[i] == 0; } w.close(); r.close(); }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testReadAndWriteOnCapacity() throws IOException { File tmpFile = File.createTempFile("readtest", "bin"); SequentialWriter w = SequentialWriter.open(tmpFile); // Fully write the file and sync.. byte[] in = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE); w.write(in); RandomAccessReader r = RandomAccessReader.open(w, fs); // Read it into a same size array. byte[] out = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE]; r.read(out);//from www .j av a 2 s .c om // Cannot read any more. int negone = r.read(); assert negone == -1 : "We read past the end of the file, should have gotten EOF -1. Instead, " + negone; r.close(); w.close(); }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testLength() throws IOException { File tmpFile = File.createTempFile("lengthtest", "bin"); SequentialWriter w = SequentialWriter.open(tmpFile); assertEquals(0, w.length());/*from w w w.j a v a 2 s. c o m*/ // write a chunk smaller then our buffer, so will not be flushed // to disk byte[] lessThenBuffer = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE / 2); w.write(lessThenBuffer); assertEquals(lessThenBuffer.length, w.length()); // sync the data and check length w.sync(); assertEquals(lessThenBuffer.length, w.length()); // write more then the buffer can hold and check length byte[] biggerThenBuffer = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE * 2); w.write(biggerThenBuffer); assertEquals(biggerThenBuffer.length + lessThenBuffer.length, w.length()); w.close(); // will use cachedlength RandomAccessReader r = RandomAccessReader.open(new Path(tmpFile.getPath()), fs); assertEquals(lessThenBuffer.length + biggerThenBuffer.length, r.length()); r.close(); }
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'; }// ww w. j a va 2 s . c om 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 testSeek() throws Exception { SequentialWriter w = createTempFile("brafSeek"); byte[] data = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE + 20); w.write(data); w.close();//from www . j a v a 2 s . c om final RandomAccessReader file = RandomAccessReader.open(w, fs); file.seek(0); assertEquals(file.getFilePointer(), 0); assertEquals(file.bytesRemaining(), file.length()); file.seek(20); assertEquals(file.getFilePointer(), 20); assertEquals(file.bytesRemaining(), file.length() - 20); // trying to seek past the end of the file should produce EOFException expectException(new Callable<Object>() { public Object call() { file.seek(file.length() + 30); return null; } }, IllegalArgumentException.class); expectException(new Callable<Object>() { public Object call() throws IOException { file.seek(-1); return null; } }, IllegalArgumentException.class); // throws IllegalArgumentException file.close(); }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testSkipBytes() throws IOException { SequentialWriter w = createTempFile("brafSkipBytes"); w.write(generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE * 2)); w.close();/*from w w w . j av a 2 s. c om*/ RandomAccessReader file = RandomAccessReader.open(w, fs); file.seek(0); // back to the beginning of the file assertEquals(file.skipBytes(10), 10); assertEquals(file.bytesRemaining(), file.length() - 10); int initialPosition = (int) file.getFilePointer(); // can't skip more than file size assertEquals(file.skipBytes((int) file.length() + 10), file.length() - initialPosition); assertEquals(file.getFilePointer(), file.length()); assert file.bytesRemaining() == 0 && file.isEOF(); file.seek(0); // skipping negative amount should return 0 assertEquals(file.skipBytes(-1000), 0); assertEquals(file.getFilePointer(), 0); assertEquals(file.bytesRemaining(), file.length()); file.close(); }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testGetFilePointer() throws IOException { final SequentialWriter w = createTempFile("brafGetFilePointer"); assertEquals(w.getFilePointer(), 0); // initial position should be 0 w.write(generateByteArray(20)); assertEquals(w.getFilePointer(), 20); // position 20 after writing 20 bytes w.sync();/*www.j ava 2 s . com*/ RandomAccessReader r = RandomAccessReader.open(w, fs); // position should change after skip bytes r.seek(0); r.skipBytes(15); assertEquals(r.getFilePointer(), 15); r.read(); assertEquals(r.getFilePointer(), 16); r.read(new byte[4]); assertEquals(r.getFilePointer(), 20); w.close(); r.close(); }
From source file:com.fullcontact.cassandra.io.util.BufferedRandomAccessFileTest.java
License:Apache License
@Test public void testBytesRemaining() throws IOException { SequentialWriter w = createTempFile("brafBytesRemaining"); int toWrite = RandomAccessReader.DEFAULT_BUFFER_SIZE + 10; w.write(generateByteArray(toWrite)); w.sync();//from w w w. j av a 2 s .c o m RandomAccessReader r = RandomAccessReader.open(w, fs); assertEquals(r.bytesRemaining(), toWrite); for (int i = 1; i <= r.length(); i++) { r.read(); assertEquals(r.bytesRemaining(), r.length() - i); } r.seek(0); r.skipBytes(10); assertEquals(r.bytesRemaining(), r.length() - 10); w.close(); r.close(); }