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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::sync::Arc;
use std::sync::Mutex;

use crate::matrix::Matrix;
use crate::Field;

#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Error {
    AlreadySet,
    NotSquare,
}

#[derive(Debug)]
pub struct InversionTree<F: Field> {
    pub root: Mutex<InversionNode<F>>,
    total_shards: usize,
}

#[derive(Debug)]
pub struct InversionNode<F: Field> {
    pub matrix: Option<Arc<Matrix<F>>>,
    pub children: Vec<Option<InversionNode<F>>>,
}

impl<F: Field> InversionTree<F> {
    pub fn new(data_shards: usize, parity_shards: usize) -> InversionTree<F> {
        InversionTree {
            root: Mutex::new(InversionNode::new(
                Some(Arc::new(Matrix::identity(data_shards))),
                data_shards + parity_shards,
            )),
            total_shards: data_shards + parity_shards,
        }
    }

    pub fn get_inverted_matrix(&self, invalid_indices: &[usize]) -> Option<Arc<Matrix<F>>> {
        if invalid_indices.len() == 0 {
            match self.root.lock().unwrap().matrix {
                None => panic!(),
                Some(ref x) => return Some(Arc::clone(x)),
            }
        }

        self.root
            .lock()
            .unwrap()
            .get_inverted_matrix(invalid_indices, self.total_shards, 0)
    }

    pub fn insert_inverted_matrix(
        &self,
        invalid_indices: &[usize],
        matrix: &Arc<Matrix<F>>,
    ) -> Result<(), Error> {
        // If no invalid indices were given then we are done because the
        // root node is already set with the identity matrix.
        if invalid_indices.len() == 0 {
            return Err(Error::AlreadySet);
        }

        if !matrix.is_square() {
            return Err(Error::NotSquare);
        }

        // Lock the tree for writing and reading before accessing the tree.
        // Recursively create nodes for the inverted matrix in the tree until
        // we reach the node to insert the matrix to.  We start by passing in
        // 0 as the parent index as we start at the root of the tree.
        self.root.lock().unwrap().insert_inverted_matrix(
            matrix,
            invalid_indices,
            self.total_shards,
            0,
        );

        Ok(())
    }
}

impl<F: Field> InversionNode<F> {
    pub fn new(matrix: Option<Arc<Matrix<F>>>, children_count: usize) -> InversionNode<F> {
        let mut children = Vec::with_capacity(children_count);
        for _ in 0..children_count {
            children.push(None);
        }
        InversionNode { matrix, children }
    }

    fn get_child<'a>(
        &'a mut self,
        offset: usize,
        requested_index: usize,
        total_shards: usize,
    ) -> &'a mut InversionNode<F> {
        let node_index = requested_index - offset;
        {
            let node = &mut self.children[node_index];
            match *node {
                None => {
                    *node = Some(Self::new(None, total_shards - offset));
                }
                Some(_) => {}
            }
        }
        match self.children[node_index] {
            None => panic!(),
            Some(ref mut x) => x,
        }
    }

    pub fn get_inverted_matrix(
        &mut self,
        invalid_indices: &[usize],
        total_shards: usize,
        offset: usize,
    ) -> Option<Arc<Matrix<F>>> {
        if invalid_indices.len() == 0 {
            match self.matrix {
                None => None,
                Some(ref m) => Some(Arc::clone(m)),
            }
        } else {
            let requested_index = invalid_indices[0];
            let remaining_indices = &invalid_indices[1..];
            self.get_child(offset, requested_index, total_shards)
                .get_inverted_matrix(remaining_indices, total_shards, requested_index + 1)
        }
    }

    pub fn insert_inverted_matrix(
        &mut self,
        matrix: &Arc<Matrix<F>>,
        invalid_indices: &[usize],
        total_shards: usize,
        offset: usize,
    ) {
        if invalid_indices.len() == 0 {
            self.matrix = Some(Arc::clone(matrix));
        } else {
            let requested_index = invalid_indices[0];
            let remaining_indices = &invalid_indices[1..];
            self.get_child(offset, requested_index, total_shards)
                .insert_inverted_matrix(
                    matrix,
                    remaining_indices,
                    total_shards,
                    requested_index + 1,
                )
        }
    }
}

#[cfg(test)]
mod tests {
    use rand;

