/// <summary> /// Provides the data of a Url given it's unique ID. It transparently undertakes the /// decompression of the data stored in the database for the given Url. /// </summary> /// <param name="urlID">The ID of the Url</param> /// <returns>The Url's data as a string, or an empty string if the operation fails.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is given for Url ID.</exception> /// <exception cref="CWException">If the given Url's data cannot be found in the database.</exception> public string ProvideUrlData(int urlID) { if (urlID <= 0) { throw new ArgumentOutOfRangeException("urlID"); } string retVal = string.Empty; try { if (!ConnectToDatabase()) { if (!ConnectToDatabase()) { throw new CWDBConnectionFailedException(); } } SqlCommand cmd = new SqlCommand("cw_select_url_data", dbcon); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@url_id", SqlDbType.Int); cmd.Parameters[0].Value = urlID; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); da.Dispose(); cmd.Dispose(); if (ds.Tables[0].Rows.Count == 0) { ds.Dispose(); throw new CWException("The data for the specified Url are not available."); } else { DataRow dr = ds.Tables[0].Rows[0]; byte [] data = (byte [])dr[1]; int original_length = (int)dr[3]; CompressionUtils.DecompressToString(data, out retVal); ds.Dispose(); //if(retVal.Length != original_length) //{ // //log("Warning: Invalid string length of decompressed data."); //} } } catch (Exception e) { throw e; } finally { DisconnectFromDatabase(); } return(retVal); }
/// <summary> /// Selects a random Url to extract words from. /// </summary> /// <param name="UrlID">The ID of the selected Url.</param> /// <param name="data">The data of the selected Url.</param> private void SelectUrlForWordExtraction(out int UrlID, out string data) { UrlID = 0; data = String.Empty; try { try { dbcon.Open(); } catch {} if (dbcon.State == ConnectionState.Closed) { //log a message return; } SqlCommand cmd = new SqlCommand("cw_select_url_for_word_extraction", dbcon); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandTimeout = settings.DBActionTimeout; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); da.Dispose(); cmd.Dispose(); dbcon.Close(); if (ds.Tables.Count == 0) { return; } UrlID = (int)ds.Tables[0].Rows[0][0]; byte [] buffer = (byte [])ds.Tables[0].Rows[0][1]; CompressionUtils.DecompressToString(buffer, out data); ds.Dispose(); } catch { if (dbcon.State != ConnectionState.Closed) { try { dbcon.Close(); } catch {} } GC.Collect(); } }