1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use std::ops::Add;
use std::num::Wrapping;

use num::{NumCast, Bounded, One};

const TRANSACTION_ID_PREALLOC_LEN: usize = 2048;

/// Generates blocks of transaction ids where each block is shuffled before being used.
///
/// Works on all primitive numbers as well as non pot numbers. For numbers whose max value
/// is smaller than the pre allocation length, it is possible for duplicates of those numbers
/// to appear in the same block and, by extension, one after the other back to back.
pub struct TIDGenerator<T> {
    next_alloc: T,
    curr_index: usize,
    trans_ids: [T; TRANSACTION_ID_PREALLOC_LEN],
}

impl<T> TIDGenerator<T>
    where T: Copy + Bounded + NumCast + One + Eq,
          Wrapping<T>: Add<Wrapping<T>, Output = Wrapping<T>>
{
    /// Create a new TIDGenerator.
    pub fn new() -> TIDGenerator<T> {
        let (next_alloc, mut transaction_ids) = generate_tids(T::min_value());

        ::fisher_shuffle(&mut transaction_ids);

        TIDGenerator {
            next_alloc: next_alloc,
            curr_index: 0,
            trans_ids: transaction_ids,
        }
    }

    /// Grab the next transaction id that was generated.
    pub fn generate(&mut self) -> T {
        let opt_transaction_id = self.trans_ids.get(self.curr_index).map(|t| *t);

        if let Some(transaction_id) = opt_transaction_id {
            self.curr_index += 1;

            transaction_id
        } else {
            let (next_alloc, mut transaction_ids) = generate_tids(self.next_alloc);

            ::fisher_shuffle(&mut transaction_ids);

            self.next_alloc = next_alloc;
            self.trans_ids = transaction_ids;
            self.curr_index = 0;

            self.generate()
        }
    }
}

/// Generate a new block of transaction ids starting from the current allocation marker.
fn generate_tids<T>(curr_alloc: T) -> (T, [T; TRANSACTION_ID_PREALLOC_LEN])
    where T: Copy + Bounded + NumCast + One + Eq,
          Wrapping<T>: Add<Wrapping<T>, Output = Wrapping<T>>
{
    let max_trans_id = T::max_value();

    let (next_alloc_start, next_alloc_end) = match NumCast::from(TRANSACTION_ID_PREALLOC_LEN) {
        Some(alloc_len) => {
            if curr_alloc == max_trans_id {
                (T::min_value(), alloc_len)
            } else {
                (curr_alloc, (Wrapping(curr_alloc) + Wrapping(alloc_len)).0)
            }
        }
        None => {
            // If the cast failed, we presume our type has a smaller max value than the pre
            // allocation amount. In that case, just repeat the values and return the same
            // next alloc since we are really just wrapping around multiple time to fill in
            // the transaction ids (not ideal but if the client wants this...).
            (curr_alloc, curr_alloc)
        }
    };
    let mut transaction_ids = [T::one(); TRANSACTION_ID_PREALLOC_LEN];

    let mut next_tid = next_alloc_start;
    for slot in transaction_ids.iter_mut() {
        *slot = next_tid;

        next_tid = (Wrapping(next_tid) + Wrapping(T::one())).0;
    }

    (next_alloc_end, transaction_ids)
}

#[cfg(test)]
mod tests {
    use super::TIDGenerator;

    #[test]
    fn positive_single_prealloc_u8_overflow() {
        let u8_num_values = 2u32.pow(0u8.count_zeros()) as usize;
        let duplicates_to_find = super::TRANSACTION_ID_PREALLOC_LEN / u8_num_values;

        let mut generator = TIDGenerator::<u8>::new();
        let mut tid_count = vec![0u8; u8_num_values];

        // Loop around the pre allocation length once
        for tid in (0..super::TRANSACTION_ID_PREALLOC_LEN).map(|_| generator.generate()) {
            let index = tid as usize;

            tid_count[index] += 1;
        }

        for count in tid_count.iter() {
            assert_eq!(*count, duplicates_to_find as u8);
        }
    }

