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()));