rust/hg-cpython/src/discovery.rs
changeset 51239 2e2832e00f6c
parent 51182 a6e293b21743
child 51240 0993a3520dc6
equal deleted inserted replaced
51238:578c049f0408 51239:2e2832e00f6c
    40         repo: PyObject,
    40         repo: PyObject,
    41         targetheads: PyObject,
    41         targetheads: PyObject,
    42         respectsize: bool,
    42         respectsize: bool,
    43         randomize: bool = true
    43         randomize: bool = true
    44     ) -> PyResult<PartialDiscovery> {
    44     ) -> PyResult<PartialDiscovery> {
       
    45         Self::inner_new(py, repo, targetheads, respectsize, randomize)
       
    46     }
       
    47 
       
    48     def addcommons(&self, commons: PyObject) -> PyResult<PyObject> {
       
    49         self.inner_addcommons(py, commons)
       
    50     }
       
    51 
       
    52     def addmissings(&self, missings: PyObject) -> PyResult<PyObject> {
       
    53         self.inner_addmissings(py, missings)
       
    54     }
       
    55 
       
    56     def addinfo(&self, sample: PyObject) -> PyResult<PyObject> {
       
    57         self.inner_addinfo(py, sample)
       
    58     }
       
    59 
       
    60     def hasinfo(&self) -> PyResult<bool> {
       
    61         Ok(self.inner(py).borrow().has_info())
       
    62     }
       
    63 
       
    64     def iscomplete(&self) -> PyResult<bool> {
       
    65         Ok(self.inner(py).borrow().is_complete())
       
    66     }
       
    67 
       
    68     def stats(&self) -> PyResult<PyDict> {
       
    69         let stats = self.inner(py).borrow().stats();
       
    70         let as_dict: PyDict = PyDict::new(py);
       
    71         as_dict.set_item(py, "undecided",
       
    72                          stats.undecided.map(
       
    73                              |l| l.to_py_object(py).into_object())
       
    74                              .unwrap_or_else(|| py.None()))?;
       
    75         Ok(as_dict)
       
    76     }
       
    77 
       
    78     def commonheads(&self) -> PyResult<HashSet<PyRevision>> {
       
    79         let res = self.inner(py).borrow().common_heads()
       
    80                     .map_err(|e| GraphError::pynew(py, e))?;
       
    81         Ok(res.into_iter().map(Into::into).collect())
       
    82     }
       
    83 
       
    84     def takefullsample(&self, headrevs: PyObject,
       
    85                        size: usize) -> PyResult<PyObject> {
       
    86         self.inner_takefullsample(py, headrevs, size)
       
    87     }
       
    88 
       
    89     def takequicksample(&self, headrevs: PyObject,
       
    90                         size: usize) -> PyResult<PyObject> {
       
    91         self.inner_takequicksample(py, headrevs, size)
       
    92     }
       
    93 
       
    94 });
       
    95 
       
    96 impl PartialDiscovery {
       
    97     fn inner_new(
       
    98         py: Python,
       
    99         repo: PyObject,
       
   100         targetheads: PyObject,
       
   101         respectsize: bool,
       
   102         randomize: bool,
       
   103     ) -> PyResult<Self> {
    45         let index = repo.getattr(py, "changelog")?.getattr(py, "index")?;
   104         let index = repo.getattr(py, "changelog")?.getattr(py, "index")?;
    46         let index = pyindex_to_graph(py, index)?;
   105         let index = pyindex_to_graph(py, index)?;
    47         let target_heads = rev_pyiter_collect(py, &targetheads, &index)?;
   106         let target_heads = rev_pyiter_collect(py, &targetheads, &index)?;
    48         Self::create_instance(
   107         Self::create_instance(
    49             py,
   108             py,
    55             ))),
   114             ))),
    56             RefCell::new(index),
   115             RefCell::new(index),
    57         )
   116         )
    58     }
   117     }
    59 
   118 
    60     def addcommons(&self, commons: PyObject) -> PyResult<PyObject> {
   119     fn inner_addinfo(
    61         let index = self.index(py).borrow();
   120         &self,
    62         let commons_vec: Vec<_> = rev_pyiter_collect(py, &commons, &*index)?;
   121         py: Python,
    63         let mut inner = self.inner(py).borrow_mut();
   122         sample: PyObject,
    64         inner.add_common_revisions(commons_vec)
   123     ) -> PyResult<PyObject> {
    65         .map_err(|e| GraphError::pynew(py, e))?;
       
    66     Ok(py.None())
       
    67 }
       
    68 
       
    69     def addmissings(&self, missings: PyObject) -> PyResult<PyObject> {
       
    70         let index = self.index(py).borrow();
       
    71         let missings_vec: Vec<_> = rev_pyiter_collect(py, &missings, &*index)?;
       
    72         let mut inner = self.inner(py).borrow_mut();
       
    73         inner.add_missing_revisions(missings_vec)
       
    74             .map_err(|e| GraphError::pynew(py, e))?;
       
    75         Ok(py.None())
       
    76     }
       
    77 
       
    78     def addinfo(&self, sample: PyObject) -> PyResult<PyObject> {
       
    79         let mut missing: Vec<Revision> = Vec::new();
   124         let mut missing: Vec<Revision> = Vec::new();
    80         let mut common: Vec<Revision> = Vec::new();
   125         let mut common: Vec<Revision> = Vec::new();
    81         for info in sample.iter(py)? { // info is a pair (Revision, bool)
   126         for info in sample.iter(py)? {
       
   127             // info is a pair (Revision, bool)
    82             let mut revknown = info?.iter(py)?;
   128             let mut revknown = info?.iter(py)?;
    83             let rev: PyRevision = revknown.next().unwrap()?.extract(py)?;
   129             let rev: PyRevision = revknown.next().unwrap()?.extract(py)?;
    84             // This is fine since we're just using revisions as integers
   130             // This is fine since we're just using revisions as integers
    85             // for the purposes of discovery
   131             // for the purposes of discovery
    86             let rev = Revision(rev.0);
   132             let rev = Revision(rev.0);
    90             } else {
   136             } else {
    91                 missing.push(rev);
   137                 missing.push(rev);
    92             }
   138             }
    93         }
   139         }
    94         let mut inner = self.inner(py).borrow_mut();
   140         let mut inner = self.inner(py).borrow_mut();
    95         inner.add_common_revisions(common)
   141         inner
    96             .map_err(|e| GraphError::pynew(py, e))?;
   142             .add_common_revisions(common)
    97         inner.add_missing_revisions(missing)
   143             .map_err(|e| GraphError::pynew(py, e))?;
       
   144         inner
       
   145             .add_missing_revisions(missing)
    98             .map_err(|e| GraphError::pynew(py, e))?;
   146             .map_err(|e| GraphError::pynew(py, e))?;
    99         Ok(py.None())
   147         Ok(py.None())
   100     }
   148     }
   101 
   149 
   102     def hasinfo(&self) -> PyResult<bool> {
   150     fn inner_addcommons(
   103         Ok(self.inner(py).borrow().has_info())
   151         &self,
   104     }
   152         py: Python,
   105 
   153         commons: PyObject,
   106     def iscomplete(&self) -> PyResult<bool> {
   154     ) -> PyResult<PyObject> {
   107         Ok(self.inner(py).borrow().is_complete())
   155         let index = self.index(py).borrow();
   108     }
   156         let commons_vec: Vec<_> = rev_pyiter_collect(py, &commons, &*index)?;
   109 
   157         let mut inner = self.inner(py).borrow_mut();
   110     def stats(&self) -> PyResult<PyDict> {
   158         inner
   111         let stats = self.inner(py).borrow().stats();
   159             .add_common_revisions(commons_vec)
   112         let as_dict: PyDict = PyDict::new(py);
   160             .map_err(|e| GraphError::pynew(py, e))?;
   113         as_dict.set_item(py, "undecided",
   161         Ok(py.None())
   114                          stats.undecided.map(
   162     }
   115                              |l| l.to_py_object(py).into_object())
   163 
   116                              .unwrap_or_else(|| py.None()))?;
   164     fn inner_addmissings(
   117         Ok(as_dict)
   165         &self,
   118     }
   166         py: Python,
   119 
   167         missings: PyObject,
   120     def commonheads(&self) -> PyResult<HashSet<PyRevision>> {
   168     ) -> PyResult<PyObject> {
   121         let res = self.inner(py).borrow().common_heads()
   169         let index = self.index(py).borrow();
   122                     .map_err(|e| GraphError::pynew(py, e))?;
   170         let missings_vec: Vec<_> = rev_pyiter_collect(py, &missings, &*index)?;
   123         Ok(res.into_iter().map(Into::into).collect())
   171         let mut inner = self.inner(py).borrow_mut();
   124     }
   172         inner
   125 
   173             .add_missing_revisions(missings_vec)
   126     def takefullsample(&self, _headrevs: PyObject,
   174             .map_err(|e| GraphError::pynew(py, e))?;
   127                        size: usize) -> PyResult<PyObject> {
   175         Ok(py.None())
   128         let mut inner = self.inner(py).borrow_mut();
   176     }
   129         let sample = inner.take_full_sample(size)
   177 
       
   178     fn inner_takefullsample(
       
   179         &self,
       
   180         py: Python,
       
   181         _headrevs: PyObject,
       
   182         size: usize,
       
   183     ) -> PyResult<PyObject> {
       
   184         let mut inner = self.inner(py).borrow_mut();
       
   185         let sample = inner
       
   186             .take_full_sample(size)
   130             .map_err(|e| GraphError::pynew(py, e))?;
   187             .map_err(|e| GraphError::pynew(py, e))?;
   131         let as_vec: Vec<PyObject> = sample
   188         let as_vec: Vec<PyObject> = sample
   132             .iter()
   189             .iter()
   133             .map(|rev| PyRevision(rev.0).to_py_object(py).into_object())
   190             .map(|rev| PyRevision(rev.0).to_py_object(py).into_object())
   134             .collect();
   191             .collect();
   135         Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
   192         Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
   136     }
   193     }
   137 
   194 
   138     def takequicksample(&self, headrevs: PyObject,
   195     fn inner_takequicksample(
   139                         size: usize) -> PyResult<PyObject> {
   196         &self,
       
   197         py: Python,
       
   198         headrevs: PyObject,
       
   199         size: usize,
       
   200     ) -> PyResult<PyObject> {
   140         let index = self.index(py).borrow();
   201         let index = self.index(py).borrow();
   141         let mut inner = self.inner(py).borrow_mut();
   202         let mut inner = self.inner(py).borrow_mut();
   142         let revsvec: Vec<_> = rev_pyiter_collect(py, &headrevs, &*index)?;
   203         let revsvec: Vec<_> = rev_pyiter_collect(py, &headrevs, &*index)?;
   143         let sample = inner.take_quick_sample(revsvec, size)
   204         let sample = inner
       
   205             .take_quick_sample(revsvec, size)
   144             .map_err(|e| GraphError::pynew(py, e))?;
   206             .map_err(|e| GraphError::pynew(py, e))?;
   145         let as_vec: Vec<PyObject> = sample
   207         let as_vec: Vec<PyObject> = sample
   146             .iter()
   208             .iter()
   147             .map(|rev| PyRevision(rev.0).to_py_object(py).into_object())
   209             .map(|rev| PyRevision(rev.0).to_py_object(py).into_object())
   148             .collect();
   210             .collect();
   149         Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
   211         Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
   150     }
   212     }
   151 
   213 }
   152 });
       
   153 
   214 
   154 /// Create the module, with __package__ given from parent
   215 /// Create the module, with __package__ given from parent
   155 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
   216 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
   156     let dotted_name = &format!("{}.discovery", package);
   217     let dotted_name = &format!("{}.discovery", package);
   157     let m = PyModule::new(py, dotted_name)?;
   218     let m = PyModule::new(py, dotted_name)?;