Entry { public static Writer newEntry( String logName, int initialBufferSize, boolean envelopeBeforeTransmit, CompressionCodec.Type codec, StatsLogger statsLogger) { return new EnvelopedEntryWriter( logName, initialBufferSize, envelopeBeforeTransmit, codec, statsLogger); } private  Entry(long logSegmentSequenceNumber,
                  long entryId,
                  long startSequenceId,
                  boolean envelopedEntry,
                  boolean deserializeRecordSet,
                  byte[] data,
                  int offset,
                  int length,
                  Optional<Long> txidToSkipTo,
                  Optional<DLSN> dlsnToSkipTo); static Writer newEntry(
            String logName,
            int initialBufferSize,
            boolean envelopeBeforeTransmit,
            CompressionCodec.Type codec,
            StatsLogger statsLogger); static Builder newBuilder(); byte[] getRawData(); Reader reader();  }
@Test(timeout = 20000) public void testWriteTooLongRecord() throws Exception { Writer writer = Entry.newEntry( "test-write-too-long-record", 1024, false, CompressionCodec.Type.NONE, NullStatsLogger.INSTANCE); assertEquals("zero bytes", 0, writer.getNumBytes()); assertEquals("zero records", 0, writer.getNumRecords()); LogRecord largeRecord = new LogRecord(1L, new byte[MAX_LOGRECORD_SIZE + 1]); try { writer.writeRecord(largeRecord, new Promise<DLSN>()); Assert.fail("Should fail on writing large record"); } catch (LogRecordTooLongException lrtle) { } assertEquals("zero bytes", 0, writer.getNumBytes()); assertEquals("zero records", 0, writer.getNumRecords()); Buffer buffer = writer.getBuffer(); Assert.assertEquals("zero bytes", 0, buffer.size()); }
@Test(timeout = 20000) public void testWriteRecordSet() throws Exception { Writer writer = Entry.newEntry( "test-write-recordset", 1024, true, CompressionCodec.Type.NONE, NullStatsLogger.INSTANCE); assertEquals("zero bytes", 0, writer.getNumBytes()); assertEquals("zero records", 0, writer.getNumRecords()); List<Future<DLSN>> writePromiseList = Lists.newArrayList(); for (int i = 0; i < 5; i++) { LogRecord record = new LogRecord(i, ("record-" + i).getBytes(UTF_8)); record.setPositionWithinLogSegment(i); Promise<DLSN> writePromise = new Promise<DLSN>(); writer.writeRecord(record, writePromise); writePromiseList.add(writePromise); assertEquals((i + 1) + " records", (i + 1), writer.getNumRecords()); } final LogRecordSet.Writer recordSetWriter = LogRecordSet.newWriter(1024, CompressionCodec.Type.NONE); List<Future<DLSN>> recordSetPromiseList = Lists.newArrayList(); for (int i = 0; i < 5; i++) { ByteBuffer record = ByteBuffer.wrap(("record-" + (i + 5)).getBytes(UTF_8)); Promise<DLSN> writePromise = new Promise<DLSN>(); recordSetWriter.writeRecord(record, writePromise); recordSetPromiseList.add(writePromise); assertEquals((i + 1) + " records", (i + 1), recordSetWriter.getNumRecords()); } final ByteBuffer recordSetBuffer = recordSetWriter.getBuffer(); byte[] data = new byte[recordSetBuffer.remaining()]; recordSetBuffer.get(data); LogRecord setRecord = new LogRecord(5L, data); setRecord.setPositionWithinLogSegment(5); setRecord.setRecordSet(); Promise<DLSN> writePromise = new Promise<DLSN>(); writePromise.addEventListener(new FutureEventListener<DLSN>() { @Override public void onSuccess(DLSN dlsn) { recordSetWriter.completeTransmit( dlsn.getLogSegmentSequenceNo(), dlsn.getEntryId(), dlsn.getSlotId()); } @Override public void onFailure(Throwable cause) { recordSetWriter.abortTransmit(cause); } }); writer.writeRecord(setRecord, writePromise); writePromiseList.add(writePromise); for (int i = 0; i < 5; i++) { LogRecord record = new LogRecord(i + 10, ("record-" + (i + 10)).getBytes(UTF_8)); record.setPositionWithinLogSegment(i + 10); writePromise = new Promise<DLSN>(); writer.writeRecord(record, writePromise); writePromiseList.add(writePromise); assertEquals((i + 11) + " records", (i + 11), writer.getNumRecords()); } Buffer buffer = writer.getBuffer(); writer.completeTransmit(1L, 1L); List<DLSN> writeResults = Await.result(Future.collect(writePromiseList)); for (int i = 0; i < 5; i++) { Assert.assertEquals(new DLSN(1L, 1L, i), writeResults.get(i)); } Assert.assertEquals(new DLSN(1L, 1L, 5), writeResults.get(5)); for (int i = 0; i < 5; i++) { Assert.assertEquals(new DLSN(1L, 1L, (10 + i)), writeResults.get(6 + i)); } List<DLSN> recordSetWriteResults = Await.result(Future.collect(recordSetPromiseList)); for (int i = 0; i < 5; i++) { Assert.assertEquals(new DLSN(1L, 1L, (5 + i)), recordSetWriteResults.get(i)); } verifyReadResult(buffer, 1L, 1L, 1L, true, new DLSN(1L, 1L, 2L), 3, 5, 5, new DLSN(1L, 1L, 2L), 2L); verifyReadResult(buffer, 1L, 1L, 1L, true, new DLSN(1L, 1L, 7L), 0, 3, 5, new DLSN(1L, 1L, 7L), 7L); verifyReadResult(buffer, 1L, 1L, 1L, true, new DLSN(1L, 1L, 12L), 0, 0, 3, new DLSN(1L, 1L, 12L), 12L); verifyReadResult(buffer, 1L, 1L, 1L, false, new DLSN(1L, 1L, 2L), 3, 5, 5, new DLSN(1L, 1L, 2L), 2L); verifyReadResult(buffer, 1L, 1L, 1L, false, new DLSN(1L, 1L, 7L), 0, 3, 5, new DLSN(1L, 1L, 7L), 7L); verifyReadResult(buffer, 1L, 1L, 1L, false, new DLSN(1L, 1L, 12L), 0, 0, 3, new DLSN(1L, 1L, 12L), 12L); }