|
| 1 | +// A Grouper is used to group partitions. |
| 2 | +pub trait Grouper { |
| 3 | + // group groups the number of partitions into a vec of groups. |
| 4 | + fn group(&self, num_partitions: usize) -> Vec<PartitionGroup>; |
| 5 | +} |
| 6 | + |
| 7 | +// PartitionGroup is a struct that represents a range of partitions from [start, end). This is |
| 8 | +// more space efficient than a vector of u64s. |
| 9 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 10 | +pub struct PartitionGroup { |
| 11 | + start: usize, |
| 12 | + end: usize, |
| 13 | +} |
| 14 | + |
| 15 | +impl PartitionGroup { |
| 16 | + // new creates a new PartitionGroup containing partitions in the range [start..end). |
| 17 | + pub fn new(start: usize, end: usize) -> Self { |
| 18 | + Self { start, end } |
| 19 | + } |
| 20 | + |
| 21 | + // start is the first in the range |
| 22 | + pub fn start(&self) -> usize { |
| 23 | + self.start |
| 24 | + } |
| 25 | + |
| 26 | + // end is the exclusive end partition in the range |
| 27 | + pub fn end(&self) -> usize { |
| 28 | + self.end |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// PartitionGrouper groups a number partitions together depending on a partition_group_size. |
| 33 | +// Ex. 10 partitions with a group size of 3 will yield groups [(0..3), (3..6), (6..9), (9)]. |
| 34 | +// - A partition_group_size of 0 will panic |
| 35 | +// - Grouping 0 partitions will result an empty vec |
| 36 | +// - It's possible for bad groupings to exist. Ex. if the group size is 99 and there are 100 |
| 37 | +// partitions, then you will get unbalanced partitions [(0..99), (99)] |
| 38 | +#[derive(Debug, Copy, Clone, PartialEq)] |
| 39 | +pub struct PartitionGrouper { |
| 40 | + partition_group_size: usize, |
| 41 | +} |
| 42 | + |
| 43 | +impl PartitionGrouper { |
| 44 | + pub fn new(partition_group_size: usize) -> Self { |
| 45 | + assert!( |
| 46 | + partition_group_size > 0, |
| 47 | + "partition groups cannot be size 0" |
| 48 | + ); |
| 49 | + PartitionGrouper { |
| 50 | + partition_group_size, |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl Grouper for PartitionGrouper { |
| 56 | + // group implements the Grouper trait |
| 57 | + fn group(&self, num_partitions: usize) -> Vec<PartitionGroup> { |
| 58 | + (0..num_partitions) |
| 59 | + .step_by(self.partition_group_size) |
| 60 | + .map(|start| { |
| 61 | + let end = std::cmp::min(start + self.partition_group_size, num_partitions); |
| 62 | + PartitionGroup { start, end } |
| 63 | + }) |
| 64 | + .collect() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod tests { |
| 70 | + use super::*; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_partition_grouper_basic() { |
| 74 | + let grouper = PartitionGrouper::new(4); |
| 75 | + let groups = grouper.group(10); |
| 76 | + |
| 77 | + let expected = vec![ |
| 78 | + PartitionGroup { start: 0, end: 4 }, |
| 79 | + PartitionGroup { start: 4, end: 8 }, |
| 80 | + PartitionGroup { start: 8, end: 10 }, |
| 81 | + ]; |
| 82 | + |
| 83 | + assert_eq!(groups, expected); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_partition_grouper_uneven() { |
| 88 | + let grouper = PartitionGrouper::new(2); |
| 89 | + let groups = grouper.group(5); |
| 90 | + |
| 91 | + let expected = vec![ |
| 92 | + PartitionGroup { start: 0, end: 2 }, |
| 93 | + PartitionGroup { start: 2, end: 4 }, |
| 94 | + PartitionGroup { start: 4, end: 5 }, |
| 95 | + ]; |
| 96 | + |
| 97 | + assert_eq!(groups, expected); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + #[should_panic] |
| 102 | + fn test_invalid_group_size() { |
| 103 | + PartitionGrouper::new(0); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_num_partitions_smaller_than_group_size() { |
| 108 | + let g = PartitionGrouper::new(2); |
| 109 | + let groups = g.group(1); |
| 110 | + let expected = vec![ |
| 111 | + PartitionGroup { start: 0, end: 1 }, |
| 112 | + ]; |
| 113 | + assert_eq!(groups, expected); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_num_partitions_equal_to_group_size() { |
| 118 | + let g = PartitionGrouper::new(2); |
| 119 | + let groups = g.group(2); |
| 120 | + let expected = vec![ |
| 121 | + PartitionGroup { start: 0, end: 2 }, |
| 122 | + ]; |
| 123 | + assert_eq!(groups, expected); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn test_zero_partitions_to_group() { |
| 128 | + let g = PartitionGrouper::new(2); |
| 129 | + let groups = g.group(0); |
| 130 | + let expected = vec![]; |
| 131 | + assert_eq!(groups, expected); |
| 132 | + } |
| 133 | +} |
0 commit comments