private void checkPowerOff() { if (handler != null) { handler.checkPowerOff(); } }
private void checkWritingAllowed() { if (handler != null && checkedWriting) { handler.checkWritingAllowed(); } }
private FileStoreOutputStream initLarge(DataHandler h) { this.handler = h; this.tableId = 0; this.linked = false; this.precision = 0; this.small = null; this.hash = 0; String compressionAlgorithm = h.getLobCompressionAlgorithm(type); this.compressed = compressionAlgorithm != null; synchronized (h) { String path = h.getDatabasePath(); if ((path != null) && (path.length() == 0)) { path = new File(Utils.getProperty("java.io.tmpdir", "."), SysProperties.PREFIX_TEMP_FILE).getAbsolutePath(); } objectId = getNewObjectId(h); fileName = getFileNamePrefix(path, objectId) + Constants.SUFFIX_TEMP_FILE; tempFile = h.openFile(fileName, "rw", false); tempFile.autoDelete(); } return new FileStoreOutputStream(tempFile, h, compressionAlgorithm); }
@Override public void remove() { if (fileName != null) { if (tempFile != null) { tempFile.stopAutoDelete(); } // synchronize on the database, to avoid concurrent temp file // creation / deletion / backup synchronized (handler.getLobSyncObject()) { FileUtils.delete(fileName); } } if (handler != null) { handler.getLobStorage().removeLob(this); } }
@Override public InputStream getInputStream() { if (small != null) { return new ByteArrayInputStream(small); } else if (fileName != null) { FileStore store = handler.openFile(fileName, "r", true); boolean alwaysClose = SysProperties.lobCloseBetweenReads; return new BufferedInputStream(new FileStoreInputStream(store, handler, false, alwaysClose), Constants.IO_BUFFER_SIZE); } long byteCount = (type == Value.BLOB) ? precision : -1; try { return handler.getLobStorage().getInputStream(this, hmac, byteCount); } catch (IOException e) { throw DbException.convertIOException(e, toString()); } }
boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null; long remaining = Long.MAX_VALUE; if (length >= 0 && length < remaining) { len = IOUtils.readFully(reader, buff, len); if (len <= handler.getMaxLengthInplaceLob()) { byte[] small = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8); return ValueLobDb.createSmallLob(Value.CLOB, small, len);
private FileStoreOutputStream initLarge(DataHandler handler) throws IOException, SQLException { this.handler = handler; this.tableId = 0; this.linked = false; this.precision = 0; this.small = null; this.hash = 0; String compressionAlgorithm = handler.getLobCompressionAlgorithm(type); this.compression = compressionAlgorithm != null; synchronized (handler) { if (handler.getLobFilesInDirectories()) { objectId = getNewObjectId(handler); fileName = getFileNamePrefix(handler.getDatabasePath(), objectId) + ".temp.db"; } else { objectId = handler.allocateObjectId(false, true); fileName = handler.createTempFile(); } tempFile = handler.openFile(fileName, "rw", false); tempFile.autoDelete(); } FileStoreOutputStream out = new FileStoreOutputStream(tempFile, handler, compressionAlgorithm); return out; }
private static synchronized void deleteFile(DataHandler handler, String fileName) { // synchronize on the database, to avoid concurrent temp file creation / // deletion / backup synchronized (handler.getLobSyncObject()) { FileUtils.delete(fileName); } }
private static String createTempLobFileName(DataHandler handler) throws IOException { String path = handler.getDatabasePath(); if (path.length() == 0) { path = SysProperties.PREFIX_TEMP_FILE; } return FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true, true); }
@Override public Value copy(DataHandler database, int tableId) { if (small == null) { return handler.getLobStorage().copyLob(this, tableId, getPrecision()); } else if (small.length > database.getMaxLengthInplaceLob()) { LobStorageInterface s = database.getLobStorage(); Value v; if (type == Value.BLOB) { v = s.createBlob(getInputStream(), getPrecision()); } else { v = s.createClob(getReader(), getPrecision()); } Value v2 = v.copy(database, tableId); v.remove(); return v2; } return this; }
private static void invalidateFileList(DataHandler h, String dir) { SmallLRUCache<String, String[]> cache = h.getLobFileListCache(); if (cache != null) { synchronized (cache) { cache.remove(dir); } } }
@Override public InputStream getInputStream() { if (fileName == null) { return new ByteArrayInputStream(small); } FileStore store = handler.openFile(fileName, "r", true); boolean alwaysClose = SysProperties.lobCloseBetweenReads; return new BufferedInputStream( new FileStoreInputStream(store, handler, compressed, alwaysClose), Constants.IO_BUFFER_SIZE); }
this.hmac = null; this.fileName = createTempLobFileName(handler); this.tempFile = this.handler.openFile(fileName, "rw", false); this.tempFile.autoDelete(); long tmpPrecision = 0; boolean compress = this.handler.getLobCompressionAlgorithm(Value.BLOB) != null; try (FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null)) { while (true) {
private static int getBufferSize(DataHandler handler, boolean compress, long remaining) { if (remaining < 0 || remaining > Integer.MAX_VALUE) { remaining = Integer.MAX_VALUE; } int inplace = handler.getMaxLengthInplaceLob(); long m = compress ? Constants.IO_BUFFER_SIZE_COMPRESS : Constants.IO_BUFFER_SIZE; if (m < remaining && m <= inplace) { // using "1L" to force long arithmetic because // inplace could be Integer.MAX_VALUE m = Math.min(remaining, inplace + 1L); // the buffer size must be bigger than the inplace lob, otherwise we // can't know if it must be stored in-place or not m = MathUtils.roundUpLong(m, Constants.IO_BUFFER_SIZE); } m = Math.min(remaining, m); m = MathUtils.convertLongToInt(m); if (m < 0) { m = Integer.MAX_VALUE; } return (int) m; }
private void createFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) throws IOException { try (FileStoreOutputStream out = initLarge(h)) { boolean compress = h.getLobCompressionAlgorithm(Value.CLOB) != null; while (true) { precision += len; byte[] b = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8); out.write(b, 0, b.length); remaining -= len; if (remaining <= 0) { break; } len = getBufferSize(h, compress, remaining); len = IOUtils.readFully(in, buff, len); if (len == 0) { break; } } } }
@Override public JavaObjectSerializer getJavaObjectSerializer() { return session.getDataHandler().getJavaObjectSerializer(); }
@Override public CompareMode getCompareMode() { return session.getDataHandler().getCompareMode(); } }
private static String getFileName(DataHandler handler, int tableId, int objectId) { if (SysProperties.CHECK && tableId == 0 && objectId == 0) { throw Message.getInternalError("0 LOB"); } if (handler.getLobFilesInDirectories()) { String table = tableId < 0 ? ".temp" : ".t" + tableId; return getFileNamePrefix(handler.getDatabasePath(), objectId) + table + Constants.SUFFIX_LOB_FILE; } else { return handler.getDatabasePath() + "." + tableId + "." + objectId + Constants.SUFFIX_LOB_FILE; } }
return createSmallLob(Value.CLOB, s.getBytes(StandardCharsets.UTF_8)); boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null; long remaining = Long.MAX_VALUE; if (length >= 0 && length < remaining) { len = IOUtils.readFully(in, buff, len); if (len <= handler.getMaxLengthInplaceLob()) { byte[] small = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8); return ValueLob.createSmallLob(Value.CLOB, small);
@Override public InputStream getInputStream(long oneBasedOffset, long length) { long byteCount; InputStream inputStream; if (small != null) { return super.getInputStream(oneBasedOffset, length); } else if (fileName != null) { FileStore store = handler.openFile(fileName, "r", true); boolean alwaysClose = SysProperties.lobCloseBetweenReads; byteCount = store.length(); inputStream = new BufferedInputStream(new FileStoreInputStream(store, handler, false, alwaysClose), Constants.IO_BUFFER_SIZE); } else { byteCount = (type == Value.BLOB) ? precision : -1; try { inputStream = handler.getLobStorage().getInputStream(this, hmac, byteCount); } catch (IOException e) { throw DbException.convertIOException(e, toString()); } } return ValueLob.rangeInputStream(inputStream, oneBasedOffset, length, byteCount); }