Skip to content
Open
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
50 changes: 22 additions & 28 deletions src/main/java/org/apache/datasketches/cpc/CpcSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@
import static java.lang.Math.log;
import static java.lang.Math.sqrt;
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.datasketches.common.Util.LS;
import static org.apache.datasketches.common.Util.invPow2;
import static org.apache.datasketches.common.Util.zeroPad;
import static org.apache.datasketches.cpc.CpcUtil.bitMatrixOfSketch;
import static org.apache.datasketches.cpc.CpcUtil.checkLgK;
import static org.apache.datasketches.cpc.CpcUtil.countBitsSetInMatrix;
import static org.apache.datasketches.hash.MurmurHash3.hash;

import java.lang.foreign.MemorySegment;
import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.datasketches.common.Family;
import org.apache.datasketches.common.Util;
import org.apache.datasketches.hash.ItemHash;

/**
* This is a unique-counting sketch that implements the
Expand Down Expand Up @@ -309,9 +308,7 @@ public byte[] toByteArray() {
* @param datum The given long datum.
*/
public void update(final long datum) {
final long[] data = { datum };
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(datum, seed));
}

/**
Expand All @@ -324,10 +321,7 @@ public void update(final long datum) {
* @param datum The given double datum.
*/
public void update(final double datum) {
final double d = (datum == 0.0) ? 0.0 : datum; // canonicalize -0.0, 0.0
final long[] data = { Double.doubleToLongBits(d) };// canonicalize all NaN forms
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(datum, seed));
}

/**
Expand All @@ -345,10 +339,7 @@ public void update(final double datum) {
* @param datum The given String.
*/
public void update(final String datum) {
if ((datum == null) || datum.isEmpty()) { return; }
final byte[] data = datum.getBytes(UTF_8);
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(datum, seed));
}

/**
Expand All @@ -358,9 +349,7 @@ public void update(final String datum) {
* @param data The given byte array.
*/
public void update(final byte[] data) {
if ((data == null) || (data.length == 0)) { return; }
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(data, seed));
}

/**
Expand All @@ -370,9 +359,7 @@ public void update(final byte[] data) {
* @param data The given ByteBuffer
*/
public void update(final ByteBuffer data) {
if ((data == null) || !data.hasRemaining()) { return; }
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(data, seed));
}

/**
Expand All @@ -385,9 +372,7 @@ public void update(final ByteBuffer data) {
* @param data The given char array.
*/
public void update(final char[] data) {
if ((data == null) || (data.length == 0)) { return; }
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(data, seed));
}

/**
Expand All @@ -397,9 +382,7 @@ public void update(final char[] data) {
* @param data The given int array.
*/
public void update(final int[] data) {
if ((data == null) || (data.length == 0)) { return; }
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(data, seed));
}

/**
Expand All @@ -409,9 +392,20 @@ public void update(final int[] data) {
* @param data The given long array.
*/
public void update(final long[] data) {
if ((data == null) || (data.length == 0)) { return; }
final long[] arr = hash(data, seed);
hashUpdate(arr[0], arr[1]);
update(ItemHash.of(data, seed));
}

/**
* Present this sketch with a precomputed item hash, as if updating with the item itself.
* A null hash is ignored.
*
* @param hash the item hash, computed with this sketch's seed
* @throws org.apache.datasketches.common.SketchesArgumentException if the hash was computed with a different seed
*/
public void update(final ItemHash hash) {
if (hash == null) { return; }
hash.checkSeed(seed);
hashUpdate(hash.h0(), hash.h1());
}

