| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, and the |
| 3 |
* EPL 1.0 (https://h2database.com/html/license.html). Initial Developer: H2 |
| 4 |
* Group |
| 5 |
*/ |
| 6 |
package org.h2.value; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.Reader; |
| 11 |
|
| 12 |
import org.h2.engine.Constants; |
| 13 |
import org.h2.engine.SysProperties; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.store.DataHandler; |
| 16 |
import org.h2.store.LobStorageFrontend; |
| 17 |
import org.h2.store.LobStorageInterface; |
| 18 |
import org.h2.store.RangeInputStream; |
| 19 |
import org.h2.store.RangeReader; |
| 20 |
import org.h2.store.fs.FileUtils; |
| 21 |
import org.h2.util.IOUtils; |
| 22 |
import org.h2.util.MathUtils; |
| 23 |
import org.h2.util.StringUtils; |
| 24 |
import org.h2.util.Utils; |
| 25 |
import org.h2.value.lob.LobData; |
| 26 |
import org.h2.value.lob.LobDataDatabase; |
| 27 |
import org.h2.value.lob.LobDataFetchOnDemand; |
| 28 |
import org.h2.value.lob.LobDataInMemory; |
| 29 |
|
| 30 |
/** |
| 31 |
* A implementation of the BINARY LARGE OBJECT and CHARACTER LARGE OBJECT data |
| 32 |
* types. Small objects are kept in memory and stored in the record. Large |
| 33 |
* objects are either stored in the database, or in temporary files. |
| 34 |
*/ |
| 35 |
public abstract class ValueLob extends Value { |
| 36 |
|
| 37 |
static final int BLOCK_COMPARISON_SIZE = 512; |
| 38 |
|
| 39 |
private static void rangeCheckUnknown(long zeroBasedOffset, long length) { |
| 40 |
if (zeroBasedOffset < 0) { |
| 41 |
throw DbException.getInvalidValueException("offset", zeroBasedOffset + 1); |
| 42 |
} |
| 43 |
if (length < 0) { |
| 44 |
throw DbException.getInvalidValueException("length", length); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create an input stream that is s subset of the given stream. |
| 50 |
* |
| 51 |
* @param inputStream the source input stream |
| 52 |
* @param oneBasedOffset the offset (1 means no offset) |
| 53 |
* @param length the length of the result, in bytes |
| 54 |
* @param dataSize the length of the input, in bytes |
| 55 |
* @return the smaller input stream |
| 56 |
*/ |
| 57 |
protected static InputStream rangeInputStream(InputStream inputStream, long oneBasedOffset, long length, |
| 58 |
long dataSize) { |
| 59 |
if (dataSize > 0) { |
| 60 |
rangeCheck(oneBasedOffset - 1, length, dataSize); |
| 61 |
} else { |
| 62 |
rangeCheckUnknown(oneBasedOffset - 1, length); |
| 63 |
} |
| 64 |
try { |
| 65 |
return new RangeInputStream(inputStream, oneBasedOffset - 1, length); |
| 66 |
} catch (IOException e) { |
| 67 |
throw DbException.getInvalidValueException("offset", oneBasedOffset); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Create a reader that is s subset of the given reader. |
| 73 |
* |
| 74 |
* @param reader the input reader |
| 75 |
* @param oneBasedOffset the offset (1 means no offset) |
| 76 |
* @param length the length of the result, in bytes |
| 77 |
* @param dataSize the length of the input, in bytes |
| 78 |
* @return the smaller input stream |
| 79 |
*/ |
| 80 |
static Reader rangeReader(Reader reader, long oneBasedOffset, long length, long dataSize) { |
| 81 |
if (dataSize > 0) { |
| 82 |
rangeCheck(oneBasedOffset - 1, length, dataSize); |
| 83 |
} else { |
| 84 |
rangeCheckUnknown(oneBasedOffset - 1, length); |
| 85 |
} |
| 86 |
try { |
| 87 |
return new RangeReader(reader, oneBasedOffset - 1, length); |
| 88 |
} catch (IOException e) { |
| 89 |
throw DbException.getInvalidValueException("offset", oneBasedOffset); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private TypeInfo type; |
| 94 |
|
| 95 |
final LobData lobData; |
| 96 |
|
| 97 |
/** |
| 98 |
* Length in bytes. |
| 99 |
*/ |
| 100 |
long octetLength; |
| 101 |
|
| 102 |
/** |
| 103 |
* Length in characters. |
| 104 |
*/ |
| 105 |
long charLength; |
| 106 |
|
| 107 |
/** |
| 108 |
* Cache the hashCode because it can be expensive to compute. |
| 109 |
*/ |
| 110 |
private int hash; |
| 111 |
|
| 112 |
ValueLob(LobData lobData, long octetLength, long charLength) { |
| 113 |
this.lobData = lobData; |
| 114 |
this.octetLength = octetLength; |
| 115 |
this.charLength = charLength; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create file name for temporary LOB storage |
| 120 |
* @param handler to get path from |
| 121 |
* @return full path and name of the created file |
| 122 |
* @throws IOException if file creation fails |
| 123 |
*/ |
| 124 |
static String createTempLobFileName(DataHandler handler) throws IOException { |
| 125 |
String path = handler.getDatabasePath(); |
| 126 |
if (path.isEmpty()) { |
| 127 |
path = SysProperties.PREFIX_TEMP_FILE; |
| 128 |
} |
| 129 |
return FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true); |
| 130 |
} |
| 131 |
|
| 132 |
static int getBufferSize(DataHandler handler, long remaining) { |
| 133 |
if (remaining < 0 || remaining > Integer.MAX_VALUE) { |
| 134 |
remaining = Integer.MAX_VALUE; |
| 135 |
} |
| 136 |
int inplace = handler.getMaxLengthInplaceLob(); |
| 137 |
long m = Constants.IO_BUFFER_SIZE; |
| 138 |
if (m < remaining && m <= inplace) { |
| 139 |
// using "1L" to force long arithmetic because |
| 140 |
// inplace could be Integer.MAX_VALUE |
| 141 |
m = Math.min(remaining, inplace + 1L); |
| 142 |
// the buffer size must be bigger than the inplace lob, otherwise we |
| 143 |
// can't know if it must be stored in-place or not |
| 144 |
m = MathUtils.roundUpLong(m, Constants.IO_BUFFER_SIZE); |
| 145 |
} |
| 146 |
m = Math.min(remaining, m); |
| 147 |
m = MathUtils.convertLongToInt(m); |
| 148 |
if (m < 0) { |
| 149 |
m = Integer.MAX_VALUE; |
| 150 |
} |
| 151 |
return (int) m; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Check if this value is linked to a specific table. For values that are |
| 156 |
* kept fully in memory, this method returns false. |
| 157 |
* |
| 158 |
* @return true if it is |
| 159 |
*/ |
| 160 |
public boolean isLinkedToTable() { |
| 161 |
return lobData.isLinkedToTable(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Remove the underlying resource, if any. For values that are kept fully in |
| 166 |
* memory this method has no effect. |
| 167 |
*/ |
| 168 |
public void remove() { |
| 169 |
lobData.remove(this); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Copy a large value, to be used in the given table. For values that are |
| 174 |
* kept fully in memory this method has no effect. |
| 175 |
* |
| 176 |
* @param database the data handler |
| 177 |
* @param tableId the table where this object is used |
| 178 |
* @return the new value or itself |
| 179 |
*/ |
| 180 |
public abstract ValueLob copy(DataHandler database, int tableId); |
| 181 |
|
| 182 |
@Override |
| 183 |
public TypeInfo getType() { |
| 184 |
TypeInfo type = this.type; |
| 185 |
if (type == null) { |
| 186 |
int valueType = getValueType(); |
| 187 |
this.type = type = new TypeInfo(valueType, valueType == CLOB ? charLength : octetLength, 0, null); |
| 188 |
} |
| 189 |
return type; |
| 190 |
} |
| 191 |
|
| 192 |
DbException getStringTooLong(long precision) { |
| 193 |
return DbException.getValueTooLongException("CHARACTER VARYING", readString(81), precision); |
| 194 |
} |
| 195 |
|
| 196 |
String readString(int len) { |
| 197 |
try { |
| 198 |
return IOUtils.readStringAndClose(getReader(), len); |
| 199 |
} catch (IOException e) { |
| 200 |
throw DbException.convertIOException(e, toString()); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public Reader getReader() { |
| 206 |
return IOUtils.getReader(getInputStream()); |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public byte[] getBytes() { |
| 211 |
if (lobData instanceof LobDataInMemory) { |
| 212 |
return Utils.cloneByteArray(getSmall()); |
| 213 |
} |
| 214 |
return getBytesInternal(); |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public byte[] getBytesNoCopy() { |
| 219 |
if (lobData instanceof LobDataInMemory) { |
| 220 |
return getSmall(); |
| 221 |
} |
| 222 |
return getBytesInternal(); |
| 223 |
} |
| 224 |
|
| 225 |
private byte[] getSmall() { |
| 226 |
byte[] small = ((LobDataInMemory) lobData).getSmall(); |
| 227 |
int p = small.length; |
| 228 |
if (p > Constants.MAX_STRING_LENGTH) { |
| 229 |
throw DbException.getValueTooLongException("BINARY VARYING", StringUtils.convertBytesToHex(small, 41), p); |
| 230 |
} |
| 231 |
return small; |
| 232 |
} |
| 233 |
|
| 234 |
abstract byte[] getBytesInternal(); |
| 235 |
|
| 236 |
DbException getBinaryTooLong(long precision) { |
| 237 |
return DbException.getValueTooLongException("BINARY VARYING", StringUtils.convertBytesToHex(readBytes(41)), |
| 238 |
precision); |
| 239 |
} |
| 240 |
|
| 241 |
byte[] readBytes(int len) { |
| 242 |
try { |
| 243 |
return IOUtils.readBytesAndClose(getInputStream(), len); |
| 244 |
} catch (IOException e) { |
| 245 |
throw DbException.convertIOException(e, toString()); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public int hashCode() { |
| 251 |
if (hash == 0) { |
| 252 |
int valueType = getValueType(); |
| 253 |
long length = valueType == Value.CLOB ? charLength : octetLength; |
| 254 |
if (length > 4096) { |
| 255 |
// TODO: should calculate the hash code when saving, and store |
| 256 |
// it in the database file |
| 257 |
return (int) (length ^ (length >>> 32)); |
| 258 |
} |
| 259 |
hash = Utils.getByteArrayHash(getBytesNoCopy()); |
| 260 |
} |
| 261 |
return hash; |
| 262 |
} |
| 263 |
|
| 264 |
@Override |
| 265 |
public boolean equals(Object other) { |
| 266 |
if (!(other instanceof ValueLob)) |
| 267 |
return false; |
| 268 |
ValueLob otherLob = (ValueLob) other; |
| 269 |
if (hashCode() != otherLob.hashCode()) |
| 270 |
return false; |
| 271 |
return compareTypeSafe((Value) other, null, null) == 0; |
| 272 |
} |
| 273 |
|
| 274 |
@Override |
| 275 |
public int getMemory() { |
| 276 |
return lobData.getMemory(); |
| 277 |
} |
| 278 |
|
| 279 |
public LobData getLobData() { |
| 280 |
return lobData; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Create an independent copy of this value, that will be bound to a result. |
| 285 |
* |
| 286 |
* @return the value (this for small objects) |
| 287 |
*/ |
| 288 |
public ValueLob copyToResult() { |
| 289 |
if (lobData instanceof LobDataDatabase) { |
| 290 |
LobStorageInterface s = lobData.getDataHandler().getLobStorage(); |
| 291 |
if (!s.isReadOnly()) { |
| 292 |
return s.copyLob(this, LobStorageFrontend.TABLE_RESULT); |
| 293 |
} |
| 294 |
} |
| 295 |
return this; |
| 296 |
} |
| 297 |
|
| 298 |
final void formatLobDataComment(StringBuilder builder) { |
| 299 |
if (lobData instanceof LobDataDatabase) { |
| 300 |
LobDataDatabase lobDb = (LobDataDatabase) lobData; |
| 301 |
builder.append(" /* table: ").append(lobDb.getTableId()).append(" id: ").append(lobDb.getLobId()) |
| 302 |
.append(" */)"); |
| 303 |
} else if (lobData instanceof LobDataFetchOnDemand) { |
| 304 |
LobDataFetchOnDemand lobDemand = (LobDataFetchOnDemand) lobData; |
| 305 |
builder.append(" /* table: ").append(lobDemand.getTableId()).append(" id: ") |
| 306 |
.append(lobDemand.getLobId()).append(" */)"); |
| 307 |
} else { |
| 308 |
builder.append(" /* ").append(lobData.toString().replaceAll("\\*/", "\\\\*\\\\/")) |
| 309 |
.append(" */"); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
} |