@Test public void testMergeBars() { EquiDepthStreamHistogram histo = new EquiDepthStreamHistogram(2, 1); histo.bars.add(a_b); histo.bars.add(b_c); histo.bars.add(c_d); histo.bars.add(d_e); histo.totalCount = 20; a_b.incrementCount(3); b_c.incrementCount(2); c_d.incrementCount(10); d_e.incrementCount(5); histo.mergeBars(); assertEquals(3, histo.bars.size()); Bar mergedBar = histo.bars.get(0); assertEquals(5, mergedBar.getSize()); assertArrayEquals(bytesA, mergedBar.getLeftBoundInclusive()); assertArrayEquals(bytesC, mergedBar.getRightBoundExclusive()); histo.mergeBars(); assertEquals(2, histo.bars.size()); mergedBar = histo.bars.get(0); assertEquals(15, mergedBar.getSize()); assertArrayEquals(bytesA, mergedBar.getLeftBoundInclusive()); assertArrayEquals(bytesD, mergedBar.getRightBoundExclusive()); histo.mergeBars(); assertEquals(2, histo.bars.size()); }
@VisibleForTesting boolean mergeBars() { Preconditions.checkState(bars.size() > 1, "Need at least two bars to merge"); int currIdx = 0; Bar currBar = bars.get(currIdx); Bar nextBar = bars.get(currIdx + 1); long currMinSum = Long.MAX_VALUE; int currMinIdx = currIdx; Pair<Bar, Bar> minBars = new Pair<>(currBar, nextBar); while (nextBar != null) { long sum = currBar.getSize() + nextBar.getSize(); if (sum < currMinSum) { currMinSum = sum; minBars = new Pair<>(currBar, nextBar); currMinIdx = currIdx; } currBar = nextBar; nextBar = ++currIdx < bars.size() - 1 ? bars.get(currIdx+1) : null; } if (currMinSum >= getMaxBarSize()) { return false; } Bar leftBar = minBars.getFirst(); Bar rightBar = minBars.getSecond(); Bar newBar = new Bar(leftBar.getLeftBoundInclusive(), rightBar.getRightBoundExclusive()); if (leftBar.getSize() >= rightBar.getSize()) { newBar.incrementCount(rightBar.getCount()); newBar.addBlockedBar(new Bar(leftBar)); } else { newBar.incrementCount(leftBar.getCount()); newBar.addBlockedBar(new Bar(rightBar)); } newBar.addBlockedBars(leftBar.getBlockedBars()); newBar.addBlockedBars(rightBar.getBlockedBars()); bars.subList(currMinIdx, currMinIdx + 2).clear(); bars.add(newBar); Collections.sort(bars); if (LOGGER.isTraceEnabled()) { LOGGER.trace(String.format("Merged left=%s , right=%s , newBar=%s", leftBar, rightBar, newBar)); } return true; }
EquiDepthStreamHistogram { @VisibleForTesting boolean mergeBars() { Preconditions.checkState(bars.size() > 1, "Need at least two bars to merge"); int currIdx = 0; Bar currBar = bars.get(currIdx); Bar nextBar = bars.get(currIdx + 1); long currMinSum = Long.MAX_VALUE; int currMinIdx = currIdx; Pair<Bar, Bar> minBars = new Pair<>(currBar, nextBar); while (nextBar != null) { long sum = currBar.getSize() + nextBar.getSize(); if (sum < currMinSum) { currMinSum = sum; minBars = new Pair<>(currBar, nextBar); currMinIdx = currIdx; } currBar = nextBar; nextBar = ++currIdx < bars.size() - 1 ? bars.get(currIdx+1) : null; } if (currMinSum >= getMaxBarSize()) { return false; } Bar leftBar = minBars.getFirst(); Bar rightBar = minBars.getSecond(); Bar newBar = new Bar(leftBar.getLeftBoundInclusive(), rightBar.getRightBoundExclusive()); if (leftBar.getSize() >= rightBar.getSize()) { newBar.incrementCount(rightBar.getCount()); newBar.addBlockedBar(new Bar(leftBar)); } else { newBar.incrementCount(leftBar.getCount()); newBar.addBlockedBar(new Bar(rightBar)); } newBar.addBlockedBars(leftBar.getBlockedBars()); newBar.addBlockedBars(rightBar.getBlockedBars()); bars.subList(currMinIdx, currMinIdx + 2).clear(); bars.add(newBar); Collections.sort(bars); if (LOGGER.isTraceEnabled()) { LOGGER.trace(String.format("Merged left=%s , right=%s , newBar=%s", leftBar, rightBar, newBar)); } return true; } }
EquiDepthStreamHistogram { @VisibleForTesting boolean mergeBars() { Preconditions.checkState(bars.size() > 1, "Need at least two bars to merge"); int currIdx = 0; Bar currBar = bars.get(currIdx); Bar nextBar = bars.get(currIdx + 1); long currMinSum = Long.MAX_VALUE; int currMinIdx = currIdx; Pair<Bar, Bar> minBars = new Pair<>(currBar, nextBar); while (nextBar != null) { long sum = currBar.getSize() + nextBar.getSize(); if (sum < currMinSum) { currMinSum = sum; minBars = new Pair<>(currBar, nextBar); currMinIdx = currIdx; } currBar = nextBar; nextBar = ++currIdx < bars.size() - 1 ? bars.get(currIdx+1) : null; } if (currMinSum >= getMaxBarSize()) { return false; } Bar leftBar = minBars.getFirst(); Bar rightBar = minBars.getSecond(); Bar newBar = new Bar(leftBar.getLeftBoundInclusive(), rightBar.getRightBoundExclusive()); if (leftBar.getSize() >= rightBar.getSize()) { newBar.incrementCount(rightBar.getCount()); newBar.addBlockedBar(new Bar(leftBar)); } else { newBar.incrementCount(leftBar.getCount()); newBar.addBlockedBar(new Bar(rightBar)); } newBar.addBlockedBars(leftBar.getBlockedBars()); newBar.addBlockedBars(rightBar.getBlockedBars()); bars.subList(currMinIdx, currMinIdx + 2).clear(); bars.add(newBar); Collections.sort(bars); if (LOGGER.isTraceEnabled()) { LOGGER.trace(String.format("Merged left=%s , right=%s , newBar=%s", leftBar, rightBar, newBar)); } return true; } EquiDepthStreamHistogram(int numBuckets);  EquiDepthStreamHistogram(int numBuckets, int expansionFactor); }
EquiDepthStreamHistogram { @VisibleForTesting boolean mergeBars() { Preconditions.checkState(bars.size() > 1, "Need at least two bars to merge"); int currIdx = 0; Bar currBar = bars.get(currIdx); Bar nextBar = bars.get(currIdx + 1); long currMinSum = Long.MAX_VALUE; int currMinIdx = currIdx; Pair<Bar, Bar> minBars = new Pair<>(currBar, nextBar); while (nextBar != null) { long sum = currBar.getSize() + nextBar.getSize(); if (sum < currMinSum) { currMinSum = sum; minBars = new Pair<>(currBar, nextBar); currMinIdx = currIdx; } currBar = nextBar; nextBar = ++currIdx < bars.size() - 1 ? bars.get(currIdx+1) : null; } if (currMinSum >= getMaxBarSize()) { return false; } Bar leftBar = minBars.getFirst(); Bar rightBar = minBars.getSecond(); Bar newBar = new Bar(leftBar.getLeftBoundInclusive(), rightBar.getRightBoundExclusive()); if (leftBar.getSize() >= rightBar.getSize()) { newBar.incrementCount(rightBar.getCount()); newBar.addBlockedBar(new Bar(leftBar)); } else { newBar.incrementCount(leftBar.getCount()); newBar.addBlockedBar(new Bar(rightBar)); } newBar.addBlockedBars(leftBar.getBlockedBars()); newBar.addBlockedBars(rightBar.getBlockedBars()); bars.subList(currMinIdx, currMinIdx + 2).clear(); bars.add(newBar); Collections.sort(bars); if (LOGGER.isTraceEnabled()) { LOGGER.trace(String.format("Merged left=%s , right=%s , newBar=%s", leftBar, rightBar, newBar)); } return true; } EquiDepthStreamHistogram(int numBuckets);  EquiDepthStreamHistogram(int numBuckets, int expansionFactor); void addValue(byte[] value); List<Bucket> computeBuckets(); long getTotalCount(); }
EquiDepthStreamHistogram { @VisibleForTesting boolean mergeBars() { Preconditions.checkState(bars.size() > 1, "Need at least two bars to merge"); int currIdx = 0; Bar currBar = bars.get(currIdx); Bar nextBar = bars.get(currIdx + 1); long currMinSum = Long.MAX_VALUE; int currMinIdx = currIdx; Pair<Bar, Bar> minBars = new Pair<>(currBar, nextBar); while (nextBar != null) { long sum = currBar.getSize() + nextBar.getSize(); if (sum < currMinSum) { currMinSum = sum; minBars = new Pair<>(currBar, nextBar); currMinIdx = currIdx; } currBar = nextBar; nextBar = ++currIdx < bars.size() - 1 ? bars.get(currIdx+1) : null; } if (currMinSum >= getMaxBarSize()) { return false; } Bar leftBar = minBars.getFirst(); Bar rightBar = minBars.getSecond(); Bar newBar = new Bar(leftBar.getLeftBoundInclusive(), rightBar.getRightBoundExclusive()); if (leftBar.getSize() >= rightBar.getSize()) { newBar.incrementCount(rightBar.getCount()); newBar.addBlockedBar(new Bar(leftBar)); } else { newBar.incrementCount(leftBar.getCount()); newBar.addBlockedBar(new Bar(rightBar)); } newBar.addBlockedBars(leftBar.getBlockedBars()); newBar.addBlockedBars(rightBar.getBlockedBars()); bars.subList(currMinIdx, currMinIdx + 2).clear(); bars.add(newBar); Collections.sort(bars); if (LOGGER.isTraceEnabled()) { LOGGER.trace(String.format("Merged left=%s , right=%s , newBar=%s", leftBar, rightBar, newBar)); } return true; } EquiDepthStreamHistogram(int numBuckets);  EquiDepthStreamHistogram(int numBuckets, int expansionFactor); void addValue(byte[] value); List<Bucket> computeBuckets(); long getTotalCount();  }