##// END OF EJS Templates
rust: Remove hex parsing from the nodemap...
Simon Sapin -
r47161:18a261b1 default
parent child Browse files
Show More
@@ -49,7 +49,7 b' fn create(index: &Index, path: &Path) ->'
49 49
50 50 fn query(index: &Index, nm: &NodeTree, prefix: &str) {
51 51 let start = Instant::now();
52 let res = nm.find_hex(index, prefix);
52 let res = NodePrefix::from_hex(prefix).map(|p| nm.find_bin(index, p));
53 53 println!("Result found in {:?}: {:?}", start.elapsed(), res);
54 54 }
55 55
@@ -13,8 +13,7 b''
13 13 //! is used in a more abstract context.
14 14
15 15 use super::{
16 node::NULL_NODE, FromHexError, Node, NodePrefix, Revision, RevlogIndex,
17 NULL_REVISION,
16 node::NULL_NODE, Node, NodePrefix, Revision, RevlogIndex, NULL_REVISION,
18 17 };
19 18
20 19 use bytes_cast::{unaligned, BytesCast};
@@ -27,17 +26,10 b' use std::ops::Index;'
27 26 #[derive(Debug, PartialEq)]
28 27 pub enum NodeMapError {
29 28 MultipleResults,
30 InvalidNodePrefix,
31 29 /// A `Revision` stored in the nodemap could not be found in the index
32 30 RevisionNotInIndex(Revision),
33 31 }
34 32
35 impl From<FromHexError> for NodeMapError {
36 fn from(_: FromHexError) -> Self {
37 NodeMapError::InvalidNodePrefix
38 }
39 }
40
41 33 /// Mapping system from Mercurial nodes to revision numbers.
42 34 ///
43 35 /// ## `RevlogIndex` and `NodeMap`
@@ -85,21 +77,6 b' pub trait NodeMap {'
85 77 prefix: NodePrefix,
86 78 ) -> Result<Option<Revision>, NodeMapError>;
87 79
88 /// Find the unique Revision whose `Node` hexadecimal string representation
89 /// starts with a given prefix
90 ///
91 /// If no Revision matches the given prefix, `Ok(None)` is returned.
92 ///
93 /// If several Revisions match the given prefix, a [`MultipleResults`]
94 /// error is returned.
95 fn find_hex(
96 &self,
97 idx: &impl RevlogIndex,
98 prefix: &str,
99 ) -> Result<Option<Revision>, NodeMapError> {
100 self.find_bin(idx, NodePrefix::from_hex(prefix)?)
101 }
102
103 80 /// Give the size of the shortest node prefix that determines
104 81 /// the revision uniquely.
105 82 ///
@@ -117,16 +94,6 b' pub trait NodeMap {'
117 94 node_prefix: NodePrefix,
118 95 ) -> Result<Option<usize>, NodeMapError>;
119 96
120 /// Same as `unique_prefix_len_bin`, with the hexadecimal representation
121 /// of the prefix as input.
122 fn unique_prefix_len_hex(
123 &self,
124 idx: &impl RevlogIndex,
125 prefix: &str,
126 ) -> Result<Option<usize>, NodeMapError> {
127 self.unique_prefix_len_bin(idx, NodePrefix::from_hex(prefix)?)
128 }
129
130 97 /// Same as `unique_prefix_len_bin`, with a full `Node` as input
131 98 fn unique_prefix_len_node(
132 99 &self,
@@ -802,6 +769,10 b' mod tests {'
802 769 ])
803 770 }
804 771
772 fn hex(s: &str) -> NodePrefix {
773 NodePrefix::from_hex(s).unwrap()
774 }
775
805 776 #[test]
806 777 fn test_nt_debug() {
807 778 let nt = sample_nodetree();
@@ -820,11 +791,11 b' mod tests {'
820 791 pad_insert(&mut idx, 1, "1234deadcafe");
821 792
822 793 let nt = NodeTree::from(vec![block! {1: Rev(1)}]);
823 assert_eq!(nt.find_hex(&idx, "1")?, Some(1));
824 assert_eq!(nt.find_hex(&idx, "12")?, Some(1));
825 assert_eq!(nt.find_hex(&idx, "1234de")?, Some(1));
826 assert_eq!(nt.find_hex(&idx, "1a")?, None);
827 assert_eq!(nt.find_hex(&idx, "ab")?, None);
794 assert_eq!(nt.find_bin(&idx, hex("1"))?, Some(1));
795 assert_eq!(nt.find_bin(&idx, hex("12"))?, Some(1));
796 assert_eq!(nt.find_bin(&idx, hex("1234de"))?, Some(1));
797 assert_eq!(nt.find_bin(&idx, hex("1a"))?, None);
798 assert_eq!(nt.find_bin(&idx, hex("ab"))?, None);
828 799
829 800 // and with full binary Nodes
830 801 assert_eq!(nt.find_node(&idx, idx.get(&1).unwrap())?, Some(1));
@@ -841,12 +812,12 b' mod tests {'
841 812
842 813 let nt = sample_nodetree();
843 814
844 assert_eq!(nt.find_hex(&idx, "0"), Err(MultipleResults));
845 assert_eq!(nt.find_hex(&idx, "01"), Ok(Some(9)));
846 assert_eq!(nt.find_hex(&idx, "00"), Err(MultipleResults));
847 assert_eq!(nt.find_hex(&idx, "00a"), Ok(Some(0)));
848 assert_eq!(nt.unique_prefix_len_hex(&idx, "00a"), Ok(Some(3)));
849 assert_eq!(nt.find_hex(&idx, "000"), Ok(Some(NULL_REVISION)));
815 assert_eq!(nt.find_bin(&idx, hex("0")), Err(MultipleResults));
816 assert_eq!(nt.find_bin(&idx, hex("01")), Ok(Some(9)));
817 assert_eq!(nt.find_bin(&idx, hex("00")), Err(MultipleResults));
818 assert_eq!(nt.find_bin(&idx, hex("00a")), Ok(Some(0)));
819 assert_eq!(nt.unique_prefix_len_bin(&idx, hex("00a")), Ok(Some(3)));
820 assert_eq!(nt.find_bin(&idx, hex("000")), Ok(Some(NULL_REVISION)));
850 821 }
851 822
852 823 #[test]
@@ -864,13 +835,13 b' mod tests {'
864 835 root: block![0: Block(1), 1:Block(3), 12: Rev(2)],
865 836 masked_inner_blocks: 1,
866 837 };
867 assert_eq!(nt.find_hex(&idx, "10")?, Some(1));
868 assert_eq!(nt.find_hex(&idx, "c")?, Some(2));
869 assert_eq!(nt.unique_prefix_len_hex(&idx, "c")?, Some(1));
870 assert_eq!(nt.find_hex(&idx, "00"), Err(MultipleResults));
871 assert_eq!(nt.find_hex(&idx, "000")?, Some(NULL_REVISION));
872 assert_eq!(nt.unique_prefix_len_hex(&idx, "000")?, Some(3));
873 assert_eq!(nt.find_hex(&idx, "01")?, Some(9));
838 assert_eq!(nt.find_bin(&idx, hex("10"))?, Some(1));
839 assert_eq!(nt.find_bin(&idx, hex("c"))?, Some(2));
840 assert_eq!(nt.unique_prefix_len_bin(&idx, hex("c"))?, Some(1));
841 assert_eq!(nt.find_bin(&idx, hex("00")), Err(MultipleResults));
842 assert_eq!(nt.find_bin(&idx, hex("000"))?, Some(NULL_REVISION));
843 assert_eq!(nt.unique_prefix_len_bin(&idx, hex("000"))?, Some(3));
844 assert_eq!(nt.find_bin(&idx, hex("01"))?, Some(9));
874 845 assert_eq!(nt.masked_readonly_blocks(), 2);
875 846 Ok(())
876 847 }
@@ -903,14 +874,14 b' mod tests {'
903 874 &self,
904 875 prefix: &str,
905 876 ) -> Result<Option<Revision>, NodeMapError> {
906 self.nt.find_hex(&self.index, prefix)
877 self.nt.find_bin(&self.index, hex(prefix))
907 878 }
908 879
909 880 fn unique_prefix_len_hex(
910 881 &self,
911 882 prefix: &str,
912 883 ) -> Result<Option<usize>, NodeMapError> {
913 self.nt.unique_prefix_len_hex(&self.index, prefix)
884 self.nt.unique_prefix_len_bin(&self.index, hex(prefix))
914 885 }
915 886
916 887 /// Drain `added` and restart a new one
@@ -17,7 +17,7 b' use cpython::{'
17 17 };
18 18 use hg::{
19 19 nodemap::{Block, NodeMapError, NodeTree},
20 revlog::{nodemap::NodeMap, RevlogIndex},
20 revlog::{nodemap::NodeMap, NodePrefix, RevlogIndex},
21 21 Revision,
22 22 };
23 23 use std::cell::RefCell;
@@ -107,7 +107,9 b' py_class!(pub class MixedIndex |py| {'
107 107 String::from_utf8_lossy(node.data(py)).to_string()
108 108 };
109 109
110 nt.find_hex(idx, &node_as_string)
110 let prefix = NodePrefix::from_hex(&node_as_string).map_err(|_| PyErr::new::<ValueError, _>(py, "Invalid node or prefix"))?;
111
112 nt.find_bin(idx, prefix)
111 113 // TODO make an inner API returning the node directly
112 114 .map(|opt| opt.map(
113 115 |rev| PyBytes::new(py, idx.node(rev).unwrap().as_bytes())))
@@ -468,9 +470,6 b' fn nodemap_error(py: Python, err: NodeMa'
468 470 match err {
469 471 NodeMapError::MultipleResults => revlog_error(py),
470 472 NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r),
471 NodeMapError::InvalidNodePrefix => {
472 PyErr::new::<ValueError, _>(py, "Invalid node or prefix")
473 }
474 473 }
475 474 }
476 475
General Comments 0
You need to be logged in to leave comments. Login now