private <T> Put addSlices (Put existingPut, TileSerializer<List<T>> serializer, TileData<List<T>> tile) throws IOException { // Figure out into how many slices to divide the data int slices = numSlices(tile); // Store the whole thing pyramidded. int slicesPerWrite = 1; while (slicesPerWrite < slices) { // Divide the tile into slices, storing each of them individually in their own column for (int startSlice = 0; startSlice < slices; startSlice = startSlice + slicesPerWrite) { int endSlice = startSlice + slicesPerWrite - 1; TileData<List<T>> slice = new DenseTileMultiSliceView<T>(tile, startSlice, endSlice).harden(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(slice, baos); existingPut = addToPut(existingPut, rowIdFromTileIndex(tile.getDefinition()), getSliceColumn(startSlice, endSlice), baos.toByteArray()); } // If not pyramidding, bail out after our first time through. if (_doPyramidding) slicesPerWrite = slicesPerWrite * 2; else slicesPerWrite = slices; } return existingPut; }
public TileData<List<T>> harden () { TileIndex index = getDefinition(); if (isDense()) { DenseTileData<List<T>> hardened = new DenseTileData<>(index); hardened.setDefaultValue(_base.getDefaultValue()); for (int x=0; x<index.getXBins(); ++x) { for (int y=0; y<index.getYBins(); ++y) { hardened.setBin(x, y, getBin(x, y)); } } return hardened; } else { SparseTileData<List<T>> hardened = new SparseTileData<>(index, _base.getDefaultValue()); for (int x=0; x<index.getXBins(); ++x) { for (int y=0; y<index.getYBins(); ++y) { List<T> value = getBin(x, y); if (null != value) { hardened.setBin(x, y, getBin(x, y)); } } } return hardened; } }
private boolean isDense () { TileIndex index = getDefinition(); int nullBins = 0; int bins = 0; for (int x=0; x<index.getXBins(); ++x) { for (int y=0; y<index.getYBins(); ++y) { if (binNullForUs(_base.getBin(x, y))) ++nullBins; ++bins; } } return nullBins*2 < bins; }
@Test public void testMultiSliceView () { int start = 1, end = 2; // since we modify the tile, we won't use a static class tile TileData<List<Double>> sourceListTile = new DenseTileData<>(new TileIndex(1, 1, 1, 2, 2), Arrays.asList(Arrays.asList( 1.0, 2.0, 3.0, 4.0), Arrays.asList( 5.0, 6.0, 7.0, 8.0), Arrays.asList( 9.0, 10.0, 11.0, 12.0), Arrays.asList(13.0, 14.0, 15.0, 16.0))); DenseTileMultiSliceView<Double> underTest = new DenseTileMultiSliceView<Double>(sourceListTile, start, end); Assert.assertEquals(1, underTest.getDefinition().getLevel()); Assert.assertEquals(1, underTest.getDefinition().getX()); Assert.assertEquals(1, underTest.getDefinition().getY()); Assert.assertEquals(2, underTest.getDefinition().getXBins()); Assert.assertEquals(2, underTest.getDefinition().getYBins()); for (int y=0; y<underTest.getDefinition().getYBins(); y++) { for (int x=0; x<underTest.getDefinition().getXBins(); x++) { List<Double> bin = underTest.getBin(x,y); List<Double> sourceBin = sourceListTile.getBin(x, y); Assert.assertEquals(end-start+1, bin.size()); for (int i=start; i <= end; ++i) { Assert.assertEquals(sourceBin.get(i), bin.get(i-start)); } } } }
@Test public void testUnmodifiableBinReplacement () { List<Integer> binVal = new ArrayList<>(); binVal.add(3); binVal.add(4); binVal.add(5); TileData<List<Integer>> base = new DenseTileData<List<Integer>>(new TileIndex(0, 0, 0, 1, 1)); base.setBin(0, 0, Collections.unmodifiableList(binVal)); TileData<List<Integer>> slice = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(2, 0)); slice.setBin(0, 0, Arrays.asList(6, 2)); Assert.assertEquals(2, base.getBin(0, 0).get(0).intValue()); Assert.assertEquals(4, base.getBin(0, 0).get(1).intValue()); Assert.assertEquals(6, base.getBin(0, 0).get(2).intValue()); Assert.assertEquals(3, binVal.get(0).intValue()); Assert.assertEquals(4, binVal.get(1).intValue()); Assert.assertEquals(5, binVal.get(2).intValue()); }
@Override public List<T> getBin(int x, int y) { List<T> origValue = _base.getBin(x, y); if (binNullForUs(origValue)) return null; List<T> result = new ArrayList<>(_slices.size()); for (int slice: _slices) { if (origValue.size() <= slice) result.add(null); else result.add(origValue.get(slice)); } return result; }
@Test public void testModifiableBinReplacement () { List<Integer> binVal = new ArrayList<>(); binVal.add(3); binVal.add(4); binVal.add(5); TileData<List<Integer>> base = new DenseTileData<List<Integer>>(new TileIndex(0, 0, 0, 1, 1)); base.setBin(0, 0, binVal); TileData<List<Integer>> slice = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(2, 0)); slice.setBin(0, 0, Arrays.asList(6, 2)); Assert.assertEquals(2, base.getBin(0, 0).get(0).intValue()); Assert.assertEquals(4, base.getBin(0, 0).get(1).intValue()); Assert.assertEquals(6, base.getBin(0, 0).get(2).intValue()); Assert.assertEquals(2, binVal.get(0).intValue()); Assert.assertEquals(4, binVal.get(1).intValue()); Assert.assertEquals(6, binVal.get(2).intValue()); }
@Test public void testBinRetrieval () { TileData<List<Integer>> base = new DenseTileData(new TileIndex(0, 0, 0, 1, 1)); base.setBin(0, 0, Arrays.asList(-0, -1, -2, -3, -4, -5, -6, -7)); TileData<List<Integer>> slice1 = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(1, 3, 5, 7)); TileData<List<Integer>> slice2 = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(6, 4, 2, 0)); List<Integer> b1 = slice1.getBin(0, 0); Assert.assertEquals(4, b1.size()); Assert.assertEquals(-1, b1.get(0).intValue()); Assert.assertEquals(-3, b1.get(1).intValue()); Assert.assertEquals(-5, b1.get(2).intValue()); Assert.assertEquals(-7, b1.get(3).intValue()); List<Integer> b2 = slice2.getBin(0, 0); Assert.assertEquals(4, b2.size()); Assert.assertEquals(-6, b2.get(0).intValue()); Assert.assertEquals(-4, b2.get(1).intValue()); Assert.assertEquals(-2, b2.get(2).intValue()); Assert.assertEquals(-0, b2.get(3).intValue()); } }
@Test public void testNullBinReplacement () { TileData<List<Integer>> base = new DenseTileData<List<Integer>>(new TileIndex(0, 0, 0, 1, 1), (List<Integer>) null); TileData<List<Integer>> slice = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(2, 0)); slice.setBin(0, 0, Arrays.asList(4, 2)); Assert.assertEquals(2, base.getBin(0, 0).get(0).intValue()); Assert.assertNull( base.getBin(0, 0).get(1)); Assert.assertEquals(4, base.getBin(0, 0).get(2).intValue()); } @Test
TileData<List<Integer>> inputSlice = new DenseTileMultiSliceView<Integer>(inputTile, i, i); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(inputSlice, baos);