/// Finds a place where chunk data of a given length can be put.
///
/// If cannot find a place to put chunk data will extend file.
Finds a place where chunk data of a given length can be put.
If cannot find a place to put chunk data will extend file.
Finds a place where chunk data of a given length can be put . If cannot find a place to put chunk data will extend file .
fn find_place(
        &mut self,
        chunk_x: u8,
        chunk_z: u8,
        chunk_length: u32,
    ) -> Result<AnvilChunkMetadata, io::Error> {
        let sectors_required = (chunk_length / REGION_SECTOR_BYTES_LENGTH as u32) as u8 + 1;
        let metadata = self.get_metadata(chunk_x, chunk_z);
        // Can place chunk in the old sectors.
        if metadata.sectors == sectors_required {
            return Ok(metadata);
        }
        // Release used sectors.
        for i in 0..metadata.sectors {
            let sector_index = metadata.sector_index as usize + i as usize;
            self.used_sectors.set(sector_index, false);
        }
        let file_length = self.file.metadata()?.len();
        let total_sectors = file_length / REGION_SECTOR_BYTES_LENGTH as u64;
        // Trying to find enough big gap between sectors to put chunk.
        let mut sectors_free = 0;
        for sector_index in 0..total_sectors {
            if self.used_sectors[sector_index as usize] {
                sectors_free = 0;
                continue;
            }
            sectors_free += 1;
            // Can put chunk in gap.
            if sectors_free == sectors_required {
                let put_sector_index = sector_index as u32 - sectors_free as u32;
                // Acquire used sectors.
                for i in 0..sectors_free {
                    let sector_index = put_sector_index as usize + i as usize;
                    self.used_sectors.set(sector_index, true);
                }
                return Ok(AnvilChunkMetadata::new(put_sector_index, sectors_free, 0));
            }
        }
        // Extending file because cannot find a place to put chunk data.
        let extend_sectors = sectors_required - sectors_free;
        let extend_length = (REGION_SECTOR_BYTES_LENGTH * extend_sectors as u16) as u64;
        self.file.set_len(file_length + extend_length)?;
        // Mark new sectors as used.
        for _ in 0..extend_sectors {
            self.used_sectors.push(true);
        }
        return Ok(AnvilChunkMetadata::new(
            total_sectors as u32 - sectors_free as u32,
            sectors_required,
            0,
        ));
    }
fn find_place ( & mut self , chunk_x : u8 , chunk_z : u8 , chunk_length : u32 , ) -> Result < AnvilChunkMetadata , io :: Error > { let sectors_required = ( chunk_length / REGION_SECTOR_BYTES_LENGTH as u32 ) as u8 + 1 ; let metadata = self . get_metadata ( chunk_x , chunk_z ) ; // Can place chunk in the old sectors. if metadata . sectors == sectors_required { return Ok ( metadata ) ; } // Release used sectors. for i in 0 .. metadata . sectors { let sector_index = metadata . sector_index as usize + i as usize ; self . used_sectors . set ( sector_index , false ) ; } let file_length = self . file . metadata ( ) ? . len ( ) ; let total_sectors = file_length / REGION_SECTOR_BYTES_LENGTH as u64 ; // Trying to find enough big gap between sectors to put chunk. let mut sectors_free = 0 ; for sector_index in 0 .. total_sectors { if self . used_sectors [ sector_index as usize ] { sectors_free = 0 ; continue ; } sectors_free += 1 ; // Can put chunk in gap. if sectors_free == sectors_required { let put_sector_index = sector_index as u32 - sectors_free as u32 ; // Acquire used sectors. for i in 0 .. sectors_free { let sector_index = put_sector_index as usize + i as usize ; self . used_sectors . set ( sector_index , true ) ; } return Ok ( AnvilChunkMetadata :: new ( put_sector_index , sectors_free , 0 ) ) ; } } // Extending file because cannot find a place to put chunk data. let extend_sectors = sectors_required - sectors_free ; let extend_length = ( REGION_SECTOR_BYTES_LENGTH * extend_sectors as u16 ) as u64 ; self . file . set_len ( file_length + extend_length ) ? ; // Mark new sectors as used. for _ in 0 .. extend_sectors { self . used_sectors . push ( true ) ; } return Ok ( AnvilChunkMetadata :: new ( total_sectors as u32 - sectors_free as u32 , sectors_required , 0 , ) ) ; }
Finds a place where chunk data of a given length can be put.
Finds a place where chunk data of a given length can be put .
&mut self
chunk_x
u8
chunk_z
u8
chunk_length
u32