##// 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 fn query(index: &Index, nm: &NodeTree, prefix: &str) {
50 fn query(index: &Index, nm: &NodeTree, prefix: &str) {
51 let start = Instant::now();
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 println!("Result found in {:?}: {:?}", start.elapsed(), res);
53 println!("Result found in {:?}: {:?}", start.elapsed(), res);
54 }
54 }
55
55
@@ -13,8 +13,7 b''
13 //! is used in a more abstract context.
13 //! is used in a more abstract context.
14
14
15 use super::{
15 use super::{
16 node::NULL_NODE, FromHexError, Node, NodePrefix, Revision, RevlogIndex,
16 node::NULL_NODE, Node, NodePrefix, Revision, RevlogIndex, NULL_REVISION,
17 NULL_REVISION,
18 };
17 };
19
18
20 use bytes_cast::{unaligned, BytesCast};
19 use bytes_cast::{unaligned, BytesCast};
@@ -27,17 +26,10 b' use std::ops::Index;'
27 #[derive(Debug, PartialEq)]
26 #[derive(Debug, PartialEq)]
28 pub enum NodeMapError {
27 pub enum NodeMapError {
29 MultipleResults,
28 MultipleResults,
30 InvalidNodePrefix,
31 /// A `Revision` stored in the nodemap could not be found in the index
29 /// A `Revision` stored in the nodemap could not be found in the index
32 RevisionNotInIndex(Revision),
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 /// Mapping system from Mercurial nodes to revision numbers.
33 /// Mapping system from Mercurial nodes to revision numbers.
42 ///
34 ///
43 /// ## `RevlogIndex` and `NodeMap`
35 /// ## `RevlogIndex` and `NodeMap`
@@ -85,21 +77,6 b' pub trait NodeMap {'
85 prefix: NodePrefix,
77 prefix: NodePrefix,
86 ) -> Result<Option<Revision>, NodeMapError>;
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 /// Give the size of the shortest node prefix that determines
80 /// Give the size of the shortest node prefix that determines
104 /// the revision uniquely.
81 /// the revision uniquely.
105 ///
82 ///
@@ -117,16 +94,6 b' pub trait NodeMap {'
117 node_prefix: NodePrefix,
94 node_prefix: NodePrefix,
118 ) -> Result<Option<usize>, NodeMapError>;
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 /// Same as `unique_prefix_len_bin`, with a full `Node` as input
97 /// Same as `unique_prefix_len_bin`, with a full `Node` as input
131 fn unique_prefix_len_node(
98 fn unique_prefix_len_node(
132 &self,
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 #[test]
776 #[test]
806 fn test_nt_debug() {
777 fn test_nt_debug() {
807 let nt = sample_nodetree();
778 let nt = sample_nodetree();
@@ -820,11 +791,11 b' mod tests {'
820 pad_insert(&mut idx, 1, "1234deadcafe");
791 pad_insert(&mut idx, 1, "1234deadcafe");
821
792
822 let nt = NodeTree::from(vec![block! {1: Rev(1)}]);
793 let nt = NodeTree::from(vec![block! {1: Rev(1)}]);
823 assert_eq!(nt.find_hex(&idx, "1")?, Some(1));
794 assert_eq!(nt.find_bin(&idx, hex("1"))?, Some(1));
824 assert_eq!(nt.find_hex(&idx, "12")?, Some(1));
795 assert_eq!(nt.find_bin(&idx, hex("12"))?, Some(1));
825 assert_eq!(nt.find_hex(&idx, "1234de")?, Some(1));
796 assert_eq!(nt.find_bin(&idx, hex("1234de"))?, Some(1));
826 assert_eq!(nt.find_hex(&idx, "1a")?, None);
797 assert_eq!(nt.find_bin(&idx, hex("1a"))?, None);
827 assert_eq!(nt.find_hex(&idx, "ab")?, None);
798 assert_eq!(nt.find_bin(&idx, hex("ab"))?, None);
828
799
829 // and with full binary Nodes
800 // and with full binary Nodes
830 assert_eq!(nt.find_node(&idx, idx.get(&1).unwrap())?, Some(1));
801 assert_eq!(nt.find_node(&idx, idx.get(&1).unwrap())?, Some(1));
@@ -841,12 +812,12 b' mod tests {'
841
812
842 let nt = sample_nodetree();
813 let nt = sample_nodetree();
843
814
844 assert_eq!(nt.find_hex(&idx, "0"), Err(MultipleResults));
815 assert_eq!(nt.find_bin(&idx, hex("0")), Err(MultipleResults));
845 assert_eq!(nt.find_hex(&idx, "01"), Ok(Some(9)));
816 assert_eq!(nt.find_bin(&idx, hex("01")), Ok(Some(9)));
846 assert_eq!(nt.find_hex(&idx, "00"), Err(MultipleResults));
817 assert_eq!(nt.find_bin(&idx, hex("00")), Err(MultipleResults));
847 assert_eq!(nt.find_hex(&idx, "00a"), Ok(Some(0)));
818 assert_eq!(nt.find_bin(&idx, hex("00a")), Ok(Some(0)));
848 assert_eq!(nt.unique_prefix_len_hex(&idx, "00a"), Ok(Some(3)));
819 assert_eq!(nt.unique_prefix_len_bin(&idx, hex("00a")), Ok(Some(3)));
849 assert_eq!(nt.find_hex(&idx, "000"), Ok(Some(NULL_REVISION)));
820 assert_eq!(nt.find_bin(&idx, hex("000")), Ok(Some(NULL_REVISION)));
850 }
821 }
851
822
852 #[test]
823 #[test]
@@ -864,13 +835,13 b' mod tests {'
864 root: block![0: Block(1), 1:Block(3), 12: Rev(2)],
835 root: block![0: Block(1), 1:Block(3), 12: Rev(2)],
865 masked_inner_blocks: 1,
836 masked_inner_blocks: 1,
866 };
837 };
867 assert_eq!(nt.find_hex(&idx, "10")?, Some(1));
838 assert_eq!(nt.find_bin(&idx, hex("10"))?, Some(1));
868 assert_eq!(nt.find_hex(&idx, "c")?, Some(2));
839 assert_eq!(nt.find_bin(&idx, hex("c"))?, Some(2));
869 assert_eq!(nt.unique_prefix_len_hex(&idx, "c")?, Some(1));
840 assert_eq!(nt.unique_prefix_len_bin(&idx, hex("c"))?, Some(1));
870 assert_eq!(nt.find_hex(&idx, "00"), Err(MultipleResults));
841 assert_eq!(nt.find_bin(&idx, hex("00")), Err(MultipleResults));
871 assert_eq!(nt.find_hex(&idx, "000")?, Some(NULL_REVISION));
842 assert_eq!(nt.find_bin(&idx, hex("000"))?, Some(NULL_REVISION));
872 assert_eq!(nt.unique_prefix_len_hex(&idx, "000")?, Some(3));
843 assert_eq!(nt.unique_prefix_len_bin(&idx, hex("000"))?, Some(3));
873 assert_eq!(nt.find_hex(&idx, "01")?, Some(9));
844 assert_eq!(nt.find_bin(&idx, hex("01"))?, Some(9));
874 assert_eq!(nt.masked_readonly_blocks(), 2);
845 assert_eq!(nt.masked_readonly_blocks(), 2);
875 Ok(())
846 Ok(())
876 }
847 }
@@ -903,14 +874,14 b' mod tests {'
903 &self,
874 &self,
904 prefix: &str,
875 prefix: &str,
905 ) -> Result<Option<Revision>, NodeMapError> {
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 fn unique_prefix_len_hex(
880 fn unique_prefix_len_hex(
910 &self,
881 &self,
911 prefix: &str,
882 prefix: &str,
912 ) -> Result<Option<usize>, NodeMapError> {
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 /// Drain `added` and restart a new one
887 /// Drain `added` and restart a new one
@@ -17,7 +17,7 b' use cpython::{'
17 };
17 };
18 use hg::{
18 use hg::{
19 nodemap::{Block, NodeMapError, NodeTree},
19 nodemap::{Block, NodeMapError, NodeTree},
20 revlog::{nodemap::NodeMap, RevlogIndex},
20 revlog::{nodemap::NodeMap, NodePrefix, RevlogIndex},
21 Revision,
21 Revision,
22 };
22 };
23 use std::cell::RefCell;
23 use std::cell::RefCell;
@@ -107,7 +107,9 b' py_class!(pub class MixedIndex |py| {'
107 String::from_utf8_lossy(node.data(py)).to_string()
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 // TODO make an inner API returning the node directly
113 // TODO make an inner API returning the node directly
112 .map(|opt| opt.map(
114 .map(|opt| opt.map(
113 |rev| PyBytes::new(py, idx.node(rev).unwrap().as_bytes())))
115 |rev| PyBytes::new(py, idx.node(rev).unwrap().as_bytes())))
@@ -468,9 +470,6 b' fn nodemap_error(py: Python, err: NodeMa'
468 match err {
470 match err {
469 NodeMapError::MultipleResults => revlog_error(py),
471 NodeMapError::MultipleResults => revlog_error(py),
470 NodeMapError::RevisionNotInIndex(r) => rev_not_in_index(py, r),
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