Simplify transaction for video updates.

This commit is contained in:
Kavin 2022-11-18 21:08:37 +00:00
parent adfc0850ed
commit f9b89663be
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
4 changed files with 53 additions and 42 deletions

View File

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

View File

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

View File

@ -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;
}
}

View File

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