diff options
author | Vitaly Takmazov | 2019-03-01 17:38:31 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2019-03-01 17:38:31 +0300 |
commit | 95f489a6f2e6504177ce7b33f29f9bf53344566e (patch) | |
tree | 1779c4f8214ac14904c2957f82f446ff53aee07a /src/main/java/com/juick/service/MessagesServiceImpl.java | |
parent | baedd85ddb7370e3421135a402c3c48531a039f4 (diff) |
using batchUpdate for tags
Diffstat (limited to 'src/main/java/com/juick/service/MessagesServiceImpl.java')
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 50043582..2a107fbf 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2017, Juick + * Copyright (C) 2008-2019, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,7 +17,9 @@ package com.juick.service; -import com.juick.*; +import com.juick.Message; +import com.juick.Reaction; +import com.juick.User; import com.juick.model.AnonymousUser; import com.juick.model.PrivacyOpts; import com.juick.model.ResponseReply; @@ -29,6 +31,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.IncorrectResultSizeDataAccessException; +import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; @@ -37,12 +40,24 @@ import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import javax.annotation.Nonnull; import javax.inject.Inject; import java.net.URI; -import java.sql.*; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.Types; import java.time.Instant; -import java.util.*; +import java.util.Collections; +import java.util.Comparator; import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; /** @@ -119,7 +134,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ */ @Transactional @Override - public int createMessage(final int uid, final String txt, final String attachment, final Collection<com.juick.Tag> tags) { + public int createMessage(final int uid, final String txt, final String attachment, final List<com.juick.Tag> tags) { SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName("messages") .usingColumns("user_id", "attach", "ts") .usingGeneratedKeyColumns("message_id"); @@ -132,19 +147,21 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } int mid = simpleJdbcInsert.executeAndReturnKey(insertMap).intValue(); if (mid > 0) { - if (CollectionUtils.isNotEmpty(tags)) { - List<Object[]> params = new ArrayList<>(tags.size()); - - for (Tag tag : tags) { - params.add(new Object[]{mid, tag.TID}); - } - getJdbcTemplate().batchUpdate( "INSERT INTO messages_tags(message_id, tag_id) VALUES (?, ?)", - params, new int[]{Types.INTEGER, Types.INTEGER}); + new BatchPreparedStatementSetter() { + @Override + public void setValues(@Nonnull PreparedStatement ps, int i) throws SQLException { + ps.setInt(1, mid); + ps.setInt(2, tags.get(i).TID); + } + @Override + public int getBatchSize() { + return tags.size(); + } + }); } - getJdbcTemplate().update( "INSERT INTO messages_txt(message_id, txt, updated_at) VALUES (?, ?, ?)", new Object[]{mid, txt, Timestamp.from(now)}, |