📄 valuelob.java
字号:
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(handler, compress, remaining);
len = IOUtils.readFully(in, buff, 0, len);
if (len <= 0) {
break;
}
}
} finally {
out.close();
}
} catch (IOException e) {
throw Message.convertIOException(e, null);
}
}
public Value convertTo(int t) throws SQLException {
if (t == type) {
return this;
} else if (t == Value.CLOB) {
ValueLob copy = ValueLob.createClob(getReader(), -1, handler);
return copy;
} else if (t == Value.BLOB) {
ValueLob copy = ValueLob.createBlob(getInputStream(), -1, handler);
return copy;
}
return super.convertTo(t);
}
public boolean isLinked() {
return linked;
}
public String getFileName() {
return fileName;
}
public void close() throws SQLException {
if (fileName != null) {
if (tempFile != null) {
tempFile.stopAutoDelete();
}
deleteFile(handler, fileName);
}
}
public void unlink() throws SQLException {
if (linked && fileName != null) {
String temp;
// synchronize on the database, to avoid concurrent temp file
// creation / deletion / backup
synchronized (handler) {
if (handler.getLobFilesInDirectories()) {
temp = getFileName(handler, -1, objectId);
} else {
// just to get a filename - an empty file will be created
temp = handler.createTempFile();
}
deleteFile(handler, temp);
renameFile(handler, fileName, temp);
tempFile = FileStore.open(handler, temp, "rw", null);
tempFile.autoDelete();
tempFile.closeSilently();
fileName = temp;
linked = false;
}
}
}
public Value link(DataHandler handler, int tabId) throws SQLException {
if (fileName == null) {
this.tableId = tabId;
return this;
}
if (linked) {
ValueLob copy = ValueLob.copy(this);
if (handler.getLobFilesInDirectories()) {
copy.objectId = getNewObjectId(handler);
} else {
copy.objectId = handler.allocateObjectId(false, true);
}
copy.tableId = tabId;
String live = getFileName(handler, copy.tableId, copy.objectId);
copyFile(handler, fileName, live);
copy.fileName = live;
copy.linked = true;
return copy;
}
if (!linked) {
this.tableId = tabId;
String live = getFileName(handler, tableId, objectId);
tempFile.stopAutoDelete();
tempFile = null;
renameFile(handler, fileName, live);
fileName = live;
linked = true;
}
return this;
}
public int getTableId() {
return tableId;
}
public int getObjectId() {
return objectId;
}
public int getType() {
return type;
}
public long getPrecision() {
return precision;
}
public String getString() {
int len = precision > Integer.MAX_VALUE || precision == 0 ? Integer.MAX_VALUE : (int) precision;
try {
if (type == Value.CLOB) {
if (small != null) {
return StringUtils.utf8Decode(small);
}
return IOUtils.readStringAndClose(getReader(), len);
} else {
byte[] buff;
if (small != null) {
buff = small;
} else {
buff = IOUtils.readBytesAndClose(getInputStream(), len);
}
return ByteUtils.convertBytesToString(buff);
}
} catch (IOException e) {
throw Message.convertToInternal(Message.convertIOException(e, fileName));
}
}
public byte[] getBytes() throws SQLException {
byte[] data = getBytesNoCopy();
return ByteUtils.cloneByteArray(data);
}
public byte[] getBytesNoCopy() throws SQLException {
if (small != null) {
return small;
}
try {
return IOUtils.readBytesAndClose(getInputStream(), Integer.MAX_VALUE);
} catch (IOException e) {
throw Message.convertIOException(e, fileName);
}
}
public int hashCode() {
if (hash == 0) {
if (precision > 4096) {
// TODO: should calculate the hash code when saving, and store
// it in the data file
return (int) (precision ^ (precision >> 32));
}
try {
hash = ByteUtils.getByteArrayHash(getBytes());
} catch (SQLException e) {
throw Message.convertToInternal(e);
}
}
return hash;
}
protected int compareSecure(Value v, CompareMode mode) throws SQLException {
if (type == Value.CLOB) {
int c = getString().compareTo(v.getString());
return c == 0 ? 0 : (c < 0 ? -1 : 1);
} else {
byte[] v2 = v.getBytesNoCopy();
return ByteUtils.compareNotNull(getBytes(), v2);
}
}
public Object getObject() {
if (type == Value.CLOB) {
return getReader();
} else {
return getInputStream();
}
}
public Reader getReader() {
try {
return IOUtils.getReader(getInputStream());
} catch (SQLException e) {
throw Message.convertToInternal(e);
}
}
public InputStream getInputStream() {
try {
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, compression, alwaysClose),
Constants.IO_BUFFER_SIZE);
} catch (SQLException e) {
throw Message.convertToInternal(e);
}
}
public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
long p = getPrecision();
// TODO test if setBinaryStream with -1 works for other databases a well
if (p > Integer.MAX_VALUE || p <= 0) {
p = -1;
}
if (type == Value.BLOB) {
prep.setBinaryStream(parameterIndex, getInputStream(), (int) p);
} else {
prep.setCharacterStream(parameterIndex, getReader(), (int) p);
}
}
public String getSQL() {
try {
String s;
if (type == Value.CLOB) {
s = getString();
return StringUtils.quoteStringSQL(s);
} else {
byte[] buff = getBytes();
s = ByteUtils.convertBytesToString(buff);
return "X'" + s + "'";
}
} catch (SQLException e) {
throw Message.convertToInternal(e);
}
}
public String toString() {
if (small == null) {
return getClass().getName() + " file: " + fileName + " type: " + type + " precision: " + precision;
} else {
return getSQL();
}
}
public byte[] getSmall() {
return small;
}
public int getDisplaySize() {
return MathUtils.convertLongToInt(getPrecision());
}
public boolean equals(Object other) {
try {
return other instanceof ValueLob && compareSecure((Value) other, null) == 0;
} catch (SQLException e) {
throw Message.convertToInternal(e);
}
}
public void convertToFileIfRequired(DataHandler handler) throws SQLException {
if (Constants.AUTO_CONVERT_LOB_TO_FILES && small != null && small.length > handler.getMaxLengthInplaceLob()) {
boolean compress = handler.getLobCompressionAlgorithm(type) != null;
int len = getBufferSize(handler, compress, Long.MAX_VALUE);
int tabId = tableId;
if (type == Value.BLOB) {
createFromStream(new byte[len], 0, getInputStream(), Long.MAX_VALUE, handler);
} else {
createFromReader(new char[len], 0, getReader(), Long.MAX_VALUE, handler);
}
Value v2 = link(handler, tabId);
if (SysProperties.CHECK && v2 != this) {
throw Message.getInternalError();
}
}
}
public static void removeAllForTable(DataHandler handler, int tableId) throws SQLException {
if (handler.getLobFilesInDirectories()) {
String dir = getFileNamePrefix(handler.getDatabasePath(), 0);
removeAllForTable(handler, dir, tableId);
} else {
String prefix = handler.getDatabasePath();
String dir = FileUtils.getParent(prefix);
String[] list = FileUtils.listFiles(dir);
for (int i = 0; i < list.length; i++) {
String name = list[i];
if (name.startsWith(prefix + "." + tableId + ".") && name.endsWith(".lob.db")) {
deleteFile(handler, name);
}
}
}
}
private static void removeAllForTable(DataHandler handler, String dir, int tableId) throws SQLException {
String[] list = FileUtils.listFiles(dir);
for (int i = 0; i < list.length; i++) {
if (FileUtils.isDirectory(list[i])) {
removeAllForTable(handler, list[i], tableId);
} else {
String name = list[i];
if (name.endsWith(".t" + tableId + ".lob.db")) {
deleteFile(handler, name);
}
}
}
}
public boolean useCompression() {
return compression;
}
public boolean isFileBased() {
return fileName != null;
}
private static synchronized void deleteFile(DataHandler handler, String fileName) throws SQLException {
// synchronize on the database, to avoid concurrent temp file creation /
// deletion / backup
synchronized (handler.getLobSyncObject()) {
FileUtils.delete(fileName);
}
}
private static synchronized void renameFile(DataHandler handler, String oldName, String newName)
throws SQLException {
synchronized (handler.getLobSyncObject()) {
FileUtils.rename(oldName, newName);
}
}
private void copyFile(DataHandler handler, String fileName, String live) throws SQLException {
synchronized (handler.getLobSyncObject()) {
FileSystem.getInstance(fileName).copy(fileName, live);
}
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getMemory() {
if (small != null) {
return small.length + 32;
}
return 128;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -