From 716954aa9af3ed1c4a224886f303bce1e96b7d6c Mon Sep 17 00:00:00 2001 From: Benjamin Gamard Date: Fri, 30 Mar 2018 21:16:13 +0200 Subject: [PATCH] indexing in transactional context --- .../async/DocumentCreatedAsyncListener.java | 2 +- .../async/DocumentDeletedAsyncListener.java | 2 +- .../async/FileDeletedAsyncListener.java | 2 +- .../async/FileProcessingAsyncListener.java | 40 +++++++++---------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java b/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java index 13cb762b..6c781385 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java +++ b/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java @@ -39,7 +39,7 @@ public class DocumentCreatedAsyncListener { contributor.setUserId(event.getUserId()); contributorDao.create(contributor); - // Update Lucene index + // Update index AppContext.getInstance().getIndexingHandler().createDocument(event.getDocument()); }); } diff --git a/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentDeletedAsyncListener.java b/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentDeletedAsyncListener.java index cd4e0cd3..879695bf 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentDeletedAsyncListener.java +++ b/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentDeletedAsyncListener.java @@ -29,7 +29,7 @@ public class DocumentDeletedAsyncListener { log.info("Document deleted event: " + documentDeletedAsyncEvent.toString()); } - // Update Lucene index + // Update index AppContext.getInstance().getIndexingHandler().deleteDocument(documentDeletedAsyncEvent.getDocumentId()); } } diff --git a/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java b/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java index f40de158..c4b80afc 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java +++ b/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java @@ -35,7 +35,7 @@ public class FileDeletedAsyncListener { File file = fileDeletedAsyncEvent.getFile(); FileUtil.delete(file); - // Update Lucene index + // Update index AppContext.getInstance().getIndexingHandler().deleteDocument(file.getId()); } } diff --git a/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileProcessingAsyncListener.java b/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileProcessingAsyncListener.java index 97db3da4..e5167241 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileProcessingAsyncListener.java +++ b/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileProcessingAsyncListener.java @@ -53,8 +53,8 @@ public class FileProcessingAsyncListener { processFile(event); - // Update Lucene index - AppContext.getInstance().getIndexingHandler().createFile(event.getFile()); + // Update index + TransactionUtil.handle(() -> AppContext.getInstance().getIndexingHandler().createFile(event.getFile())); FileUtil.endProcessingFile(event.getFile().getId()); } @@ -70,10 +70,13 @@ public class FileProcessingAsyncListener { log.info("File updated event: " + event.toString()); } - processFile(event); + TransactionUtil.handle(() -> { + // Generate thumbnail, extract content + processFile(event); - // Update Lucene index - AppContext.getInstance().getIndexingHandler().updateFile(event.getFile()); + // Update index + AppContext.getInstance().getIndexingHandler().updateFile(event.getFile()); + }); FileUtil.endProcessingFile(event.getFile().getId()); } @@ -94,12 +97,9 @@ public class FileProcessingAsyncListener { } // Get the user from the database - final AtomicReference user = new AtomicReference<>(); - TransactionUtil.handle(() -> { - UserDao userDao = new UserDao(); - user.set(userDao.getById(event.getUserId())); - }); - if (user.get() == null) { + UserDao userDao = new UserDao(); + User user = userDao.getById(event.getUserId()); + if (user == null) { // The user has been deleted meanwhile FileUtil.endProcessingFile(file.getId()); return; @@ -107,7 +107,7 @@ public class FileProcessingAsyncListener { // Generate file variations try { - Cipher cipher = EncryptionUtil.getEncryptionCipher(user.get().getPrivateKey()); + Cipher cipher = EncryptionUtil.getEncryptionCipher(user.getPrivateKey()); BufferedImage image = formatHandler.generateThumbnail(event.getUnencryptedFile()); if (image != null) { // Generate thumbnails from image @@ -142,15 +142,13 @@ public class FileProcessingAsyncListener { log.info(MessageFormat.format("File content extracted in {0}ms", System.currentTimeMillis() - startTime)); // Save the file to database - TransactionUtil.handle(() -> { - FileDao fileDao = new FileDao(); - if (fileDao.getActiveById(file.getId()) == null) { - // The file has been deleted since the text extraction started, ignore the result - return; - } + FileDao fileDao = new FileDao(); + if (fileDao.getActiveById(file.getId()) == null) { + // The file has been deleted since the text extraction started, ignore the result + return; + } - file.setContent(content.get()); - fileDao.update(file); - }); + file.setContent(content.get()); + fileDao.update(file); } }