Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions src/main/java/org/apache/datasketches/tuple/CompactTupleSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
import static java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED;
import static java.lang.foreign.ValueLayout.JAVA_LONG_UNALIGNED;
import static org.apache.datasketches.tuple.PreambleUtil.COMPACT_FLAGS_BYTE;
import static org.apache.datasketches.tuple.PreambleUtil.COMPACT_SER_VER;
import static org.apache.datasketches.tuple.PreambleUtil.COMPACT_SER_VER_LEGACY;
import static org.apache.datasketches.tuple.PreambleUtil.COMPACT_SER_VER_WITH_CLASS_NAME;
import static org.apache.datasketches.tuple.PreambleUtil.FAMILY_BYTE;
import static org.apache.datasketches.tuple.PreambleUtil.LEGACY_FLAGS_BYTE;
import static org.apache.datasketches.tuple.PreambleUtil.PREAMBLE_LONGS_BYTE;
import static org.apache.datasketches.tuple.PreambleUtil.RETAINED_ENTRIES_INT;
import static org.apache.datasketches.tuple.PreambleUtil.SEED_HASH_SHORT;
import static org.apache.datasketches.tuple.PreambleUtil.SER_VER_BYTE;
import static org.apache.datasketches.tuple.PreambleUtil.SKETCH_TYPE_BYTE;
import static org.apache.datasketches.tuple.PreambleUtil.THETA_LONG;
import static org.apache.datasketches.thetacommon.HashOperations.count;

