From 81fa92361db546eb763d55bced4afa482658f8a6 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 5 Aug 2022 00:03:47 +0530 Subject: [PATCH] Fix and optimize pubsub timer query. --- src/main/java/me/kavin/piped/Main.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 8fa084b..9518fae 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -14,7 +14,10 @@ import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter; import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import java.io.IOException; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; import java.util.concurrent.TimeUnit; public class Main { @@ -65,19 +68,22 @@ public class Main { CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaQuery criteria = cb.createQuery(PubSub.class); var root = criteria.from(PubSub.class); - var userRoot = criteria.from(User.class); + var userSq = criteria.subquery(User.class); + var userRoot = userSq.from(User.class); + userSq.select(userRoot.get("subscribed_ids")); var subquery = criteria.subquery(UnauthenticatedSubscription.class); var subRoot = subquery.from(UnauthenticatedSubscription.class); subquery.select(subRoot.get("id")) .where(cb.gt(subRoot.get("subscribedAt"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.SUBSCRIPTIONS_EXPIRY))); criteria.select(root) - .where(cb.or( - cb.and( + .where(cb.and( cb.lessThan(root.get("subbedAt"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4)), - cb.isMember(root.get("id"), userRoot.>get("subscribed_ids")) - ), - root.get("id").in(subquery) - )); + cb.or( + root.get("id").in(userSq), + root.get("id").in(subquery) + ) + ) + ); List pubSubList = s.createQuery(criteria).list();