Change to new shuffle generator

This replaces PR #1239, taking the original code and adding requested cleanups.
master
Evan Harris 5 years ago committed by François-Xavier Thomas
parent 86064d6b01
commit 59492c28c3
  1. 5
      airsonic-main/src/main/java/org/airsonic/player/service/search/SearchServiceImpl.java
  2. 43
      airsonic-main/src/main/java/org/airsonic/player/service/search/SearchServiceUtilities.java

@ -50,9 +50,6 @@ public class SearchServiceImpl implements SearchService {
@Autowired @Autowired
private SearchServiceUtilities util; private SearchServiceUtilities util;
// TODO Should be changed to SecureRandom?
private final Random random = new Random(System.currentTimeMillis());
@Override @Override
public SearchResult search(SearchCriteria criteria, List<MusicFolder> musicFolders, public SearchResult search(SearchCriteria criteria, List<MusicFolder> musicFolders,
IndexType indexType) { IndexType indexType) {
@ -108,7 +105,7 @@ public class SearchServiceImpl implements SearchService {
List<D> result = new ArrayList<>(); List<D> result = new ArrayList<>();
while (!docs.isEmpty() && result.size() < count) { while (!docs.isEmpty() && result.size() < count) {
int randomPos = random.nextInt(docs.size()); int randomPos = util.nextInt.apply(docs.size());
Document document = searcher.doc(docs.get(randomPos)); Document document = searcher.doc(docs.get(randomPos));
id2ListCallBack.accept(result, util.getId.apply(document)); id2ListCallBack.accept(result, util.getId.apply(document));
docs.remove(randomPos); docs.remove(randomPos);

@ -31,13 +31,21 @@ import org.airsonic.player.service.MediaFileService;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
@ -71,6 +79,41 @@ public class SearchServiceUtilities {
@Autowired @Autowired
private MediaFileService mediaFileService; private MediaFileService mediaFileService;
private static final Logger LOG = LoggerFactory.getLogger(SearchServiceUtilities.class);
private Random random;
private static Random createSecureRandom(String algorithm) {
SecureRandom secureRandom = null;
try {
LOG.debug("{} is used to create a random list of songs.", algorithm);
secureRandom = SecureRandom.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
LOG.error("Usually unreachable.", e);
}
return secureRandom;
}
{
List<String> algorithms = Arrays.asList(Security.getProviders()).stream()
.flatMap(p -> p.getServices().stream().filter(s -> "SecureRandom".equals(s.getType())))
.map(s -> s.getAlgorithm()).collect(Collectors.toList());
for (String algorithmName : new String[]{"NativePRNG", "SHA1PRNG"}) {
if (algorithms.contains(algorithmName)) {
random = createSecureRandom(algorithmName);
}
}
if (isEmpty(random)) {
random = new Random(System.currentTimeMillis());
LOG.debug("NativePRNG and SHA1PRNG cannot be used on this platform.");
}
}
public Function<Integer, Integer> nextInt = (range) -> random.nextInt(range);
public final Function<Long, Integer> round = (i) -> { public final Function<Long, Integer> round = (i) -> {
// return // return
// NumericUtils.floatToSortableInt(i); // NumericUtils.floatToSortableInt(i);

Loading…
Cancel
Save