Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BloomFilter equals tests and refactoring #448

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.17.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.teragrep</groupId>
<artifactId>pth_06</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import com.teragrep.pth10.ast.bo.Token;
import com.teragrep.pth10.ast.commands.logicalstatement.LogicalStatementCatalyst;
import com.teragrep.pth10.ast.commands.logicalstatement.LogicalStatementXML;
import com.teragrep.pth10.ast.commands.transformstatement.teragrep.ModeFromBloomContext;
import com.teragrep.pth10.ast.commands.transformstatement.teragrep.ContextBloomMode;
import com.teragrep.pth10.ast.commands.transformstatement.teragrep.EstimateColumnFromBloomContext;
import com.teragrep.pth10.ast.commands.transformstatement.teragrep.InputColumnFromBloomContext;
import com.teragrep.pth10.ast.commands.transformstatement.teragrep.OutputColumnFromBloomContext;
Expand Down Expand Up @@ -480,7 +480,7 @@ public Node visitT_bloomModeParameter(final DPLParser.T_bloomModeParameterContex
@Override
public Node visitT_bloomOptionParameter(final DPLParser.T_bloomOptionParameterContext ctx) {
// values from context
final ContextValue<TeragrepBloomStep.BloomMode> mode = new ModeFromBloomContext(ctx);
final ContextValue<TeragrepBloomStep.BloomMode> mode = new ContextBloomMode(ctx);
final ContextValue<String> inputCol = new InputColumnFromBloomContext(ctx);
final ContextValue<String> outputCol = new OutputColumnFromBloomContext(ctx, inputCol.value());
final ContextValue<String> estimateCol = new EstimateColumnFromBloomContext(ctx, inputCol.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
import com.teragrep.pth10.steps.teragrep.TeragrepBloomStep;
import com.teragrep.pth_03.antlr.DPLParser;

public final class ModeFromBloomContext implements ContextValue<TeragrepBloomStep.BloomMode> {
public final class ContextBloomMode implements ContextValue<TeragrepBloomStep.BloomMode> {

private final DPLParser.T_bloomOptionParameterContext ctx;

public ModeFromBloomContext(final DPLParser.T_bloomOptionParameterContext ctx) {
public ContextBloomMode(final DPLParser.T_bloomOptionParameterContext ctx) {
this.ctx = ctx;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import java.sql.Connection;
import java.util.Iterator;
import java.util.Objects;

public final class BloomFilterForeachPartitionFunction implements ForeachPartitionFunction<Row> {

Expand Down Expand Up @@ -101,4 +102,18 @@ public void call(final Iterator<Row> iter) throws Exception {
conn.commit();
}
}

@Override
public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass())
return false;
final BloomFilterForeachPartitionFunction cast = (BloomFilterForeachPartitionFunction) o;
return filterTypes.equals(cast.filterTypes) && lazyConnection.equals(cast.lazyConnection)
&& overwrite == cast.overwrite && tableName.equals(cast.tableName) && regex.equals(cast.regex);
}

@Override
public int hashCode() {
return Objects.hash(filterTypes, lazyConnection, overwrite, tableName, regex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public TeragrepBloomFilter(
String tableName,
String regex
) {
this(partition, new ToBloomFilter(bytes), connection, filterTypes, tableName, regex);
this(partition, new ToBloomFilter(bytes).fromBytes(), connection, filterTypes, tableName, regex);
}

public TeragrepBloomFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,127 +46,33 @@
package com.teragrep.pth10.steps.teragrep.bloomfilter;

import org.apache.spark.util.sketch.BloomFilter;
import org.apache.spark.util.sketch.IncompatibleMergeException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* BloomFilter from byte[] used in a constructor of TeragrepBloomFilter
*
* @see TeragrepBloomFilter
*/
public final class ToBloomFilter extends BloomFilter {
public final class ToBloomFilter {

private final byte[] bytes;
private final List<BloomFilter> cache;

public ToBloomFilter(byte[] bytes) {
super();
public ToBloomFilter(final byte[] bytes) {
this.bytes = bytes;
this.cache = new ArrayList<>();
}

private BloomFilter fromBytes() {
// cache used to keep just one instance of the BloomFilter impl
if (cache.isEmpty()) {
final BloomFilter filter;
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
filter = BloomFilter.readFrom(bais);
}
catch (IOException e) {
throw new RuntimeException("Error reading bytes to filter: " + e.getMessage());
}
cache.add(filter);
public BloomFilter fromBytes() {
final BloomFilter filter;
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
filter = BloomFilter.readFrom(bais);
}
return cache.get(0);
}

@Override
public double expectedFpp() {
return fromBytes().expectedFpp();
}

@Override
public long bitSize() {
return fromBytes().bitSize();
}

@Override
public boolean put(final Object item) {
return fromBytes().put(item);
}

@Override
public boolean putString(final String item) {
return fromBytes().putString(item);
}

@Override
public boolean putLong(final long item) {
return fromBytes().putLong(item);
}

@Override
public boolean putBinary(final byte[] item) {
return fromBytes().putBinary(item);
}

@Override
public boolean isCompatible(final BloomFilter other) {
if (other.getClass() == this.getClass()) {
final ToBloomFilter cast = (ToBloomFilter) other;
return fromBytes().isCompatible(cast.fromBytes());
}
return fromBytes().isCompatible(other);
}

@Override
public BloomFilter mergeInPlace(final BloomFilter other) throws IncompatibleMergeException {
if (other.getClass() == this.getClass()) {
final ToBloomFilter cast = (ToBloomFilter) other;
return fromBytes().mergeInPlace(cast.fromBytes());
catch (IOException e) {
throw new RuntimeException("Error reading bytes to filter: " + e.getMessage());
}
return fromBytes().mergeInPlace(other);
}

@Override
public BloomFilter intersectInPlace(final BloomFilter other) throws IncompatibleMergeException {
if (other.getClass() == this.getClass()) {
final ToBloomFilter cast = (ToBloomFilter) other;
return fromBytes().intersectInPlace(cast.fromBytes());
}
return fromBytes().intersectInPlace(other);
}

@Override
public boolean mightContain(final Object item) {
return fromBytes().mightContain(item);
}

@Override
public boolean mightContainString(final String item) {
return fromBytes().mightContainString(item);
}

@Override
public boolean mightContainLong(final long item) {
return fromBytes().mightContainLong(item);
}

@Override
public boolean mightContainBinary(final byte[] item) {
return fromBytes().mightContainBinary(item);
}

@Override
public void writeTo(final OutputStream out) throws IOException {
fromBytes().writeTo(out);
return filter;
}

@Override
Expand All @@ -183,6 +89,6 @@ public boolean equals(final Object object) {

@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(bytes), cache);
return Arrays.hashCode(bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Teragrep Data Processing Language (DPL) translator for Apache Spark (pth_10)
* Copyright (C) 2019-2024 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.pth10.steps.teragrep.bloomfilter;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

public class BloomFilterForeachPartitionFunctionTest {

@Test
public void testEqualsVerifier() {
EqualsVerifier
.forClass(BloomFilterForeachPartitionFunction.class)
.withNonnullFields("filterTypes", "lazyConnection", "overwrite", "tableName", "regex")
.verify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.*;

import java.sql.Connection;
Expand Down Expand Up @@ -182,6 +183,11 @@ void testNotEqualsIgnoreConstraints() {
Assertions.assertNotEquals(table1, table2);
}

@Test
void testEqualsVerifier() {
EqualsVerifier.forClass(BloomFilterTable.class).withNonnullFields("tableSQL", "conn").verify();
}

private Properties getDefaultProperties() {
final Properties properties = new Properties();
properties.put("dpl.pth_10.bloom.db.username", username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.apache.spark.util.sketch.BloomFilter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -170,6 +171,11 @@ public void testNotEquals() {
assertNotEquals(filterTypes1, filterTypes2);
}

@Test
public void testEqualsVerifier() {
EqualsVerifier.forClass(FilterTypes.class).withNonnullFields("config").verify();
}

public Properties defaultProperties() {
Properties properties = new Properties();
properties.put("dpl.pth_10.bloom.db.username", username);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Teragrep Data Processing Language (DPL) translator for Apache Spark (pth_10)
* Copyright (C) 2019-2024 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.pth10.steps.teragrep.bloomfilter;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

public class JournalDBNameFromConfigTest {

@Test
public void testEqualsVerifier() {
EqualsVerifier.forClass(JournalDBNameFromConfig.class).withNonnullFields("config").verify();
}
}
Loading