You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2020/07/07 23:37:06 UTC
[incubator-datasketches-java] 03/10: Consolidated all compacting
operations into CompactOperations.
This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch Refactor_Theta_Tuple
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-java.git
commit f4317e5b0d0925e0cc1462cbac0648cf4b97ae9e
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Sun Jun 28 09:20:36 2020 -0700
Consolidated all compacting operations into CompactOperations.
---
.../datasketches/theta/CompactOperations.java | 95 +++++++++++++++++-----
.../datasketches/theta/DirectCompactSketch.java | 6 +-
.../theta/DirectQuickSelectSketchR.java | 4 +-
.../datasketches/theta/ForwardCompatibility.java | 6 +-
.../theta/HeapCompactOrderedSketch.java | 70 ----------------
.../datasketches/theta/HeapCompactSketch.java | 63 ++++++++++----
.../theta/HeapCompactUnorderedSketch.java | 70 ----------------
.../datasketches/theta/HeapUpdateSketch.java | 5 +-
.../java/org/apache/datasketches/theta/Sketch.java | 54 ------------
.../apache/datasketches/theta/UpdateSketch.java | 88 --------------------
.../datasketches/theta/CompactSketchTest.java | 6 +-
.../ConcurrentDirectQuickSelectSketchTest.java | 4 +-
.../theta/ConcurrentHeapQuickSelectSketchTest.java | 4 +-
.../theta/DirectQuickSelectSketchTest.java | 4 +-
.../theta/ForwardCompatibilityTest.java | 2 +-
.../datasketches/theta/HeapAlphaSketchTest.java | 4 +-
.../theta/HeapQuickSelectSketchTest.java | 4 +-
17 files changed, 146 insertions(+), 343 deletions(-)
diff --git a/src/main/java/org/apache/datasketches/theta/CompactOperations.java b/src/main/java/org/apache/datasketches/theta/CompactOperations.java
index 882e0e8..2c17ade 100644
--- a/src/main/java/org/apache/datasketches/theta/CompactOperations.java
+++ b/src/main/java/org/apache/datasketches/theta/CompactOperations.java
@@ -100,11 +100,7 @@ final class CompactOperations {
if (single) {
return new SingleItemSketch(hashArrOut[0], seedHash);
}
- if (dstOrderedOut) {
- return new HeapCompactOrderedSketch(hashArrOut, empty, seedHash, curCount, thetaLong);
- } else {
- return new HeapCompactUnorderedSketch(hashArrOut, empty, seedHash, curCount, thetaLong);
- }
+ return new HeapCompactSketch(hashArrOut, empty, seedHash, curCount, thetaLong, dstOrderedOut);
}
}
@@ -116,7 +112,7 @@ final class CompactOperations {
* @param dstMem Used for the target CompactSketch if it is Direct.
* @return a CompactSketch of the correct form.
*/
- @SuppressWarnings("unused") //to replace CompactSketch.anyMemoryToCompactHeap
+ @SuppressWarnings("unused")
static CompactSketch memoryToCompact(
final Memory srcMem,
final boolean dstOrdered,
@@ -136,10 +132,10 @@ final class CompactOperations {
final boolean srcEmptyFlag = (srcFlags & EMPTY_FLAG_MASK) > 0;
final boolean srcCompactFlag = (srcFlags & COMPACT_FLAG_MASK) > 0;
final boolean srcOrderedFlag = (srcFlags & ORDERED_FLAG_MASK) > 0;
- //final boolean srcSingleFlag = (srcFlags & SINGLEITEM_FLAG_MASK) > 0;
+ final boolean srcSingleFlag = (srcFlags & SINGLEITEM_FLAG_MASK) > 0;
- final boolean single =
- SingleItemSketch.otherCheckForSingleItem(srcPreLongs, srcSerVer, srcFamId, srcFlags);
+ final boolean single = srcSingleFlag
+ || SingleItemSketch.otherCheckForSingleItem(srcPreLongs, srcSerVer, srcFamId, srcFlags);
//extract pre1 and pre2 fields
final int curCount = single ? 1 : (srcPreLongs > 1) ? extractCurCount(srcMem) : 0;
@@ -189,13 +185,8 @@ final class CompactOperations {
final Memory tgtMem = loadCompactMemory(hashArr, srcSeedHash, curCount, thetaLong, dstMem,
(byte)srcFlags, srcPreLongs);
return new DirectCompactSketch(tgtMem);
-
} else { //heap
- if (dstOrderedOut) {
- return new HeapCompactOrderedSketch(hashArr, srcEmptyFlag, srcSeedHash, curCount, thetaLong);
- } else {
- return new HeapCompactUnorderedSketch(hashArr, srcEmptyFlag, srcSeedHash, curCount, thetaLong);
- }
+ return new HeapCompactSketch(hashArr, srcEmptyFlag, srcSeedHash, curCount, thetaLong, dstOrderedOut);
}
}
@@ -262,11 +253,6 @@ final class CompactOperations {
return dstMem; //curCount == 0, theta could be < 1.0
}
- static final int computeCompactPreLongs(final long thetaLong, final boolean empty,
- final int curCount) {
- return (thetaLong < Long.MAX_VALUE) ? 3 : empty ? 1 : (curCount > 1) ? 2 : 1;
- }
-
/**
* Copies then compacts, cleans, and may sort the resulting array.
* The source cache can be a hash table with interstitial zeros or
@@ -302,5 +288,74 @@ final class CompactOperations {
return cacheOut;
}
+ /*
+ * The truth table for empty, curCount and theta when compacting is as follows:
+ * <pre>
+ * Num Theta CurCount Empty State Comments
+ * 0 1.0 0 T OK The Normal Empty State
+ * 1 1.0 0 F Internal This can result from an intersection of two exact, disjoint sets,
+ * or AnotB of two exact, identical sets. There is no probability
+ * distribution, so change to empty. Return {Th = 1.0, 0, T}.
+ * This is handled in SetOperation.createCompactSketch().
+ * 2 1.0 !0 T Error Empty=T and curCount !0 should never co-exist.
+ * This is checked in all compacting operations.
+ * 3 1.0 !0 F OK This corresponds to a sketch in exact mode
+ * 4 <1.0 0 T Internal This can be an initial UpdateSketch state if p < 1.0,
+ * so change theta to 1.0. Return {Th = 1.0, 0, T}.
+ * This is handled in UpdateSketch.compact() and toByteArray().
+ * 5 <1.0 0 F OK This can result from set operations
+ * 6 <1.0 !0 T Error Empty=T and curCount !0 should never co-exist.
+ * This is checked in all compacting operations.
+ * 7 <1.0 !0 F OK This corresponds to a sketch in estimation mode
+ * </pre>
+ * #4 is handled by <i>correctThetaOnCompat(boolean, int)</i> (below).
+ * #2 & #6 handled by <i>checkIllegalCurCountAndEmpty(boolean, int)</i>
+ */
+
+ /**
+ * This corrects a temporary anomalous condition where compact() is called on an UpdateSketch
+ * that was initialized with p < 1.0 and update() was never called. In this case Theta < 1.0,
+ * curCount = 0, and empty = true. The correction is to change Theta to 1.0, which makes the
+ * returning sketch empty. This should only be used in the compaction or serialization of an
+ * UpdateSketch.
+ * @param empty the given empty state
+ * @param curCount the given curCount
+ * @param thetaLong the given thetaLong
+ * @return thetaLong
+ */
+ static final long correctThetaOnCompact(final boolean empty, final int curCount,
+ final long thetaLong) { //handles #4 above
+ return (empty && (curCount == 0)) ? Long.MAX_VALUE : thetaLong;
+ }
+
+ /**
+ * This checks for the illegal condition where curCount > 0 and the state of
+ * empty = true. This check can be used anywhere a sketch is returned or a sketch is created
+ * from complete arguments.
+ * @param empty the given empty state
+ * @param curCount the given current count
+ */ //This handles #2 and #6 above
+ static final void checkIllegalCurCountAndEmpty(final boolean empty, final int curCount) {
+ if (empty && (curCount != 0)) { //this handles #2 and #6 above
+ throw new SketchesStateException("Illegal State: Empty=true and Current Count != 0.");
+ }
+ }
+
+ static final int computeCompactPreLongs(final long thetaLong, final boolean empty,
+ final int curCount) {
+ return (thetaLong < Long.MAX_VALUE) ? 3 : empty ? 1 : (curCount > 1) ? 2 : 1;
+ }
+
+ /**
+ * This checks for the singleItem Compact Sketch.
+ * @param empty the given empty state
+ * @param curCount the given curCount
+ * @param thetaLong the given thetaLong
+ * @return true if notEmpty, curCount = 1 and theta = 1.0;
+ */
+ static final boolean isSingleItem(final boolean empty, final int curCount,
+ final long thetaLong) {
+ return !empty && (curCount == 1) && (thetaLong == Long.MAX_VALUE);
+ }
}
diff --git a/src/main/java/org/apache/datasketches/theta/DirectCompactSketch.java b/src/main/java/org/apache/datasketches/theta/DirectCompactSketch.java
index a695944..aac4431 100644
--- a/src/main/java/org/apache/datasketches/theta/DirectCompactSketch.java
+++ b/src/main/java/org/apache/datasketches/theta/DirectCompactSketch.java
@@ -19,6 +19,8 @@
package org.apache.datasketches.theta;
+import static org.apache.datasketches.theta.CompactOperations.checkIllegalCurCountAndEmpty;
+import static org.apache.datasketches.theta.CompactOperations.memoryToCompact;
import static org.apache.datasketches.theta.PreambleUtil.ORDERED_FLAG_MASK;
import static org.apache.datasketches.theta.PreambleUtil.checkMemorySeedHash;
import static org.apache.datasketches.theta.PreambleUtil.extractCurCount;
@@ -76,7 +78,7 @@ class DirectCompactSketch extends CompactSketch {
@Override
public CompactSketch compact(final boolean dstOrdered, final WritableMemory dstMem) {
- return CompactOperations.memoryToCompact(mem_, dstOrdered, dstMem);
+ return memoryToCompact(mem_, dstOrdered, dstMem);
}
@Override
@@ -146,7 +148,7 @@ class DirectCompactSketch extends CompactSketch {
@Override
public byte[] toByteArray() {
final int curCount = getRetainedEntries(true);
- Sketch.checkIllegalCurCountAndEmpty(isEmpty(), curCount);
+ checkIllegalCurCountAndEmpty(isEmpty(), curCount);
final int preLongs = extractPreLongs(mem_);
final int outBytes = (curCount + preLongs) << 3;
final byte[] byteArrOut = new byte[outBytes];
diff --git a/src/main/java/org/apache/datasketches/theta/DirectQuickSelectSketchR.java b/src/main/java/org/apache/datasketches/theta/DirectQuickSelectSketchR.java
index 97eb82a..f5bfb49 100644
--- a/src/main/java/org/apache/datasketches/theta/DirectQuickSelectSketchR.java
+++ b/src/main/java/org/apache/datasketches/theta/DirectQuickSelectSketchR.java
@@ -170,7 +170,7 @@ class DirectQuickSelectSketchR extends UpdateSketch {
@Override
public byte[] toByteArray() { //MY_FAMILY is stored in wmem_
- Sketch.checkIllegalCurCountAndEmpty(isEmpty(), extractCurCount(wmem_));
+ CompactOperations.checkIllegalCurCountAndEmpty(isEmpty(), extractCurCount(wmem_));
final byte lgArrLongs = wmem_.getByte(LG_ARR_LONGS_BYTE);
final int preambleLongs = wmem_.getByte(PREAMBLE_LONGS_BYTE) & 0X3F;
final int lengthBytes = (preambleLongs + (1 << lgArrLongs)) << 3;
@@ -178,7 +178,7 @@ class DirectQuickSelectSketchR extends UpdateSketch {
final WritableMemory mem = WritableMemory.wrap(byteArray);
wmem_.copyTo(0, mem, 0, lengthBytes);
final long thetaLong =
- Sketch.correctThetaOnCompact(isEmpty(), extractCurCount(wmem_), extractThetaLong(wmem_));
+ CompactOperations.correctThetaOnCompact(isEmpty(), extractCurCount(wmem_), extractThetaLong(wmem_));
PreambleUtil.insertThetaLong(wmem_, thetaLong);
return byteArray;
}
diff --git a/src/main/java/org/apache/datasketches/theta/ForwardCompatibility.java b/src/main/java/org/apache/datasketches/theta/ForwardCompatibility.java
index 733dfc1..9da0b56 100644
--- a/src/main/java/org/apache/datasketches/theta/ForwardCompatibility.java
+++ b/src/main/java/org/apache/datasketches/theta/ForwardCompatibility.java
@@ -100,7 +100,7 @@ final class ForwardCompatibility {
final long[] compactOrderedCache = new long[curCount];
srcMem.getLongArray(preLongs << 3, compactOrderedCache, 0, curCount);
- return new HeapCompactOrderedSketch(compactOrderedCache, false, seedHash, curCount, thetaLong);
+ return new HeapCompactSketch(compactOrderedCache, false, seedHash, curCount, thetaLong, true);
}
/**
@@ -146,7 +146,7 @@ final class ForwardCompatibility {
validateInputSize(reqBytesIn, memCap);
final long[] compactOrderedCache = new long[curCount];
srcMem.getLongArray(preLongs << 3, compactOrderedCache, 0, curCount);
- return new HeapCompactOrderedSketch(compactOrderedCache, false, seedHash, curCount, thetaLong);
+ return new HeapCompactSketch(compactOrderedCache, false, seedHash, curCount, thetaLong,true);
}
if (preLongs == 3) { //pre0 + count + theta
reqBytesIn = (preLongs) << 3; //
@@ -167,7 +167,7 @@ final class ForwardCompatibility {
validateInputSize(reqBytesIn, memCap);
final long[] compactOrderedCache = new long[curCount];
srcMem.getLongArray(preLongs << 3, compactOrderedCache, 0, curCount);
- return new HeapCompactOrderedSketch(compactOrderedCache, false, seedHash, curCount, thetaLong);
+ return new HeapCompactSketch(compactOrderedCache, false, seedHash, curCount, thetaLong, true);
}
throw new SketchesArgumentException("PreLongs must be 1,2, or 3: " + preLongs);
}
diff --git a/src/main/java/org/apache/datasketches/theta/HeapCompactOrderedSketch.java b/src/main/java/org/apache/datasketches/theta/HeapCompactOrderedSketch.java
deleted file mode 100644
index dbaf823..0000000
--- a/src/main/java/org/apache/datasketches/theta/HeapCompactOrderedSketch.java
+++ /dev/null
@@ -1,70 +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.theta;
-
-import org.apache.datasketches.memory.WritableMemory;
-
-/**
- * An on-heap, compact, ordered, read-only sketch.
- *
- * @author Lee Rhodes
- */
-final class HeapCompactOrderedSketch extends HeapCompactSketch {
-
- /**
- * Constructs this sketch from correct, valid components.
- * @param cache in compact form
- * @param empty The correct <a href="{@docRoot}/resources/dictionary.html#empty">Empty</a>.
- * @param seedHash The correct
- * <a href="{@docRoot}/resources/dictionary.html#seedHash">Seed Hash</a>.
- * @param curCount correct value
- * @param thetaLong The correct
- * <a href="{@docRoot}/resources/dictionary.html#thetaLong">thetaLong</a>.
- */
- HeapCompactOrderedSketch(final long[] cache, final boolean empty, final short seedHash,
- final int curCount, final long thetaLong) {
- super(cache, empty, seedHash, curCount, thetaLong);
- }
-
- //Sketch interface
-
- @Override
- public CompactSketch compact() {
- return this;
- }
-
- @Override
- public CompactSketch compact(final boolean dstOrdered, final WritableMemory dstMem) {
- return CompactOperations.componentsToCompact(
- getThetaLong(), getRetainedEntries(), getSeedHash(), isEmpty(),
- true, true, dstOrdered, dstMem, getCache());
- }
-
- @Override
- public byte[] toByteArray() {
- return toByteArray(true); //ordered
- }
-
- @Override
- public boolean isOrdered() {
- return true;
- }
-
-}
diff --git a/src/main/java/org/apache/datasketches/theta/HeapCompactSketch.java b/src/main/java/org/apache/datasketches/theta/HeapCompactSketch.java
index 74dff7d..00f8939 100644
--- a/src/main/java/org/apache/datasketches/theta/HeapCompactSketch.java
+++ b/src/main/java/org/apache/datasketches/theta/HeapCompactSketch.java
@@ -19,10 +19,17 @@
package org.apache.datasketches.theta;
+import static org.apache.datasketches.theta.CompactOperations.checkIllegalCurCountAndEmpty;
+import static org.apache.datasketches.theta.CompactOperations.componentsToCompact;
+import static org.apache.datasketches.theta.CompactOperations.computeCompactPreLongs;
+import static org.apache.datasketches.theta.CompactOperations.correctThetaOnCompact;
+import static org.apache.datasketches.theta.CompactOperations.isSingleItem;
+import static org.apache.datasketches.theta.CompactOperations.loadCompactMemory;
import static org.apache.datasketches.theta.PreambleUtil.COMPACT_FLAG_MASK;
import static org.apache.datasketches.theta.PreambleUtil.EMPTY_FLAG_MASK;
import static org.apache.datasketches.theta.PreambleUtil.ORDERED_FLAG_MASK;
import static org.apache.datasketches.theta.PreambleUtil.READ_ONLY_FLAG_MASK;
+import static org.apache.datasketches.theta.PreambleUtil.SINGLEITEM_FLAG_MASK;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.WritableMemory;
@@ -32,13 +39,15 @@ import org.apache.datasketches.memory.WritableMemory;
*
* @author Lee Rhodes
*/
-abstract class HeapCompactSketch extends CompactSketch {
+class HeapCompactSketch extends CompactSketch {
+ private final long thetaLong_; //computed
+ private final int curCount_;
+ private final int preLongs_; //computed
private final short seedHash_;
private final boolean empty_;
- private final int curCount_;
- private final long thetaLong_;
+ private final boolean ordered_;
+ private final boolean singleItem_;
private final long[] cache_;
- private final int preLongs_;
/**
* Constructs this sketch from correct, valid components.
@@ -51,18 +60,33 @@ abstract class HeapCompactSketch extends CompactSketch {
* <a href="{@docRoot}/resources/dictionary.html#thetaLong">thetaLong</a>.
*/
HeapCompactSketch(final long[] cache, final boolean empty, final short seedHash,
- final int curCount, final long thetaLong) {
- empty_ = empty;
+ final int curCount, final long thetaLong, final boolean ordered) {
seedHash_ = seedHash;
- curCount_ = empty ? 0 : curCount;
- thetaLong_ = empty ? Long.MAX_VALUE : thetaLong;
+ curCount_ = curCount;
+ empty_ = empty;
+ ordered_ = ordered;
cache_ = cache;
- preLongs_ = CompactOperations.computeCompactPreLongs(thetaLong, empty, curCount);
+ //computed
+ thetaLong_ = correctThetaOnCompact(empty, curCount, thetaLong);
+ preLongs_ = computeCompactPreLongs(thetaLong, empty, curCount); //considers singleItem
+ singleItem_ = isSingleItem(empty, curCount, thetaLong);
+ checkIllegalCurCountAndEmpty(empty, curCount);
}
//Sketch
@Override
+ public CompactSketch compact() {
+ return compact(true, null);
+ }
+
+ @Override
+ public CompactSketch compact(final boolean dstOrdered, final WritableMemory dstMem) {
+ return componentsToCompact(getThetaLong(), getRetainedEntries(), getSeedHash(), isEmpty(),
+ true, ordered_, dstOrdered, dstMem, getCache());
+ }
+
+ @Override
public int getCurrentBytes(final boolean compact) { //already compact; ignored
return (preLongs_ + curCount_) << 3;
}
@@ -98,6 +122,11 @@ abstract class HeapCompactSketch extends CompactSketch {
}
@Override
+ public boolean isOrdered() {
+ return ordered_;
+ }
+
+ @Override
public HashIterator iterator() {
return new HeapHashIterator(cache_, cache_.length, thetaLong_);
}
@@ -124,17 +153,19 @@ abstract class HeapCompactSketch extends CompactSketch {
return seedHash_;
}
- //only called from sub classes
- byte[] toByteArray(final boolean ordered) {
- Sketch.checkIllegalCurCountAndEmpty(empty_, curCount_);
+ //use of Memory is convenient. The byteArray and Memory are loaded simulaneously.
+ @Override
+ public byte[] toByteArray() {
final int bytes = getCurrentBytes(true);
final byte[] byteArray = new byte[bytes];
final WritableMemory dstMem = WritableMemory.wrap(byteArray);
- final int emptyBit = isEmpty() ? (byte) EMPTY_FLAG_MASK : 0;
- final int orderedBit = ordered ? (byte) ORDERED_FLAG_MASK : 0;
- final byte flags = (byte) (emptyBit | READ_ONLY_FLAG_MASK | COMPACT_FLAG_MASK | orderedBit);
+ final int emptyBit = isEmpty() ? EMPTY_FLAG_MASK : 0;
+ final int orderedBit = ordered_ ? ORDERED_FLAG_MASK : 0;
+ final int singleItemBit = singleItem_ ? SINGLEITEM_FLAG_MASK : 0;
+ final byte flags = (byte) (emptyBit | READ_ONLY_FLAG_MASK | COMPACT_FLAG_MASK
+ | orderedBit | singleItemBit);
final int preLongs = getCurrentPreambleLongs(true);
- CompactOperations.loadCompactMemory(getCache(), getSeedHash(), getRetainedEntries(true), getThetaLong(),
+ loadCompactMemory(getCache(), getSeedHash(), getRetainedEntries(true), getThetaLong(),
dstMem, flags, preLongs);
return byteArray;
}
diff --git a/src/main/java/org/apache/datasketches/theta/HeapCompactUnorderedSketch.java b/src/main/java/org/apache/datasketches/theta/HeapCompactUnorderedSketch.java
deleted file mode 100644
index 6b59a02..0000000
--- a/src/main/java/org/apache/datasketches/theta/HeapCompactUnorderedSketch.java
+++ /dev/null
@@ -1,70 +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.theta;
-
-import org.apache.datasketches.memory.WritableMemory;
-
-/**
- * An on-heap, compact, unordered, read-only sketch.
- *
- * @author Lee Rhodes
- */
-final class HeapCompactUnorderedSketch extends HeapCompactSketch {
-
- /**
- * Constructs this sketch from correct, valid components.
- * @param cache in compact form
- * @param empty The correct <a href="{@docRoot}/resources/dictionary.html#empty">Empty</a>.
- * @param seedHash The correct
- * <a href="{@docRoot}/resources/dictionary.html#seedHash">Seed Hash</a>.
- * @param curCount correct value
- * @param thetaLong The correct
- * <a href="{@docRoot}/resources/dictionary.html#thetaLong">thetaLong</a>.
- */
- HeapCompactUnorderedSketch(final long[] cache, final boolean empty, final short seedHash,
- final int curCount, final long thetaLong) {
- super(cache, empty, seedHash, curCount, thetaLong);
- }
-
- //Sketch interface
-
- @Override //ordered, on-heap
- public CompactSketch compact() {
- return compact(true, null);
- }
-
- @Override
- public CompactSketch compact(final boolean dstOrdered, final WritableMemory dstMem) {
- return CompactOperations.componentsToCompact(
- getThetaLong(), getRetainedEntries(), getSeedHash(), isEmpty(),
- true, false, dstOrdered, dstMem, getCache());
- }
-
- @Override
- public byte[] toByteArray() {
- return toByteArray(false); //unordered
- }
-
- @Override
- public boolean isOrdered() {
- return false;
- }
-
-}
diff --git a/src/main/java/org/apache/datasketches/theta/HeapUpdateSketch.java b/src/main/java/org/apache/datasketches/theta/HeapUpdateSketch.java
index 16efd44..5c642ec 100644
--- a/src/main/java/org/apache/datasketches/theta/HeapUpdateSketch.java
+++ b/src/main/java/org/apache/datasketches/theta/HeapUpdateSketch.java
@@ -106,7 +106,7 @@ abstract class HeapUpdateSketch extends UpdateSketch {
byte[] toByteArray(final int preLongs, final byte familyID) {
if (isDirty()) { rebuild(); }
- Sketch.checkIllegalCurCountAndEmpty(isEmpty(), getRetainedEntries(true));
+ CompactOperations.checkIllegalCurCountAndEmpty(isEmpty(), getRetainedEntries(true));
final int preBytes = (preLongs << 3) & 0X3F;
final int dataBytes = getCurrentDataLongs(false) << 3;
final byte[] byteArrOut = new byte[preBytes + dataBytes];
@@ -124,7 +124,8 @@ abstract class HeapUpdateSketch extends UpdateSketch {
insertCurCount(memOut, this.getRetainedEntries(true));
insertP(memOut, getP());
- final long thetaLong = correctThetaOnCompact(isEmpty(), getRetainedEntries(), getThetaLong());
+ final long thetaLong =
+ CompactOperations.correctThetaOnCompact(isEmpty(), getRetainedEntries(), getThetaLong());
insertThetaLong(memOut, thetaLong);
//Flags: BigEnd=0, ReadOnly=0, Empty=X, compact=0, ordered=0
diff --git a/src/main/java/org/apache/datasketches/theta/Sketch.java b/src/main/java/org/apache/datasketches/theta/Sketch.java
index b8c7bd7..e87b515 100644
--- a/src/main/java/org/apache/datasketches/theta/Sketch.java
+++ b/src/main/java/org/apache/datasketches/theta/Sketch.java
@@ -39,7 +39,6 @@ import static org.apache.datasketches.theta.SingleItemSketch.otherCheckForSingle
import org.apache.datasketches.BinomialBoundsN;
import org.apache.datasketches.Family;
import org.apache.datasketches.SketchesArgumentException;
-import org.apache.datasketches.SketchesStateException;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.WritableMemory;
@@ -610,59 +609,6 @@ public abstract class Sketch {
}
}
- /*
- * The truth table for empty, curCount and theta when compacting is as follows:
- * <pre>
- * Num Theta CurCount Empty State Comments
- * 0 1.0 0 T OK The Normal Empty State
- * 1 1.0 0 F Internal This can result from an intersection of two exact, disjoint sets,
- * or AnotB of two exact, identical sets. There is no probability
- * distribution, so change to empty. Return {Th = 1.0, 0, T}.
- * This is handled in SetOperation.createCompactSketch().
- * 2 1.0 !0 T Error Empty=T and curCount !0 should never co-exist.
- * This is checked in all compacting operations.
- * 3 1.0 !0 F OK This corresponds to a sketch in exact mode
- * 4 <1.0 0 T Internal This can be an initial UpdateSketch state if p < 1.0,
- * so change theta to 1.0. Return {Th = 1.0, 0, T}.
- * This is handled in UpdateSketch.compact() and toByteArray().
- * 5 <1.0 0 F OK This can result from set operations
- * 6 <1.0 !0 T Error Empty=T and curCount !0 should never co-exist.
- * This is checked in all compacting operations.
- * 7 <1.0 !0 F OK This corresponds to a sketch in estimation mode
- * </pre>
- */
-
- /**
- * This checks for the illegal condition where curCount > 0 and the state of
- * empty = true. This check can be used anywhere a sketch is returned or a sketch is created
- * from complete arguments.
- * @param empty the given empty state
- * @param curCount the given current count
- */ //This handles #2 and #6 above
- static final void checkIllegalCurCountAndEmpty(final boolean empty, final int curCount) {
- if (empty && (curCount != 0)) { //this handles #2 and #6 above
- throw new SketchesStateException("Illegal State: Empty=true and Current Count != 0.");
- }
- }
-
- /**
- * This corrects a temporary anomalous condition where compact() is called on an UpdateSketch
- * that was initialized with p < 1.0 and update() was never called. In this case Theta < 1.0,
- * curCount = 0, and empty = true. The correction is to change Theta to 1.0, which makes the
- * returning sketch empty. This should only be used in the compaction or serialization of an
- * UpdateSketch.
- * @param empty the given empty state
- * @param curCount the given curCount
- * @param thetaLong the given thetaLong
- * @return thetaLong
- */ //This handles #4 above
- static final long correctThetaOnCompact(final boolean empty, final int curCount,
- final long thetaLong) {
- return (empty && (curCount == 0) && (thetaLong < Long.MAX_VALUE)) ? Long.MAX_VALUE : thetaLong;
- }
-
-
-
static final double estimate(final long thetaLong, final int curCount) {
return curCount * (LONG_MAX_VALUE_AS_DOUBLE / thetaLong);
}
diff --git a/src/main/java/org/apache/datasketches/theta/UpdateSketch.java b/src/main/java/org/apache/datasketches/theta/UpdateSketch.java
index 5a14b6e..f94d8b7 100644
--- a/src/main/java/org/apache/datasketches/theta/UpdateSketch.java
+++ b/src/main/java/org/apache/datasketches/theta/UpdateSketch.java
@@ -143,94 +143,6 @@ public abstract class UpdateSketch extends Sketch {
//return compact(this, dstOrdered, dstMem);
}
- static CompactSketch compact(final UpdateSketch usk, final boolean dstOrdered,
- final WritableMemory dstMem) {
- final int curCount = usk.getRetainedEntries(true);
- final boolean empty = usk.isEmpty();
- checkIllegalCurCountAndEmpty(empty, curCount);
-
- if (empty) {
- final EmptyCompactSketch sk = EmptyCompactSketch.getInstance();
- if (dstMem != null) {
- dstMem.putByteArray(0, sk.toByteArray(), 0, 8);
- }
- return sk;
- }
- //not empty
- final long thetaLong = correctThetaOnCompact(empty, curCount, usk.getThetaLong());
- final short seedHash = usk.getSeedHash();
- final long[] cache = usk.getCache();
- if ((thetaLong == Long.MAX_VALUE) && (curCount == 1)) {
-
- final long[] cacheOut = CompactOperations.compactCache(cache, curCount, thetaLong, dstOrdered);
- final long hash = cacheOut[0];
- final SingleItemSketch sis = new SingleItemSketch(hash, usk.getSeedHash());
- if (dstMem != null) {
- dstMem.putByteArray(0, sis.toByteArray(), 0, 16);
- }
- return sis;
- }
- if (dstMem == null) {
- final long[] cacheOut = CompactOperations.compactCache(cache, curCount, thetaLong, dstOrdered);
- if (dstOrdered) {
- return new HeapCompactOrderedSketch(cacheOut, false, seedHash, curCount, thetaLong);
- } else {
- return new HeapCompactUnorderedSketch(cacheOut, false, seedHash, curCount, thetaLong);
- }
- //return compactHeap(usk, dstOrdered, curCount, thetaLong);
- } else {
- return compactDirect(usk, dstMem, dstOrdered, curCount, thetaLong);
- }
- }
-
- /**
- * Converts the given UpdateSketch to a compact form.
- * EmptyCompactSketch and SingleItemSketch have already been checked.
- * @param sketch the given UpdateSketch
- * @param ordered true if the destination is to be ordered.
- * @param curCount the number of retained entries.
- * @param thetaLong the value of theta.
- * @return a CompactSketch
- */
- static CompactSketch compactHeap(final UpdateSketch sketch, final boolean ordered,
- final int curCount, final long thetaLong) {
- final short seedHash = sketch.getSeedHash();
- final long[] cache = sketch.getCache();
- final long[] cacheOut = CompactOperations.compactCache(cache, curCount, thetaLong, ordered);
- if (ordered) {
- return new HeapCompactOrderedSketch(cacheOut, false, seedHash, curCount, thetaLong);
- } else {
- return new HeapCompactUnorderedSketch(cacheOut, false, seedHash, curCount, thetaLong);
- }
- }
-
- /**
- * Converts the given UpdateSketch to a compact form.
- * EmptyCompactSketch and SingleItemSketch have already been checked.
- * @param sketch the given UpdateSketch
- * @param dstMem the given destination Memory. This clears it before use.
- * @param ordered true if the destination is to be ordered.
- * @param curCount the number of retained entries.
- * @param thetaLong the value of theta.
- * @return a CompactSketch.
- */
- static CompactSketch compactDirect(final UpdateSketch sketch,
- final WritableMemory dstMem, final boolean ordered, final int curCount, final long thetaLong) {
- final int preLongs = CompactOperations.computeCompactPreLongs(thetaLong, false, curCount);
- final short seedHash = sketch.getSeedHash();
- final long[] cache = sketch.getCache();
- final long[] cacheOut = CompactOperations.compactCache(cache, curCount, thetaLong, ordered);
- if (ordered) {
- final byte flags = (byte)(READ_ONLY_FLAG_MASK | COMPACT_FLAG_MASK | ORDERED_FLAG_MASK);
- CompactOperations.loadCompactMemory(cacheOut, seedHash, curCount, thetaLong, dstMem, flags, preLongs);
- } else {
- final byte flags = (byte)(READ_ONLY_FLAG_MASK | COMPACT_FLAG_MASK);
- CompactOperations.loadCompactMemory(cacheOut, seedHash, curCount, thetaLong, dstMem, flags, preLongs);
- }
- return new DirectCompactSketch(dstMem);
- }
-
-
@Override
public boolean isCompact() {
return false;
diff --git a/src/test/java/org/apache/datasketches/theta/CompactSketchTest.java b/src/test/java/org/apache/datasketches/theta/CompactSketchTest.java
index a79f72a..a129766 100644
--- a/src/test/java/org/apache/datasketches/theta/CompactSketchTest.java
+++ b/src/test/java/org/apache/datasketches/theta/CompactSketchTest.java
@@ -159,11 +159,7 @@ public class CompactSketchTest {
}
} else {
assertFalse(testSk.hasMemory());
- if (ordered) {
- assertTrue(testSk instanceof HeapCompactOrderedSketch);
- } else {
- assertTrue(testSk instanceof HeapCompactUnorderedSketch);
- }
+ assertTrue(testSk instanceof HeapCompactSketch);
}
assertEquals(testSk.getSeedHash(), refSk.getSeedHash());
assertEquals(testSk.getRetainedEntries(true), refSk.getRetainedEntries());
diff --git a/src/test/java/org/apache/datasketches/theta/ConcurrentDirectQuickSelectSketchTest.java b/src/test/java/org/apache/datasketches/theta/ConcurrentDirectQuickSelectSketchTest.java
index db69dd7..acca0b6 100644
--- a/src/test/java/org/apache/datasketches/theta/ConcurrentDirectQuickSelectSketchTest.java
+++ b/src/test/java/org/apache/datasketches/theta/ConcurrentDirectQuickSelectSketchTest.java
@@ -372,7 +372,7 @@ public class ConcurrentDirectQuickSelectSketchTest {
assertEquals(csk.getUpperBound(2), localUB);
assertFalse(csk.isEmpty());
assertEquals(csk.isEstimationMode(), estimating);
- assertEquals(csk.getClass().getSimpleName(), "HeapCompactUnorderedSketch");
+ assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
csk = shared.compact(true, null);
assertEquals(csk.getEstimate(), localEst);
@@ -380,7 +380,7 @@ public class ConcurrentDirectQuickSelectSketchTest {
assertEquals(csk.getUpperBound(2), localUB);
assertFalse(csk.isEmpty());
assertEquals(csk.isEstimationMode(), estimating);
- assertEquals(csk.getClass().getSimpleName(), "HeapCompactOrderedSketch");
+ assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
int bytes = local.getCurrentBytes(true);
assertEquals(bytes, (k*8) + (Family.COMPACT.getMaxPreLongs() << 3));
diff --git a/src/test/java/org/apache/datasketches/theta/ConcurrentHeapQuickSelectSketchTest.java b/src/test/java/org/apache/datasketches/theta/ConcurrentHeapQuickSelectSketchTest.java
index 21b580b..61bd5be 100644
--- a/src/test/java/org/apache/datasketches/theta/ConcurrentHeapQuickSelectSketchTest.java
+++ b/src/test/java/org/apache/datasketches/theta/ConcurrentHeapQuickSelectSketchTest.java
@@ -311,7 +311,7 @@ public class ConcurrentHeapQuickSelectSketchTest {
assertEquals(comp1.isEmpty(), false);
assertEquals(comp1.isEstimationMode(), estimating);
assertEquals(comp1.getCurrentBytes(true), localCompBytes);
- assertEquals(comp1.getClass().getSimpleName(), "HeapCompactUnorderedSketch");
+ assertEquals(comp1.getClass().getSimpleName(), "HeapCompactSketch");
comp2 = shared.compact(true, null);
@@ -321,7 +321,7 @@ public class ConcurrentHeapQuickSelectSketchTest {
assertEquals(comp2.isEmpty(), false);
assertEquals(comp2.isEstimationMode(), estimating);
assertEquals(comp2.getCurrentBytes(true), localCompBytes);
- assertEquals(comp2.getClass().getSimpleName(), "HeapCompactOrderedSketch");
+ assertEquals(comp2.getClass().getSimpleName(), "HeapCompactSketch");
byte[] memArr2 = new byte[localCompBytes];
WritableMemory mem2 = WritableMemory.wrap(memArr2); //allocate mem for compact form
diff --git a/src/test/java/org/apache/datasketches/theta/DirectQuickSelectSketchTest.java b/src/test/java/org/apache/datasketches/theta/DirectQuickSelectSketchTest.java
index 56070bc..d44625d 100644
--- a/src/test/java/org/apache/datasketches/theta/DirectQuickSelectSketchTest.java
+++ b/src/test/java/org/apache/datasketches/theta/DirectQuickSelectSketchTest.java
@@ -326,7 +326,7 @@ public class DirectQuickSelectSketchTest {
assertEquals(csk.getUpperBound(2), uskUB);
assertEquals(csk.isEmpty(), false);
assertEquals(csk.isEstimationMode(), estimating);
- assertEquals(csk.getClass().getSimpleName(), "HeapCompactUnorderedSketch");
+ assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
csk = usk.compact(true, null);
assertEquals(csk.getEstimate(), uskEst);
@@ -334,7 +334,7 @@ public class DirectQuickSelectSketchTest {
assertEquals(csk.getUpperBound(2), uskUB);
assertEquals(csk.isEmpty(), false);
assertEquals(csk.isEstimationMode(), estimating);
- assertEquals(csk.getClass().getSimpleName(), "HeapCompactOrderedSketch");
+ assertEquals(csk.getClass().getSimpleName(), "HeapCompactSketch");
int bytes = usk.getCurrentBytes(true);
assertEquals(bytes, (k*8) + (Family.COMPACT.getMaxPreLongs() << 3));
diff --git a/src/test/java/org/apache/datasketches/theta/ForwardCompatibilityTest.java b/src/test/java/org/apache/datasketches/theta/ForwardCompatibilityTest.java
index e5d040c..0a30d38 100644
--- a/src/test/java/org/apache/datasketches/theta/ForwardCompatibilityTest.java
+++ b/src/test/java/org/apache/datasketches/theta/ForwardCompatibilityTest.java
@@ -199,7 +199,7 @@ public class ForwardCompatibilityTest {
assertEquals(sketch.hasMemory(), false);
assertEquals(sketch.isCompact(), true);
assertEquals(sketch.isOrdered(), true);
- assertTrue(sketch instanceof HeapCompactOrderedSketch);
+ assertTrue(sketch instanceof HeapCompactSketch);
}
@Test
diff --git a/src/test/java/org/apache/datasketches/theta/HeapAlphaSketchTest.java b/src/test/java/org/apache/datasketches/theta/HeapAlphaSketchTest.java
index 12d300a..62ff8ef 100644
--- a/src/test/java/org/apache/datasketches/theta/HeapAlphaSketchTest.java
+++ b/src/test/java/org/apache/datasketches/theta/HeapAlphaSketchTest.java
@@ -252,7 +252,7 @@ public class HeapAlphaSketchTest {
assertEquals(comp1.isEmpty(), false);
assertEquals(comp1.isEstimationMode(), estimating);
- assertEquals(comp1.getClass().getSimpleName(), "HeapCompactUnorderedSketch");
+ assertEquals(comp1.getClass().getSimpleName(), "HeapCompactSketch");
comp2 = usk.compact(true, null);
@@ -263,7 +263,7 @@ public class HeapAlphaSketchTest {
assertEquals(comp2.isEstimationMode(), estimating);
assertEquals(comp1bytes, comp2.getCurrentBytes(true)); //flag is not relevant
assertEquals(comp1curCount, comp2.getRetainedEntries(true)); //flag is not relevant
- assertEquals(comp2.getClass().getSimpleName(), "HeapCompactOrderedSketch");
+ assertEquals(comp2.getClass().getSimpleName(), "HeapCompactSketch");
int bytes = usk.getCurrentBytes(true);
int alphaBytes = sk1.getRetainedEntries(true) * 8;
diff --git a/src/test/java/org/apache/datasketches/theta/HeapQuickSelectSketchTest.java b/src/test/java/org/apache/datasketches/theta/HeapQuickSelectSketchTest.java
index a75cb9a..c230a88 100644
--- a/src/test/java/org/apache/datasketches/theta/HeapQuickSelectSketchTest.java
+++ b/src/test/java/org/apache/datasketches/theta/HeapQuickSelectSketchTest.java
@@ -251,7 +251,7 @@ public class HeapQuickSelectSketchTest {
assertEquals(comp1.isEmpty(), false);
assertEquals(comp1.isEstimationMode(), estimating);
assertEquals(comp1.getCurrentBytes(true), uskCompBytes);
- assertEquals(comp1.getClass().getSimpleName(), "HeapCompactUnorderedSketch");
+ assertEquals(comp1.getClass().getSimpleName(), "HeapCompactSketch");
comp2 = usk.compact(true, null);
@@ -261,7 +261,7 @@ public class HeapQuickSelectSketchTest {
assertEquals(comp2.isEmpty(), false);
assertEquals(comp2.isEstimationMode(), estimating);
assertEquals(comp2.getCurrentBytes(true), uskCompBytes);
- assertEquals(comp2.getClass().getSimpleName(), "HeapCompactOrderedSketch");
+ assertEquals(comp2.getClass().getSimpleName(), "HeapCompactSketch");
byte[] memArr2 = new byte[uskCompBytes];
WritableMemory mem2 = WritableMemory.wrap(memArr2); //allocate mem for compact form
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org