/**
Expand Down
157 changes: 157 additions & 0 deletions src/main/java/org/apache/datasketches/hash/ItemHash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* 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.hash;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.lang.foreign.MemorySegment;
import java.nio.ByteBuffer;

import org.apache.datasketches.common.SketchesArgumentException;

/**
* The canonicalized 128-bit MurmurHash3 of one update item, together with the seed it was computed with.
*
* <p>Every hash-based sketch derives its internal value from this hash, so one instance can update
* several sketches that share the seed, producing the same result as updating each with the item.
* The factory methods return null for null or empty items, which sketches ignore.</p>
*/
public final class ItemHash {
private final long h0;
private final long h1;
private final long seed;

private ItemHash(final long[] hash, final long seed) {
this.h0 = hash[0];
this.h1 = hash[1];
this.seed = seed;
}

/**
* Hashes a long.
* @param datum the item
* @param seed the update seed
* @return the item hash
*/
public static ItemHash of(final long datum, final long seed) {
return new ItemHash(MurmurHash3.hash(datum, seed), seed);
}

/**
* Hashes a double, treating -0.0 as 0.0 and all NaN forms as one value.
* @param datum the item
* @param seed the update seed
* @return the item hash
*/
public static ItemHash of(final double datum, final long seed) {
return of(Double.doubleToLongBits(datum == 0.0 ? 0.0 : datum), seed);
}

/**
* Hashes the UTF-8 encoding of a String.
* @param datum the item
* @param seed the update seed
* @return the item hash, or null if the item is null or empty
*/
public static ItemHash of(final String datum, final long seed) {
return datum == null || datum.isEmpty() ? null : new ItemHash(MurmurHash3.hash(datum.getBytes(UTF_8), seed), seed);
}

/**
* Hashes a byte array.
* @param data the item
* @param seed the update seed
* @return the item hash, or null if the item is null or empty
*/
public static ItemHash of(final byte[] data, final long seed) {
return data == null || data.length == 0 ? null : new ItemHash(MurmurHash3.hash(data, seed), seed);
}

/**
* Hashes a char array.
* @param data the item
* @param seed the update seed
* @return the item hash, or null if the item is null or empty
*/
public static ItemHash of(final char[] data, final long seed) {
return data == null || data.length == 0 ? null : new ItemHash(MurmurHash3.hash(data, seed), seed);
}

/**
* Hashes an int array.
* @param data the item
* @param seed the update seed
* @return the item hash, or null if the item is null or empty
*/
public static ItemHash of(final int[] data, final long seed) {
return data == null || data.length == 0 ? null : new ItemHash(MurmurHash3.hash(data, seed), seed);
}

/**
* Hashes a long array.
* @param data the item
* @param seed the update seed
* @return the item hash, or null if the item is null or empty
*/
public static ItemHash of(final long[] data, final long seed) {
return data == null || data.length == 0 ? null : new ItemHash(MurmurHash3.hash(data, seed), seed);
}

/**
* Hashes the remaining bytes of a ByteBuffer.
* @param data the item
* @param seed the update seed
* @return the item hash, or null if the item is null or has no remaining bytes
*/
public static ItemHash of(final ByteBuffer data, final long seed) {
return data == null || !data.hasRemaining() ? null : new ItemHash(MurmurHash3.hash(data, seed), seed);
}

/**
* Hashes the bytes of a MemorySegment.
* @param data the item
* @param seed the update seed
* @return the item hash, or null if the item is null or empty
*/
public static ItemHash of(final MemorySegment data, final long seed) {
return data == null || data.byteSize() == 0 ? null : new ItemHash(MurmurHash3.hash(data, seed), seed);
}

/**
* Returns the low 64 bits of the hash.
* @return the low 64 bits
*/
public long h0() { return h0; }

/**
* Returns the high 64 bits of the hash.
* @return the high 64 bits
*/
public long h1() { return h1; }

/**
* Checks that this hash was computed with the given seed.
* @param seed the seed of the sketch being updated
* @throws SketchesArgumentException if the seeds differ
*/
public void checkSeed(final long seed) {
if (this.seed != seed) { throw new SketchesArgumentException("ItemHash seed does not match the sketch seed"); }
}
}
Loading
Loading