Added open for overwrite optimization that stops existing content being copied when the file

is opened for write access.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2256 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gary Spencer
2006-01-31 10:22:54 +00:00
parent 9bac8270f4
commit db4ea8197f

View File

@@ -150,6 +150,15 @@ public class ContentNetworkFile extends NetworkFile
return netFile; return netFile;
} }
/**
* Class constructor
*
* @param transactionService TransactionService
* @param nodeService NodeService
* @param contentService ContentService
* @param nodeRef NodeRef
* @param name String
*/
private ContentNetworkFile( private ContentNetworkFile(
TransactionService transactionService, TransactionService transactionService,
NodeService nodeService, NodeService nodeService,
@@ -164,7 +173,12 @@ public class ContentNetworkFile extends NetworkFile
this.contentService = contentService; this.contentService = contentService;
this.nodeRef = nodeRef; this.nodeRef = nodeRef;
} }
/**
* Return the file details as a string
*
* @return String
*/
public String toString() public String toString()
{ {
StringBuilder sb = new StringBuilder(50); StringBuilder sb = new StringBuilder(50);
@@ -201,6 +215,16 @@ public class ContentNetworkFile extends NetworkFile
int access = getGrantedAccess(); int access = getGrantedAccess();
return (access == NetworkFile.READWRITE || access == NetworkFile.WRITEONLY); return (access == NetworkFile.READWRITE || access == NetworkFile.WRITEONLY);
} }
/**
* Determine if the file content data has been opened
*
* @return boolean
*/
public final boolean hasContent()
{
return content != null ? true : false;
}
/** /**
* Opens the channel for reading or writing depending on the access mode. * Opens the channel for reading or writing depending on the access mode.
@@ -208,6 +232,7 @@ public class ContentNetworkFile extends NetworkFile
* If the channel is already open, it is left. * If the channel is already open, it is left.
* *
* @param write true if the channel must be writable * @param write true if the channel must be writable
* @param trunc true if the writable channel does not require the previous content data
* @throws AccessDeniedException if this network file is read only * @throws AccessDeniedException if this network file is read only
* @throws AlfrescoRuntimeException if this network file represents a directory * @throws AlfrescoRuntimeException if this network file represents a directory
* *
@@ -216,7 +241,7 @@ public class ContentNetworkFile extends NetworkFile
* @see NetworkFile#WRITEONLY * @see NetworkFile#WRITEONLY
* @see NetworkFile#READWRITE * @see NetworkFile#READWRITE
*/ */
private synchronized void openContent(boolean write) throws AccessDeniedException, AlfrescoRuntimeException private synchronized void openContent(boolean write, boolean trunc) throws AccessDeniedException, AlfrescoRuntimeException
{ {
if (isDirectory()) if (isDirectory())
{ {
@@ -265,9 +290,9 @@ public class ContentNetworkFile extends NetworkFile
writableChannel = true; writableChannel = true;
// get the writable channel // Get the writable channel, do not copy existing content data if the file is to be truncated
// TODO: Decide on truncation strategy
channel = ((ContentWriter) content).getFileChannel(false); channel = ((ContentWriter) content).getFileChannel( trunc);
} }
else else
{ {
@@ -287,7 +312,11 @@ public class ContentNetworkFile extends NetworkFile
} }
} }
@Override /**
* Close the file
*
* @exception IOException
*/
public synchronized void closeFile() throws IOException public synchronized void closeFile() throws IOException
{ {
if (isDirectory()) // ignore if this is a directory if (isDirectory()) // ignore if this is a directory
@@ -333,16 +362,40 @@ public class ContentNetworkFile extends NetworkFile
} }
} }
@Override /**
* Truncate or extend the file to the specified length
*
* @param size long
* @exception IOException
*/
public synchronized void truncateFile(long size) throws IOException public synchronized void truncateFile(long size) throws IOException
{ {
// open the channel for writing // If the content data channel has not been opened yet and the requested size is zero
openContent(true); // then this is an open for overwrite so the existing content data is not copied
// truncate the channel
channel.truncate(size); if ( hasContent() == false && size == 0L)
// set modification flag {
// Open content for overwrite, no need to copy existing content data
openContent(true, true);
}
else
{
// Normal open for write
openContent(true, false);
// Truncate or extend the channel
channel.truncate(size);
}
// Set modification flag
modified = true; modified = true;
// done
// Debug
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
logger.debug("Truncated channel: " + logger.debug("Truncated channel: " +
@@ -364,7 +417,7 @@ public class ContentNetworkFile extends NetworkFile
{ {
// Open the channel for writing // Open the channel for writing
openContent(true); openContent(true, false);
// Write to the channel // Write to the channel
@@ -401,8 +454,9 @@ public class ContentNetworkFile extends NetworkFile
*/ */
public synchronized int readFile(byte[] buffer, int length, int position, long fileOffset) throws IOException public synchronized int readFile(byte[] buffer, int length, int position, long fileOffset) throws IOException
{ {
// open the channel for reading // Open the channel for reading
openContent(false);
openContent(false, false);
// read from the channel // read from the channel
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, position, length); ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, position, length);
@@ -437,7 +491,7 @@ public class ContentNetworkFile extends NetworkFile
public synchronized void flushFile() throws IOException public synchronized void flushFile() throws IOException
{ {
// open the channel for writing // open the channel for writing
openContent(true); openContent(true, false);
// flush the channel - metadata flushing is not important // flush the channel - metadata flushing is not important
channel.force(false); channel.force(false);
// done // done