/** * Create a LOB value with the given parameters. * * @param type the data type * @param handler the file handler * @param tableId the table object id * @param objectId the object id * @param precision the precision (length in elements) * @param compression if compression is used * @return the value object */ public static ValueLob openLinked(int type, int tableId, int objectId, long precision) { String fileName = getFileName(tableId, objectId); return new ValueLob(type, fileName, tableId, objectId, true/* linked */, precision); }
@Override public boolean equals(Object other) { return other instanceof ValueLob && compareSecure((Value) other, null) == 0; }
@Override public Value convertPrecision(long precision, boolean force) { if (this.precision <= precision) { return this; } ValueLob lob; if (type == CLOB) { lob = ValueLob.createClob(getReader(), precision); } else { lob = ValueLob.createBlob(getInputStream(), precision); } return lob; }
@Override public void unlink() { if (linked && fileName != null) { String temp; // synchronize on the database, to avoid concurrent temp file // creation / deletion / backup temp = getFileName(-1, objectId); deleteFile(temp); renameFile(fileName, temp); fileName = temp; linked = false; } }
/** * Store the lob data to a file if the size of the buffer is larger than the * maximum size for an in-place lob. * * @param h the data handler */ public void convertToFileIfRequired() { try { if (small != null && small.length > getMaxLengthInplaceLob()) { int len = getBufferSize(Long.MAX_VALUE); int tabId = tableId; if (type == Value.BLOB) { createFromStream( DataUtils.newBytes(len), 0, getInputStream(), Long.MAX_VALUE); } else { createFromReader( new char[len], 0, getReader(), Long.MAX_VALUE); } Value v2 = link(tabId); if (SysProperties.CHECK && v2 != this) { DbException.throwInternalError(); } } } catch (IOException e) { throw DbException.convertIOException(e, null); } }
@Override public Value link(int tabId) { if (fileName == null) { this.tableId = tabId; return this; } if (linked) { ValueLob copy = ValueLob.copy(this); copy.objectId = getNewObjectId(); copy.tableId = tabId; String live = getFileName(copy.tableId, copy.objectId); copyFileTo(fileName, live); copy.fileName = live; copy.linked = true; return copy; } if (!linked) { this.tableId = tabId; String live = getFileName(tableId, objectId); renameFile(fileName, live); fileName = live; linked = true; } return this; }
remaining = length; int len = getBufferSize(remaining); char[] buff; if (len >= Integer.MAX_VALUE) { len = IOUtils.readFully(in, buff, len); if (len <= getMaxLengthInplaceLob()) { byte[] small = new String(buff, 0, len).getBytes(Constants.UTF8); return ValueLob.createSmallLob(Value.CLOB, small); ValueLob lob = new ValueLob(Value.CLOB, null); lob.createFromReader(buff, len, in, remaining); return lob; } catch (IOException e) {
remaining = length; int len = getBufferSize(remaining); byte[] buff; if (len >= Integer.MAX_VALUE) { len = IOUtils.readFully(in, buff, len); if (len <= getMaxLengthInplaceLob()) { byte[] small = DataUtils.newBytes(len); System.arraycopy(buff, 0, small, 0, len); return ValueLob.createSmallLob(Value.BLOB, small); ValueLob lob = new ValueLob(Value.BLOB, null); lob.createFromStream(buff, len, in, remaining); return lob; } catch (IOException e) {
/** * Create a small lob using the given byte array. * * @param type the type (Value.BLOB or CLOB) * @param small the byte array * @return the lob value */ private static ValueLob createSmallLob(int type, byte[] small) { return new ValueLob(type, small); }
private static ValueLob copy(ValueLob lob) { ValueLob copy = new ValueLob(lob.type, lob.fileName, lob.tableId, lob.objectId, lob.linked, lob.precision); copy.small = lob.small; return copy; }
/** * Create an independent copy of this temporary value. * The file will not be deleted automatically. * * @return the value */ @Override public ValueLob copyToTemp() { ValueLob lob; if (type == CLOB) { lob = ValueLob.createClob(getReader(), precision); } else { lob = ValueLob.createBlob(getInputStream(), precision); } return lob; }
/** * Create a LOB value with the given parameters. * * @param type the data type * @param handler the file handler * @param tableId the table object id * @param objectId the object id * @param precision the precision (length in elements) * @param compression if compression is used * @param fileName the file name * @return the value object */ public static ValueLob openUnlinked(int type, int tableId, int objectId, long precision, String fileName) { return new ValueLob(type, fileName, tableId, objectId, false/* linked */, precision); }
/** * Convert a lob to another data type. The data is fully read in memory * except when converting to BLOB or CLOB. * * @param t the new type * @return the converted value */ @Override public Value convertTo(int t) { if (t == type) { return this; } else if (t == Value.CLOB) { ValueLob copy = ValueLob.createClob(getReader(), -1); return copy; } else if (t == Value.BLOB) { ValueLob copy = ValueLob.createBlob(getInputStream(), -1); return copy; } return super.convertTo(t); }