1use {
2 crate::accounts_db::AccountStorageEntry,
3 log::*,
4 solana_clock::Slot,
5 solana_measure::measure::Measure,
6 std::{
7 collections::HashMap,
8 ops::{Bound, Range, RangeBounds},
9 sync::Arc,
10 },
11};
12
13pub struct SortedStorages<'a> {
15 range: Range<Slot>,
17 storages: HashMap<Slot, &'a Arc<AccountStorageEntry>>,
21}
22
23impl<'a> SortedStorages<'a> {
24 pub fn empty() -> Self {
26 SortedStorages {
27 range: Range::default(),
28 storages: HashMap::default(),
29 }
30 }
31
32 pub fn iter_range<R>(&'a self, range: &R) -> SortedStoragesIter<'a>
34 where
35 R: RangeBounds<Slot>,
36 {
37 SortedStoragesIter::new(self, range)
38 }
39
40 fn get(&self, slot: Slot) -> Option<&Arc<AccountStorageEntry>> {
41 self.storages.get(&slot).copied()
42 }
43
44 pub fn range_width(&self) -> Slot {
45 self.range.end - self.range.start
46 }
47
48 pub fn range(&self) -> &Range<Slot> {
49 &self.range
50 }
51
52 pub fn max_slot_inclusive(&self) -> Slot {
53 self.range.end.saturating_sub(1)
54 }
55
56 pub fn slot_count(&self) -> usize {
57 self.storages.len()
58 }
59
60 pub fn storage_count(&self) -> usize {
61 self.storages.len()
62 }
63
64 pub fn new(source: &'a [Arc<AccountStorageEntry>]) -> Self {
67 let slots = source.iter().map(|storage| {
68 storage.slot() });
70 Self::new_with_slots(source.iter().zip(slots), None, None)
71 }
72
73 pub fn new_with_slots(
77 source: impl Iterator<Item = (&'a Arc<AccountStorageEntry>, Slot)> + Clone,
78 min_slot: Option<Slot>,
80 max_slot_inclusive: Option<Slot>,
85 ) -> Self {
86 let mut min = Slot::MAX;
87 let mut max = Slot::MIN;
88 let mut adjust_min_max = |slot| {
89 min = std::cmp::min(slot, min);
90 max = std::cmp::max(slot + 1, max);
91 };
92 if let Some(slot) = min_slot {
94 adjust_min_max(slot);
95 }
96 if let Some(slot) = max_slot_inclusive {
97 adjust_min_max(slot);
98 }
99
100 let mut slot_count = 0;
101 let mut time = Measure::start("get slot");
102 let source_ = source.clone();
103 let mut storage_count = 0;
104 source_.for_each(|(_, slot)| {
105 storage_count += 1;
106 slot_count += 1;
107 adjust_min_max(slot);
108 });
109 time.stop();
110 let mut time2 = Measure::start("sort");
111 let range;
112 let mut storages = HashMap::default();
113 if min > max {
114 range = Range::default();
115 } else {
116 range = Range {
117 start: min,
118 end: max,
119 };
120 source.for_each(|(original_storages, slot)| {
121 assert!(
122 storages.insert(slot, original_storages).is_none(),
123 "slots are not unique"
124 ); });
126 }
127 time2.stop();
128 debug!("SortedStorages, times: {}, {}", time.as_us(), time2.as_us());
129 Self { range, storages }
130 }
131}
132
133pub struct SortedStoragesIter<'a> {
137 range: Range<Slot>,
139 storages: &'a SortedStorages<'a>,
141 next_slot: Slot,
143}
144
145impl<'a> Iterator for SortedStoragesIter<'a> {
146 type Item = (Slot, Option<&'a Arc<AccountStorageEntry>>);
147
148 fn next(&mut self) -> Option<Self::Item> {
149 let slot = self.next_slot;
150 if slot < self.range.end {
151 self.next_slot += 1;
153 Some((slot, self.storages.get(slot)))
154 } else {
155 None
157 }
158 }
159}
160
161impl<'a> SortedStoragesIter<'a> {
162 pub fn new<R: RangeBounds<Slot>>(
163 storages: &'a SortedStorages<'a>,
164 range: &R,
165 ) -> SortedStoragesIter<'a> {
166 let storage_range = storages.range();
167 let next_slot = match range.start_bound() {
168 Bound::Unbounded => {
169 storage_range.start }
171 Bound::Included(x) => *x,
172 Bound::Excluded(x) => *x + 1, };
174 let end_exclusive_slot = match range.end_bound() {
175 Bound::Unbounded => {
176 storage_range.end }
178 Bound::Included(x) => *x + 1, Bound::Excluded(x) => *x,
180 };
181 let range = next_slot..end_exclusive_slot;
185 SortedStoragesIter {
186 range,
187 storages,
188 next_slot,
189 }
190 }
191}
192
193#[cfg(test)]
194mod tests {
195 use {
196 super::*,
197 crate::{
198 accounts_db::{AccountStorageEntry, AccountsFileId},
199 accounts_file::{AccountsFile, AccountsFileProvider},
200 append_vec::AppendVec,
201 },
202 std::sync::Arc,
203 };
204
205 impl<'a> SortedStorages<'a> {
206 pub fn new_debug(
207 source: &[(&'a Arc<AccountStorageEntry>, Slot)],
208 min: Slot,
209 len: usize,
210 ) -> Self {
211 let mut storages = HashMap::default();
212 let range = Range {
213 start: min,
214 end: min + len as Slot,
215 };
216 for (storage, slot) in source {
217 storages.insert(*slot, *storage);
218 }
219
220 Self { range, storages }
221 }
222
223 pub fn new_for_tests(storages: &[&'a Arc<AccountStorageEntry>], slots: &[Slot]) -> Self {
224 assert_eq!(storages.len(), slots.len());
225 SortedStorages::new_with_slots(
226 storages.iter().cloned().zip(slots.iter().cloned()),
227 None,
228 None,
229 )
230 }
231 }
232
233 #[test]
234 fn test_sorted_storages_range_iter() {
235 let storages = SortedStorages::empty();
236 let check = |(slot, storages): (Slot, Option<&Arc<AccountStorageEntry>>)| {
237 assert!(storages.is_none());
238 slot
239 };
240 assert_eq!(
241 (0..5).collect::<Vec<_>>(),
242 storages.iter_range(&(..5)).map(check).collect::<Vec<_>>()
243 );
244 assert_eq!(
245 (1..5).collect::<Vec<_>>(),
246 storages.iter_range(&(1..5)).map(check).collect::<Vec<_>>()
247 );
248 assert_eq!(
249 (0..0).collect::<Vec<_>>(),
250 storages.iter_range(&(..)).map(check).collect::<Vec<_>>()
251 );
252 assert_eq!(
253 (0..0).collect::<Vec<_>>(),
254 storages.iter_range(&(1..)).map(check).collect::<Vec<_>>()
255 );
256
257 let s1 = create_sample_store(1);
259 let storages = SortedStorages::new_for_tests(&[&s1], &[3]);
260 let check = |(slot, storages): (Slot, Option<&Arc<AccountStorageEntry>>)| {
261 assert!(
262 (slot != 3) ^ storages.is_some(),
263 "slot: {slot}, storages: {storages:?}"
264 );
265 slot
266 };
267 for start in 0..5 {
268 for end in 0..5 {
269 assert_eq!(
270 (start..end).collect::<Vec<_>>(),
271 storages
272 .iter_range(&(start..end))
273 .map(check)
274 .collect::<Vec<_>>()
275 );
276 }
277 }
278 assert_eq!(
279 (3..5).collect::<Vec<_>>(),
280 storages.iter_range(&(..5)).map(check).collect::<Vec<_>>()
281 );
282 assert_eq!(
283 (1..=3).collect::<Vec<_>>(),
284 storages.iter_range(&(1..)).map(check).collect::<Vec<_>>()
285 );
286 assert_eq!(
287 (3..=3).collect::<Vec<_>>(),
288 storages.iter_range(&(..)).map(check).collect::<Vec<_>>()
289 );
290
291 let store2 = create_sample_store(2);
293 let store4 = create_sample_store(4);
294
295 let storages = SortedStorages::new_for_tests(&[&store2, &store4], &[2, 4]);
296 let check = |(slot, storage): (Slot, Option<&Arc<AccountStorageEntry>>)| {
297 assert!(
298 (slot != 2 && slot != 4)
299 ^ storage
300 .map(|storage| storage.id() == (slot as AccountsFileId))
301 .unwrap_or(false),
302 "slot: {slot}, storage: {storage:?}"
303 );
304 slot
305 };
306 for start in 0..5 {
307 for end in 0..5 {
308 assert_eq!(
309 (start..end).collect::<Vec<_>>(),
310 storages
311 .iter_range(&(start..end))
312 .map(check)
313 .collect::<Vec<_>>()
314 );
315 }
316 }
317 assert_eq!(
318 (2..5).collect::<Vec<_>>(),
319 storages.iter_range(&(..5)).map(check).collect::<Vec<_>>()
320 );
321 assert_eq!(
322 (1..=4).collect::<Vec<_>>(),
323 storages.iter_range(&(1..)).map(check).collect::<Vec<_>>()
324 );
325 assert_eq!(
326 (2..=4).collect::<Vec<_>>(),
327 storages.iter_range(&(..)).map(check).collect::<Vec<_>>()
328 );
329 }
330
331 #[test]
332 fn test_sorted_storages_new_with_slots() {
333 let store = create_sample_store(1);
334 let start = 33;
335 let end = 44;
336
337 {
343 let min = start + 1;
344 let max = end - 1;
345 let storages = SortedStorages::new_with_slots(
346 [(&store, end), (&store, start)].iter().cloned(),
347 Some(min),
348 Some(max),
349 );
350 assert_eq!(storages.storages.len(), 2);
351 assert_eq!(storages.range, start..end + 1);
352 }
353
354 {
360 let min = start + 1;
361 let max = end + 1;
362 let storages = SortedStorages::new_with_slots(
363 [(&store, end), (&store, start)].iter().cloned(),
364 Some(min),
365 Some(max),
366 );
367 assert_eq!(storages.storages.len(), 2);
368 assert_eq!(storages.range, start..max + 1);
369 }
370
371 {
377 let min = start - 1;
378 let max = end - 1;
379 let storages = SortedStorages::new_with_slots(
380 [(&store, end), (&store, start)].iter().cloned(),
381 Some(min),
382 Some(max),
383 );
384 assert_eq!(storages.storages.len(), 2);
385 assert_eq!(storages.range, min..end + 1);
386 }
387
388 {
394 let min = start - 1;
395 let max = end + 1;
396 let storages = SortedStorages::new_with_slots(
397 [(&store, end), (&store, start)].iter().cloned(),
398 Some(min),
399 Some(max),
400 );
401 assert_eq!(storages.storages.len(), 2);
402 assert_eq!(storages.range, min..max + 1);
403 }
404 }
405
406 #[test]
407 #[should_panic(expected = "slots are not unique")]
408 fn test_sorted_storages_duplicate_slots() {
409 let store = create_sample_store(1);
410 SortedStorages::new_for_tests(&[&store, &store], &[0, 0]);
411 }
412
413 #[test]
414 fn test_sorted_storages_none() {
415 let result = SortedStorages::empty();
416 assert_eq!(result.range, Range::default());
417 assert_eq!(result.slot_count(), 0);
418 assert_eq!(result.storages.len(), 0);
419 assert!(result.get(0).is_none());
420 }
421
422 #[test]
423 fn test_sorted_storages_1() {
424 let store = create_sample_store(1);
425 let slot = 4;
426 let vecs = [&store];
427 let result = SortedStorages::new_for_tests(&vecs, &[slot]);
428 assert_eq!(
429 result.range,
430 Range {
431 start: slot,
432 end: slot + 1
433 }
434 );
435 assert_eq!(result.slot_count(), 1);
436 assert_eq!(result.storages.len(), 1);
437 assert_eq!(result.get(slot).unwrap().id(), store.id());
438 }
439
440 fn create_sample_store(id: AccountsFileId) -> Arc<AccountStorageEntry> {
441 let tf = crate::append_vec::test_utils::get_append_vec_path("create_sample_store");
442 let (_temp_dirs, paths) = crate::accounts_db::get_temp_accounts_paths(1).unwrap();
443 let size: usize = 123;
444 let slot = 0;
445 let mut data = AccountStorageEntry::new(
446 &paths[0],
447 slot,
448 id,
449 size as u64,
450 AccountsFileProvider::AppendVec,
451 );
452 let av = AccountsFile::AppendVec(AppendVec::new(&tf.path, true, 1024 * 1024));
453 data.accounts = av;
454
455 Arc::new(data)
456 }
457
458 #[test]
459 fn test_sorted_storages_2() {
460 let store = create_sample_store(1);
461 let store2 = create_sample_store(2);
462 let slots = [4, 7];
463 let vecs = [&store, &store2];
464 let result = SortedStorages::new_for_tests(&vecs, &slots);
465 assert_eq!(
466 result.range,
467 Range {
468 start: slots[0],
469 end: slots[1] + 1,
470 }
471 );
472 assert_eq!(result.slot_count(), 2);
473 assert_eq!(result.storages.len(), 2);
474 assert!(result.get(0).is_none());
475 assert!(result.get(3).is_none());
476 assert!(result.get(5).is_none());
477 assert!(result.get(6).is_none());
478 assert!(result.get(8).is_none());
479 assert_eq!(result.get(slots[0]).unwrap().id(), store.id());
480 assert_eq!(result.get(slots[1]).unwrap().id(), store2.id());
481 }
482}