import java.lang.foreign.MemorySegment;
Expand All @@ -42,9 +54,6 @@
* @param <S> type of Summary
*/
public final class CompactTupleSketch<S extends Summary> extends TupleSketch<S> {
private static final byte serialVersionWithSummaryClassNameUID = 1;
private static final byte serialVersionUIDLegacy = 2;
private static final byte serialVersionUID = 3;
private static final short defaultSeedHash = (short) 37836; // for compatibility with C++
private final long[] hashArr_;
private S[] summaryArr_;
Expand Down Expand Up @@ -76,19 +85,19 @@ private enum Flags { IS_RESERVED, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED
*/
CompactTupleSketch(final MemorySegment seg, final SummaryDeserializer<S> deserializer) {
super(Long.MAX_VALUE, true, null);
int offset = 0;
final byte preambleLongs = seg.get(JAVA_BYTE, offset++);
final byte version = seg.get(JAVA_BYTE, offset++);
final byte familyId = seg.get(JAVA_BYTE, offset++);
final byte preambleLongs = seg.get(JAVA_BYTE, PREAMBLE_LONGS_BYTE);
final byte version = seg.get(JAVA_BYTE, SER_VER_BYTE);
final byte familyId = seg.get(JAVA_BYTE, FAMILY_BYTE);
SerializerDeserializer.validateFamily(familyId, preambleLongs);
if (version > serialVersionUID) {
if (version > COMPACT_SER_VER) {
throw new SketchesArgumentException(
"Unsupported serial version. Expected: " + serialVersionUID + " or lower, actual: " + version);
"Unsupported serial version. Expected: " + COMPACT_SER_VER + " or lower, actual: " + version);
}
SerializerDeserializer
.validateType(seg.get(JAVA_BYTE, offset++), SerializerDeserializer.SketchType.CompactTupleSketch);
if (version <= serialVersionUIDLegacy) { // legacy serial format
final byte flags = seg.get(JAVA_BYTE, offset++);
.validateType(seg.get(JAVA_BYTE, SKETCH_TYPE_BYTE), SerializerDeserializer.SketchType.CompactTupleSketch);
if (version <= COMPACT_SER_VER_LEGACY) { // legacy serial format: fields after the flags are packed
final byte flags = seg.get(JAVA_BYTE, LEGACY_FLAGS_BYTE);
int offset = LEGACY_FLAGS_BYTE + 1;
empty_ = (flags & (1 << FlagsLegacy.IS_EMPTY.ordinal())) > 0;
final boolean isThetaIncluded = (flags & (1 << FlagsLegacy.IS_THETA_INCLUDED.ordinal())) > 0;
if (isThetaIncluded) {
Expand All @@ -100,12 +109,12 @@ private enum Flags { IS_RESERVED, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED
final boolean hasEntries = (flags & (1 << FlagsLegacy.HAS_ENTRIES.ordinal())) > 0;
if (hasEntries) {
int classNameLength = 0;
if (version == serialVersionWithSummaryClassNameUID) {
if (version == COMPACT_SER_VER_WITH_CLASS_NAME) {
classNameLength = seg.get(JAVA_BYTE, offset++);
}
final int count = seg.get(JAVA_INT_UNALIGNED, offset);
offset += Integer.BYTES;
if (version == serialVersionWithSummaryClassNameUID) {
if (version == COMPACT_SER_VER_WITH_CLASS_NAME) {
offset += classNameLength;
}
hashArr_ = new long[count];
Expand All @@ -122,25 +131,21 @@ private enum Flags { IS_RESERVED, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED
summaryArr_ = null;
}
} else { // current serial format
offset++; //skip unused byte
final byte flags = seg.get(JAVA_BYTE, offset++);
offset += 2; //skip 2 unused bytes
final byte flags = seg.get(JAVA_BYTE, COMPACT_FLAGS_BYTE);
empty_ = (flags & (1 << Flags.IS_EMPTY.ordinal())) > 0;
thetaLong_ = Long.MAX_VALUE;
int count = 0;
if (!empty_) {
if (preambleLongs == 1) {
count = 1;
} else {
count = seg.get(JAVA_INT_UNALIGNED, offset);
offset += Integer.BYTES;
offset += 4; // unused
count = seg.get(JAVA_INT_UNALIGNED, RETAINED_ENTRIES_INT);
if (preambleLongs > 2) {
thetaLong_ = seg.get(JAVA_LONG_UNALIGNED, offset);
offset += Long.BYTES;
thetaLong_ = seg.get(JAVA_LONG_UNALIGNED, THETA_LONG);
}
}
}
int offset = preambleLongs * Long.BYTES; // the entries start after the preamble
hashArr_ = new long[count];

for (int i = 0; i < count; i++) {
Expand Down Expand Up @@ -210,30 +215,26 @@ public byte[] toByteArray() {

final int sizeBytes = (Long.BYTES * preambleLongs) + (Long.BYTES * count) + summariesSizeBytes;
final byte[] bytes = new byte[sizeBytes];
int offset = 0;
bytes[offset++] = (byte) preambleLongs;
bytes[offset++] = serialVersionUID;
bytes[offset++] = (byte) Family.TUPLE.getID();
bytes[offset++] = (byte) SerializerDeserializer.SketchType.CompactTupleSketch.ordinal();
offset++; // unused
bytes[offset++] = (byte) (
bytes[PREAMBLE_LONGS_BYTE] = (byte) preambleLongs;
bytes[SER_VER_BYTE] = COMPACT_SER_VER;
bytes[FAMILY_BYTE] = (byte) Family.TUPLE.getID();
bytes[SKETCH_TYPE_BYTE] = (byte) SerializerDeserializer.SketchType.CompactTupleSketch.ordinal();
bytes[COMPACT_FLAGS_BYTE] = (byte) (
(1 << Flags.IS_COMPACT.ordinal())
| (1 << Flags.IS_READ_ONLY.ordinal())
| (isEmpty() ? 1 << Flags.IS_EMPTY.ordinal() : 0)
);
ByteArrayUtil.putShortLE(bytes, offset, isEmpty() ? 0 : defaultSeedHash); // empty image carries zero, as in theta
offset += Short.BYTES;
// an empty image carries zero, as in theta
ByteArrayUtil.putShortLE(bytes, SEED_HASH_SHORT, isEmpty() ? 0 : defaultSeedHash);
if (!isEmpty()) {
if (!isSingleItem) {
ByteArrayUtil.putIntLE(bytes, offset, count);
offset += Integer.BYTES;
offset += 4; // unused
ByteArrayUtil.putIntLE(bytes, RETAINED_ENTRIES_INT, count);
if (isEstimationMode()) {
ByteArrayUtil.putLongLE(bytes, offset, thetaLong_);
offset += Long.BYTES;
ByteArrayUtil.putLongLE(bytes, THETA_LONG, thetaLong_);
}
}
}
int offset = preambleLongs * Long.BYTES; // the entries start after the preamble
for (int i = 0; i < count; i++) {
ByteArrayUtil.putLongLE(bytes, offset, hashArr_[i]);
offset += Long.BYTES;
Expand Down
195 changes: 195 additions & 0 deletions src/main/java/org/apache/datasketches/tuple/PreambleUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* 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.tuple;

//@formatter:off

/**
* This class documents the serialized forms of the generic tuple sketches,
* {@link CompactTupleSketch} and the updatable {@link QuickSelectSketch}, in one place.
* The ArrayOfDoubles sketches in the <i>arrayofdoubles</i> sub-package have their own layouts,
* which are documented in those classes.
*
* <p>MAP: Low significance bytes of this <i>long</i> data structure are on the right.
* All multi-byte values (<i>short</i>, <i>int</i>, <i>long</i>, <i>float</i> and <i>double</i>)
* are stored little-endian. The <i>byte</i> values are treated as unsigned.</p>
*
* <p>Unlike the theta sketches, a tuple sketch image is not a fixed-size table of longs.
* Each retained entry is a hash (8 bytes) followed by the bytes of its Summary, and a Summary
* may have any length. The Summary bytes are written by {@link Summary#toByteArray()} and read by a
* {@link SummaryDeserializer}, both supplied by the user. The sketch image does not record the
* Summary type. A reader must therefore know which Summary was used to write the image.</p>
*
* <h2>CompactTupleSketch, serial version 3 (current)</h2>
*
* <p>This form is binary compatible with the C++ and Go compact tuple sketches, as long as
* every language uses the same Summary encoding.</p>
*
* <p>Flags: ReadOnly, Compact, Empty if empty. The Ordered flag is defined but Java does not set it.
* The seed hash is zero when the sketch is empty, as in theta.</p>
*
* <p>An empty sketch, or a single-item sketch that is not in estimation mode, uses 1 preamble long.
* The single entry, if any, starts at byte 8.</p>
*
* <pre>
* Long || Start Byte Adr:
* Adr:
* || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* 0 || Seed Hash | Flags | unused | SkType | FamID | SerVer | PreLongs = 1 |
*
* || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
* 1 ||-------------------------Hash of the single entry (if not empty)-------------------|
*
* || | | | | | | | 16 |
* 2 ||-------------------------Summary of the single entry-------------------------------|
* </pre>
*
* <p>An exact (not estimating) sketch with more than one entry uses 2 preamble longs.</p>
*
* <pre>
* Long || Start Byte Adr:
* Adr:
* || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* 0 || Seed Hash | Flags | unused | SkType | FamID | SerVer | PreLongs = 2 |
*
* || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
* 1 ||-------------unused----------------|----------Retained Entries Count---------------|
*
* || 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
* 2 ||-------------Entries: hash (8 bytes), then Summary bytes, repeated Count times-----|
* </pre>
*
* <p>An estimating sketch uses 3 preamble longs.</p>
*
* <pre>
* Long || Start Byte Adr:
* Adr:
* || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* 0 || Seed Hash | Flags | unused | SkType | FamID | SerVer | PreLongs = 3 |
*
* || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
* 1 ||-------------unused----------------|----------Retained Entries Count---------------|
*
* || 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
* 2 ||------------------------------THETA_LONG-------------------------------------------|
*
* || 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
* 3 ||-------------Entries: hash (8 bytes), then Summary bytes, repeated Count times-----|
* </pre>
*
* <h2>CompactTupleSketch, serial versions 1 and 2 (legacy, read only)</h2>
*
* <p>Java no longer writes these forms but can still read them. The fields after byte 4 are
* packed with no alignment, and each one is present only if its flag is set.</p>
*
* <pre>
* Byte Adr | Size | Field
* 0 | 1 | PreLongs
* 1 | 1 | SerVer = 1 or 2
* 2 | 1 | FamID
* 3 | 1 | SkType = CompactTupleSketch
* 4 | 1 | Flags (legacy bits, see below)
* 5 | 8 | THETA_LONG, if IS_THETA_INCLUDED
* next | 1 | Summary class-name length N, if HAS_ENTRIES and SerVer = 1
* next | 4 | Retained Entries Count, if HAS_ENTRIES
* next | N | Summary class name, if HAS_ENTRIES and SerVer = 1 (skipped)
* next | 8*C | Count hashes, if HAS_ENTRIES
* next | var | Count Summaries, if HAS_ENTRIES
* </pre>
*
* <h2>QuickSelectSketch (updatable), serial version 2 (deprecated)</h2>
*
* <p>Serializing an updatable sketch is deprecated. This form has no seed hash and is
* read only by Java. As with the legacy compact form, the fields after byte 7 are
* packed, and each one is present only if its flag is set.</p>
*
* <pre>
* Long || Start Byte Adr:
* Adr:
* || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* 0 || LgRF | LgArr | LgNom | Flags | SkType | FamID | SerVer | PreLongs = 1 |
*
* Byte Adr | Size | Field
* 8 | 4 | Sampling probability p (float), if IS_IN_SAMPLING_MODE
* next | 8 | THETA_LONG, if IS_THETA_INCLUDED
* next | 4 | Retained Entries Count, if HAS_ENTRIES
* next | var | Entries: hash (8 bytes), then Summary bytes, repeated Count times
* </pre>
*
* <h2>Flags byte</h2>
*
* <pre>
* Bit | CompactTupleSketch v3 | CompactTupleSketch v1, v2 | QuickSelectSketch v2
* 0 | Reserved | IS_BIG_ENDIAN (unused) | Reserved
* 1 | IS_READ_ONLY | IS_EMPTY | IS_IN_SAMPLING_MODE
* 2 | IS_EMPTY | HAS_ENTRIES | IS_EMPTY
* 3 | IS_COMPACT | IS_THETA_INCLUDED | HAS_ENTRIES
* 4 | IS_ORDERED | | IS_THETA_INCLUDED
* </pre>
*
* <h2>Summary encodings of the example Summaries in this library</h2>
*
* <p>The Summaries in the <i>adouble</i>, <i>aninteger</i> and <i>strings</i> sub-packages are
* examples of user-defined Summaries. Their encodings are specific to Java, and other languages
* can read them only through custom code. The Mode byte is the ordinal of the Summary's Mode:
* Sum = 0, Min = 1, Max = 2, AlwaysOne = 3.</p>
*
* <pre>
* Summary | Bytes | Encoding
* DoubleSummary | 9 | double value (bytes 0-7), Mode (byte 8)
* IntegerSummary | 5 | int value (bytes 0-3), Mode (byte 4)
* ArrayOfStringsSummary | var | int total bytes, byte number of strings,
* | | then for each string: int length, then its UTF-8 bytes
* </pre>
*
* @author Lee Rhodes
*/
final class PreambleUtil {
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed

private PreambleUtil() {}

// Byte addresses common to all forms
static final int PREAMBLE_LONGS_BYTE = 0;
static final int SER_VER_BYTE = 1;
static final int FAMILY_BYTE = 2;
static final int SKETCH_TYPE_BYTE = 3;

// CompactTupleSketch, serial version 3
static final int COMPACT_FLAGS_BYTE = 5;
static final int SEED_HASH_SHORT = 6; //bytes 6,7
static final int RETAINED_ENTRIES_INT = 8; //only if PreLongs > 1
static final int THETA_LONG = 16; //only if PreLongs = 3

// CompactTupleSketch, serial versions 1 and 2, and QuickSelectSketch
static final int LEGACY_FLAGS_BYTE = 4;

// QuickSelectSketch, serial version 2
static final int LG_NOM_ENTRIES_BYTE = 5;
static final int LG_CUR_CAPACITY_BYTE = 6;
static final int LG_RESIZE_FACTOR_BYTE = 7;
static final int QS_VARIABLE_START = 8;

// Serial versions
static final byte COMPACT_SER_VER_WITH_CLASS_NAME = 1;
static final byte COMPACT_SER_VER_LEGACY = 2;
static final byte COMPACT_SER_VER = 3;
static final byte QUICK_SELECT_SER_VER = 2;

}
Loading
Loading