| 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.value; |
| 7 |
|
| 8 |
import java.io.BufferedReader; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.InputStream; |
| 11 |
import java.io.Reader; |
| 12 |
import java.nio.charset.StandardCharsets; |
| 13 |
|
| 14 |
import org.h2.engine.CastDataProvider; |
| 15 |
import org.h2.engine.Constants; |
| 16 |
import org.h2.engine.SysProperties; |
| 17 |
import org.h2.message.DbException; |
| 18 |
import org.h2.store.DataHandler; |
| 19 |
import org.h2.store.FileStore; |
| 20 |
import org.h2.store.FileStoreOutputStream; |
| 21 |
import org.h2.store.LobStorageInterface; |
| 22 |
import org.h2.store.RangeReader; |
| 23 |
import org.h2.util.Bits; |
| 24 |
import org.h2.util.IOUtils; |
| 25 |
import org.h2.util.MathUtils; |
| 26 |
import org.h2.util.StringUtils; |
| 27 |
import org.h2.value.lob.LobData; |
| 28 |
import org.h2.value.lob.LobDataDatabase; |
| 29 |
import org.h2.value.lob.LobDataFetchOnDemand; |
| 30 |
import org.h2.value.lob.LobDataFile; |
| 31 |
import org.h2.value.lob.LobDataInMemory; |
| 32 |
|
| 33 |
/** |
| 34 |
* Implementation of the CHARACTER LARGE OBJECT data type. |
| 35 |
*/ |
| 36 |
public final class ValueClob extends ValueLob { |
| 37 |
|
| 38 |
/** |
| 39 |
* Creates a small CLOB value that can be stored in the row directly. |
| 40 |
* |
| 41 |
* @param data |
| 42 |
* the data in UTF-8 encoding |
| 43 |
* @return the CLOB |
| 44 |
*/ |
| 45 |
public static ValueClob createSmall(byte[] data) { |
| 46 |
return new ValueClob(new LobDataInMemory(data), data.length, |
| 47 |
new String(data, StandardCharsets.UTF_8).length()); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Creates a small CLOB value that can be stored in the row directly. |
| 52 |
* |
| 53 |
* @param data |
| 54 |
* the data in UTF-8 encoding |
| 55 |
* @param charLength |
| 56 |
* the count of characters, must be exactly the same as count of |
| 57 |
* characters in the data |
| 58 |
* @return the CLOB |
| 59 |
*/ |
| 60 |
public static ValueClob createSmall(byte[] data, long charLength) { |
| 61 |
return new ValueClob(new LobDataInMemory(data), data.length, charLength); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Creates a small CLOB value that can be stored in the row directly. |
| 66 |
* |
| 67 |
* @param string |
| 68 |
* the string with value |
| 69 |
* @return the CLOB |
| 70 |
*/ |
| 71 |
public static ValueClob createSmall(String string) { |
| 72 |
byte[] bytes = string.getBytes(StandardCharsets.UTF_8); |
| 73 |
return new ValueClob(new LobDataInMemory(bytes), bytes.length, string.length()); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Create a temporary CLOB value from a stream. |
| 78 |
* |
| 79 |
* @param in |
| 80 |
* the reader |
| 81 |
* @param length |
| 82 |
* the number of characters to read, or -1 for no limit |
| 83 |
* @param handler |
| 84 |
* the data handler |
| 85 |
* @return the lob value |
| 86 |
*/ |
| 87 |
public static ValueClob createTempClob(Reader in, long length, DataHandler handler) { |
| 88 |
if (length >= 0) { |
| 89 |
// Otherwise BufferedReader may try to read more data than needed |
| 90 |
// and that |
| 91 |
// blocks the network level |
| 92 |
try { |
| 93 |
in = new RangeReader(in, 0, length); |
| 94 |
} catch (IOException e) { |
| 95 |
throw DbException.convert(e); |
| 96 |
} |
| 97 |
} |
| 98 |
BufferedReader reader; |
| 99 |
if (in instanceof BufferedReader) { |
| 100 |
reader = (BufferedReader) in; |
| 101 |
} else { |
| 102 |
reader = new BufferedReader(in, Constants.IO_BUFFER_SIZE); |
| 103 |
} |
| 104 |
try { |
| 105 |
long remaining = Long.MAX_VALUE; |
| 106 |
if (length >= 0 && length < remaining) { |
| 107 |
remaining = length; |
| 108 |
} |
| 109 |
int len = ValueLob.getBufferSize(handler, remaining); |
| 110 |
char[] buff; |
| 111 |
if (len >= Integer.MAX_VALUE) { |
| 112 |
String data = IOUtils.readStringAndClose(reader, -1); |
| 113 |
buff = data.toCharArray(); |
| 114 |
len = buff.length; |
| 115 |
} else { |
| 116 |
buff = new char[len]; |
| 117 |
reader.mark(len); |
| 118 |
len = IOUtils.readFully(reader, buff, len); |
| 119 |
} |
| 120 |
if (len <= handler.getMaxLengthInplaceLob()) { |
| 121 |
return ValueClob.createSmall(new String(buff, 0, len)); |
| 122 |
} |
| 123 |
reader.reset(); |
| 124 |
return createTemporary(handler, reader, remaining); |
| 125 |
} catch (IOException e) { |
| 126 |
throw DbException.convertIOException(e, null); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Create a CLOB in a temporary file. |
| 132 |
*/ |
| 133 |
private static ValueClob createTemporary(DataHandler handler, Reader in, long remaining) throws IOException { |
| 134 |
String fileName = ValueLob.createTempLobFileName(handler); |
| 135 |
FileStore tempFile = handler.openFile(fileName, "rw", false); |
| 136 |
tempFile.autoDelete(); |
| 137 |
|
| 138 |
long octetLength = 0L, charLength = 0L; |
| 139 |
try (FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null)) { |
| 140 |
char[] buff = new char[Constants.IO_BUFFER_SIZE]; |
| 141 |
while (true) { |
| 142 |
int len = ValueLob.getBufferSize(handler, remaining); |
| 143 |
len = IOUtils.readFully(in, buff, len); |
| 144 |
if (len == 0) { |
| 145 |
break; |
| 146 |
} |
| 147 |
// TODO reduce memory allocation |
| 148 |
byte[] data = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8); |
| 149 |
out.write(data); |
| 150 |
octetLength += data.length; |
| 151 |
charLength += len; |
| 152 |
} |
| 153 |
} |
| 154 |
return new ValueClob(new LobDataFile(handler, fileName, tempFile), octetLength, charLength); |
| 155 |
} |
| 156 |
|
| 157 |
public ValueClob(LobData lobData, long octetLength, long charLength) { |
| 158 |
super(lobData, octetLength, charLength); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public int getValueType() { |
| 163 |
return CLOB; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public String getString() { |
| 168 |
if (charLength > Constants.MAX_STRING_LENGTH) { |
| 169 |
throw getStringTooLong(charLength); |
| 170 |
} |
| 171 |
if (lobData instanceof LobDataInMemory) { |
| 172 |
return new String(((LobDataInMemory) lobData).getSmall(), StandardCharsets.UTF_8); |
| 173 |
} |
| 174 |
return readString((int) charLength); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
byte[] getBytesInternal() { |
| 179 |
long p = octetLength; |
| 180 |
if (p >= 0L) { |
| 181 |
if (p > Constants.MAX_STRING_LENGTH) { |
| 182 |
throw getBinaryTooLong(p); |
| 183 |
} |
| 184 |
return readBytes((int) p); |
| 185 |
} |
| 186 |
if (octetLength > Constants.MAX_STRING_LENGTH) { |
| 187 |
throw getBinaryTooLong(octetLength()); |
| 188 |
} |
| 189 |
byte[] b = readBytes(Integer.MAX_VALUE); |
| 190 |
octetLength = p = b.length; |
| 191 |
if (p > Constants.MAX_STRING_LENGTH) { |
| 192 |
throw getBinaryTooLong(p); |
| 193 |
} |
| 194 |
return b; |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public InputStream getInputStream() { |
| 199 |
return lobData.getInputStream(-1L); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public InputStream getInputStream(long oneBasedOffset, long length) { |
| 204 |
return rangeInputStream(lobData.getInputStream(-1L), oneBasedOffset, length, -1L); |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public Reader getReader(long oneBasedOffset, long length) { |
| 209 |
return rangeReader(getReader(), oneBasedOffset, length, charLength); |
| 210 |
} |
| 211 |
|
| 212 |
@Override |
| 213 |
public int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) { |
| 214 |
if (v == this) { |
| 215 |
return 0; |
| 216 |
} |
| 217 |
ValueClob v2 = (ValueClob) v; |
| 218 |
LobData lobData = this.lobData, lobData2 = v2.lobData; |
| 219 |
if (lobData.getClass() == lobData2.getClass()) { |
| 220 |
if (lobData instanceof LobDataInMemory) { |
| 221 |
return Integer.signum(getString().compareTo(v2.getString())); |
| 222 |
} else if (lobData instanceof LobDataDatabase) { |
| 223 |
if (((LobDataDatabase) lobData).getLobId() == ((LobDataDatabase) lobData2).getLobId()) { |
| 224 |
return 0; |
| 225 |
} |
| 226 |
} else if (lobData instanceof LobDataFetchOnDemand) { |
| 227 |
if (((LobDataFetchOnDemand) lobData).getLobId() == ((LobDataFetchOnDemand) lobData2).getLobId()) { |
| 228 |
return 0; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
return compare(this, v2); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Compares two CLOB values directly. |
| 237 |
* |
| 238 |
* @param v1 |
| 239 |
* first CLOB value |
| 240 |
* @param v2 |
| 241 |
* second CLOB value |
| 242 |
* @return result of comparison |
| 243 |
*/ |
| 244 |
private static int compare(ValueClob v1, ValueClob v2) { |
| 245 |
long minPrec = Math.min(v1.charLength, v2.charLength); |
| 246 |
try (Reader reader1 = v1.getReader(); Reader reader2 = v2.getReader()) { |
| 247 |
char[] buf1 = new char[BLOCK_COMPARISON_SIZE]; |
| 248 |
char[] buf2 = new char[BLOCK_COMPARISON_SIZE]; |
| 249 |
for (; minPrec >= BLOCK_COMPARISON_SIZE; minPrec -= BLOCK_COMPARISON_SIZE) { |
| 250 |
if (IOUtils.readFully(reader1, buf1, BLOCK_COMPARISON_SIZE) != BLOCK_COMPARISON_SIZE |
| 251 |
|| IOUtils.readFully(reader2, buf2, BLOCK_COMPARISON_SIZE) != BLOCK_COMPARISON_SIZE) { |
| 252 |
throw DbException.getUnsupportedException("Invalid LOB"); |
| 253 |
} |
| 254 |
int cmp = Bits.compareNotNull(buf1, buf2); |
| 255 |
if (cmp != 0) { |
| 256 |
return cmp; |
| 257 |
} |
| 258 |
} |
| 259 |
for (;;) { |
| 260 |
int c1 = reader1.read(), c2 = reader2.read(); |
| 261 |
if (c1 < 0) { |
| 262 |
return c2 < 0 ? 0 : -1; |
| 263 |
} |
| 264 |
if (c2 < 0) { |
| 265 |
return 1; |
| 266 |
} |
| 267 |
if (c1 != c2) { |
| 268 |
return c1 < c2 ? -1 : 1; |
| 269 |
} |
| 270 |
} |
| 271 |
} catch (IOException ex) { |
| 272 |
throw DbException.convert(ex); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 278 |
if ((sqlFlags & REPLACE_LOBS_FOR_TRACE) != 0 |
| 279 |
&& (!(lobData instanceof LobDataInMemory) || charLength > SysProperties.MAX_TRACE_DATA_LENGTH)) { |
| 280 |
builder.append("SPACE(").append(charLength); |
| 281 |
formatLobDataComment(builder); |
| 282 |
} else { |
| 283 |
if ((sqlFlags & (REPLACE_LOBS_FOR_TRACE | NO_CASTS)) == 0) { |
| 284 |
StringUtils.quoteStringSQL(builder.append("CAST("), getString()).append(" AS CHARACTER LARGE OBJECT(") |
| 285 |
.append(charLength).append("))"); |
| 286 |
} else { |
| 287 |
StringUtils.quoteStringSQL(builder, getString()); |
| 288 |
} |
| 289 |
} |
| 290 |
return builder; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Convert the precision to the requested value. |
| 295 |
* |
| 296 |
* @param precision |
| 297 |
* the new precision |
| 298 |
* @return the truncated or this value |
| 299 |
*/ |
| 300 |
ValueClob convertPrecision(long precision) { |
| 301 |
if (this.charLength <= precision) { |
| 302 |
return this; |
| 303 |
} |
| 304 |
ValueClob lob; |
| 305 |
DataHandler handler = lobData.getDataHandler(); |
| 306 |
if (handler != null) { |
| 307 |
lob = createTempClob(getReader(), precision, handler); |
| 308 |
} else { |
| 309 |
try { |
| 310 |
lob = createSmall(IOUtils.readStringAndClose(getReader(), MathUtils.convertLongToInt(precision))); |
| 311 |
} catch (IOException e) { |
| 312 |
throw DbException.convertIOException(e, null); |
| 313 |
} |
| 314 |
} |
| 315 |
return lob; |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
public ValueLob copy(DataHandler database, int tableId) { |
| 320 |
if (lobData instanceof LobDataInMemory) { |
| 321 |
byte[] small = ((LobDataInMemory) lobData).getSmall(); |
| 322 |
if (small.length > database.getMaxLengthInplaceLob()) { |
| 323 |
LobStorageInterface s = database.getLobStorage(); |
| 324 |
ValueClob v = s.createClob(getReader(), charLength); |
| 325 |
ValueLob v2 = v.copy(database, tableId); |
| 326 |
v.remove(); |
| 327 |
return v2; |
| 328 |
} |
| 329 |
return this; |
| 330 |
} else if (lobData instanceof LobDataDatabase) { |
| 331 |
return database.getLobStorage().copyLob(this, tableId); |
| 332 |
} else { |
| 333 |
throw new UnsupportedOperationException(); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public long charLength() { |
| 339 |
return charLength; |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public long octetLength() { |
| 344 |
long p = octetLength; |
| 345 |
if (p < 0L) { |
| 346 |
if (lobData instanceof LobDataInMemory) { |
| 347 |
p = ((LobDataInMemory) lobData).getSmall().length; |
| 348 |
} else { |
| 349 |
try (InputStream is = getInputStream()) { |
| 350 |
p = 0L; |
| 351 |
for (;;) { |
| 352 |
p += is.skip(Long.MAX_VALUE); |
| 353 |
if (is.read() < 0) { |
| 354 |
break; |
| 355 |
} |
| 356 |
p++; |
| 357 |
} |
| 358 |
} catch (IOException e) { |
| 359 |
throw DbException.convertIOException(e, null); |
| 360 |
} |
| 361 |
} |
| 362 |
octetLength = p; |
| 363 |
} |
| 364 |
return p; |
| 365 |
} |
| 366 |
|
| 367 |
} |