Пример #1
0
        /// <summary>
        /// Gets or sets the file location. When a file is set the file data is immediately read in memory to extract the filehash. It will retain in memory afterwards.
        /// </summary>
        /// <param name="fileName"></param>
        public void LoadFile(string fileName)
        {
            FileInfo finfo = new FileInfo(fileName);

            if (finfo.Exists)
            {
                if (this.fileLocation == fileName)
                {
                    return;
                }
                this.fileLocation = fileName;
                this.location     = Path.GetRandomFileName();

                // and open a new stream
                PersistentStream persistentStream = new PersistentStream(new MemoryStream());

                // copy the file
                byte[] buffer     = new byte[512];
                Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                int    cnt        = 0;
                while ((cnt = fileStream.Read(buffer, 0, 512)) > 0)
                {
                    persistentStream.Write(buffer, 0, cnt);
                }

                DataStream = persistentStream;

                UpdateInCollection();
            }
        }
Пример #2
0
        public void Persistent_stream_connection_exception()
        {
            var pStream = new PersistentStream(null, Connecter_throws_exception().ConnectorFunc, 0);

            //since the stream is null, reading will cause an exception and reconnect will be invoked
            Assert.NotEqual(0, pStream.Read(new byte[100], 0, 100));
        }
Пример #3
0
        public void ReadFlakyStream()
        {
            var pStream = new PersistentStream(GetWebRequest_flaky_stream(), 0);
            var buffer  = new byte[4096];

            Assert.Equal(100, pStream.Read(buffer, 0, 100));
        }
Пример #4
0
        //todo: can have just one method for both file and http streams
        //used in custom annotation lambda
        public static Stream GetAppropriateStream(PersistentStream pStream)
        {
            var header = GetHeader(pStream);
            var compressionAlgorithm = IdentifyCompressionAlgorithm(header);

            pStream.Position = 0;
            var appropriateStream = GetAppropriateStream(pStream, compressionAlgorithm);

            return(appropriateStream);
        }
Пример #5
0
        /// <summary>
        /// Gets or sets the file location. When a file is set the file data is immediately read in memory to extract the filehash. It will retain in memory afterwards.
        /// </summary>
        /// <param name="fileName"></param>
        public void LoadFile(string fileName)
        {
            FileInfo finfo = new FileInfo(fileName);
            if (finfo.Exists)
            {
                if (this.fileLocation == fileName)
                    return;
                this.fileLocation = fileName;
                this.location = Path.GetRandomFileName();

                // and open a new stream
                PersistentStream persistentStream = new PersistentStream(new MemoryStream());

                // copy the file
                byte[] buffer = new byte[512];
                Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                int cnt = 0;
                while ((cnt = fileStream.Read(buffer, 0, 512)) > 0)
                {
                    persistentStream.Write(buffer, 0, cnt);
                }

                DataStream = persistentStream;

                UpdateInCollection();
            }
        }
Пример #6
0
 /// <summary>
 /// Creates a MSNObject.
 /// </summary>		
 public MSNObject()
 {
     DataStream = new PersistentStream(new MemoryStream());
 }
