|
1 | 1 | package org.heigit.ohsome.oshdb.api.mapreducer.backend;
|
2 | 2 |
|
| 3 | +import static java.util.Spliterators.spliteratorUnknownSize; |
3 | 4 | import static java.util.stream.Collectors.joining;
|
4 | 5 |
|
5 | 6 | import java.io.IOException;
|
6 | 7 | import java.io.ObjectInputStream;
|
| 8 | +import java.sql.Connection; |
7 | 9 | import java.sql.ResultSet;
|
8 | 10 | import java.sql.SQLException;
|
9 | 11 | import java.util.Iterator;
|
10 | 12 | import java.util.NoSuchElementException;
|
11 | 13 | import java.util.Optional;
|
12 |
| -import java.util.Spliterators; |
13 |
| -import java.util.stream.Collectors; |
14 | 14 | import java.util.stream.Stream;
|
15 | 15 | import java.util.stream.StreamSupport;
|
16 | 16 | import javax.annotation.Nonnull;
|
@@ -82,49 +82,60 @@ protected Stream<GridOSHEntity> getOshCellsStream(CellIdRange cellIdRange) {
|
82 | 82 | pstmt.setLong(2, cellIdRange.getStart().getId());
|
83 | 83 | pstmt.setLong(3, cellIdRange.getEnd().getId());
|
84 | 84 | var oshCellsRawData = pstmt.executeQuery();
|
85 |
| - return StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
86 |
| - new Iterator<GridOSHEntity>() { |
87 |
| - GridOSHEntity next; |
88 |
| - @Override |
89 |
| - public boolean hasNext() { |
90 |
| - return next != null || (next = getNext()) != null; |
91 |
| - } |
92 |
| - |
93 |
| - @Override |
94 |
| - public GridOSHEntity next() { |
95 |
| - if (!hasNext()) { |
96 |
| - throw new NoSuchElementException(); |
97 |
| - } |
98 |
| - var grid = next; |
99 |
| - next = null; |
100 |
| - return grid; |
101 |
| - } |
102 |
| - |
103 |
| - private GridOSHEntity getNext() { |
104 |
| - try { |
105 |
| - if (!oshCellsRawData.next()) { |
106 |
| - try { |
107 |
| - oshCellsRawData.close(); |
108 |
| - } finally { |
109 |
| - conn.close(); |
110 |
| - } |
111 |
| - return null; |
112 |
| - } |
113 |
| - return readOshCellRawData(oshCellsRawData); |
114 |
| - } catch (IOException | ClassNotFoundException | SQLException e) { |
115 |
| - var exception = new OSHDBException(e); |
116 |
| - try { |
117 |
| - conn.close(); |
118 |
| - } catch (Exception e2) { |
119 |
| - exception.addSuppressed(e2); |
120 |
| - } |
121 |
| - throw exception; |
122 |
| - } |
123 |
| - } |
124 |
| - }, 0 |
| 85 | + return StreamSupport.stream(spliteratorUnknownSize( |
| 86 | + new GridOSHEntityIterator(oshCellsRawData, conn), 0 |
125 | 87 | ), false);
|
126 | 88 | } catch (SQLException e) {
|
127 | 89 | throw new OSHDBException(e);
|
128 | 90 | }
|
129 | 91 | }
|
| 92 | + |
| 93 | + private class GridOSHEntityIterator implements Iterator<GridOSHEntity> { |
| 94 | + |
| 95 | + private final ResultSet oshCellsRawData; |
| 96 | + private final Connection conn; |
| 97 | + GridOSHEntity next; |
| 98 | + |
| 99 | + public GridOSHEntityIterator(ResultSet oshCellsRawData, Connection conn) { |
| 100 | + this.oshCellsRawData = oshCellsRawData; |
| 101 | + this.conn = conn; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean hasNext() { |
| 106 | + return next != null || (next = getNext()) != null; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public GridOSHEntity next() { |
| 111 | + if (!hasNext()) { |
| 112 | + throw new NoSuchElementException(); |
| 113 | + } |
| 114 | + var grid = next; |
| 115 | + next = null; |
| 116 | + return grid; |
| 117 | + } |
| 118 | + |
| 119 | + private GridOSHEntity getNext() { |
| 120 | + try { |
| 121 | + if (!oshCellsRawData.next()) { |
| 122 | + try { |
| 123 | + oshCellsRawData.close(); |
| 124 | + } finally { |
| 125 | + conn.close(); |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | + return readOshCellRawData(oshCellsRawData); |
| 130 | + } catch (IOException | ClassNotFoundException | SQLException e) { |
| 131 | + var exception = new OSHDBException(e); |
| 132 | + try { |
| 133 | + conn.close(); |
| 134 | + } catch (Exception e2) { |
| 135 | + exception.addSuppressed(e2); |
| 136 | + } |
| 137 | + throw exception; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
130 | 141 | }
|
0 commit comments