Replace native query with criteria api for PubSub subscriptions (#191)

This commit is contained in:
Kavin 2022-02-15 11:55:08 +00:00 committed by GitHub
parent 498849ec6b
commit a03ae434be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,16 +7,16 @@ import me.kavin.piped.utils.DownloaderImpl;
import me.kavin.piped.utils.Multithreading; import me.kavin.piped.utils.Multithreading;
import me.kavin.piped.utils.ResponseHelper; import me.kavin.piped.utils.ResponseHelper;
import me.kavin.piped.utils.obj.db.PubSub; import me.kavin.piped.utils.obj.db.PubSub;
import me.kavin.piped.utils.obj.db.User;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.query.Query; import org.hibernate.query.Query;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.localization.Localization;
import java.util.Collections; import javax.persistence.criteria.CriteriaBuilder;
import java.util.List; import javax.persistence.criteria.CriteriaQuery;
import java.util.Timer; import java.util.*;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class Main { public class Main {
@ -33,10 +33,17 @@ public class Main {
try { try {
Session s = DatabaseSessionFactory.createSession(); Session s = DatabaseSessionFactory.createSession();
List<PubSub> pubSubList = s.createNativeQuery( CriteriaBuilder cb = s.getCriteriaBuilder();
"select distinct pubsub.* from pubsub inner join users_subscribed on pubsub.id = users_subscribed.channel where pubsub.subbed_at < :time", CriteriaQuery<PubSub> criteria = cb.createQuery(PubSub.class);
PubSub.class).setParameter("time", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4)) var root = criteria.from(PubSub.class);
.getResultList(); var userRoot = criteria.from(User.class);
criteria.select(root)
.where(cb.and(
cb.lessThan(root.get("subbedAt"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4)),
cb.isMember(root.get("id"), userRoot.<Collection<String>>get("subscribed_ids"))
)).distinct(true);
List<PubSub> pubSubList = s.createQuery(criteria).list();
Collections.shuffle(pubSubList); Collections.shuffle(pubSubList);