Skip to content

Commit

Permalink
合并FileStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Oct 13, 2015
1 parent 55fd13c commit 513da60
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 784 deletions.
Expand Up @@ -33,7 +33,7 @@
import org.lealone.db.SessionWithState; import org.lealone.db.SessionWithState;
import org.lealone.db.SetTypes; import org.lealone.db.SetTypes;
import org.lealone.db.SysProperties; import org.lealone.db.SysProperties;
import org.lealone.storage.FileStore; import org.lealone.storage.FileStorage;
import org.lealone.storage.LobStorage; import org.lealone.storage.LobStorage;
import org.lealone.storage.fs.FileUtils; import org.lealone.storage.fs.FileUtils;
import org.lealone.transaction.Transaction; import org.lealone.transaction.Transaction;
Expand Down Expand Up @@ -457,24 +457,24 @@ public int getMaxLengthInplaceLob() {
} }


@Override @Override
public FileStore openFile(String name, String mode, boolean mustExist) { public FileStorage openFile(String name, String mode, boolean mustExist) {
if (mustExist && !FileUtils.exists(name)) { if (mustExist && !FileUtils.exists(name)) {
throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name); throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name);
} }
FileStore store; FileStorage fileStorage;
if (cipher == null) { if (cipher == null) {
store = FileStore.open(this, name, mode); fileStorage = FileStorage.open(this, name, mode);
} else { } else {
store = FileStore.open(this, name, mode, cipher, fileEncryptionKey, 0); fileStorage = FileStorage.open(this, name, mode, cipher, fileEncryptionKey, 0);
} }
store.setCheckedWriting(false); fileStorage.setCheckedWriting(false);
try { try {
store.init(); fileStorage.init();
} catch (DbException e) { } catch (DbException e) {
store.closeSilently(); fileStorage.closeSilently();
throw e; throw e;
} }
return store; return fileStorage;
} }


@Override @Override
Expand Down
Expand Up @@ -9,14 +9,14 @@
import org.lealone.common.util.MathUtils; import org.lealone.common.util.MathUtils;
import org.lealone.db.Constants; import org.lealone.db.Constants;
import org.lealone.db.DataHandler; import org.lealone.db.DataHandler;
import org.lealone.storage.FileStore; import org.lealone.storage.FileStorage;


