synchronized void trim ( long startKey , long endKey ) { /* * [3, 4, 5, 9] -> [10, 13, 14, 15] -> [21, 24, 29, 30] -> [31] :: start layout * |5______________________________23| :: trim(5, 23) * [5, 9] -> [10, 13, 14, 15] -> [21] :: result layout */ final Iterator < Chunk > descendingIterator = chunks . descendingIterator ( ) ; while ( descendingIterator . hasNext ( ) ) { final Chunk currentTail = descendingIterator . next ( ) ; if ( isFirstElementIsEmptyOrGreaterEqualThanKey ( currentTail , endKey ) ) { freeChunk ( currentTail ) ; descendingIterator . remove ( ) ; } else { currentTail . cursor = findFirstIndexOfGreaterEqualElements ( currentTail . keys , currentTail . startIndex , currentTail . cursor , endKey ) ; break ; } } final Iterator < Chunk > iterator = chunks . iterator ( ) ; while ( iterator . hasNext ( ) ) { final Chunk currentHead = iterator . next ( ) ; if ( isLastElementIsLessThanKey ( currentHead , startKey ) ) { freeChunk ( currentHead ) ; iterator . remove ( ) ; } else { final int newStartIndex = findFirstIndexOfGreaterEqualElements ( currentHead . keys , currentHead . startIndex , currentHead . cursor , startKey ) ; if ( currentHead . startIndex != newStartIndex ) { currentHead . startIndex = newStartIndex ; currentHead . chunkSize = currentHead . cursor - currentHead . startIndex ; } break ; } } }
Try to trim all beyond specified boundaries .