rust/hg-core/src/revlog/revlog.rs
changeset 46032 8d6164098782
parent 45806 7252f5237352
child 46033 88e741bf2d93
equal deleted inserted replaced
46031:c701f616d852 46032:8d6164098782
    19 
    19 
    20 pub enum RevlogError {
    20 pub enum RevlogError {
    21     IoError(std::io::Error),
    21     IoError(std::io::Error),
    22     UnsuportedVersion(u16),
    22     UnsuportedVersion(u16),
    23     InvalidRevision,
    23     InvalidRevision,
       
    24     /// Found more than one entry whose ID match the requested prefix
       
    25     AmbiguousPrefix,
    24     Corrupted,
    26     Corrupted,
    25     UnknowDataFormat(u8),
    27     UnknowDataFormat(u8),
    26 }
    28 }
    27 
    29 
    28 fn mmap_open(path: &Path) -> Result<Mmap, std::io::Error> {
    30 fn mmap_open(path: &Path) -> Result<Mmap, std::io::Error> {
    91     /// Return the full data associated to a node.
    93     /// Return the full data associated to a node.
    92     #[timed]
    94     #[timed]
    93     pub fn get_node_rev(&self, node: &[u8]) -> Result<Revision, RevlogError> {
    95     pub fn get_node_rev(&self, node: &[u8]) -> Result<Revision, RevlogError> {
    94         // This is brute force. But it is fast enough for now.
    96         // This is brute force. But it is fast enough for now.
    95         // Optimization will come later.
    97         // Optimization will come later.
       
    98         let mut found_by_prefix = None;
    96         for rev in (0..self.len() as Revision).rev() {
    99         for rev in (0..self.len() as Revision).rev() {
    97             let index_entry =
   100             let index_entry =
    98                 self.index.get_entry(rev).ok_or(RevlogError::Corrupted)?;
   101                 self.index.get_entry(rev).ok_or(RevlogError::Corrupted)?;
    99             if node == index_entry.hash() {
   102             if index_entry.hash() == node {
   100                 return Ok(rev);
   103                 return Ok(rev);
   101             }
   104             }
   102         }
   105             if index_entry.hash().starts_with(node) {
   103         Err(RevlogError::InvalidRevision)
   106                 if found_by_prefix.is_some() {
       
   107                     return Err(RevlogError::AmbiguousPrefix);
       
   108                 }
       
   109                 found_by_prefix = Some(rev)
       
   110             }
       
   111         }
       
   112         found_by_prefix.ok_or(RevlogError::InvalidRevision)
   104     }
   113     }
   105 
   114 
   106     /// Return the full data associated to a revision.
   115     /// Return the full data associated to a revision.
   107     ///
   116     ///
   108     /// All entries required to build the final data out of deltas will be
   117     /// All entries required to build the final data out of deltas will be