Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
SongY123 committed Apr 11, 2024
1 parent 24072cb commit 1dc2ccd
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 63 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,8 @@ openhufu:
implementor:
aggregate:
sum: com.hufudb.openhufu.owner.implementor.aggregate.sum.SecretSharingSum
count: null
max: null
min: null
avg: null
join: com.hufudb.openhufu.owner.implementor.join.HashJoin
```
3. Build OpenHuFu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public enum ErrorCode {
// data type error
DATA_TYPE_NOT_SUPPORT(40001, "data type: {} not support"),

// query error
QUERY_ERROR(50001, "query error"),

UNSUPPORTED_OPERATION(60001, "unsupported operation {}"),

SETUP_FAILED(90001, "setup failed"),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,24 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin) {
return privacySpatialJoin(plan, isDistanceJoin, false);
}

private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, boolean isUsingKNNFunc) {
private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin,
boolean isUsingKNNFunc) {
DataSet left = ownerSideQuery(plan.getChildren().get(0));
DataSetIterator leftIter = left.getIterator();
List<ArrayRow> arrayRows = new ArrayList<>();

boolean containsLeftKey = false;
int leftKey = -1;
for (OpenHuFuPlan.Expression expression: plan.getSelectExps()) {
for (OpenHuFuPlan.Expression expression : plan.getSelectExps()) {
if (expression.getOpType().equals(OpenHuFuPlan.OperatorType.REF)
&& expression.getI32() == plan.getJoinCond().getCondition().getIn(0).getI32()) {
&& expression.getI32() == plan.getJoinCond().getCondition().getIn(0).getI32()) {
containsLeftKey = true;
}
}
if (!containsLeftKey) {
for (int i = 0; i < plan.getChildren().get(0).getSelectExps().size(); i++) {
if (plan.getChildren().get(0).getSelectExps().get(i).getI32()
== plan.getJoinCond().getCondition().getIn(0).getI32()) {
if (plan.getChildren().get(0).getSelectExps().get(i).getI32() == plan.getJoinCond()
.getCondition().getIn(0).getI32()) {
leftKey = i;
break;
}
Expand All @@ -157,16 +158,17 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, bool

boolean containsRightKey = false;
int rightKey = -1;
for (OpenHuFuPlan.Expression expression: plan.getSelectExps()) {
for (OpenHuFuPlan.Expression expression : plan.getSelectExps()) {
if (expression.getOpType().equals(OpenHuFuPlan.OperatorType.REF)
&& expression.getI32() == plan.getJoinCond().getCondition().getIn(1).getI32()) {
&& expression.getI32() == plan.getJoinCond().getCondition().getIn(1).getI32()) {
containsRightKey = true;
}
}
if (!containsRightKey) {
for (int i = 0; i < plan.getChildren().get(1).getSelectExps().size(); i++) {
if (plan.getChildren().get(1).getSelectExps().get(i).getI32()
== plan.getJoinCond().getCondition().getIn(1).getI32() - plan.getChildren().get(0).getSelectExps().size()) {
== plan.getJoinCond().getCondition().getIn(1).getI32() - plan.getChildren().get(0)
.getSelectExps().size()) {
rightKey = i;
break;
}
Expand All @@ -176,11 +178,13 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, bool
int leftRef = plan.getJoinCond().getCondition().getIn(0).getI32();
DataSet rightDataSet;
if (isDistanceJoin) {
rightDataSet = ownerSideQuery(DistanceJoin
.generateDistanceQueryPlan(plan, leftIter.get(leftRef).toString(), rightKey));
}
else {
rightDataSet = privacyKNN((UnaryPlan) KNNJoin.generateKNNQueryPlan(plan, leftIter.get(leftRef).toString(), rightKey), isUsingKNNFunc);
rightDataSet = ownerSideQuery(
DistanceJoin.generateDistanceQueryPlan(plan, leftIter.get(leftRef).toString(),
rightKey));
} else {
rightDataSet = privacyKNN(
(UnaryPlan) KNNJoin.generateKNNQueryPlan(plan, leftIter.get(leftRef).toString(),
rightKey), isUsingKNNFunc);
}
DataSetIterator rightIter = rightDataSet.getIterator();
while (rightIter.next()) {
Expand All @@ -189,18 +193,18 @@ private DataSet privacySpatialJoin(BinaryPlan plan, boolean isDistanceJoin, bool
}
}
Schema schema;
schema = ExpressionUtils.createSchema(plan.getSelectExps());
schema = ExpressionUtils.createSchema(plan.getSelectExps());
LOG.info(schema.toString());
return new ArrayDataSet(schema, arrayRows);
}

@Override
public DataSet implement(Plan plan) {
LOG.info(plan.toString());
boolean isUsingKNNFuc = plan instanceof LeafPlan
&& !plan.getWhereExps().isEmpty()
&& plan.getWhereExps().get(0).getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC)
&& plan.getWhereExps().get(0).getStr().equals("knn");
boolean isUsingKNNFuc =
plan instanceof LeafPlan && !plan.getWhereExps().isEmpty() && plan.getWhereExps().get(0)
.getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC) && plan.getWhereExps().get(0)
.getStr().equals("knn");
if (isUsingKNNFuc) {
plan = KNNConverter.convertKNN((LeafPlan) plan);
}
Expand Down Expand Up @@ -231,7 +235,7 @@ private DataSet privacyKNN(UnaryPlan plan, boolean isUsingKNNFunc) {
double left = 0;
double right = 1000000;
// if (USE_DP) {
right = kNNRadiusQuery(plan) * 2;
right = kNNRadiusQuery(plan) * 2;
// }
double deviation = 1e-6;
int loop = 0;
Expand All @@ -257,25 +261,25 @@ private DataSet privacyKNN(UnaryPlan plan, boolean isUsingKNNFunc) {
}
while (left + deviation <= right) {
double mid = (left + right) / 2;
// int sign = privacyCompare(plan);
int sign = (int) privacyCompare(plan, mid, k);
LOG.debug("loop {} with sign {}", loop, sign);
if (sign < 0) {
left = mid;
} else if (sign > 0) {
right = mid;
} else {
loop++;
DataSet dataSet = ArrayDataSet.materialize(kNNCircleRangeQuery(plan, mid, isUsingKNNFunc));
return dataSet;
}
loop++;
}
return kNNCircleRangeQuery(plan, right, isUsingKNNFunc);
}

private double kNNRadiusQuery(UnaryPlan plan) {
//todo -sjz
DataSetIterator dataSet = ownerSideQuery(BinarySearchKNN.generateKNNRadiusQueryPlan(plan)).getIterator();
DataSetIterator dataSet =
ownerSideQuery(BinarySearchKNN.generateKNNRadiusQueryPlan(plan)).getIterator();
double right = 1000000;
while (dataSet.next()) {
double res = (double) dataSet.get(0);
Expand All @@ -286,21 +290,27 @@ private double kNNRadiusQuery(UnaryPlan plan) {
}
return right;
}

private Pair<Double, Double> dPRangeCount(UnaryPlan plan) {
//todo -sjz
ownerSideQuery(BinarySearchKNN.generateDPRangeCountPlan(plan));
return null;
}

private long privacyCompare(UnaryPlan plan, double range, long k) {
//todo -sjz now it is using secretSharingSum
DataSetIterator dataSet = ownerSideQuery(BinarySearchKNN.generatePrivacyComparePlan(plan, range)).getIterator();
//todo -sjz now it is using secretSharingSum
DataSetIterator dataSet =
ownerSideQuery(BinarySearchKNN.generatePrivacyComparePlan(plan, range)).getIterator();
dataSet.next();
long res = (long) dataSet.get(0);
return res - k;
}

private DataSet kNNCircleRangeQuery(UnaryPlan plan, double range, boolean isUsingKNNFunc) {
return ownerSideQuery(BinarySearchKNN.generateKNNCircleRangeQueryPlan(plan, range, isUsingKNNFunc));
return ownerSideQuery(
BinarySearchKNN.generateKNNCircleRangeQueryPlan(plan, range, isUsingKNNFunc));
}

private boolean isMultiPartySecureKNN(UnaryPlan unary) {
LeafPlan leaf = (LeafPlan) unary.getChildren().get(0);
boolean hasLimit = leaf.getOffset() != 0 || leaf.getFetch() != 0;
Expand All @@ -311,7 +321,8 @@ private boolean isMultiPartySecureKNN(UnaryPlan unary) {
return false;
}
int orderRef = leaf.getOrders().get(0).getRef();
if (!(leaf.getSelectExps().get(orderRef).getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC)
if (!(
leaf.getSelectExps().get(orderRef).getOpType().equals(OpenHuFuPlan.OperatorType.SCALAR_FUNC)
&& leaf.getSelectExps().get(orderRef).getStr().equals("distance"))) {
return false;
}
Expand All @@ -321,6 +332,7 @@ private boolean isMultiPartySecureKNN(UnaryPlan unary) {
}
return false;
}

@Override
public DataSet binaryQuery(BinaryPlan binary) {
List<Plan> children = binary.getChildren();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public interface AggregateFunction<T, E> {
* add a T element to the set which is to be aggregated,
* meanwhile, update the output value (which is a E)
*/
// local add
void add(T ele);
// global aggregate
E aggregate();
AggregateFunction<T, E> copy();

public static <T, E> List<AggregateFunction<T, E>> copy(List<AggregateFunction<T, E>> funcs) {
static <T, E> List<AggregateFunction<T, E>> copy(List<AggregateFunction<T, E>> funcs) {
return funcs.stream().map(func -> func.copy()).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import com.hufudb.openhufu.data.storage.Row;

public abstract interface Aggregator extends AggregateFunction<Row, Row> {
public abstract boolean hasNext();
public interface Aggregator extends AggregateFunction<Row, Row> {
boolean hasNext();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@
import com.hufudb.openhufu.common.exception.OpenHuFuException;
import com.hufudb.openhufu.data.schema.Schema;
import com.hufudb.openhufu.proto.OpenHuFuData;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.Function;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.math3.distribution.LaplaceDistribution;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;

import java.util.*;
import java.util.function.Function;

/*
* Used for security union, insert fake record to dataset
*/
public class RandomDataSet {
public final static GeometryFactory geoFactory = new GeometryFactory();
private final static double RANDOM_SET_SCALE = 0.5;
private final static double EPS = 1.0;
public final static int RANDOM_SET_OFFSET = 10;
private final static LaplaceDistribution lap = new LaplaceDistribution(0, 1 / EPS);
private final static Random random = new Random();

private static final GeometryFactory geoFactory = new GeometryFactory();
private static final double RANDOM_SET_SCALE = 0.5;
private static final double EPS = 1.0;
private static final int RANDOM_SET_OFFSET = 10;
private static final LaplaceDistribution lap = new LaplaceDistribution(0, 1 / EPS);
private static final Random random = new SecureRandom();
private final Schema schema;
private final DataSet source;
private final List<ArrayRow> originRows;
private final int originSize;
private final int resultSize;
Expand All @@ -31,7 +40,6 @@ public class RandomDataSet {

public RandomDataSet(DataSet source) {
this.schema = source.getSchema();
this.source = source;
this.originRows = ArrayDataSet.materialize(source).rows;
this.originSize = originRows.size();
this.randomRows = new ArrayList<>();
Expand Down Expand Up @@ -129,7 +137,8 @@ private Object getRandomValueFromData(int columnIndex) {
Geometry geometry = (Geometry) originRows.get(r).get(columnIndex);
if (geometry instanceof Point) {
Point p = (Point) geometry;
return geoFactory.createPoint(new Coordinate(p.getX() + lap.sample(), p.getX() + lap.sample()));
return geoFactory.createPoint(
new Coordinate(p.getX() + lap.sample(), p.getX() + lap.sample()));
} else {
throw new OpenHuFuException(ErrorCode.DATA_TYPE_NOT_SUPPORT, type);
}
Expand Down Expand Up @@ -163,7 +172,7 @@ private Object getRandomValue(int columnIndex) {
case GEOMETRY:
return geoFactory.createPoint(new Coordinate(lap.sample(), lap.sample()));
case STRING:
return RandomStringUtils.randomAlphanumeric(RANDOM_SET_OFFSET);
return RandomStringUtils.randomAlphanumeric(RANDOM_SET_OFFSET); // NOSONAR
default:
throw new OpenHuFuException(ErrorCode.DATA_TYPE_NOT_SUPPORT, type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hufudb.openhufu.owner;

import com.hufudb.openhufu.common.exception.ErrorCode;
import com.hufudb.openhufu.common.exception.OpenHuFuException;
import com.hufudb.openhufu.owner.adapter.Adapter;
import com.hufudb.openhufu.owner.checker.Checker;
import com.hufudb.openhufu.owner.config.ImplementorConfig;
Expand Down Expand Up @@ -61,7 +63,7 @@ public void query(QueryPlanProto request, StreamObserver<DataSetProto> responseO
Plan plan = Plan.fromProto(request);
LOG.info("receives plan:\n{}", plan);
if (!Checker.check(plan, schemaManager)) {
LOG.warn("Check fail for plan {}", request.toString());
LOG.warn("Check fail for plan {}", request);
responseObserver.onCompleted();
return;
}
Expand All @@ -71,7 +73,7 @@ public void query(QueryPlanProto request, StreamObserver<DataSetProto> responseO
output.stream();
output.close();
} catch (Exception e) {
LOG.error("Error in query", e);
throw new OpenHuFuException(ErrorCode.QUERY_ERROR, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public class OwnerImplementorFactory {

static {
aggFuncType2ClassName = new HashMap<>();
aggFuncType2ClassName.put(AggFuncType.COUNT, ImplementorConfig.getImplementorClassName(
Implementor.AGG_COUNT));
aggFuncType2ClassName.put(AggFuncType.SUM, ImplementorConfig.getImplementorClassName(
Implementor.AGG_SUM));
aggFuncType2ClassName.put(AggFuncType.MAX, ImplementorConfig.getImplementorClassName(
Implementor.AGG_MAX));
aggFuncType2ClassName.put(AggFuncType.MIN, ImplementorConfig.getImplementorClassName(
Implementor.AGG_MIN));
aggFuncType2ClassName.put(AggFuncType.SUM, ImplementorConfig.getImplementorClassName(
Implementor.AGG_SUM));
aggFuncType2ClassName.put(AggFuncType.AVG, ImplementorConfig.getImplementorClassName(
Implementor.AGG_AVG));
// aggFuncType2ClassName.put(AggFuncType.COUNT, ImplementorConfig.getImplementorClassName(
// Implementor.AGG_COUNT));
// aggFuncType2ClassName.put(AggFuncType.AVG, ImplementorConfig.getImplementorClassName(
// Implementor.AGG_AVG));
joinClassName = ImplementorConfig.getImplementorClassName(Implementor.JOIN);
}

Expand All @@ -50,7 +50,7 @@ public static OwnerAggregateFunction getAggregationFunction(AggFuncType aggFuncT
} catch (ClassNotFoundException e) {
throw new OpenHuFuException(e, ErrorCode.IMPLEMENTOR_CLASS_NOT_FOUND, className);
} catch (NoSuchMethodException e) {
throw new OpenHuFuException(e, ErrorCode.IMPLEMENTOR_CONSTRUCTOR_NOT_FOUND, className);
throw new OpenHuFuException(e , ErrorCode.IMPLEMENTOR_CONSTRUCTOR_NOT_FOUND, className);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new OpenHuFuException(e, ErrorCode.IMPLEMENTOR_CREATE_FAILED, className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@

public abstract class OwnerAggregateFunction implements AggregateFunction<Row, Comparable> {

protected final boolean twoParty;
protected final int inputRef;
protected final OpenHuFuData.ColumnType type;
protected final OpenHuFuPlan.TaskInfo taskInfo;

public OwnerAggregateFunction(int inputRef, OpenHuFuData.ColumnType type, OpenHuFuPlan.TaskInfo taskInfo) {
public OwnerAggregateFunction(int inputRef, OpenHuFuData.ColumnType type, OpenHuFuPlan.TaskInfo taskInfo, Boolean twoParty) {
this.inputRef = inputRef;
this.type = type;
this.taskInfo = taskInfo;
this.twoParty = twoParty;
// if the algorithm is twoParty, the size of the parties should be 2
assert (taskInfo.getPartiesList().size() == 2) == twoParty;
}

/*
* the parameters of constructor for each aggregate function class
*/
Expand Down
Loading

0 comments on commit 1dc2ccd

Please sign in to comment.