Use parallel streams for some operations. (#264)

This commit is contained in:
Kavin 2022-05-12 11:54:28 +01:00 committed by GitHub
parent ce70bd037b
commit 87917f0da2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 16 deletions

View file

@ -49,9 +49,11 @@ public class Main {
Collections.shuffle(pubSubList); Collections.shuffle(pubSubList);
for (PubSub pubsub : pubSubList) pubSubList.stream().parallel()
if (pubsub != null) .forEach(pubSub -> {
Multithreading.runAsyncLimitedPubSub(() -> ResponseHelper.subscribePubSub(pubsub.getId())); if (pubSub != null)
Multithreading.runAsyncLimitedPubSub(() -> ResponseHelper.subscribePubSub(pubSub.getId()));
});
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -53,6 +53,7 @@ import javax.persistence.criteria.Root;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE; import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
@ -933,13 +934,13 @@ public class ResponseHelper {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
var channels = DatabaseHelper.getChannelsFromIds(s, Arrays.asList(channelIds)); var channels = DatabaseHelper.getChannelsFromIds(s, Arrays.asList(channelIds));
outer: Arrays.stream(channelIds).parallel()
for (String channelId : channelIds) { .filter(channelId ->
for (var channel : channels) channels.stream().parallel()
if (channel.getUploaderId().equals(channelId)) .filter(channel -> channel.getUploaderId().equals(channelId))
continue outer; .findFirst().isEmpty()
Multithreading.runAsyncLimited(() -> saveChannel(channelId)); )
} .forEach(channelId -> Multithreading.runAsyncLimited(() -> saveChannel(channelId)));
} }
}); });
@ -957,8 +958,6 @@ public class ResponseHelper {
if (user != null) { if (user != null) {
try (Session s = DatabaseSessionFactory.createSession()) { try (Session s = DatabaseSessionFactory.createSession()) {
List<SubscriptionChannel> subscriptionItems = new ObjectArrayList<>();
CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaBuilder cb = s.getCriteriaBuilder();
var query = cb.createQuery(me.kavin.piped.utils.obj.db.Channel.class); var query = cb.createQuery(me.kavin.piped.utils.obj.db.Channel.class);
var root = query.from(me.kavin.piped.utils.obj.db.Channel.class); var root = query.from(me.kavin.piped.utils.obj.db.Channel.class);
@ -971,10 +970,12 @@ public class ResponseHelper {
var channels = s.createQuery(query).list(); var channels = s.createQuery(query).list();
channels.forEach(channel -> subscriptionItems.add(new SubscriptionChannel("/channel/" + channel.getUploaderId(), List<SubscriptionChannel> subscriptionItems = channels
channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified()))); .stream().parallel()
.sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader))
subscriptionItems.sort(Comparator.comparing(o -> o.name)); .map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(),
channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified()))
.collect(Collectors.toUnmodifiableList());
return Constants.mapper.writeValueAsBytes(subscriptionItems); return Constants.mapper.writeValueAsBytes(subscriptionItems);
} }