Use Criteria API for deleting old videos. (#302)

This commit is contained in:
Kavin 2022-07-04 07:09:20 +01:00 committed by GitHub
parent 17b9a974de
commit 606cbf5ca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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