##// END OF EJS Templates
rust-index: add an abstraction to support bytes added at runtimes...
Raphaël Gomès -
r52081:1ef4a36a default
parent child Browse files
Show More
@@ -79,9 +79,50 b' impl IndexHeader {'
79 }
79 }
80 }
80 }
81
81
82 /// Abstracts the access to the index bytes since they can be spread between
83 /// the immutable (bytes) part and the mutable (added) part if any appends
84 /// happened. This makes it transparent for the callers.
85 struct IndexData {
86 /// Immutable bytes, most likely taken from disk
87 bytes: Box<dyn Deref<Target = [u8]> + Send>,
88 /// Bytes that were added after reading the index
89 added: Vec<u8>,
90 }
91
92 impl IndexData {
93 pub fn new(bytes: Box<dyn Deref<Target = [u8]> + Send>) -> Self {
94 Self {
95 bytes,
96 added: vec![],
97 }
98 }
99
100 pub fn len(&self) -> usize {
101 self.bytes.len() + self.added.len()
102 }
103 }
104
105 impl std::ops::Index<std::ops::Range<usize>> for IndexData {
106 type Output = [u8];
107
108 fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
109 let start = index.start;
110 let end = index.end;
111 let immutable_len = self.bytes.len();
112 if start < immutable_len {
113 if end > immutable_len {
114 panic!("index data cannot span existing and added ranges");
115 }
116 &self.bytes[index]
117 } else {
118 &self.added[start - immutable_len..end - immutable_len]
119 }
120 }
121 }
122
82 /// A Revlog index
123 /// A Revlog index
83 pub struct Index {
124 pub struct Index {
84 bytes: Box<dyn Deref<Target = [u8]> + Send>,
125 bytes: IndexData,
85 /// Offsets of starts of index blocks.
126 /// Offsets of starts of index blocks.
86 /// Only needed when the index is interleaved with data.
127 /// Only needed when the index is interleaved with data.
87 offsets: Option<Vec<usize>>,
128 offsets: Option<Vec<usize>>,
@@ -150,7 +191,7 b' impl Index {'
150
191
151 if offset == bytes.len() {
192 if offset == bytes.len() {
152 Ok(Self {
193 Ok(Self {
153 bytes,
194 bytes: IndexData::new(bytes),
154 offsets: Some(offsets),
195 offsets: Some(offsets),
155 uses_generaldelta,
196 uses_generaldelta,
156 })
197 })
@@ -159,7 +200,7 b' impl Index {'
159 }
200 }
160 } else {
201 } else {
161 Ok(Self {
202 Ok(Self {
162 bytes,
203 bytes: IndexData::new(bytes),
163 offsets: None,
204 offsets: None,
164 uses_generaldelta,
205 uses_generaldelta,
165 })
206 })
General Comments 0
You need to be logged in to leave comments. Login now