Merge pull request #451 from TeamPiped/playlist-fixes

Simplify transaction for video updates.
This commit is contained in:
Kavin 2022-11-18 21:09:03 +00:00 committed by GitHub
commit d6bfc8f9ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 42 deletions

View file

@ -106,7 +106,7 @@ public class ChannelHandlers {
.filter(v -> v.getId().equals(id)) .filter(v -> v.getId().equals(id))
.findFirst(); .findFirst();
if (video.isPresent()) { if (video.isPresent()) {
VideoHelpers.updateVideo(s, id, item); VideoHelpers.updateVideo(id, item);
} else { } else {
VideoHelpers.handleNewVideo("https://youtube.com/watch?v=" + id, time, channel); VideoHelpers.handleNewVideo("https://youtube.com/watch?v=" + id, time, channel);
} }

View file

@ -150,7 +150,7 @@ public class DatabaseHelper {
sq.select(sqRoot.get("videos").get("id")) sq.select(sqRoot.get("videos").get("id"))
.where(cb.equal(sqRoot.get("playlist_id"), UUID.fromString(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(); return s.createQuery(cr).list();
} }

View file

@ -52,8 +52,13 @@ public class VideoHelpers {
Math.max(infoTime, time), info.getThumbnailUrl(), info.isShortFormContent(), channel); Math.max(infoTime, time), info.getThumbnailUrl(), info.isShortFormContent(), channel);
var tr = s.beginTransaction(); var tr = s.beginTransaction();
try {
s.insert(video); s.insert(video);
tr.commit(); tr.commit();
} catch (Exception e) {
tr.rollback();
ExceptionHandler.handle(e);
}
return; return;
} }
} }
@ -66,12 +71,9 @@ public class VideoHelpers {
public static void updateVideo(String id, StreamInfoItem item, long time) { public static void updateVideo(String id, StreamInfoItem item, long time) {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try { try {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { if (!updateVideo(id, item.getViewCount(), item.getDuration(), item.getName())) {
if (!updateVideo(s, id, item.getViewCount(), item.getDuration(), item.getName())) {
handleNewVideo(item.getUrl(), time, null); handleNewVideo(item.getUrl(), time, null);
} }
}
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
} }
@ -81,22 +83,21 @@ public class VideoHelpers {
public static void updateVideo(String id, StreamInfo info, long time) { public static void updateVideo(String id, StreamInfo info, long time) {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try { try {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { if (!updateVideo(id, info.getViewCount(), info.getDuration(), info.getName())) {
if (!updateVideo(s, id, info.getViewCount(), info.getDuration(), info.getName())) {
handleNewVideo(info, time, null); handleNewVideo(info, time, null);
} }
}
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
} }
}); });
} }
public static void updateVideo(StatelessSession s, String id, StreamInfoItem item) { public static void updateVideo(String id, StreamInfoItem item) {
updateVideo(s, id, item.getViewCount(), item.getDuration(), item.getName()); 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 cb = s.getCriteriaBuilder();
var cu = cb.createCriteriaUpdate(Video.class); var cu = cb.createCriteriaUpdate(Video.class);
@ -114,10 +115,20 @@ public class VideoHelpers {
cu.set(root.get("views"), views); cu.set(root.get("views"), views);
} }
long updated;
var tr = s.beginTransaction(); var tr = s.beginTransaction();
long updated = s.createMutationQuery(cu).executeUpdate(); try {
updated = s.createMutationQuery(cu).executeUpdate();
tr.commit(); tr.commit();
} catch (Exception e) {
tr.rollback();
// return true, so that we don't try to insert a video!
return true;
}
return updated > 0; return updated > 0;
} }
} }
}

View file

@ -137,15 +137,14 @@ public class SyncRunner implements Runnable {
case "video.piped.stream.info" -> { case "video.piped.stream.info" -> {
FederatedVideoInfo info = mapper.treeToValue(content, FederatedVideoInfo.class); FederatedVideoInfo info = mapper.treeToValue(content, FederatedVideoInfo.class);
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { if (!VideoHelpers.updateVideo(info.getVideoId(),
if (!VideoHelpers.updateVideo(s, info.getVideoId(),
info.getViews(), info.getViews(),
info.getDuration(), info.getDuration(),
info.getTitle())) { info.getTitle())) {
VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(), VideoHelpers.handleNewVideo("https://www.youtube.com/watch?v=" + info.getVideoId(),
System.currentTimeMillis(), null); System.currentTimeMillis(), null);
} }
}
}); });
} }
case "video.piped.channel.info" -> { case "video.piped.channel.info" -> {
@ -171,6 +170,7 @@ public class SyncRunner implements Runnable {
response.get("next_batch").asText(); response.get("next_batch").asText();
} catch (Exception ignored) { } catch (Exception ignored) {
ignored.printStackTrace();
Thread.sleep(1000); Thread.sleep(1000);
} }
} }