Пример #7
0
	/// <summary>
	/// Retrieve object header, links and properties.
	/// </summary>
	/// <param name="header">In/Out header value.</param>
	/// <param name="links">Array of object links.</param>
	/// <param name="props">Array of object properties.</param>
	public void Retrieve( ref HEADER header, out LINK[] links,
						  out PROPERTY[] props )
	{
		#region debug info
#if (DEBUG)
		Debug.Print( "-> ODB.Retrieve( {0}, {1} )", header.ID, header.Type );
#endif
		#endregion

		List<PROPERTY> _props = new List<PROPERTY>();	// list to store properties of object
		List<LINK> _links = new List<LINK>();	// list to store child proxy objects
		DbDataReader dr = null;
		DbCommand cmd = null;
		// init out parameters
		links = null;
		props = null;

		// open connection and start new transaction if required
		TransactionBegin();
		try {
			// get object header
			HEADER newHeader = get_header( header.ID );

			if( header.Stamp == newHeader.Stamp ) {
				header = newHeader;
				// close connection and commit transaction if required
				TransactionCommit();
				return;
			}

			#region retrive props from _properties
			cmd = new SqlCommand( string.Format(
					"SELECT [Name], [Value] FROM [dbo].[_properties] WHERE [ObjectID] = {0}",
					header.ID) );
			cmd.Connection = m_con;
			cmd.Transaction = m_trans;

			dr = cmd.ExecuteReader( CommandBehavior.SingleResult );
			try {
				// read all simple properties of object
				while( dr.Read() ) {
					// read properties from row
					string name = (string) dr["Name"];
					object val	= dr["Value"];

					// convert byte array to memory stream
					if( val.GetType() == typeof(Byte[] ) ) {
						val = new PersistentStream((Byte[])val );
					}
					// build PersistentProperty upon recieved name and value and
					// save property in collection
					_props.Add( new PROPERTY( name, new ValueBox(val), PROPERTY.STATE.New ));
				}
			} finally {
				// Dispose SqlDataReader
				dr.Dispose();
			}
			#endregion

			#region retrive props from _images
			cmd = new SqlCommand( string.Format(
				"SELECT [Name] FROM [dbo].[_images] WHERE [ObjectID] = {0}",
				header.ID) );
			cmd.Connection = m_con;
			cmd.Transaction = m_trans;

			SqlDataAdapter da = new SqlDataAdapter( (SqlCommand)cmd );
			DataTable dt = new DataTable(); // table for object proxy properties

			da.Fill( dt ); // fill table
			DataTableReader dtr = new DataTableReader(dt);
			try {
				while( dtr.Read() ) {
					// save data from SqlDataReader because we need non SequentialAccess in datarow
					string name = (string) dtr["Name"];
					// save property in collection
					_props.Add( new PROPERTY( name,
											  new ValueBox( read_blob( header.ID, name ) ),
											  PROPERTY.STATE.New ));
				}
			} finally {
				dtr.Dispose();
			}
			#endregion

			#region retrive links
			cmd = new SqlCommand( string.Format(
				"SELECT [ID], [ObjectName], [ObjectType], [TimeStamp]\n" +
				"FROM [dbo].[_objects]\n" +
				"WHERE [ID] IN (SELECT Child FROM [dbo].[_links] WHERE Parent = {0})",
				header.ID) );
			cmd.Connection = m_con;
			cmd.Transaction = m_trans;

			dr = cmd.ExecuteReader( CommandBehavior.SingleResult );
			try {
				while( dr.Read() ) {
					// save child header
					_links.Add( new LINK(
						new HEADER((string) dr["ObjectType"],
							Convert.ToInt32( dr["ID"] ),
							Convert.ToDateTime( dr["TimeStamp"] ),
							(string) dr["ObjectName"] ),
							LINK.STATE.New));
				}
			} finally { dr.Dispose(); }
			#endregion

			props = _props.ToArray();
			links = _links.ToArray();
			header = newHeader;
		} catch( Exception ex ) {
			#region debug info
#if (DEBUG)
			Debug.Print( "[ERROR] @ ODB.Retrive: {0}", ex.ToString() );
#endif
			#endregion
			// rollback failed transaction
			TransactionRollback();
			throw;
		}
		// close connection and commit transaction if required
		TransactionCommit();

		#region debug info
#if (DEBUG)
		Debug.Print( "<- ODB.Retrieve({0}, {1})", header.ID, header.Type );
#endif
		#endregion
	}
