@Test public void testFindSegment() throws IOException, StorageException { ArrayList<Segment> segments = new ArrayList<>(); for (long segmentIndex = 0; segmentIndex < NUM_SEGMENTS; segmentIndex++) { Segment segment = mock(Segment.class); long firstTransactionId = FIRST_TRANSACTION_ID + segmentIndex * SEGMENT_SIZE_THRESHOLD; long recordIndex = 0; for (; recordIndex < SEGMENT_SIZE_THRESHOLD; recordIndex++) { long transactionId = firstTransactionId + recordIndex; if (transactionId > LAST_TRANSACTION_ID) { break; } when(segment.getRecord(transactionId)).thenReturn(new Record(transactionId, mock(ReqId.class), 0, new byte[0], 0)); } when(segment.firstTransactionId()).thenReturn(firstTransactionId); when(segment.maxTransactionId()).thenReturn(firstTransactionId + recordIndex - 1); segments.add(segment); } assertEquals(FIRST_TRANSACTION_ID, SegmentFinder.findSegment(segments, FIRST_TRANSACTION_ID).getRecord(FIRST_TRANSACTION_ID).transactionId); assertEquals(null, SegmentFinder.findSegment(segments, FIRST_TRANSACTION_ID - 1)); assertEquals(LAST_TRANSACTION_ID, SegmentFinder.findSegment(segments, LAST_TRANSACTION_ID).getRecord(LAST_TRANSACTION_ID).transactionId); assertEquals(null, SegmentFinder.findSegment(segments, LAST_TRANSACTION_ID + 1)); assertEquals(100, SegmentFinder.findSegment(segments, 100).getRecord(100).transactionId); assertEquals(101, SegmentFinder.findSegment(segments, 100).getRecord(101).transactionId); assertEquals(null, SegmentFinder.findSegment(segments, 100).getRecord(99)); }
static Segment findSegment(ArrayList<Segment> segments, long transactionId) { synchronized (segments) { int n = segments.size(); Segment lastSegment = segments.get(n - 1); if (transactionId >= lastSegment.firstTransactionId()) { return transactionId <= lastSegment.maxTransactionId() ? lastSegment : null; } int i = 1; while (i < n && segments.get(n - 1 - i).firstTransactionId() > transactionId) { i *= 2; } return binarySearch(segments, Math.max(0, n - 1 - i), n - 1 - (i / 2), transactionId); } }
SegmentFinder { static Segment findSegment(ArrayList<Segment> segments, long transactionId) { synchronized (segments) { int n = segments.size(); Segment lastSegment = segments.get(n - 1); if (transactionId >= lastSegment.firstTransactionId()) { return transactionId <= lastSegment.maxTransactionId() ? lastSegment : null; } int i = 1; while (i < n && segments.get(n - 1 - i).firstTransactionId() > transactionId) { i *= 2; } return binarySearch(segments, Math.max(0, n - 1 - i), n - 1 - (i / 2), transactionId); } } }
SegmentFinder { static Segment findSegment(ArrayList<Segment> segments, long transactionId) { synchronized (segments) { int n = segments.size(); Segment lastSegment = segments.get(n - 1); if (transactionId >= lastSegment.firstTransactionId()) { return transactionId <= lastSegment.maxTransactionId() ? lastSegment : null; } int i = 1; while (i < n && segments.get(n - 1 - i).firstTransactionId() > transactionId) { i *= 2; } return binarySearch(segments, Math.max(0, n - 1 - i), n - 1 - (i / 2), transactionId); } } private  SegmentFinder(); }
SegmentFinder { static Segment findSegment(ArrayList<Segment> segments, long transactionId) { synchronized (segments) { int n = segments.size(); Segment lastSegment = segments.get(n - 1); if (transactionId >= lastSegment.firstTransactionId()) { return transactionId <= lastSegment.maxTransactionId() ? lastSegment : null; } int i = 1; while (i < n && segments.get(n - 1 - i).firstTransactionId() > transactionId) { i *= 2; } return binarySearch(segments, Math.max(0, n - 1 - i), n - 1 - (i / 2), transactionId); } } private  SegmentFinder();  }
SegmentFinder { static Segment findSegment(ArrayList<Segment> segments, long transactionId) { synchronized (segments) { int n = segments.size(); Segment lastSegment = segments.get(n - 1); if (transactionId >= lastSegment.firstTransactionId()) { return transactionId <= lastSegment.maxTransactionId() ? lastSegment : null; } int i = 1; while (i < n && segments.get(n - 1 - i).firstTransactionId() > transactionId) { i *= 2; } return binarySearch(segments, Math.max(0, n - 1 - i), n - 1 - (i / 2), transactionId); } } private  SegmentFinder();   }