public synchronized void wrap ( List < Segment > segments ) throws IOException { int newWorkingGeneration = ( _workingGeneration + 1 ) % Integer . MAX_VALUE ; int newWorkingSectionOffset = ( _workingSectionOffset + _bytesPerSection ) % _bytesPerSegment ; int cnt = segments . size ( ) ; int pos = _segmentDataStart + newWorkingSectionOffset ; // Ensure buffer capacity ensureCapacity ( cnt ) ; int newLiveSegmentCount = 0 ; for ( int index = 0 ; index < cnt ; index ++ ) { if ( segments . get ( index ) != null ) newLiveSegmentCount ++ ; } // Update compact-section for ( int index = 0 ; index < cnt ; index ++ ) { Segment seg = segments . get ( index ) ; if ( seg != null ) { if ( seg . getLoadSize ( ) > 0 ) { // Segment populated with data writeInt ( pos , LIVE_SEGMENT ) ; writeInt ( pos + 4 , seg . getLoadSize ( ) ) ; } else { // Segment initialized without any data writeInt ( pos , FREE_SEGMENT ) ; writeInt ( pos + 4 , 0 ) ; // Treat it as a non-live segment newLiveSegmentCount -- ; } } else { // Segment not alive writeInt ( pos , FREE_SEGMENT ) ; writeInt ( pos + 4 , 0 ) ; } pos += _bytesPerSegment ; } _mmapBuffer . force ( ) ; writeInt ( newWorkingSectionOffset , newWorkingGeneration ) ; writeInt ( newWorkingSectionOffset + 4 , newLiveSegmentCount ) ; _mmapBuffer . force ( ) ; // Switch working section to the new one _liveSegmentCount = newLiveSegmentCount ; _workingGeneration = newWorkingGeneration ; _workingSectionOffset = newWorkingSectionOffset ; _log . info ( _metaFile . getCanonicalPath ( ) + " updated" ) ; _log . info ( "workingGeneration=" + _workingGeneration + " liveSegmentCount=" + _liveSegmentCount ) ; }
Wrap a list of segments and persist meta data into the . meta file .