-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from cirnoooo123/main
implement SecretUnion
- Loading branch information
Showing
8 changed files
with
609 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
data/src/main/java/com/hufudb/openhufu/data/storage/RandomDataSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package com.hufudb.openhufu.data.storage; | ||
|
||
import com.hufudb.openhufu.common.exception.ErrorCode; | ||
import com.hufudb.openhufu.common.exception.OpenHuFuException; | ||
import com.hufudb.openhufu.data.schema.Schema; | ||
import com.hufudb.openhufu.proto.OpenHuFuData; | ||
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; | ||
|
||
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 final Schema schema; | ||
private final DataSet source; | ||
private final List<ArrayRow> originRows; | ||
private final int originSize; | ||
private final int resultSize; | ||
private final List<ArrayRow> randomRows; | ||
private final Map<Object, List<ArrayRow>> randomRowMap; | ||
|
||
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<>(); | ||
this.randomRowMap = new HashMap<>(); | ||
int size = (int) Math.ceil((double) originSize * RANDOM_SET_SCALE + lap.sample()); | ||
this.resultSize = size > 0 ? size : (int) Math.abs(lap.sample()); | ||
if (originSize == 0) { | ||
init(this::getRandomValue); | ||
} else { | ||
init(this::getRandomValueFromData); | ||
} | ||
this.mix(); | ||
} | ||
|
||
private void init(Function<Integer, Object> randomFunc) { | ||
final int headerSize = schema.size(); | ||
if (headerSize == 0) { | ||
return; | ||
} | ||
for (int i = 0; i < resultSize; ++i) { | ||
Object[] objects = new Object[headerSize]; | ||
Object key = randomFunc.apply(0); | ||
objects[0] = key; | ||
for (int j = 1; j < headerSize; ++j) { | ||
Object value = randomFunc.apply(j); | ||
objects[j] = value; | ||
} | ||
ArrayRow row = new ArrayRow(objects); | ||
randomRows.add(row); | ||
recordRandomRow(key, row); | ||
} | ||
} | ||
|
||
private void recordRandomRow(Object key, ArrayRow value) { | ||
if (randomRowMap.containsKey(key)) { | ||
randomRowMap.get(key).add(value); | ||
} else { | ||
randomRowMap.put(key, new LinkedList<>(Arrays.asList(value))); | ||
} | ||
} | ||
|
||
private void mix() { | ||
//todo index insert for ArrayList may be slow | ||
for (ArrayRow row : originRows) { | ||
int idx = (int) Math.ceil(random.nextDouble() * randomRows.size()); | ||
randomRows.add(idx, row); | ||
} | ||
} | ||
|
||
public ArrayDataSet getRandomSet() { | ||
return new ArrayDataSet(schema, randomRows); | ||
} | ||
|
||
public ArrayDataSet removeRandom(DataSet dataSet) { | ||
List<ArrayRow> newRows = new ArrayList<>(); | ||
for (ArrayRow row : ArrayDataSet.materialize(dataSet).rows) { | ||
Object key = row.get(0); | ||
List<ArrayRow> rows = randomRowMap.get(key); | ||
if (rows == null || rows.isEmpty()) { | ||
newRows.add(row); | ||
continue; | ||
} | ||
int idx = rows.indexOf(row); | ||
if (idx == -1) { | ||
newRows.add(row); | ||
} else { | ||
rows.remove(idx); | ||
} | ||
} | ||
return new ArrayDataSet(schema, newRows); | ||
} | ||
|
||
private Object getRandomValueFromData(int columnIndex) { | ||
OpenHuFuData.ColumnType type = schema.getType((columnIndex)); | ||
int r = (int) (random.nextDouble() * originSize); | ||
switch (type) { | ||
case BYTE: | ||
return (byte) originRows.get(r).get(columnIndex) + (byte) lap.sample(); | ||
case SHORT: | ||
return (short) originRows.get(r).get(columnIndex) + (short) lap.sample(); | ||
case INT: | ||
return (int) originRows.get(r).get(columnIndex) + (int) lap.sample(); | ||
case LONG: | ||
case DATE: | ||
case TIME: | ||
case TIMESTAMP: | ||
return (long) originRows.get(r).get(columnIndex) + (long) lap.sample(); | ||
case FLOAT: | ||
return (float) originRows.get(r).get(columnIndex) + (float) lap.sample(); | ||
case DOUBLE: | ||
return (double) originRows.get(r).get(columnIndex) + lap.sample(); | ||
case BOOLEAN: | ||
return lap.sample() > 0.0; | ||
case GEOMETRY: | ||
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())); | ||
} else { | ||
throw new OpenHuFuException(ErrorCode.DATA_TYPE_NOT_SUPPORT, type); | ||
} | ||
case STRING: | ||
return originRows.get(r).get(columnIndex); | ||
default: | ||
throw new OpenHuFuException(ErrorCode.DATA_TYPE_NOT_SUPPORT, type); | ||
} | ||
} | ||
|
||
private Object getRandomValue(int columnIndex) { | ||
OpenHuFuData.ColumnType type = schema.getType((columnIndex)); | ||
switch (type) { | ||
case BYTE: | ||
return (byte) lap.sample(); | ||
case SHORT: | ||
return (short) lap.sample(); | ||
case INT: | ||
return (int) lap.sample(); | ||
case LONG: | ||
case DATE: | ||
case TIME: | ||
case TIMESTAMP: | ||
return (long) lap.sample(); | ||
case FLOAT: | ||
return (float) lap.sample(); | ||
case DOUBLE: | ||
return lap.sample(); | ||
case BOOLEAN: | ||
return lap.sample() > 0.0; | ||
case GEOMETRY: | ||
return geoFactory.createPoint(new Coordinate(lap.sample(), lap.sample())); | ||
case STRING: | ||
return RandomStringUtils.randomAlphanumeric(RANDOM_SET_OFFSET); | ||
default: | ||
throw new OpenHuFuException(ErrorCode.DATA_TYPE_NOT_SUPPORT, type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.