From 606cbf5ca668fc9b5c3f7842c869fab0ea9608f4 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 4 Jul 2022 07:09:20 +0100 Subject: [PATCH] Use Criteria API for deleting old videos. (#302) --- src/main/java/me/kavin/piped/Main.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 28894b1..0cb54fc 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -7,10 +7,10 @@ import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.*; import me.kavin.piped.utils.obj.db.PubSub; import me.kavin.piped.utils.obj.db.User; +import me.kavin.piped.utils.obj.db.Video; import org.hibernate.Session; import org.hibernate.StatelessSession; import org.hibernate.Transaction; -import org.hibernate.query.Query; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter; @@ -94,10 +94,14 @@ public class Main { public void run() { try (Session s = DatabaseSessionFactory.createSession()) { + var cb = s.getCriteriaBuilder(); + var cd = cb.createCriteriaDelete(Video.class); + var root = cd.from(Video.class); + cd.where(cb.lessThan(root.get("uploaded"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))); + Transaction tr = s.beginTransaction(); - Query query = s.createQuery("delete from Video where uploaded < :time").setParameter("time", - System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)); + var query = s.createMutationQuery(cd); System.out.println(String.format("Cleanup: Removed %o old videos", query.executeUpdate()));