FileDocCategorySizeDatePackage
DiskAccessRequestImpl.javaAPI DocAzureus 3.0.3.45704Thu Jan 25 14:37:24 GMT 2007com.aelitis.azureus.core.diskmanager.access.impl

DiskAccessRequestImpl

public class DiskAccessRequestImpl extends Object implements com.aelitis.azureus.core.diskmanager.access.DiskAccessRequest

Fields Summary
protected static final short
OP_READ
protected static final short
OP_WRITE
protected static final short
OP_WRITE_AND_FREE
private com.aelitis.azureus.core.diskmanager.cache.CacheFile
file
private long
offset
private org.gudy.azureus2.core3.util.DirectByteBuffer
buffer
private com.aelitis.azureus.core.diskmanager.access.DiskAccessRequestListener
listener
private short
op
private short
cache_policy
private int
size
private volatile boolean
cancelled
Constructors Summary
protected DiskAccessRequestImpl(com.aelitis.azureus.core.diskmanager.cache.CacheFile _file, long _offset, org.gudy.azureus2.core3.util.DirectByteBuffer _buffer, com.aelitis.azureus.core.diskmanager.access.DiskAccessRequestListener _listener, short _op, short _cache_policy)

	
	
	
							
								
					
			
								
								 
	
		file			= _file;
		offset			= _offset;
		buffer			= _buffer;
		listener		= _listener;
		op				= _op;
		cache_policy	= _cache_policy;
		
		size = buffer.remaining( DirectByteBuffer.SS_FILE );
	
Methods Summary
protected booleancanBeAggregatedWith(com.aelitis.azureus.core.diskmanager.access.impl.DiskAccessRequestImpl other)

		return( op == other.getOperation() && cache_policy == other.getCachePolicy());
	
public voidcancel()

		cancelled	= true;
	
public org.gudy.azureus2.core3.util.DirectByteBuffergetBuffer()

		return( buffer );
	
public shortgetCachePolicy()

		return( cache_policy );
	
public com.aelitis.azureus.core.diskmanager.cache.CacheFilegetFile()

		return( file );
	
protected com.aelitis.azureus.core.diskmanager.access.DiskAccessRequestListenergetListener()

		return( listener );
	
public longgetOffset()

		return( offset );
	
protected intgetOperation()

		return( op );
	
public intgetPriority()

		return( listener.getPriority());
	
public intgetSize()

		return( size );
	
public booleanisCancelled()

		return( cancelled );
	
protected static voidrunAggregated(com.aelitis.azureus.core.diskmanager.access.impl.DiskAccessRequestImpl base_request, com.aelitis.azureus.core.diskmanager.access.impl.DiskAccessRequestImpl[] requests)

			// assumption - they are all for the same file, sequential offsets and aggregatable, not cancelled
		
		int			op 				= base_request.getOperation();
			
		CacheFile	file 			= base_request.getFile();
		long		offset			= base_request.getOffset();
		short		cache_policy	= base_request.getCachePolicy();
		
		DirectByteBuffer[]	buffers = new DirectByteBuffer[requests.length];
		
		long	current_offset 	= offset;
		long	total_size		= 0;
		
		for (int i=0;i<buffers.length;i++){
		
			DiskAccessRequestImpl	request = requests[i];
			
			if ( current_offset != request.getOffset()){
				
				Debug.out( "assert failed: requests not contiguous" );
			}
			
			int	size = request.getSize();
			
			current_offset += size;
			
			total_size += size;
			
			buffers[i] = request.getBuffer();
		}
		
		try{	
			if ( op == OP_READ ){
				
				file.read( buffers, offset, cache_policy );
				
			}else if ( op == OP_WRITE ){
				
				file.write( buffers, offset );
			
			}else{
				
				file.writeAndHandoverBuffers( buffers, offset );
			}
			
			base_request.getListener().requestExecuted( total_size );

			for (int i=0;i<requests.length;i++){

				DiskAccessRequestImpl	request = requests[i];
				
				request.getListener().requestComplete( request );
				
				if ( request != base_request ){
					
					request.getListener().requestExecuted( 0 );
				}
			}
			
		}catch( CacheFileManagerException e ){
			
			int	fail_index = e.getFailIndex();
			
			for (int i=0;i<fail_index;i++){
				
				DiskAccessRequestImpl	request = requests[i];
				
				request.getListener().requestComplete( request );
			}
			
			for (int i=fail_index;i<requests.length;i++){

				DiskAccessRequestImpl	request = requests[i];
				
				request.getListener().requestFailed( request, e );
			}			
		}catch( Throwable e ){
			
			for (int i=0;i<requests.length;i++){

				DiskAccessRequestImpl	request = requests[i];
				
				request.getListener().requestFailed( request, e );
			}			
		}
	
protected voidrunRequest()

		if ( cancelled ){
			
			listener.requestCancelled( this );
			
			return;
		}
		
		//System.out.println( "DiskReq:" + Thread.currentThread().getName() + ": " + op + " - " + offset );
		
		try{
			if ( op == OP_READ ){
				
				file.read( buffer, offset, cache_policy );
				
			}else if ( op == OP_WRITE ){
				
				file.write( buffer, offset );
				
			}else{
				
				file.writeAndHandoverBuffer( buffer, offset );
			}
			
			listener.requestComplete( this );
			
		}catch( Throwable e ){
			
			listener.requestFailed( this, e );
		}