This is an automated email from the git hooks/post-receive script. New commit to branch feature/4309_synchro_referential in repository observe. See https://gitlab.nuiton.org/codelutin/observe.git commit 9e5da6e908762661ba83304118a75afdd46aa178 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Aug 16 09:59:33 2016 +0200 Pouvoir récupérer une liste d'entities à partir de leur ids + pouvoir executer plusieurs scripts dans une même transaction --- .../observe/ObserveTopiaPersistenceContext.java | 5 ++ .../java/fr/ird/observe/RunScriptTopiaSqlWork.java | 79 ++++++++++++---------- .../fr/ird/observe/RunScriptsTopiaSqlWork.java | 59 ++++++++++++++++ .../ird/observe/services/ObserveServiceTopia.java | 7 ++ .../referential/sql/ApplySqlRequestWork.java | 44 ++++++++++++ 5 files changed, 160 insertions(+), 34 deletions(-) diff --git a/observe-entities/src/main/java/fr/ird/observe/ObserveTopiaPersistenceContext.java b/observe-entities/src/main/java/fr/ird/observe/ObserveTopiaPersistenceContext.java index 6ca60d4..abd3f83 100644 --- a/observe-entities/src/main/java/fr/ird/observe/ObserveTopiaPersistenceContext.java +++ b/observe-entities/src/main/java/fr/ird/observe/ObserveTopiaPersistenceContext.java @@ -150,4 +150,9 @@ public class ObserveTopiaPersistenceContext extends AbstractObserveTopiaPersiste getSqlSupport().doSqlWork(new RunScriptTopiaSqlWork(1000, content)); } + + public void executeSqlScripts(byte[]... contents) { + + getSqlSupport().doSqlWork(new RunScriptsTopiaSqlWork(1000, contents)); + } } diff --git a/observe-entities/src/main/java/fr/ird/observe/RunScriptTopiaSqlWork.java b/observe-entities/src/main/java/fr/ird/observe/RunScriptTopiaSqlWork.java index aa63ad4..eed97d7 100644 --- a/observe-entities/src/main/java/fr/ird/observe/RunScriptTopiaSqlWork.java +++ b/observe-entities/src/main/java/fr/ird/observe/RunScriptTopiaSqlWork.java @@ -54,12 +54,18 @@ public class RunScriptTopiaSqlWork implements TopiaSqlWork { protected final boolean gzip; protected final int batchSize; - public RunScriptTopiaSqlWork(int batchSize, byte[] content) throws IOException { + public RunScriptTopiaSqlWork(int batchSize, byte[] content) { this.batchSize = batchSize; this.content = content; - try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content)) { - gzip = GZUtil.isGzipStream(byteArrayInputStream); + if (content == null || content.length == 0) { + gzip = false; + } else { + try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content)) { + gzip = GZUtil.isGzipStream(byteArrayInputStream); + } catch (IOException e) { + throw new TopiaException(e); + } } } @@ -70,49 +76,54 @@ public class RunScriptTopiaSqlWork implements TopiaSqlWork { try { connection.setAutoCommit(false); - try (BufferedReader reader = createReader()) { - try (Statement statement = connection.createStatement()) { + execute0(connection); + } finally { + connection.setAutoCommit(autoCommit); + } + } - int batchSize = 0; - String command = null; - String next; - while ((next = reader.readLine()) != null) { + protected void execute0(Connection connection) throws SQLException { + try (BufferedReader reader = createReader()) { - String trimLine = next.trim(); + try (Statement statement = connection.createStatement()) { - if (trimLine.startsWith("--")) { - continue; - } + int batchSize = 0; + String command = null; + String next; + while ((next = reader.readLine()) != null) { - if (command == null) { - command = next; - } else { - command += ' ' + next; - } - if (trimLine.endsWith(";")) { - statement.addBatch(command); - if (log.isDebugEnabled()) { - log.debug("Sql statement: " + command); - } - batchSize++; - command = null; - - if (batchSize % this.batchSize == 0) { - flushStatement(statement); - } + String trimLine = next.trim(); + + if (trimLine.startsWith("--")) { + continue; + } + + if (command == null) { + command = next; + } else { + command += ' ' + next; + } + if (trimLine.endsWith(";")) { + statement.addBatch(command); + if (log.isDebugEnabled()) { + log.debug("Sql statement: " + command); } + batchSize++; + command = null; + if (batchSize % this.batchSize == 0) { + flushStatement(statement); + } } - flushStatement(statement); } - } catch (IOException e) { - throw new TopiaException(e); + flushStatement(statement); } - } finally { - connection.setAutoCommit(autoCommit); + + } catch (IOException e) { + throw new TopiaException(e); } } diff --git a/observe-entities/src/main/java/fr/ird/observe/RunScriptsTopiaSqlWork.java b/observe-entities/src/main/java/fr/ird/observe/RunScriptsTopiaSqlWork.java new file mode 100644 index 0000000..a0ef1bc --- /dev/null +++ b/observe-entities/src/main/java/fr/ird/observe/RunScriptsTopiaSqlWork.java @@ -0,0 +1,59 @@ +package fr.ird.observe; + +/*- + * #%L + * ObServe :: Entities + * %% + * Copyright (C) 2008 - 2016 IRD, Codelutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * TODO Move this in ToPIA. + * + * Created on 09/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class RunScriptsTopiaSqlWork extends RunScriptTopiaSqlWork { + + /** Logger. */ + private static final Log log = LogFactory.getLog(RunScriptsTopiaSqlWork.class); + + protected final byte[][] contents; + + public RunScriptsTopiaSqlWork(int batchSize, byte[][] contents) { + super(batchSize, null); + this.contents = contents; + } + + @Override + protected void execute0(Connection connection) throws SQLException { + + for (byte[] content : contents) { + new RunScriptTopiaSqlWork(batchSize, content).execute0(connection); + } + + } + +} diff --git a/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceTopia.java b/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceTopia.java index 3646444..ccce727 100644 --- a/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceTopia.java +++ b/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceTopia.java @@ -53,6 +53,7 @@ import org.nuiton.topia.persistence.TopiaNoResultException; import java.sql.Blob; import java.sql.SQLException; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Locale; @@ -133,6 +134,12 @@ public abstract class ObserveServiceTopia implements ObserveService { List<E> entities = dao.findAll(); return entities; } + public <E extends ObserveEntity> List<E> loadEntities(Class<E> entityType, Collection<String> ids) { + ObserveTopiaPersistenceContext persistenceContext = getTopiaPersistenceContext(); + TopiaDao<E> dao = persistenceContext.getDao(entityType); + List<E> entities = dao.forTopiaIdIn(ids).findAll(); + return entities; + } protected <E extends ObserveEntity> Optional<Date> getLastUpdate(Class<E> entityType) { diff --git a/observe-services-topia/src/main/java/fr/ird/observe/services/service/actions/synchro/referential/sql/ApplySqlRequestWork.java b/observe-services-topia/src/main/java/fr/ird/observe/services/service/actions/synchro/referential/sql/ApplySqlRequestWork.java new file mode 100644 index 0000000..3c32d62 --- /dev/null +++ b/observe-services-topia/src/main/java/fr/ird/observe/services/service/actions/synchro/referential/sql/ApplySqlRequestWork.java @@ -0,0 +1,44 @@ +package fr.ird.observe.services.service.actions.synchro.referential.sql; + +import org.nuiton.topia.persistence.support.TopiaSqlWork; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * Created on 14/08/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ApplySqlRequestWork implements TopiaSqlWork { + + private static final int BATCH_SIZE = 100; + + private final Iterable<String> sqlRequests; + + public ApplySqlRequestWork(Iterable<String> sqlRequests) { + this.sqlRequests = sqlRequests; + } + + @Override + public void execute(Connection connection) throws SQLException { + + Statement statement = connection.createStatement(); + + int count = 0; + for (String sqlRequest : sqlRequests) { + statement.addBatch(sqlRequest); + if ((count % BATCH_SIZE) == 0) { + flush(statement); + } + } + flush(statement); + + } + + private void flush(Statement statement) throws SQLException { + statement.executeBatch(); + statement.clearBatch(); + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.