    use std::collections::HashMap;
    use std::sync::Arc;

    use crate::galois_8;
    use crate::inversion_tree::*;
    use crate::matrix::Matrix;

    use quickcheck::{Arbitrary, Gen, QuickCheck};

    macro_rules! matrix {
        (
            $(
                [ $( $x:expr ),+ ]
            ),*
        ) => (
            Matrix::new_with_data(vec![ $( vec![$( $x ),*] ),* ])
        );
        ($rows:expr, $cols:expr) => (Matrix::new($rows, $cols));
    }

    #[test]
    fn test_new_inversion_tree() {
        let tree: InversionTree<galois_8::Field> = InversionTree::new(3, 2);

        let children = tree.root.lock().unwrap().children.len();
        assert_eq!(5, children);

        let expect = matrix!([1, 0, 0], [0, 1, 0], [0, 0, 1]);
        assert_eq!(expect, *tree.get_inverted_matrix(&[]).unwrap());
    }

    #[test]
    fn test_get_inverted_matrix() {
        let tree: InversionTree<galois_8::Field> = InversionTree::new(3, 2);

        let matrix = &*tree.get_inverted_matrix(&[]).unwrap();

        let expect = matrix!([1, 0, 0], [0, 1, 0], [0, 0, 1]);

        assert_eq!(expect, *matrix);

        let matrix = tree.get_inverted_matrix(&[1]);
        assert_eq!(None, matrix);

        let matrix = tree.get_inverted_matrix(&[1, 2]);
        assert_eq!(None, matrix);

        let matrix = Matrix::new(3, 3);
        let matrix_copy = matrix.clone();
        tree.insert_inverted_matrix(&[1], &Arc::new(matrix))
            .unwrap();

        let cached_matrix = tree.get_inverted_matrix(&[1]).unwrap();
        assert_eq!(matrix_copy, *cached_matrix);
    }

    #[test]
    fn test_insert_inverted_matrix() {
        let tree: InversionTree<galois_8::Field> = InversionTree::new(3, 2);

        let matrix = Matrix::new(3, 3);
        let matrix_copy = matrix.clone();

        tree.insert_inverted_matrix(&[1], &Arc::new(matrix))
            .unwrap();
        tree.insert_inverted_matrix(&[], &Arc::new(matrix_copy))
            .unwrap_err();

        let matrix = Matrix::new(3, 2);
        tree.insert_inverted_matrix(&[2], &Arc::new(matrix))
            .unwrap_err();

        let matrix = Matrix::new(3, 3);
        tree.insert_inverted_matrix(&[0, 1], &Arc::new(matrix))
            .unwrap();
    }

    #[test]
    fn test_double_insert_inverted_matrix() {
        let tree: InversionTree<galois_8::Field> = InversionTree::new(3, 2);

        let matrix1 = Matrix::make_random(3);
        let matrix2 = Matrix::make_random(3);

        let matrix_copy1 = matrix1.clone();
        let matrix_copy2 = matrix2.clone();

        tree.insert_inverted_matrix(&[1], &Arc::new(matrix_copy1))
            .unwrap();
        tree.insert_inverted_matrix(&[1], &Arc::new(matrix_copy2))
            .unwrap();

        let cached_matrix = tree.get_inverted_matrix(&[1]).unwrap();
        assert_eq!(matrix2, *cached_matrix);
    }

    #[test]
    fn test_extended_inverted_matrix() {
        let tree: InversionTree<galois_8::Field> = InversionTree::new(10, 3);
        let matrix = Matrix::new(10, 10);
        let matrix_copy = matrix.clone();
        let matrix2 = matrix!(
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        );
        let matrix2_copy = matrix2.clone();
        let matrix3 = matrix!(
            [9, 1, 2, 3, 4, 5, 6, 7, 8, 0],
            [9, 1, 2, 3, 4, 5, 6, 7, 8, 0],
            [9, 1, 2, 3, 4, 5, 6, 7, 8, 0],
            [9, 1, 2, 3, 4, 5, 6, 7, 8, 0],
            [9, 1, 2, 3, 4, 5, 6, 7, 8, 0],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 0],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            [1, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        );
        let matrix3_copy = matrix3.clone();

        tree.insert_inverted_matrix(&[1, 2], &Arc::new(matrix))
            .unwrap();

        let result = tree.get_inverted_matrix(&[1, 2]).unwrap();
        assert_eq!(matrix_copy, *result);

        tree.insert_inverted_matrix(&[1, 2, 5, 12], &Arc::new(matrix2))
            .unwrap();
        let result = tree.get_inverted_matrix(&[1, 2, 5, 12]).unwrap();
        assert_eq!(matrix2_copy, *result);

        tree.insert_inverted_matrix(&[0, 3, 4, 11], &Arc::new(matrix3))
            .unwrap();
        let result = tree.get_inverted_matrix(&[0, 3, 4, 11]).unwrap();
        assert_eq!(matrix3_copy, *result);
    }

