DLUtils { public static int findLogSegmentNotLessThanTxnId(List<LogSegmentMetadata> segments, long transactionId) { int found = -1; for (int i = segments.size() - 1; i >= 0; i--) { LogSegmentMetadata segment = segments.get(i); if (segment.getFirstTxId() <= transactionId) { found = i; break; } } if (found <= -1) { return -1; } if (found == 0 && segments.get(0).getFirstTxId() == transactionId) { return 0; } LogSegmentMetadata foundSegment = segments.get(found); if (foundSegment.getFirstTxId() == transactionId) { for (int i = found - 1; i >= 0; i--) { LogSegmentMetadata segment = segments.get(i); if (segment.isInProgress()) { break; } if (segment.getLastTxId() < transactionId) { break; } found = i; } return found; } else { if (foundSegment.isInProgress() || found == segments.size() - 1) { return found; } if (foundSegment.getLastTxId() >= transactionId) { return found; } return found + 1; } }  static String getZKServersFromDLUri(URI uri); static int findLogSegmentNotLessThanTxnId(List<LogSegmentMetadata> segments,
                                                     long transactionId); static Long nextLogSegmentSequenceNumber(List<LogSegmentMetadata> segmentListDesc); static long computeStartSequenceId(List<LogSegmentMetadata> logSegmentDescList,
                                              LogSegmentMetadata segment); static long deserializeLogSegmentSequenceNumber(byte[] data); static byte[] serializeLogSegmentSequenceNumber(long logSegmentSeqNo); static long deserializeTransactionId(byte[] data); static byte[] serializeTransactionId(long transactionId); static byte[] ledgerId2Bytes(long ledgerId); static long bytes2LedgerId(byte[] data);  }
@Test(timeout = 60000) public void testFindLogSegmentNotLessThanTxnId() throws Exception { long txnId = 999L; List<LogSegmentMetadata> emptyList = Lists.newArrayList(); assertEquals(-1, DLUtils.findLogSegmentNotLessThanTxnId(emptyList, txnId)); List<LogSegmentMetadata> list1 = Lists.newArrayList( completedLogSegment(1L, 1000L, 2000L)); assertEquals(-1, DLUtils.findLogSegmentNotLessThanTxnId(list1, txnId)); List<LogSegmentMetadata> list2 = Lists.newArrayList( inprogressLogSegment(1L, 1000L)); assertEquals(-1, DLUtils.findLogSegmentNotLessThanTxnId(list2, txnId)); List<LogSegmentMetadata> list3 = Lists.newArrayList( completedLogSegment(1L, 0L, 99L), completedLogSegment(2L, 1000L, 2000L) ); assertEquals(1, DLUtils.findLogSegmentNotLessThanTxnId(list3, txnId)); List<LogSegmentMetadata> list4 = Lists.newArrayList( completedLogSegment(1L, 0L, 990L), completedLogSegment(2L, 1000L, 2000L) ); assertEquals(1, DLUtils.findLogSegmentNotLessThanTxnId(list4, txnId)); List<LogSegmentMetadata> list5 = Lists.newArrayList( inprogressLogSegment(1L, 0L), inprogressLogSegment(2L, 1000L) ); assertEquals(0, DLUtils.findLogSegmentNotLessThanTxnId(list5, txnId)); List<LogSegmentMetadata> list6_0 = Lists.newArrayList( completedLogSegment(1L, 100L, 200L)); assertEquals(0, DLUtils.findLogSegmentNotLessThanTxnId(list6_0, txnId)); List<LogSegmentMetadata> list6_1 = Lists.newArrayList( completedLogSegment(1L, 100L, 199L), completedLogSegment(2L, 200L, 299L)); assertEquals(1, DLUtils.findLogSegmentNotLessThanTxnId(list6_1, txnId)); List<LogSegmentMetadata> list7 = Lists.newArrayList( inprogressLogSegment(1L, 100L)); assertEquals(0, DLUtils.findLogSegmentNotLessThanTxnId(list7, txnId)); List<LogSegmentMetadata> list8 = Lists.newArrayList( completedLogSegment(1L, 999L, 2000L)); assertEquals(0, DLUtils.findLogSegmentNotLessThanTxnId(list8, txnId)); List<LogSegmentMetadata> list9 = Lists.newArrayList( inprogressLogSegment(1L, 999L)); assertEquals(0, DLUtils.findLogSegmentNotLessThanTxnId(list9, txnId)); List<LogSegmentMetadata> list10 = Lists.newArrayList( completedLogSegment(1L, 0L, 999L), completedLogSegment(2L, 999L, 2000L)); assertEquals(0, DLUtils.findLogSegmentNotLessThanTxnId(list10, txnId)); List<LogSegmentMetadata> list11 = Lists.newArrayList( completedLogSegment(1L, 0L, 99L), completedLogSegment(2L, 999L, 2000L)); assertEquals(1, DLUtils.findLogSegmentNotLessThanTxnId(list11, txnId)); List<LogSegmentMetadata> list12 = Lists.newArrayList( inprogressLogSegment(1L, 0L), inprogressLogSegment(2L, 999L)); assertEquals(1, DLUtils.findLogSegmentNotLessThanTxnId(list12, txnId)); }