From f9b89663beb7b3ff60b7e064925282f70ca86abd Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 18 Nov 2022 21:08:37 +0000 Subject: [PATCH] Simplify transaction for video updates. --- .../server/handlers/ChannelHandlers.java | 2 +- .../me/kavin/piped/utils/DatabaseHelper.java | 2 +- .../me/kavin/piped/utils/VideoHelpers.java | 75 +++++++++++-------- .../kavin/piped/utils/matrix/SyncRunner.java | 16 ++-- 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java b/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java index 130103c..1922a97 100644 --- a/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java +++ b/src/main/java/me/kavin/piped/server/handlers/ChannelHandlers.java @@ -106,7 +106,7 @@ public class ChannelHandlers { .filter(v -> v.getId().equals(id)) .findFirst(); if (video.isPresent()) { - VideoHelpers.updateVideo(s, id, item); + VideoHelpers.updateVideo(id, item); } else { VideoHelpers.handleNewVideo("https://youtube.com/watch?v=" + id, time, channel); } diff --git a/src/main/java/me/kavin/piped/utils/DatabaseHelper.java b/src/main/java/me/kavin/piped/utils/DatabaseHelper.java index 3d5e904..7e861bf 100644 --- a/src/main/java/me/kavin/piped/utils/DatabaseHelper.java +++ b/src/main/java/me/kavin/piped/utils/DatabaseHelper.java @@ -150,7 +150,7 @@ public class DatabaseHelper { sq.select(sqRoot.get("videos").get("id")) .where(cb.equal(sqRoot.get("playlist_id"), UUID.fromString(id))); } - cr.where(cb.equal(root.get("id"), sq)); + cr.where(root.get("id").in(sq)); return s.createQuery(cr).list(); } diff --git a/src/main/java/me/kavin/piped/utils/VideoHelpers.java b/src/main/java/me/kavin/piped/utils/VideoHelpers.java index 771c016..10fe52e 100644 --- a/src/main/java/me/kavin/piped/utils/VideoHelpers.java +++ b/src/main/java/me/kavin/piped/utils/VideoHelpers.java @@ -52,8 +52,13 @@ public class VideoHelpers { Math.max(infoTime, time), info.getThumbnailUrl(), info.isShortFormContent(), channel); var tr = s.beginTransaction(); - s.insert(video); - tr.commit(); + try { + s.insert(video); + tr.commit(); + } catch (Exception e) { + tr.rollback(); + ExceptionHandler.handle(e); + } return; } } @@ -66,12 +71,9 @@ public class VideoHelpers { public static void updateVideo(String id, StreamInfoItem item, long time) { Multithreading.runAsync(() -> { try { - try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { - if (!updateVideo(s, id, item.getViewCount(), item.getDuration(), item.getName())) { - handleNewVideo(item.getUrl(), time, null); - } + if (!updateVideo(id, item.getViewCount(), item.getDuration(), item.getName())) { + handleNewVideo(item.getUrl(), time, null); } - } catch (Exception e) { ExceptionHandler.handle(e); } @@ -81,10 +83,8 @@ public class VideoHelpers { public static void updateVideo(String id, StreamInfo info, long time) { Multithreading.runAsync(() -> { try { - try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { - if (!updateVideo(s, id, info.getViewCount(), info.getDuration(), info.getName())) { - handleNewVideo(info, time, null); - } + if (!updateVideo(id, info.getViewCount(), info.getDuration(), info.getName())) { + handleNewVideo(info, time, null); } } catch (Exception e) { ExceptionHandler.handle(e); @@ -92,32 +92,43 @@ public class VideoHelpers { }); } - public static void updateVideo(StatelessSession s, String id, StreamInfoItem item) { - updateVideo(s, id, item.getViewCount(), item.getDuration(), item.getName()); + public static void updateVideo(String id, StreamInfoItem item) { + updateVideo(id, item.getViewCount(), item.getDuration(), item.getName()); } - public static boolean updateVideo(StatelessSession s, String id, long views, long duration, String title) { + public static boolean updateVideo(String id, long views, long duration, String title) { + try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { - var cb = s.getCriteriaBuilder(); - var cu = cb.createCriteriaUpdate(Video.class); - var root = cu.from(Video.class); - cu.where(cb.equal(root.get("id"), id)); + var cb = s.getCriteriaBuilder(); + var cu = cb.createCriteriaUpdate(Video.class); + var root = cu.from(Video.class); + cu.where(cb.equal(root.get("id"), id)); - if (duration > 0) { - cu.set(root.get("duration"), duration); + if (duration > 0) { + cu.set(root.get("duration"), duration); + } + if (title != null) { + cu.set(root.get("title"), title); + } + if (views > 0) { + cu.set(root.get("views"), views); + } + + long updated; + + var tr = s.beginTransaction(); + try { + updated = s.createMutationQuery(cu).executeUpdate(); + tr.commit(); + } catch (Exception e) { + tr.rollback(); + + // return true, so that we don't try to insert a video! + return true; + } + + return updated > 0; } - if (title != null) { - cu.set(root.get("title"), title); - } - if (views > 0) { - cu.set(root.get("views"), views); - } - - var tr = s.beginTransaction(); - long updated = s.createMutationQuery(cu).executeUpdate(); - tr.commit(); - - return updated > 0; } } diff --git a/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java b/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java index 7c7ae23..5e9dfc0 100644 --- a/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java +++ b/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java @@ -137,15 +137,14 @@ public class SyncRunner implements Runnable { case "video.piped.stream.info" -> { FederatedVideoInfo info = mapper.treeToValue(content, FederatedVideoInfo.class); Multithreading.runAsync(() -> { - try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { - if (!VideoHelpers.updateVideo(s, info.getVideoId(), - info.getViews(), - info.getDuration(), - info.getTitle())) { - VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(), - System.currentTimeMillis(), null); - } + if (!VideoHelpers.updateVideo(info.getVideoId(), + info.getViews(), + info.getDuration(), + info.getTitle())) { + VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(), + System.currentTimeMillis(), null); } + }); } case "video.piped.channel.info" -> { @@ -171,6 +170,7 @@ public class SyncRunner implements Runnable { response.get("next_batch").asText(); } catch (Exception ignored) { + ignored.printStackTrace(); Thread.sleep(1000); } }