fn insert_extent(&mut self, current_extent: Extent) -> Result<(), Error> {
        let mut affected_extents = vec![];

        // Get all extents that may be affected by this extent insertion.
        for ext in self.extent_tree.iter_mut() {
            if !(current_extent.overlaps(&ext.1) || current_extent.is_adjacent(&ext.1)) {
                continue;
            }
            affected_extents.push(ext.1.clone());
        }

        // Remove all the affected extents.
        for ext in &affected_extents {
            self.extent_tree.remove(&ext.start());
        }

        // Perform split/merge of current extent with one affected extent at a time.
        // Note:
        //   1. This is performed on extents in the ascending order of extent.start.
        //   2. The existing extents are assumed to be non-overlapping with all other
        //      existing extents.
        //   3. Order of insertion changes in intermediate state of the cluster.
        let mut new_extents = vec![];
        let mut remaining = Some(current_extent.clone());
        for (i, ext) in affected_extents.iter().enumerate() {
            assert!(current_extent.overlaps(&ext) || current_extent.is_adjacent(&ext));
            // If remaining extent is completely consumed then iterate over the rest of affected
            // extents and just add them.
            // Ex. say we have already inserted extents [2, 5), [5, 8), and [8, 12) all having
            // different priorities. Now the current extent is [5, 8) with lower
            // priority than all the the extents then we may end up consuming current
            // entirely before we parse all of affected_extents.
            match remaining {
                None => new_extents.push(ext.clone()),
                Some(remaining_extent) => {
                    remaining = remaining_extent.split_or_merge(&ext, &mut new_extents);
                    assert!(remaining.is_some() || (i >= affected_extents.len() - 2));
                }
            }
        }

        // Insert all the split/merged extent into the extent tree.
        for ext in new_extents {
            self.extent_tree.insert(ext.start(), ext.clone());
        }

        // It may happen that the current extent maybe unaffected or partially affected.
        // If so insert it into the tree.
        match remaining {
            Some(e) => {
                self.extent_tree.insert(e.start(), e);
            }
            _ => {}
        }

        Ok(())
    }
// insert_extent's logic is as follows.
//  * create a list of all affected extents by this insertion.
//  * for each extent in affected extents, split/merge with current extents.
//  * replace affected and current extent with the new split/merged extents list.
insert_extent's logic is as follows.
create a list of all affected extents by this insertion.
for each extent in affected extents, split/merge with current extents.
replace affected and current extent with the new split/merged extents list.
insert_extent ' s logic is as follows . create a list of all affected extents by this insertion . for each extent in affected extents split / merge with current extents . replace affected and current extent with the new split / merged extents list .
fn insert_extent(&mut self, current_extent: Extent) -> Result<(), Error> {
        let mut affected_extents = vec![];
        // Get all extents that may be affected by this extent insertion.
        for ext in self.extent_tree.iter_mut() {
            if !(current_extent.overlaps(&ext.1) || current_extent.is_adjacent(&ext.1)) {
                continue;
            }
            affected_extents.push(ext.1.clone());
        }
        // Remove all the affected extents.
        for ext in &affected_extents {
            self.extent_tree.remove(&ext.start());
        }
        // Perform split/merge of current extent with one affected extent at a time.
        // Note:
        //   1. This is performed on extents in the ascending order of extent.start.
        //   2. The existing extents are assumed to be non-overlapping with all other
        //      existing extents.
        //   3. Order of insertion changes in intermediate state of the cluster.
        let mut new_extents = vec![];
        let mut remaining = Some(current_extent.clone());
        for (i, ext) in affected_extents.iter().enumerate() {
            assert!(current_extent.overlaps(&ext) || current_extent.is_adjacent(&ext));
            // If remaining extent is completely consumed then iterate over the rest of affected
            // extents and just add them.
            // Ex. say we have already inserted extents [2, 5), [5, 8), and [8, 12) all having
            // different priorities. Now the current extent is [5, 8) with lower
            // priority than all the the extents then we may end up consuming current
            // entirely before we parse all of affected_extents.
            match remaining {
                None => new_extents.push(ext.clone()),
                Some(remaining_extent) => {
                    remaining = remaining_extent.split_or_merge(&ext, &mut new_extents);
                    assert!(remaining.is_some() || (i >= affected_extents.len() - 2));
                }
            }
        }
        // Insert all the split/merged extent into the extent tree.
        for ext in new_extents {
            self.extent_tree.insert(ext.start(), ext.clone());
        }
        // It may happen that the current extent maybe unaffected or partially affected.
        // If so insert it into the tree.
        match remaining {
            Some(e) => {
                self.extent_tree.insert(e.start(), e);
            }
            _ => {}
        }
        Ok(())
    }
fn insert_extent ( & mut self , current_extent : Extent ) -> Result < ( ) , Error > { let mut affected_extents = vec ! [ ] ; // Get all extents that may be affected by this extent insertion. for ext in self . extent_tree . iter_mut ( ) { if ! ( current_extent . overlaps ( & ext . 1 ) || current_extent . is_adjacent ( & ext . 1 ) ) { continue ; } affected_extents . push ( ext . 1 . clone ( ) ) ; } // Remove all the affected extents. for ext in & affected_extents { self . extent_tree . remove ( & ext . start ( ) ) ; } // Perform split/merge of current extent with one affected extent at a time. // Note: //   1. This is performed on extents in the ascending order of extent.start. //   2. The existing extents are assumed to be non-overlapping with all other //      existing extents. //   3. Order of insertion changes in intermediate state of the cluster. let mut new_extents = vec ! [ ] ; let mut remaining = Some ( current_extent . clone ( ) ) ; for ( i , ext ) in affected_extents . iter ( ) . enumerate ( ) { assert ! ( current_extent overlaps ( ext ) current_extent is_adjacent ( ext ) ) ; // If remaining extent is completely consumed then iterate over the rest of affected // extents and just add them. // Ex. say we have already inserted extents [2, 5), [5, 8), and [8, 12) all having // different priorities. Now the current extent is [5, 8) with lower // priority than all the the extents then we may end up consuming current // entirely before we parse all of affected_extents. match remaining { None => new_extents . push ( ext . clone ( ) ) , Some ( remaining_extent ) => { remaining = remaining_extent . split_or_merge ( & ext , & mut new_extents ) ; assert ! ( remaining is_some ( ) ( i affected_extents len ( ) 2 ) ) ; } } } // Insert all the split/merged extent into the extent tree. for ext in new_extents { self . extent_tree . insert ( ext . start ( ) , ext . clone ( ) ) ; } // It may happen that the current extent maybe unaffected or partially affected. // If so insert it into the tree. match remaining { Some ( e ) => { self . extent_tree . insert ( e . start ( ) , e ) ; } _ => { } } Ok ( ( ) ) }
insert_extent's logic is as follows.
insert_extent ' s logic is as follows .
&mut self
current_extent
Extent
&mut self
current_extent
Extent