Пример #8
0
	/// <summary>
	/// Read BLOB field to stream property
	/// </summary>
	/// <param name="objID">Stream owner object ID</param>
	/// <param name="propName">Name of stream property</param>
	/// <returns>
	/// Persistent stream that contains BLOB data.
	/// </returns>
	private PersistentStream read_blob( int objID, string propName )
	{
		#region debug info
#if (DEBUG)
		Debug.Print( "<- ODB.imageRead( {0}, '{1}' )", objID, propName );
#endif
		#endregion

		// create stream to return as result
		PersistentStream stream = new PersistentStream();

		// get pointer to BLOB field using TEXTPTR.
		DbCommand cmd = new SqlCommand( string.Format(
			"SELECT @Pointer = TEXTPTR([Value]), @Length = DataLength([Value])\n" +
			"FROM [dbo].[_images]\n" +
			"WHERE [ObjectID] = {0} AND [Name] ='{1}'",
			objID, propName) );
		cmd.Connection = m_con;
		cmd.Transaction = m_trans;
		// setup parameters
		DbParameter pointerParam = new SqlParameter("@Pointer", SqlDbType.VarBinary, 16);
		pointerParam.Direction = ParameterDirection.Output;
		cmd.Parameters.Add( pointerParam );
		DbParameter lengthParam = new SqlParameter("@Length", SqlDbType.Int);
		lengthParam.Direction = ParameterDirection.Output;
		cmd.Parameters.Add( lengthParam );
		// open connection and start new transaction if required
		TransactionBegin();
		try {
			// get pointer and length of BLOB field
			cmd.ExecuteNonQuery();

			//check that BLOB field exists
			if( pointerParam.Value == null ) {
				throw new KeyNotFoundException( ERROR_IMAGE_IS_ABSENT );
			}

			// run the query.
			// set up the READTEXT command to read the BLOB by passing the following
			// parameters: @Pointer – pointer to blob, @Offset – number of bytes to
			// skip before starting the read, @Size – number of bytes to read.
			cmd = new SqlCommand(
				"READTEXT [dbo].[_images].[Value] @Pointer @Offset @Size HOLDLOCK");
			cmd.Connection = m_con;
			cmd.Transaction = m_trans;
			// temp buffer for read/write purposes
			Byte[] buffer = new Byte[BUFFER_LENGTH];

			// set up the parameters for the command.
			cmd.Parameters.Add( new SqlParameter("@Pointer", pointerParam.Value) );
			// current offset position
			DbParameter offset = new SqlParameter("@Offset", SqlDbType.Int);
			offset.Value = 0;
			cmd.Parameters.Add( offset );
			DbParameter size =  new SqlParameter("@Size", SqlDbType.Int);
			size.Value = 0;
			cmd.Parameters.Add( size );

			while( Convert.ToInt32(offset.Value) < Convert.ToInt32( lengthParam.Value ) ) {
				// calculate buffer size - may be less than BUFFER_LENGTH for last block.
				if( (Convert.ToInt32( offset.Value ) + buffer.GetUpperBound( 0 ))
					>=
					Convert.ToInt32( lengthParam.Value ) )
					// setting size parameter
					size.Value =
						Convert.ToInt32( lengthParam.Value ) -
						Convert.ToInt32( offset.Value );
				else
					size.Value = buffer.GetUpperBound( 0 );

				// execute reader
				DbDataReader dr =
					cmd.ExecuteReader( CommandBehavior.SingleRow );

				try {
					// read data from SqlDataReader
					dr.Read();
					// put data to buffer
					// and return size of read data
					int count = Convert.ToInt32(
						dr.GetBytes( 0, 0, buffer, 0, Convert.ToInt32( size.Value ) ) );
					// append buffer data to stream
					stream.Write( buffer, 0, count );
					// increment offset
					offset.Value = Convert.ToInt32( offset.Value ) + count;
				} finally { dr.Dispose(); /*dispose DataReader*/}
			}
			// seek to begin of the stream after writing data
			stream.Seek( 0, SeekOrigin.Begin );
		} catch( Exception ex ) {
			#region dubug info
#if (DEBUG)
			Debug.Print( "[ERROR] @ ODB.imageRead: {0}", ex.Message );
#endif
			#endregion
			// rollback failed transaction
			TransactionRollback();
			throw;
		}
		// close connection and commit transaction if required
		TransactionCommit();

		#region debug info
#if (DEBUG)
		Debug.Print( "<- ODB.imageRead( {0}, '{1}' )", objID, propName );
#endif
		#endregion

		// return filled with SQL BLOB stream
		return stream;
	}