    fn make_random_invalid_indices(data_shards: usize, parity_shards: usize) -> Vec<usize> {
        let mut invalid_count = 0;
        let mut res = Vec::new();
        for i in 0..data_shards + parity_shards {
            if rand::random::<bool>() && invalid_count < parity_shards {
                res.push(i);
                invalid_count += 1;
            }
        }
        res
    }

    #[derive(Debug, Clone)]
    struct QCTreeTestParam {
        data_shards: usize,
        parity_shards: usize,
        matrix_count: usize,
        iter_order: Vec<usize>,
        read_count: usize,
    }

    impl Arbitrary for QCTreeTestParam {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let size = g.size();

            let matrix_count = 5 + size % 100;

            let mut iter_order = Vec::with_capacity(matrix_count);
            for _ in 0..matrix_count {
                iter_order.push(rand::random::<usize>());
            }

            QCTreeTestParam {
                data_shards: 1 + size % 50,
                parity_shards: 1 + size % 50,
                matrix_count,
                iter_order,
                read_count: 2 + size % 10,
            }
        }
    }

    #[test]
    fn qc_tree_same_as_hash_map() {
        QuickCheck::new()
            .min_tests_passed(10_000)
            .tests(11_000)
            .max_tests(100_000)
            .quickcheck(qc_tree_same_as_hash_map_prop as fn(QCTreeTestParam) -> bool);
    }

    // inversion tree is functionally the same as a map
    // but more efficient
    fn qc_tree_same_as_hash_map_prop(param: QCTreeTestParam) -> bool {
        let tree: InversionTree<galois_8::Field> =
            InversionTree::new(param.data_shards, param.parity_shards);
        let mut map = HashMap::with_capacity(param.matrix_count);

        let mut invalid_indices_set = Vec::with_capacity(param.matrix_count);

        for _ in 0..param.matrix_count {
            let invalid_indices =
                make_random_invalid_indices(param.data_shards, param.parity_shards);
            let matrix = Matrix::make_random(param.data_shards);
            match tree.insert_inverted_matrix(&invalid_indices, &Arc::new(matrix.clone())) {
                Ok(()) => {
                    map.insert(invalid_indices.clone(), matrix);
                    invalid_indices_set.push(invalid_indices);
                }
                Err(Error::AlreadySet) => {}
                Err(Error::NotSquare) => panic!(),
            }
        }

        for _ in 0..param.read_count {
            // iterate according to the provided order
            if invalid_indices_set.len() > 0 {
                for i in param.iter_order.iter() {
                    let i = i % invalid_indices_set.len();

                    let invalid_indices = &invalid_indices_set[i];

                    let matrix_in_tree = tree.get_inverted_matrix(invalid_indices).unwrap();
                    let matrix_in_map = map.get(invalid_indices).unwrap();
                    if matrix_in_tree.as_ref() != matrix_in_map {
                        return false;
                    }
                }
            }

            // iterate through the insertion order
            for ref invalid_indices in invalid_indices_set.iter() {
                let matrix_in_tree = tree.get_inverted_matrix(invalid_indices).unwrap();
                let matrix_in_map = map.get(*invalid_indices).unwrap();
                if matrix_in_tree.as_ref() != matrix_in_map {
                    return false;
                }
            }

            // iterate through the map's order
            for (ref invalid_indices, ref matrix_in_map) in map.iter() {
                let matrix_in_tree = tree.get_inverted_matrix(invalid_indices).unwrap();
                if matrix_in_tree.as_ref() != *matrix_in_map {
                    return false;
                }
            }
        }

        true
    }
}