/** /**
* A file store that encrypts all data before writing, and decrypts all data * A file storage that encrypts all data before writing, and decrypts all data
* after reading. Areas that were never written to (for example after calling * after reading. Areas that were never written to (for example after calling
* setLength to enlarge the file) are not encrypted (contains 0 bytes). * setLength to enlarge the file) are not encrypted (contains 0 bytes).
*/ */
public class SecureFileStore extends FileStore { public class SecureFileStore extends FileStorage {


private byte[] key; private byte[] key;
private final BlockCipher cipher; private final BlockCipher cipher;
Expand Down
22 changes: 11 additions & 11 deletions lealone-common/src/main/java/org/lealone/common/value/ValueLob.java
Expand Up @@ -24,9 +24,9 @@
import org.lealone.db.Constants; import org.lealone.db.Constants;
import org.lealone.db.DataHandler; import org.lealone.db.DataHandler;
import org.lealone.db.SysProperties; import org.lealone.db.SysProperties;
import org.lealone.storage.FileStore; import org.lealone.storage.FileStorage;
import org.lealone.storage.FileStoreInputStream; import org.lealone.storage.FileStorageInputStream;
import org.lealone.storage.FileStoreOutputStream; import org.lealone.storage.FileStorageOutputStream;
import org.lealone.storage.fs.FileUtils; import org.lealone.storage.fs.FileUtils;


/** /**
Expand Down Expand Up @@ -62,7 +62,7 @@ public class ValueLob extends Value {
private byte[] small; private byte[] small;
private int hash; private int hash;
private boolean compressed; private boolean compressed;
private FileStore tempFile; private FileStorage tempFile;


private ValueLob(int type, DataHandler handler, String fileName, int tableId, int objectId, boolean linked, private ValueLob(int type, DataHandler handler, String fileName, int tableId, int objectId, boolean linked,
long precision, boolean compressed) { long precision, boolean compressed) {
Expand Down Expand Up @@ -212,7 +212,7 @@ private static int getBufferSize(DataHandler handler, boolean compress, long rem
} }


private void createFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) throws IOException { private void createFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) throws IOException {
FileStoreOutputStream out = initLarge(h); FileStorageOutputStream out = initLarge(h);
boolean compress = h.getLobCompressionAlgorithm(Value.CLOB) != null; boolean compress = h.getLobCompressionAlgorithm(Value.CLOB) != null;
try { try {
while (true) { while (true) {
Expand Down Expand Up @@ -380,7 +380,7 @@ private static ValueLob createBlob(InputStream in, long length, DataHandler hand
} }
} }


private FileStoreOutputStream initLarge(DataHandler h) { private FileStorageOutputStream initLarge(DataHandler h) {
this.handler = h; this.handler = h;
this.tableId = 0; this.tableId = 0;
this.linked = false; this.linked = false;
Expand All @@ -400,13 +400,13 @@ private FileStoreOutputStream initLarge(DataHandler h) {
tempFile = h.openFile(fileName, "rw", false); tempFile = h.openFile(fileName, "rw", false);
tempFile.autoDelete(); tempFile.autoDelete();
} }
FileStoreOutputStream out = new FileStoreOutputStream(tempFile, h, compressionAlgorithm); FileStorageOutputStream out = new FileStorageOutputStream(tempFile, h, compressionAlgorithm);
return out; return out;
} }


private void createFromStream(byte[] buff, int len, InputStream in, long remaining, DataHandler h) private void createFromStream(byte[] buff, int len, InputStream in, long remaining, DataHandler h)
throws IOException { throws IOException {
FileStoreOutputStream out = initLarge(h); FileStorageOutputStream out = initLarge(h);
boolean compress = h.getLobCompressionAlgorithm(Value.BLOB) != null; boolean compress = h.getLobCompressionAlgorithm(Value.BLOB) != null;
try { try {
while (true) { while (true) {
Expand Down Expand Up @@ -483,7 +483,7 @@ public void unlink(DataHandler handler) {
temp = getFileName(handler, -1, objectId); temp = getFileName(handler, -1, objectId);
deleteFile(handler, temp); deleteFile(handler, temp);
renameFile(handler, fileName, temp); renameFile(handler, fileName, temp);
tempFile = FileStore.open(handler, temp, "rw"); tempFile = FileStorage.open(handler, temp, "rw");
tempFile.autoDelete(); tempFile.autoDelete();
tempFile.closeSilently(); tempFile.closeSilently();
fileName = temp; fileName = temp;
Expand Down Expand Up @@ -643,9 +643,9 @@ public InputStream getInputStream() {
if (fileName == null) { if (fileName == null) {
return new ByteArrayInputStream(small); return new ByteArrayInputStream(small);
} }
FileStore store = handler.openFile(fileName, "r", true); FileStorage fileStorage = handler.openFile(fileName, "r", true);
boolean alwaysClose = SysProperties.LOB_CLOSE_BETWEEN_READS; boolean alwaysClose = SysProperties.LOB_CLOSE_BETWEEN_READS;
return new BufferedInputStream(new FileStoreInputStream(store, handler, compressed, alwaysClose), return new BufferedInputStream(new FileStorageInputStream(fileStorage, handler, compressed, alwaysClose),
Constants.IO_BUFFER_SIZE); Constants.IO_BUFFER_SIZE);
} }


Expand Down
Expand Up @@ -23,9 +23,9 @@
import org.lealone.db.Constants; import org.lealone.db.Constants;
import org.lealone.db.DataHandler; import org.lealone.db.DataHandler;
import org.lealone.db.SysProperties; import org.lealone.db.SysProperties;
import org.lealone.storage.FileStore; import org.lealone.storage.FileStorage;
import org.lealone.storage.FileStoreInputStream; import org.lealone.storage.FileStorageInputStream;
import org.lealone.storage.FileStoreOutputStream; import org.lealone.storage.FileStorageOutputStream;
import org.lealone.storage.LobStorage; import org.lealone.storage.LobStorage;
import org.lealone.storage.fs.FileUtils; import org.lealone.storage.fs.FileUtils;


Expand All @@ -50,7 +50,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
private final long precision; private final long precision;


private final String fileName; private final String fileName;
private final FileStore tempFile; private final FileStorage tempFile;
private int tableId; private int tableId;
private int hash; private int hash;


Expand Down Expand Up @@ -89,7 +89,7 @@ private ValueLobDb(DataHandler handler, Reader in, long remaining) throws IOExce
this.fileName = createTempLobFileName(handler); this.fileName = createTempLobFileName(handler);
this.tempFile = this.handler.openFile(fileName, "rw", false); this.tempFile = this.handler.openFile(fileName, "rw", false);
this.tempFile.autoDelete(); this.tempFile.autoDelete();
FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null); FileStorageOutputStream out = new FileStorageOutputStream(tempFile, null, null);
long tmpPrecision = 0; long tmpPrecision = 0;
try { try {
char[] buff = new char[Constants.IO_BUFFER_SIZE]; char[] buff = new char[Constants.IO_BUFFER_SIZE];
Expand Down Expand Up @@ -118,7 +118,7 @@ private ValueLobDb(DataHandler handler, byte[] buff, int len, InputStream in, lo
this.fileName = createTempLobFileName(handler); this.fileName = createTempLobFileName(handler);
this.tempFile = this.handler.openFile(fileName, "rw", false); this.tempFile = this.handler.openFile(fileName, "rw", false);
this.tempFile.autoDelete(); this.tempFile.autoDelete();
FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null); FileStorageOutputStream out = new FileStorageOutputStream(tempFile, null, null);
long tmpPrecision = 0; long tmpPrecision = 0;
boolean compress = this.handler.getLobCompressionAlgorithm(Value.BLOB) != null; boolean compress = this.handler.getLobCompressionAlgorithm(Value.BLOB) != null;
try { try {
Expand Down Expand Up @@ -370,9 +370,9 @@ public InputStream getInputStream() {
if (small != null) { if (small != null) {
return new ByteArrayInputStream(small); return new ByteArrayInputStream(small);
} else if (fileName != null) { } else if (fileName != null) {
FileStore store = handler.openFile(fileName, "r", true); FileStorage fileStorage = handler.openFile(fileName, "r", true);
boolean alwaysClose = SysProperties.LOB_CLOSE_BETWEEN_READS; boolean alwaysClose = SysProperties.LOB_CLOSE_BETWEEN_READS;
return new BufferedInputStream(new FileStoreInputStream(store, handler, false, alwaysClose), return new BufferedInputStream(new FileStorageInputStream(fileStorage, handler, false, alwaysClose),
Constants.IO_BUFFER_SIZE); Constants.IO_BUFFER_SIZE);
} }
long byteCount = (type == Value.BLOB) ? precision : -1; long byteCount = (type == Value.BLOB) ? precision : -1;
Expand Down
4 changes: 2 additions & 2 deletions lealone-common/src/main/java/org/lealone/db/DataHandler.java
Expand Up @@ -11,7 +11,7 @@
import org.lealone.common.message.DbException; import org.lealone.common.message.DbException;
import org.lealone.common.util.SmallLRUCache; import org.lealone.common.util.SmallLRUCache;
import org.lealone.common.util.TempFileDeleter; import org.lealone.common.util.TempFileDeleter;
import org.lealone.storage.FileStore; import org.lealone.storage.FileStorage;
import org.lealone.storage.LobStorage; import org.lealone.storage.LobStorage;


/** /**
Expand All @@ -35,7 +35,7 @@ public interface DataHandler {
* @param mustExist whether the file must already exist * @param mustExist whether the file must already exist
* @return the file * @return the file
*/ */
FileStore openFile(String name, String mode, boolean mustExist); FileStorage openFile(String name, String mode, boolean mustExist);


/** /**
* Check if the simulated power failure occurred. * Check if the simulated power failure occurred.
Expand Down

0 comments on commit 513da60

Please sign in to comment.