Skip to content

Commit

Permalink
Random selection testing
Browse files Browse the repository at this point in the history
  • Loading branch information
denis256 committed Jan 20, 2023
1 parent a26df2d commit eac12e2
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface Polydata extends Closeable {

String COUNT = "count";

String RANDOM_COUNT = "random_count";


void prepareStorage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public BasicPolyList query(String poly, PolyQuery polyQuery) {

if (query.queryType() == BasicPolyQuery.QueryFunction.RANDOM) {
List<String> indexes = repositories.get(poly).getPolyIndex().get(index);
List<String> randomIds = randoms.randomValues(indexes, itemPerPage);
int randomCount = query.option(RANDOM_COUNT, itemPerPage);
List<String> randomIds = randoms.randomValues(indexes, randomCount);
return read(poly, new HashSet<>(randomIds));
}
final int page = query.page() < 0 ? 0 : query.page();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ public BasicPolyList query(String poly, PolyQuery polyQuery) {

if (query.queryType() == BasicPolyQuery.QueryFunction.RANDOM) {
List<String> indexes = repositories.get(poly).getPolyIndex().get(index);
List<String> randomIds = randoms.randomValues(indexes, itemPerPage);
int randomCount = query.option(RANDOM_COUNT, itemPerPage);
List<String> randomIds = randoms.randomValues(indexes, randomCount);
return read(poly, new HashSet<>(randomIds));
}
final int page = query.page() < 0 ? 0 : query.page();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ public <T> T option(String key) {
return options.fetch(key);
}

public <T> void option(String key, T value) {
public <T> T option(String key, T value) {
return options.fetch(key, value);
}

public <T> void withOption(String key, T value) {
options.put(key, value);
}


public enum QueryFunction {PAGES, RANDOM}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ public BasicPolyList query(String poly, PolyQuery polyQuery) {
Bson mongoQuery = Filters.in(INDEXES, index);
MongoCollection<Document> collection = collection(poly);
if (query.queryType() == BasicPolyQuery.QueryFunction.RANDOM) {
int randomCount = query.option(RANDOM_COUNT, itemPerPage);
long count = collection(POLY).countDocuments(mongoQuery);
Random random = new Random();
for (int i = 0; i < itemPerPage; i++) {
for (int i = 0; i < randomCount; i++) {
int skip = random.nextInt((int) count);
try (MongoCursor<Document> iterator = collection.find(mongoQuery).skip(skip)
.iterator()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,11 @@ public BasicPolyList query(String poly, PolyQuery polyQuery) {
Integer itemPerPage = query.getOptions().fetch(ITEM_PER_PAGE, defaultItemPerPage);

long count = jedis.llen(fetchId(poly, index));
int randomCount = query.option(RANDOM_COUNT, itemPerPage);

List<Integer> ids = new ArrayList<>();
if (query.queryType() == BasicPolyQuery.QueryFunction.RANDOM) {
for (int i = 0; i < itemPerPage; i++) {
for (int i = 0; i < randomCount; i++) {
ids.add(randoms.getRandom().nextInt((int) count));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ public BasicPolyList query(String poly, PolyQuery polyQuery) {
try (Connection connection = fetchConnection(poly)) {
PreparedStatement preparedStatement = null;
if (query.queryType() == BasicPolyQuery.QueryFunction.RANDOM) {
preparedStatement = connection.prepareStatement("SELECT data FROM data WHERE polydata_index LIKE ? ORDER BY RANDOM() LIMIT " + itemPerPage + " ; ");
int randomCount = query.option(RANDOM_COUNT, itemPerPage);
preparedStatement = connection.prepareStatement("SELECT data FROM data WHERE polydata_index LIKE ? ORDER BY RANDOM() LIMIT " + randomCount + " ; ");
} else {
preparedStatement = connection.prepareStatement("SELECT data FROM data WHERE polydata_index LIKE ? ORDER BY update_date DESC LIMIT " + (page * itemPerPage) + "," + itemPerPage + " ; ");
}
Expand Down

0 comments on commit eac12e2

Please sign in to comment.