Debian Codegraph @

src/main/org/h2/result/ResultRemote.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.result;
7
8 import java.io.IOException;
9 import java.util.ArrayList;
10
11 import org.h2.api.ErrorCode;
12 import org.h2.engine.SessionRemote;
13 import org.h2.engine.SysProperties;
14 import org.h2.message.DbException;
15 import org.h2.message.Trace;
16 import org.h2.value.Transfer;
17 import org.h2.value.TypeInfo;
18 import org.h2.value.Value;
19
20 /**
21 * The client side part of a result set that is kept on the server.
22 * In many cases, the complete data is kept on the client side,
23 * but for large results only a subset is in-memory.
24 */
25 public final class ResultRemote extends FetchedResult {
26
27 private int fetchSize;
28 private SessionRemote session;
29 private Transfer transfer;
30 private int id;
31 private final ResultColumn[] columns;
32 private long rowCount;
33 private long rowOffset;
34 private ArrayList<Value[]> result;
35 private final Trace trace;
36
37 public ResultRemote(SessionRemote session, Transfer transfer, int id,
38 int columnCount, int fetchSize) throws IOException {
39 this.session = session;
40 trace = session.getTrace();
41 this.transfer = transfer;
42 this.id = id;
43 this.columns = new ResultColumn[columnCount];
44 rowCount = transfer.readRowCount();
45 for (int i = 0; i < columnCount; i++) {
46 columns[i] = new ResultColumn(transfer);
47 }
48 rowId = -1;
49 this.fetchSize = fetchSize;
50 if (rowCount >= 0) {
51 fetchSize = (int) Math.min(rowCount, fetchSize);
52 result = new ArrayList<>(fetchSize);
53 } else {
54 result = new ArrayList<>();
55 }
56 synchronized (session) {
57 try {
58 if (fetchRows(fetchSize)) {
59 rowCount = result.size();
60 }
61 } catch (IOException e) {
62 throw DbException.convertIOException(e, null);
63 }
64 }
65 }
66
67 @Override
68 public boolean isLazy() {
69 return rowCount < 0L;
70 }
71
72 @Override
73 public String getAlias(int i) {
74 return columns[i].alias;
75 }
76
77 @Override
78 public String getSchemaName(int i) {
79 return columns[i].schemaName;
80 }
81
82 @Override
83 public String getTableName(int i) {
84 return columns[i].tableName;
85 }
86
87 @Override
88 public String getColumnName(int i) {
89 return columns[i].columnName;
90 }
91
92 @Override
93 public TypeInfo getColumnType(int i) {
94 return columns[i].columnType;
95 }
96
97 @Override
98 public boolean isIdentity(int i) {
99 return columns[i].identity;
100 }
101
102 @Override
103 public int getNullable(int i) {
104 return columns[i].nullable;
105 }
106
107 @Override
108 public void reset() {
109 if (rowCount < 0L || rowOffset > 0L) {
110 throw DbException.get(ErrorCode.RESULT_SET_NOT_SCROLLABLE);
111 }
112 rowId = -1;
113 currentRow = null;
114 nextRow = null;
115 afterLast = false;
116 if (session == null) {
117 return;
118 }
119 synchronized (session) {
120 session.checkClosed();
121 try {
122 session.traceOperation("RESULT_RESET", id);
123 transfer.writeInt(SessionRemote.RESULT_RESET).writeInt(id).flush();
124 } catch (IOException e) {
125 throw DbException.convertIOException(e, null);
126 }
127 }
128 }
129
130 @Override
131 public int getVisibleColumnCount() {
132 return columns.length;
133 }
134
135 @Override
136 public long getRowCount() {
137 if (rowCount < 0L) {
138 throw DbException.getUnsupportedException("Row count is unknown for lazy result.");
139 }
140 return rowCount;
141 }
142
143 @Override
144 public boolean hasNext() {
145 if (afterLast) {
146 return false;
147 }
148 if (nextRow == null) {
149 if (rowCount < 0L || rowId < rowCount - 1) {
150 long nextRowId = rowId + 1;
151 if (session != null) {
152 remapIfOld();
153 if (nextRowId - rowOffset >= result.size()) {
154 fetchAdditionalRows();
155 }
156 }
157 int index = (int) (nextRowId - rowOffset);
158 nextRow = index < result.size() ? result.get(index) : null;
159 }
160 }
161 return nextRow != null;
162 }
163
164 private void sendClose() {
165 if (session == null) {
166 return;
167 }
168 // TODO result sets: no reset possible for larger remote result sets
169 try {
170 synchronized (session) {
171 session.traceOperation("RESULT_CLOSE", id);
172 transfer.writeInt(SessionRemote.RESULT_CLOSE).writeInt(id);
173 }
174 } catch (IOException e) {
175 trace.error(e, "close");
176 } finally {
177 transfer = null;
178 session = null;
179 }
180 }
181
182 @Override
183 public void close() {
184 result = null;
185 sendClose();
186 }
187
188 private void remapIfOld() {
189 try {
190 if (id <= session.getCurrentId() - SysProperties.SERVER_CACHED_OBJECTS / 2) {
191 // object is too old - we need to map it to a new id
192 int newId = session.getNextId();
193 session.traceOperation("CHANGE_ID", id);
194 transfer.writeInt(SessionRemote.CHANGE_ID).writeInt(id).writeInt(newId);
195 id = newId;
196 // TODO remote result set: very old result sets may be
197 // already removed on the server (theoretically) - how to
198 // solve this?
199 }
200 } catch (IOException e) {
201 throw DbException.convertIOException(e, null);
202 }
203 }
204
205 private void fetchAdditionalRows() {
206 synchronized (session) {
207 session.checkClosed();
208 try {
209 rowOffset += result.size();
210 result.clear();
211 int fetch = fetchSize;
212 if (rowCount >= 0) {
213 fetch = (int) Math.min(fetch, rowCount - rowOffset);
214 } else if (fetch == Integer.MAX_VALUE) {
215 fetch = SysProperties.SERVER_RESULT_SET_FETCH_SIZE;
216 }
217 session.traceOperation("RESULT_FETCH_ROWS", id);
218 transfer.writeInt(SessionRemote.RESULT_FETCH_ROWS).writeInt(id).writeInt(fetch);
219 session.done(transfer);
220 fetchRows(fetch);
221 } catch (IOException e) {
222 throw DbException.convertIOException(e, null);
223 }
224 }
225 }
226
227 private boolean fetchRows(int fetch) throws IOException {
228 int len = columns.length;
229 for (int r = 0; r < fetch; r++) {
230 switch (transfer.readByte()) {
231 case 1: {
232 Value[] values = new Value[len];
233 for (int i = 0; i < len; i++) {
234 values[i] = transfer.readValue(columns[i].columnType);
235 }
236 result.add(values);
237 break;
238 }
239 case 0:
240 sendClose();
241 return true;
242 case -1:
243 throw SessionRemote.readException(transfer);
244 default:
245 throw DbException.getInternalError();
246 }
247 }
248 if (rowCount >= 0L && rowOffset + result.size() >= rowCount) {
249 sendClose();
250 }
251 return false;
252 }
253
254 @Override
255 public String toString() {
256 return "columns: " + columns.length + (rowCount < 0L ? " lazy" : " rows: " + rowCount) + " pos: " + rowId;
257 }
258
259 @Override
260 public int getFetchSize() {
261 return fetchSize;
262 }
263
264 @Override
265 public void setFetchSize(int fetchSize) {
266 this.fetchSize = fetchSize;
267 }
268
269 @Override
270 public boolean isClosed() {
271 return result == null;
272 }
273
274 }