@@ -20,28 +20,24 @@ class ClickHouseResult implements Result {
20
20
21
21
private static final Logger log = LoggerFactory .getLogger (ClickHouseResult .class );
22
22
23
- private final Flux <? extends Result .Segment > rowSegments ;
24
- private final Mono <? extends Result .Segment > updatedCount ;
25
23
private final Flux <? extends Result .Segment > segments ;
26
24
27
25
ClickHouseResult (ClickHouseResponse response ) {
28
- this . rowSegments = Mono .just (response )
26
+ Flux <? extends RowSegment > rowSegments = Mono .just (response )
29
27
.flatMapMany (resp -> Flux
30
28
.fromStream (StreamSupport .stream (resp .records ().spliterator (), false )
31
29
.map (rec -> ClickHousePair .of (resp .getColumns (), rec ))))
32
30
.map (pair -> new ClickHouseRow (pair .getRight (), pair .getLeft ()))
33
31
.map (RowSegment ::new );
34
- this . updatedCount = Mono .just (response ).map (ClickHouseResponse ::getSummary )
32
+ Mono <? extends UpdateCount > updatedCount = Mono .just (response ).map (ClickHouseResponse ::getSummary )
35
33
.map (ClickHouseResponseSummary ::getProgress )
36
34
.map (ClickHouseResponseSummary .Progress ::getWrittenRows )
37
35
.map (UpdateCount ::new );
38
- this .segments = Flux .concat (this . updatedCount , this . rowSegments );
36
+ this .segments = Flux .concat (updatedCount , rowSegments ). doOnComplete ( response :: close );
39
37
}
40
38
41
- ClickHouseResult (Flux <? extends Result .Segment > rowSegments , Mono <? extends Result .Segment > updatedCount ) {
42
- this .rowSegments = rowSegments ;
43
- this .updatedCount = updatedCount ;
44
- this .segments = Flux .concat (this .updatedCount , this .rowSegments );
39
+ ClickHouseResult (Flux <? extends Result .Segment > rowSegments ) {
40
+ this .segments = rowSegments ;
45
41
}
46
42
47
43
/**
@@ -51,12 +47,16 @@ class ClickHouseResult implements Result {
51
47
*/
52
48
@ Override
53
49
public Mono <Integer > getRowsUpdated () {
54
- return updatedCount .map (val -> (int ) ((UpdateCount ) val ).value ());
50
+ return this .segments .filter (segment -> segment instanceof UpdateCount )
51
+ .cast (UpdateCount .class )
52
+ .map (UpdateCount ::value )
53
+ .reduce (Long ::sum )
54
+ .map (Math ::toIntExact );
55
55
}
56
56
57
57
@ Override
58
58
public <T > Publisher <T > map (BiFunction <Row , RowMetadata , ? extends T > biFunction ) {
59
- return rowSegments .cast (RowSegment .class )
59
+ return this . segments . filter ( segment -> segment instanceof RowSegment ) .cast (RowSegment .class )
60
60
.map (RowSegment ::row ).handle ((row , sink ) -> {
61
61
try {
62
62
sink .next (biFunction .apply (row , row .getMetadata ()));
@@ -68,7 +68,7 @@ public <T> Publisher<T> map(BiFunction<Row, RowMetadata, ? extends T> biFunction
68
68
69
69
@ Override
70
70
public Result filter (Predicate <Segment > predicate ) {
71
- return new ClickHouseResult (segments .filter (predicate ), updatedCount . filter ( predicate ) );
71
+ return new ClickHouseResult (segments .filter (predicate ));
72
72
}
73
73
74
74
@ Override
0 commit comments