    #[test]
    fn positive_multiple_prealloc_u8_overflow() {
        let u8_num_values = 2u32.pow(0u8.count_zeros()) as usize;
        let duplicates_to_find = (super::TRANSACTION_ID_PREALLOC_LEN / u8_num_values) * 2;

        let mut generator = TIDGenerator::<u8>::new();
        let mut tid_count = vec![0u8; u8_num_values];

        // Loop around the pre allocation length once
        for tid in (0..(super::TRANSACTION_ID_PREALLOC_LEN * 2)).map(|_| generator.generate()) {
            let index = tid as usize;

            tid_count[index] += 1;
        }

        for count in tid_count.iter() {
            assert_eq!(*count, duplicates_to_find as u8);
        }
    }

    #[test]
    fn positive_single_prealloc_i8_overflow() {
        let i8_num_values = 2u32.pow(0i8.count_zeros()) as usize;
        let duplicates_to_find = super::TRANSACTION_ID_PREALLOC_LEN / i8_num_values;

        let mut generator = TIDGenerator::<u8>::new();
        let mut tid_count = vec![0i8; i8_num_values];

        // Loop around the pre allocation length once
        for tid in (0..super::TRANSACTION_ID_PREALLOC_LEN).map(|_| generator.generate()) {
            let index = tid as usize;

            tid_count[index] += 1;
        }

        for count in tid_count.iter() {
            assert_eq!(*count, duplicates_to_find as i8);
        }
    }

    #[test]
    fn positive_multiple_prealloc_i8_overflow() {
        let i8_num_values = 2u32.pow(0i8.count_zeros()) as usize;
        let duplicates_to_find = (super::TRANSACTION_ID_PREALLOC_LEN / i8_num_values) * 2;

        let mut generator = TIDGenerator::<u8>::new();
        let mut tid_count = vec![0i8; i8_num_values];

        // Loop around the pre allocation length once
        for tid in (0..(super::TRANSACTION_ID_PREALLOC_LEN * 2)).map(|_| generator.generate()) {
            let index = tid as usize;

            tid_count[index] += 1;
        }

        for count in tid_count.iter() {
            assert_eq!(*count, duplicates_to_find as i8);
        }
    }

    #[test]
    fn positive_single_prealloc_u32_no_overflow() {
        let mut generator = TIDGenerator::<u32>::new();
        let mut tid_count = [0u8; super::TRANSACTION_ID_PREALLOC_LEN];

        for tid in (0..super::TRANSACTION_ID_PREALLOC_LEN).map(|_| generator.generate()) {
            let index = tid as usize;

            tid_count[index] += 1;
        }

        for count in tid_count.iter() {
            assert_eq!(*count, 1);
        }
    }

    #[test]
    fn positive_multiple_prealloc_u32_no_overflow() {
        let mut generator = TIDGenerator::<u32>::new();
        let mut tid_count = [0u8; (super::TRANSACTION_ID_PREALLOC_LEN * 2)];

        for tid in (0..(super::TRANSACTION_ID_PREALLOC_LEN * 2)).map(|_| generator.generate()) {
            let index = tid as usize;

            tid_count[index] += 1;
        }

        for count in tid_count.iter() {
            assert_eq!(*count, 1);
        }
    }

    #[test]
    fn positive_multiple_prealloc_u32_overflow() {
        // Subtract 1 because this isnt the number of u32 values (single bit #32 active)
        let last_block_alloc = u32::max_value() - (super::TRANSACTION_ID_PREALLOC_LEN - 1) as u32;

        // Verify the last block
        let (next_alloc, ids) = super::generate_tids(last_block_alloc);
        let mut id_filter = [0u8; super::TRANSACTION_ID_PREALLOC_LEN];

        assert_eq!(next_alloc, 0);

        for &id in ids.iter() {
            let index = (id - last_block_alloc) as usize;

            id_filter[index] += 1;
        }
        assert!(id_filter.iter().all(|&found| found == 1));


        // Verify the overflow allocation
        let (o_next_alloc, o_ids) = super::generate_tids(next_alloc);
        let mut o_id_filter = [0u8; super::TRANSACTION_ID_PREALLOC_LEN];

        assert_eq!(o_next_alloc, super::TRANSACTION_ID_PREALLOC_LEN as u32);

        for &id in o_ids.iter() {
            let index = id as usize;

            o_id_filter[index] += 1;
        }
        assert!(o_id_filter.iter().all(|&found| found == 1));
    }
}