Skip to content

Commit 8fdf8ab

Browse files
committed
Modify AbstractSessionContextImpl.generateTempIdTable(...) so that it chunks the insertions into the temp id table to work around limitations in some versions of SQL server of a maximum of 1000 rows in an INSERT statement
1 parent 93dc581 commit 8fdf8ab

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
* Modify `AbstractSessionContextImpl.generateTempIdTable(...)` so that it chunks the insertions into the temp id table to work around limitations in some versions of SQL server of a maximum of 1000 rows in an INSERT statement.
6+
57
### [v6.121](https://github.com/replicant4j/replicant/tree/v6.121) (2022-04-07) · [Full Changelog](https://github.com/spritz/spritz/compare/v6.120...v6.121)
68

79
Changes in this release:

server/src/main/java/org/realityforge/replicant/server/ee/AbstractSessionContextImpl.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.sql.Statement;
77
import java.util.Collection;
88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.concurrent.atomic.AtomicInteger;
911
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
1013
import javax.annotation.Nonnull;
1114
import javax.annotation.Nullable;
1215
import javax.persistence.EntityManager;
@@ -73,13 +76,26 @@ protected String generateTempIdTable( @Nonnull final List<ChannelAddress> addres
7376
{
7477
return
7578
"DECLARE @Ids TABLE ( Id INTEGER NOT NULL );\n" +
76-
"INSERT INTO @Ids VALUES " +
77-
addresses.stream()
78-
.map( a -> "(" + a.getSubChannelId() + ")" )
79-
.collect( Collectors.joining( "," ) ) +
79+
chunked( addresses.stream().map( ChannelAddress::getSubChannelId ), 900 )
80+
.map( ids ->
81+
"INSERT INTO @Ids VALUES " +
82+
ids.stream().map( id -> "(" + id + ")" ).collect( Collectors.joining( "," ) ) ).
83+
collect( Collectors.joining( "\n" ) ) +
8084
"\n";
8185
}
8286

87+
@SuppressWarnings( "SameParameterValue" )
88+
private static <T> Stream<List<T>> chunked( @Nonnull final Stream<T> stream, final int chunkSize )
89+
{
90+
final AtomicInteger index = new AtomicInteger( 0 );
91+
92+
return stream
93+
.collect( Collectors.groupingBy( x -> index.getAndIncrement() / chunkSize ) )
94+
.entrySet().stream()
95+
.sorted( Map.Entry.comparingByKey() )
96+
.map( Map.Entry::getValue );
97+
}
98+
8399
protected void bulkLinkFromSourceGraphToTargetGraph( @Nonnull final ReplicantSession session,
84100
@Nullable final Object filter,
85101
@Nonnull final ChangeSet changeSet,

0 commit comments

Comments
 (0)