This is an automated email from the ASF dual-hosted git repository. leerho pushed a commit to branch removed_duplicate_req_sorted_view in repository https://gitbox.apache.org/repos/asf/datasketches-java.git
commit 0b04daf0f5aaceee114d7d66002f19191268b957 Author: Lee Rhodes <[email protected]> AuthorDate: Fri Apr 5 11:49:25 2024 -0700 REQ sketch SortedView now uses the same SV as KllFloats. --- .../org/apache/datasketches/req/ReqSketch.java | 102 +++++++++- .../datasketches/req/ReqSketchSortedView.java | 211 --------------------- .../org/apache/datasketches/common/TestUtil.java | 6 +- .../quantilescommon/CrossCheckQuantilesTest.java | 69 ++----- .../quantilescommon/ReflectUtilityTest.java | 170 ----------------- .../org/apache/datasketches/req/ReqSketchTest.java | 6 +- 6 files changed, 114 insertions(+), 450 deletions(-) diff --git a/src/main/java/org/apache/datasketches/req/ReqSketch.java b/src/main/java/org/apache/datasketches/req/ReqSketch.java index 7e19be1d..436e81f5 100644 --- a/src/main/java/org/apache/datasketches/req/ReqSketch.java +++ b/src/main/java/org/apache/datasketches/req/ReqSketch.java @@ -27,7 +27,7 @@ import java.util.Random; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; -import org.apache.datasketches.quantilescommon.FloatsSortedView; +import org.apache.datasketches.quantilescommon.FloatsSketchSortedView; import org.apache.datasketches.quantilescommon.QuantileSearchCriteria; import org.apache.datasketches.quantilescommon.QuantilesAPI; import org.apache.datasketches.quantilescommon.QuantilesFloatsSketchIterator; @@ -102,7 +102,7 @@ public final class ReqSketch extends BaseReqSketch { private int retItems = 0; //number of retained items in the sketch private int maxNomSize = 0; //sum of nominal capacities of all compactors //Objects - private ReqSketchSortedView reqSV = null; + private FloatsSketchSortedView reqSV = null; private List<ReqCompactor> compactors = new ArrayList<>(); private ReqDebug reqDebug = null; //user config, default: null, can be set after construction. @@ -362,12 +362,6 @@ public final class ReqSketch extends BaseReqSketch { return ReqSerDe.getSerBytes(this, serDeFormat); } - @Override - public FloatsSortedView getSortedView() { - refreshSortedView(); - return reqSV; - } - @Override public boolean isEmpty() { return totalN == 0; @@ -562,8 +556,96 @@ public final class ReqSketch extends BaseReqSketch { if (reqDebug != null) { reqDebug.emitNewCompactor(lgWeight); } } - private final void refreshSortedView() { - reqSV = (reqSV == null) ? new ReqSketchSortedView(this) : reqSV; + // SORTED VIEW + + @Override + public FloatsSketchSortedView getSortedView() { + refreshSortedView(); + return reqSV; + } + + private final FloatsSketchSortedView refreshSortedView() { + if (reqSV == null) { + final CreateSortedView csv = new CreateSortedView(); + reqSV = csv.getSV(); + } + return reqSV; } + private final class CreateSortedView { + float[] quantiles; + long[] cumWeights; + + FloatsSketchSortedView getSV() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } + //build the SV arrays + final List<ReqCompactor> compactors = getCompactors(); + final int numComp = compactors.size(); + final int totalQuantiles = getNumRetained(); + quantiles = new float[totalQuantiles]; //could have zero entries + cumWeights = new long[totalQuantiles]; + int count = 0; + for (int i = 0; i < numComp; i++) { + final ReqCompactor c = compactors.get(i); + final FloatBuffer bufIn = c.getBuffer(); + final long bufWeight = 1 << c.getLgWeight(); + final int bufInLen = bufIn.getCount(); + mergeSortIn(bufIn, bufWeight, count, getHighRankAccuracyMode()); + count += bufInLen; + } + createCumulativeNativeRanks(); + return new FloatsSketchSortedView( + quantiles, cumWeights, getN(), getMaxItem(), getMinItem()); + } + + /** + * Specially modified version of FloatBuffer.mergeSortIn(). Here spaceAtBottom is always false and + * the ultimate array size has already been set. However, this must simultaneously deal with + * sorting the base FloatBuffer as well. + * + * @param bufIn given FloatBuffer. If not sorted it will be sorted here. + * @param bufWeight associated weight of input FloatBuffer + * @param count tracks number of items inserted into the class arrays + */ + private void mergeSortIn(final FloatBuffer bufIn, final long bufWeight, final int count, final boolean hra) { + if (!bufIn.isSorted()) { bufIn.sort(); } + final float[] arrIn = bufIn.getArray(); //may be larger than its item count. + final int bufInLen = bufIn.getCount(); + final int totLen = count + bufInLen; + int i = count - 1; + int j = bufInLen - 1; + int h = hra ? bufIn.getCapacity() - 1 : bufInLen - 1; + for (int k = totLen; k-- > 0; ) { + if (i >= 0 && j >= 0) { //both valid + if (quantiles[i] >= arrIn[h]) { + quantiles[k] = quantiles[i]; + cumWeights[k] = cumWeights[i--]; //not yet natRanks, just individual wts + } else { + quantiles[k] = arrIn[h--]; j--; + cumWeights[k] = bufWeight; + } + } else if (i >= 0) { //i is valid + quantiles[k] = quantiles[i]; + cumWeights[k] = cumWeights[i--]; + } else if (j >= 0) { //j is valid + quantiles[k] = arrIn[h--]; j--; + cumWeights[k] = bufWeight; + } else { + break; + } + } + } + + private void createCumulativeNativeRanks() { + final int len = quantiles.length; + for (int i = 1; i < len; i++) { + cumWeights[i] += cumWeights[i - 1]; + } + if (totalN > 0) { + assert cumWeights[len - 1] == totalN; + } + } + + } //End CreateSortedView + } diff --git a/src/main/java/org/apache/datasketches/req/ReqSketchSortedView.java b/src/main/java/org/apache/datasketches/req/ReqSketchSortedView.java deleted file mode 100644 index 7f97a431..00000000 --- a/src/main/java/org/apache/datasketches/req/ReqSketchSortedView.java +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.datasketches.req; - -import static org.apache.datasketches.quantilescommon.QuantileSearchCriteria.INCLUSIVE; -import static org.apache.datasketches.quantilescommon.QuantilesAPI.EMPTY_MSG; -import static org.apache.datasketches.quantilescommon.QuantilesUtil.getNaturalRank; - -import java.util.List; - -import org.apache.datasketches.common.SketchesArgumentException; -import org.apache.datasketches.quantilescommon.FloatsSortedView; -import org.apache.datasketches.quantilescommon.FloatsSortedViewIterator; -import org.apache.datasketches.quantilescommon.InequalitySearch; -import org.apache.datasketches.quantilescommon.QuantileSearchCriteria; -import org.apache.datasketches.quantilescommon.QuantilesAPI; -import org.apache.datasketches.quantilescommon.QuantilesUtil; - -/** - * The SortedView of the ReqSketch. - * @author Alexander Saydakov - * @author Lee Rhodes - */ -public final class ReqSketchSortedView implements FloatsSortedView { - private float[] quantiles; - private long[] cumWeights; //comes in as individual weights, converted to cumulative natural weights - private final long totalN; - private final float maxItem; - private final float minItem; - - /** - * Construct from elements for testing. - * @param quantiles sorted array of quantiles - * @param cumWeights sorted, monotonically increasing cumulative weights. - * @param totalN the total number of items presented to the sketch. - */ - ReqSketchSortedView(final float[] quantiles, final long[] cumWeights, final long totalN, - final float maxItem, final float minItem) { - this.quantiles = quantiles; - this.cumWeights = cumWeights; - this.totalN = totalN; - this.maxItem = maxItem; - this.minItem = minItem; - } - - /** - * Constructs this Sorted View given the sketch - * @param sketch the given ReqSketch - */ - public ReqSketchSortedView(final ReqSketch sketch) { - if (sketch.isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } - this.totalN = sketch.getN(); - this.maxItem = sketch.getMaxItem(); - this.minItem = sketch.getMinItem(); - buildSortedViewArrays(sketch); - } - - //end of constructors - - @Override - public long[] getCumulativeWeights() { - return cumWeights.clone(); - } - - @Override - public float getMaxItem() { - return maxItem; - } - - @Override - public float getMinItem() { - return minItem; - } - - @Override - public long getN() { - return totalN; - } - - @Override - public int getNumRetained() { - return quantiles.length; - } - - @Override - public float getQuantile(final double rank, final QuantileSearchCriteria searchCrit) { - if (isEmpty()) { throw new IllegalArgumentException(QuantilesAPI.EMPTY_MSG); } - QuantilesUtil.checkNormalizedRankBounds(rank); - final int len = cumWeights.length; - final double naturalRank = getNaturalRank(rank, totalN, searchCrit); - final InequalitySearch crit = (searchCrit == INCLUSIVE) ? InequalitySearch.GE : InequalitySearch.GT; - final int index = InequalitySearch.find(cumWeights, 0, len - 1, naturalRank, crit); - if (index == -1) { - return quantiles[len - 1]; ///EXCLUSIVE (GT) case: normRank == 1.0; - } - return quantiles[index]; - } - - @Override - public float[] getQuantiles() { - return quantiles.clone(); - } - - @Override - public double getRank(final float quantile, final QuantileSearchCriteria searchCrit) { - if (isEmpty()) { throw new IllegalArgumentException(QuantilesAPI.EMPTY_MSG); } - final int len = quantiles.length; - final InequalitySearch crit = (searchCrit == INCLUSIVE) ? InequalitySearch.LE : InequalitySearch.LT; - final int index = InequalitySearch.find(quantiles, 0, len - 1, quantile, crit); - if (index == -1) { - return 0; //EXCLUSIVE (LT) case: quantile <= minQuantile; INCLUSIVE (LE) case: quantile < minQuantile - } - return (double)cumWeights[index] / totalN; - } - - @Override - public boolean isEmpty() { - return totalN == 0; - } - - @Override - public FloatsSortedViewIterator iterator() { - return new FloatsSortedViewIterator(quantiles, cumWeights); - } - - //restricted methods - - private void buildSortedViewArrays(final ReqSketch sk) { - final List<ReqCompactor> compactors = sk.getCompactors(); - final int numComp = compactors.size(); - final int totalQuantiles = sk.getNumRetained(); - quantiles = new float[totalQuantiles]; //could have zero entries - cumWeights = new long[totalQuantiles]; - int count = 0; - for (int i = 0; i < numComp; i++) { - final ReqCompactor c = compactors.get(i); - final FloatBuffer bufIn = c.getBuffer(); - final long bufWeight = 1 << c.getLgWeight(); - final int bufInLen = bufIn.getCount(); - mergeSortIn(bufIn, bufWeight, count, sk.getHighRankAccuracyMode()); - count += bufInLen; - } - createCumulativeNativeRanks(); - } - - /** - * Specially modified version of FloatBuffer.mergeSortIn(). Here spaceAtBottom is always false and - * the ultimate array size has already been set. However, this must simultaneously deal with - * sorting the base FloatBuffer as well. - * - * @param bufIn given FloatBuffer. If not sorted it will be sorted here. - * @param bufWeight associated weight of input FloatBuffer - * @param count tracks number of items inserted into the class arrays - */ - private void mergeSortIn(final FloatBuffer bufIn, final long bufWeight, final int count, final boolean hra) { - if (!bufIn.isSorted()) { bufIn.sort(); } - final float[] arrIn = bufIn.getArray(); //may be larger than its item count. - final int bufInLen = bufIn.getCount(); - final int totLen = count + bufInLen; - int i = count - 1; - int j = bufInLen - 1; - int h = hra ? bufIn.getCapacity() - 1 : bufInLen - 1; - for (int k = totLen; k-- > 0; ) { - if (i >= 0 && j >= 0) { //both valid - if (quantiles[i] >= arrIn[h]) { - quantiles[k] = quantiles[i]; - cumWeights[k] = cumWeights[i--]; //not yet natRanks, just individual wts - } else { - quantiles[k] = arrIn[h--]; j--; - cumWeights[k] = bufWeight; - } - } else if (i >= 0) { //i is valid - quantiles[k] = quantiles[i]; - cumWeights[k] = cumWeights[i--]; - } else if (j >= 0) { //j is valid - quantiles[k] = arrIn[h--]; j--; - cumWeights[k] = bufWeight; - } else { - break; - } - } - } - - private void createCumulativeNativeRanks() { - final int len = quantiles.length; - for (int i = 1; i < len; i++) { - cumWeights[i] += cumWeights[i - 1]; - } - if (totalN > 0) { - assert cumWeights[len - 1] == totalN; - } - } - -} diff --git a/src/test/java/org/apache/datasketches/common/TestUtil.java b/src/test/java/org/apache/datasketches/common/TestUtil.java index 7aab8179..0d6df94d 100644 --- a/src/test/java/org/apache/datasketches/common/TestUtil.java +++ b/src/test/java/org/apache/datasketches/common/TestUtil.java @@ -82,7 +82,7 @@ public final class TestUtil { File file; file = createTempFile(slashName); if (url.getProtocol().equals("jar")) { //definitely a jar - try (final InputStream input = Util.class.getResourceAsStream(slashName); + try (final InputStream input = TestUtil.class.getResourceAsStream(slashName); final OutputStream out = new FileOutputStream(file)) { Objects.requireNonNull(input, "InputStream is null."); int numRead = 0; @@ -98,6 +98,7 @@ public final class TestUtil { if (!file.setWritable(false, false)) { throw new IllegalStateException("Failed to set everyone 'Not Writable' on file"); } + System.out.println("HERE1"); return file; } @@ -114,7 +115,7 @@ public final class TestUtil { Objects.requireNonNull(url, "resource " + slashName + " returns null URL."); final byte[] out; if (url.getProtocol().equals("jar")) { //definitely a jar - try (final InputStream input = Util.class.getResourceAsStream(slashName)) { + try (final InputStream input = TestUtil.class.getResourceAsStream(slashName)) { out = readAllBytesFromInputStream(input); } catch (final IOException e) { throw new RuntimeException(e); } } else { //protocol says resource is not a jar, must be a file @@ -122,6 +123,7 @@ public final class TestUtil { out = Files.readAllBytes(Paths.get(getResourcePath(url))); } catch (final IOException e) { throw new RuntimeException(e); } } + System.out.println("HERE2"); return out; } diff --git a/src/test/java/org/apache/datasketches/quantilescommon/CrossCheckQuantilesTest.java b/src/test/java/org/apache/datasketches/quantilescommon/CrossCheckQuantilesTest.java index 7e3b071a..dd07ae60 100644 --- a/src/test/java/org/apache/datasketches/quantilescommon/CrossCheckQuantilesTest.java +++ b/src/test/java/org/apache/datasketches/quantilescommon/CrossCheckQuantilesTest.java @@ -29,9 +29,7 @@ import static org.apache.datasketches.quantilescommon.LinearRanksAndQuantiles.ge import static org.apache.datasketches.quantilescommon.LinearRanksAndQuantiles.getTrueItemRank; import static org.apache.datasketches.quantilescommon.QuantileSearchCriteria.EXCLUSIVE; import static org.apache.datasketches.quantilescommon.QuantileSearchCriteria.INCLUSIVE; -import static org.apache.datasketches.quantilescommon.ReflectUtilityTest.DOUBLES_SV_CTOR; -import static org.apache.datasketches.quantilescommon.ReflectUtilityTest.KLL_FLOATS_SV_CTOR; -import static org.apache.datasketches.quantilescommon.ReflectUtilityTest.REQ_SV_CTOR; + import static org.testng.Assert.assertEquals; import java.util.Comparator; @@ -46,7 +44,6 @@ import org.apache.datasketches.quantiles.DoublesSketch; import org.apache.datasketches.quantiles.ItemsSketch; import org.apache.datasketches.quantiles.UpdateDoublesSketch; import org.apache.datasketches.req.ReqSketch; -import org.apache.datasketches.req.ReqSketchSortedView; import org.testng.annotations.Test; /** @@ -140,12 +137,10 @@ public class CrossCheckQuantilesTest { KllItemsSketch<String> kllItemsSk = null; ItemsSketch<String> itemsSk = null; - ReqSketchSortedView reqFloatsSV = null; - FloatsSketchSortedView kllFloatsSV = null; - DoublesSketchSortedView kllDoublesSV = null; - DoublesSketchSortedView classicDoublesSV = null; - ItemsSketchSortedView<String> kllItemsSV = null; + FloatsSketchSortedView floatsSV = null; + DoublesSketchSortedView doublesSV = null; ItemsSketchSortedView<String> classicItemsSV = null; + ItemsSketchSortedView<String> kllItemsSV = null; public CrossCheckQuantilesTest() {} @@ -174,12 +169,11 @@ public class CrossCheckQuantilesTest { float maxFloatvalue = getMaxFloatValue(set); for (float v = 5f; v <= maxFloatvalue + 5f; v += 5f) { trueRank = getTrueFloatRank(svCumWeights[set], svFValues[set],v, crit); - testRank = reqFloatsSV.getRank(v, crit); + + testRank = floatsSV.getRank(v, crit); assertEquals(testRank, trueRank); testRank = reqFloatsSk.getRank(v, crit); assertEquals(testRank, trueRank); - testRank = kllFloatsSV.getRank(v, crit); - assertEquals(testRank, trueRank); testRank = kllFloatsSk.getRank(v, crit); assertEquals(testRank, trueRank); @@ -191,13 +185,10 @@ public class CrossCheckQuantilesTest { for (double v = 5; v <= maxDoubleValue + 5; v += 5) { trueRank = getTrueDoubleRank(svCumWeights[set], svDValues[set],v, crit); - testRank = kllDoublesSV.getRank(v, crit); + testRank = doublesSV.getRank(v, crit); assertEquals(testRank, trueRank); testRank = kllDoublesSk.getRank(v, crit); assertEquals(testRank, trueRank); - - testRank = classicDoublesSV.getRank(v, crit); - assertEquals(testRank, trueRank); testRank = classicDoublesSk.getRank(v, crit); assertEquals(testRank, trueRank); @@ -216,7 +207,6 @@ public class CrossCheckQuantilesTest { assertEquals(testRank, trueRank); testRank = kllItemsSk.getRank(s, crit); assertEquals(testRank, trueRank); - testRank = classicItemsSV.getRank(s, crit); assertEquals(testRank, trueRank); testRank = itemsSk.getRank(s, crit); @@ -237,13 +227,10 @@ public class CrossCheckQuantilesTest { double normRank = i / dTwoN; trueFQ = getTrueFloatQuantile(svCumWeights[set], svFValues[set], normRank, crit); - testFQ = reqFloatsSV.getQuantile(normRank, crit); + testFQ = floatsSV.getQuantile(normRank, crit); assertEquals(testFQ, trueFQ); testFQ = reqFloatsSk.getQuantile(normRank, crit); assertEquals(testFQ, trueFQ); - - testFQ = kllFloatsSV.getQuantile(normRank, crit); - assertEquals(testFQ, trueFQ); testFQ = kllFloatsSk.getQuantile(normRank, crit); assertEquals(testFQ, trueFQ); @@ -257,13 +244,10 @@ public class CrossCheckQuantilesTest { double normRank = i / dTwoN; trueDQ = getTrueDoubleQuantile(svCumWeights[set], svDValues[set], normRank, crit); - testDQ = kllDoublesSV.getQuantile(normRank, crit); + testDQ = doublesSV.getQuantile(normRank, crit); assertEquals(testDQ, trueDQ); testDQ = kllDoublesSk.getQuantile(normRank, crit); assertEquals(testDQ, trueDQ); - - testDQ = classicDoublesSV.getQuantile(normRank, crit); - assertEquals(testDQ, trueDQ); testDQ = classicDoublesSk.getQuantile(normRank, crit); assertEquals(testDQ, trueDQ); @@ -281,7 +265,6 @@ public class CrossCheckQuantilesTest { assertEquals(testIQ, trueIQ); testIQ = kllItemsSk.getQuantile(normRank, crit); assertEquals(testIQ, trueIQ); - testIQ = classicItemsSV.getQuantile(normRank, crit); assertEquals(testIQ, trueIQ); testIQ = itemsSk.getQuantile(normRank, crit); @@ -289,7 +272,6 @@ public class CrossCheckQuantilesTest { println("Items set: " + set + ", rank: " + normRank + ", Q: " + trueIQ + ", crit: " + crit.toString()); } - } private double getMaxDoubleValue(int set) { @@ -331,40 +313,19 @@ public class CrossCheckQuantilesTest { /*******BUILD & LOAD SVs***********/ private void buildSVs(int set) throws Exception { - reqFloatsSV = getRawReqSV(svFValues[set], svCumWeights[set], totalN[set], - svMaxFValues[set], svMinFValues[set]); - kllFloatsSV = getRawKllFloatsSV(svFValues[set], svCumWeights[set], totalN[set], + floatsSV = new FloatsSketchSortedView(svFValues[set], svCumWeights[set], totalN[set], svMaxFValues[set], svMinFValues[set]); - kllDoublesSV = getRawDoublesSV(svDValues[set], svCumWeights[set], totalN[set], - svMaxDValues[set], svMinDValues[set]); - classicDoublesSV = getRawDoublesSV(svDValues[set], svCumWeights[set], totalN[set], + doublesSV = new DoublesSketchSortedView(svDValues[set], svCumWeights[set], totalN[set], svMaxDValues[set], svMinDValues[set]); String svImax = svIValues[set][svIValues[set].length - 1]; String svImin = svIValues[set][0]; - double normRankErr = KllSketch.getNormalizedRankError(k, true); - kllItemsSV = new ItemsSketchSortedView<>(svIValues[set], svCumWeights[set], totalN[set], - comparator, svImax, svImin, normRankErr); - normRankErr = ItemsSketch.getNormalizedRankError(k, true); - classicItemsSV = new ItemsSketchSortedView<>(svIValues[set], svCumWeights[set], totalN[set], - comparator, svImax, svImin, normRankErr); - } - private final static ReqSketchSortedView getRawReqSV( - final float[] values, final long[] cumWeights, final long totalN, final float maxItem, final float minItem) - throws Exception { - return (ReqSketchSortedView) REQ_SV_CTOR.newInstance(values, cumWeights, totalN, maxItem, minItem); - } + kllItemsSV = new ItemsSketchSortedView<>(svIValues[set], svCumWeights[set], totalN[set], + comparator, svImax, svImin, KllSketch.getNormalizedRankError(k, true)); - private final static FloatsSketchSortedView getRawKllFloatsSV( - final float[] values, final long[] cumWeights, final long totalN, final float maxItem, final float minItem) - throws Exception { - return (FloatsSketchSortedView) KLL_FLOATS_SV_CTOR.newInstance(values, cumWeights, totalN, maxItem, minItem); - } + classicItemsSV = new ItemsSketchSortedView<>(svIValues[set], svCumWeights[set], totalN[set], + comparator, svImax, svImin, ItemsSketch.getNormalizedRankError(k, true)); - private final static DoublesSketchSortedView getRawDoublesSV( - final double[] values, final long[] cumWeights, final long totalN, final double maxItem, final double minItem) - throws Exception { - return (DoublesSketchSortedView) DOUBLES_SV_CTOR.newInstance(values, cumWeights, totalN, maxItem, minItem); } /********BUILD DATA SETS**********/ diff --git a/src/test/java/org/apache/datasketches/quantilescommon/ReflectUtilityTest.java b/src/test/java/org/apache/datasketches/quantilescommon/ReflectUtilityTest.java deleted file mode 100644 index e1050985..00000000 --- a/src/test/java/org/apache/datasketches/quantilescommon/ReflectUtilityTest.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.datasketches.quantilescommon; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import static org.apache.datasketches.quantilescommon.QuantileSearchCriteria.*; -import static org.testng.Assert.assertEquals; - -import org.apache.datasketches.req.ReqSketchSortedView; -import org.testng.annotations.Test; - -public final class ReflectUtilityTest { - - private ReflectUtilityTest() {} - - static final Class<?> REQ_SV; - static final Class<?> KLL_FLOATS_SV; - static final Class<?> DOUBLES_SV; - - static final Constructor<?> REQ_SV_CTOR; - static final Constructor<?> KLL_FLOATS_SV_CTOR; - static final Constructor<?> DOUBLES_SV_CTOR; - - static { - REQ_SV = getClass("org.apache.datasketches.req.ReqSketchSortedView"); - KLL_FLOATS_SV = getClass("org.apache.datasketches.quantilescommon.FloatsSketchSortedView"); - DOUBLES_SV = getClass("org.apache.datasketches.quantilescommon.DoublesSketchSortedView"); - - REQ_SV_CTOR = - getConstructor(REQ_SV, float[].class, long[].class, long.class, float.class, float.class); - KLL_FLOATS_SV_CTOR = - getConstructor(KLL_FLOATS_SV, float[].class, long[].class, long.class, float.class, float.class); - DOUBLES_SV_CTOR = - getConstructor(DOUBLES_SV, double[].class, long[].class, long.class, double.class, double.class); - } - - @Test //Example - public static void checkCtr() throws Exception { - float[] farr = { 10, 20, 30 }; - long[] larr = { 1, 2, 3 }; - long n = 3; - ReqSketchSortedView reqSV = - (ReqSketchSortedView) REQ_SV_CTOR.newInstance(farr, larr, n, 10f, 30f); - float q = reqSV.getQuantile(1.0, INCLUSIVE); - assertEquals(q, 30f); - } - - /** - * Gets a Class reference to the given class loaded by the SystemClassLoader. - * This will work for private, package-private and abstract classes. - * @param fullyQualifiedBinaryName the binary name is the name of the class file on disk. This does not instantiate - * a concrete class, but allows access to constructors, static fields and static methods. - * @return the Class object of the given class. - */ - public static Class<?> getClass(final String fullyQualifiedBinaryName) { - try { - final ClassLoader scl = ClassLoader.getSystemClassLoader(); - return scl.loadClass(fullyQualifiedBinaryName); - } catch (final ClassNotFoundException e) { - throw new RuntimeException(e); - } - } - - /** - * Gets a declared constructor given the owner class and parameter types - * @param ownerClass the Class object of the class loaded by the SystemClassLoader. - * @param parameterTypes parameter types for the constructor - * @return the constructor - */ - public static Constructor<?> getConstructor(final Class<?> ownerClass, final Class<?>... parameterTypes ) { - try { - final Constructor<?> ctor = ownerClass.getDeclaredConstructor(parameterTypes); - ctor.setAccessible(true); - return ctor; - } catch (final NoSuchMethodException | SecurityException e) { - throw new RuntimeException(e); - } - } - - /** - * Gets a class instance from its constructor and initializing arguments. - * @param constructor the given Constructor - * @param initargs the initializing arguments - * @return the instantiated class. - */ - public static Object getInstance(final Constructor<?> constructor, final Object... initargs) { - try { - constructor.setAccessible(true); - return constructor.newInstance(initargs); - } catch (final InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | SecurityException e) { - throw new RuntimeException(e); - } - } - - /** - * Gets a declared field of the given the loaded owner class and field name. The accessible flag will be set true. - * @param ownerClass the Class object of the class loaded by the SystemClassLoader. - * @param fieldName the desired field name - * @return the desired field. - */ - public static Field getField(final Class<?> ownerClass, final String fieldName) { - try { - final Field field = ownerClass.getDeclaredField(fieldName); - field.setAccessible(true); - return field; - } catch (final NoSuchFieldException | SecurityException e) { - throw new RuntimeException(e); - } - } - - /** - * Gets a field value given the loaded owner class and the Field. The accessible flag will be set true. - * @param ownerClass the loaded class owning the field - * @param field The Field object - * @return the returned value as an object. - */ - public static Object getFieldValue(final Class<?> ownerClass, final Field field) { - try { - field.setAccessible(true); - return field.get(ownerClass); - } catch (final IllegalAccessException | SecurityException | IllegalArgumentException e) { - throw new RuntimeException(e); - } - } - - /** - * Gets a declared method of the given the loaded owning class, method name and parameter types. - * The accessible flag will be set true. - * @param ownerClass the given - * @param methodName the given method name - * @param parameterTypes the list of parameter types - * @return the desired method. - */ - public static Method getMethod( - final Class<?> ownerClass, final String methodName, final Class<?>... parameterTypes ) { - try { - final Method method = (parameterTypes == null) - ? ownerClass.getDeclaredMethod(methodName) - : ownerClass.getDeclaredMethod(methodName, parameterTypes); - method.setAccessible(true); - return method; - } catch (final NoSuchMethodException | SecurityException e) { - throw new RuntimeException(e); - } - } - -} - diff --git a/src/test/java/org/apache/datasketches/req/ReqSketchTest.java b/src/test/java/org/apache/datasketches/req/ReqSketchTest.java index 78b321e1..f09712f9 100644 --- a/src/test/java/org/apache/datasketches/req/ReqSketchTest.java +++ b/src/test/java/org/apache/datasketches/req/ReqSketchTest.java @@ -28,7 +28,7 @@ import static org.testng.Assert.fail; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; -import org.apache.datasketches.quantilescommon.FloatsSortedView; +import org.apache.datasketches.quantilescommon.FloatsSketchSortedView; import org.apache.datasketches.quantilescommon.FloatsSortedViewIterator; import org.apache.datasketches.quantilescommon.QuantileSearchCriteria; import org.apache.datasketches.quantilescommon.QuantilesFloatsSketchIterator; @@ -63,7 +63,7 @@ public class ReqSketchTest { + " up=" + up + " hra=" + hra + " criterion=" + crit + LS); } final ReqSketch sk = loadSketch(k, min, max, up, hra, skDebug); - final FloatsSortedView sv = sk.getSortedView(); + final FloatsSketchSortedView sv = sk.getSortedView(); checkToString(sk, iDebug); checkSortedView(sk, iDebug); checkGetRank(sk, min, max, iDebug); @@ -152,7 +152,7 @@ public class ReqSketchTest { } private static void checkSortedView(final ReqSketch sk, final int iDebug) { - final ReqSketchSortedView sv = new ReqSketchSortedView(sk); + final FloatsSketchSortedView sv = sk.getSortedView(); final FloatsSortedViewIterator itr = sv.iterator(); final int retainedCount = sk.getNumRetained(); final long totalN = sk.getN(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