Пример #9
0
	///////////////////////////////////////////////////////////////////////
	//						SQL BLOB Section
	///////////////////////////////////////////////////////////////////////
	#region SQL BLOB interactions
	/// <summary>
	/// Saves stream property to SQL BLOB field
	/// </summary>
	/// <param name="objID">Stream owner object ID</param>
	/// <param name="propName">Name of stream property</param>
	/// <param name="stream">Stream property to save</param>
	/// <param name="isnew">Flag to check property existance.</param>
	private void save_blob( int objID, string propName, PersistentStream stream, bool isnew )
	{
		#region debug info
#if (DEBUG)
		Debug.Print( "-> ODB.imageSave( {0}, '{1}' )", objID, propName );
#endif
		#endregion
		// open connection and start new transaction if required
		TransactionBegin();

		// create command text to create new or update existing record in th table
		string sql = "DECLARE @_id as int;                                                                      \n";
		if( !isnew ) {
			sql +=   "DELETE  FROM [dbo].[_properties] WHERE [ObjectID]={0} AND [Name]='{1}';                   \n" +
					 "UPDATE  [dbo].[_images] SET @_id = [ID], [Value] = {2}                                    \n" +
					 "WHERE   @@ROWCOUNT = 0 AND [ObjectID] = {0} AND [Name] ='{1}';                            \n";
		}
		sql +=       "IF @_id IS NULL BEGIN                                                                     \n" +
					 "    INSERT INTO [dbo].[_images] ([ObjectID], [Name], [Value]) VALUES ( {0}, '{1}', {2} ); \n" +
					 "    SET @_id = SCOPE_IDENTITY();                                                          \n" +
					 "END;                                                                                      \n" +
					 "SELECT @Pointer = TEXTPTR([Value]) FROM [dbo].[_images] WHERE [ID] = @_id;                \n";
		// command that executes previous sql statement
		DbCommand cmd = new SqlCommand(string.Format( sql, objID, propName, (stream.Length > 0) ? "0x0" : "NULL"));
		cmd.Connection = m_con;
		cmd.Transaction = m_trans;

		DbParameter pointerParam  = new SqlParameter( "@Pointer", SqlDbType.Binary, 16 );
		pointerParam.Direction = ParameterDirection.Output;
		cmd.Parameters.Add( pointerParam );
		try {
			// get pointer to image data
			cmd.ExecuteNonQuery();
			// set up UPDATETEXT command, parameters, and open BinaryReader.
			cmd = new SqlCommand(
				"UPDATETEXT [dbo].[_images].[Value] @Pointer @Offset @Delete WITH LOG @Bytes");
			cmd.Connection = m_con;
			cmd.Transaction = m_trans;
			// assign value of pointer previously recieved
			cmd.Parameters.Add( new SqlParameter("@Pointer", SqlDbType.Binary, 16) );
			cmd.Parameters["@Pointer"].Value = pointerParam.Value;
			// start insertion from begin
			DbParameter offsetParam = new SqlParameter( "@Offset", SqlDbType.Int );
			offsetParam.Value = 0;
			cmd.Parameters.Add( offsetParam );
			//delete 0x0 character
			DbParameter deleteParam = new SqlParameter("@Delete", SqlDbType.Int);
			deleteParam.Value  = 1;
			cmd.Parameters.Add( deleteParam );
			DbParameter bytesParam = new SqlParameter( "@Bytes", SqlDbType.Binary );
			cmd.Parameters.Add( bytesParam );

			// save current stream position and seek to begin
			long pos = stream.Position;
			stream.Seek( 0, SeekOrigin.Begin );

			// read buffer full of data and execute UPDATETEXT statement.
			Byte[] buffer = new Byte[BUFFER_LENGTH];
			// make first read from stream
			int ret = stream.Read( buffer, 0, BUFFER_LENGTH );

			// while something is read from stream, write to apend to BLOB field
			while( ret > 0 ) {
				// initing parameters for write
				bytesParam.Value = buffer;
				bytesParam.Size = ret;
				// write to BLOB field
				cmd.ExecuteNonQuery(); // execute iteration
				deleteParam.Value = 0; // don't delete any other data
				// prepare to next iteration
				offsetParam.Value =
					Convert.ToInt32( offsetParam.Value ) + ret;
				// read from stream for next iteration
				ret = stream.Read( buffer, 0, BUFFER_LENGTH );
			}
			// restore stream position after reading
			stream.Position = pos;
		} catch( Exception ex ) {
			#region debug info
#if (DEBUG)
			Debug.Print( "[ERROR] @ ODB.imageSave: {0}", ex.ToString() );
#endif
			#endregion
			// rollback failed transaction
			TransactionRollback();
			throw;
		}
		// close connection and commit transaction if required
		TransactionCommit();
		#region debug info
#if (DEBUG)
		Debug.Print( "<- ODB.imageSave( {0}, '{1}' )", objID, propName );
#endif
		#endregion
	}
Пример #10
0
        private static VariantAnnotationsParser GetVariantAnnotationsParserFromCustomTsvStream(PersistentStream customTsvStream)
        {
            var parser = VariantAnnotationsParser.Create(new StreamReader(GZipUtilities.GetAppropriateStream(customTsvStream)));

            parser.SequenceProvider = new ReferenceSequenceProvider(PersistentStreamUtils.GetReadStream(LambdaUrlHelper.GetRefUrl(parser.Assembly)));

            return(parser);
        }
Пример #11
0
 private static GeneAnnotationsParser GetGeneAnnotationsParserFromCustomTsvStream(PersistentStream customTsvStream)
 {
     var(entrezGeneIdToSymbol, ensemblGeneIdToSymbol) = GeneUtilities.ParseUniversalGeneArchive(null, LambdaUrlHelper.GetUgaUrl());
     return(GeneAnnotationsParser.Create(new StreamReader(GZipUtilities.GetAppropriateStream(customTsvStream)), entrezGeneIdToSymbol, ensemblGeneIdToSymbol));
 }
Пример #12
0
 /// <summary>
 /// Creates a MSNObject.
 /// </summary>
 public MSNObject()
 {
     DataStream = new PersistentStream(new MemoryStream());
 }
Пример #13
0
 public void TestFlakyConnection()
 {
     // pStream attempts to connect at construction time. It should succeed at the third attempt
     var pStream = new PersistentStream(GetWebRequest_connect_on_third(), 0);
     // no exception thrown means this test succeeded
 }