Put your message here! Contact me for more information
 
 







 

Archive for the ‘Random Walk’ Category


 

I don’t like to get political on my blog, but a friend of mine sent me a link to a political quiz at ABCNews.

http://abcnews.go.com/Politics/MatchoMatic/fullpage?id=5542139

Here are my answers:


mccain-obama

I don’t disagree with some of the things that Obama is saying, but I strongly agree with McCain’s principles and leadership. And as a foreign immigrant to this country, I’m strongly offended by Obama’s way of dealing with the issue:

Obama: “We should require them to pay a fine, learn English, and go to the back of the line for citizenship behind those who came here legally. But we cannot — and should not — deport 12 million people.” (number #8)

Who the hell is Obama to say “let’s fine these illegal immigrants because they just sneaked in the country?” JERK, JERK, and JERK. How about babies that were brought to the States by their parents, granted “illegally” according to US’s immigration laws. Do these babies do anything wrong to get fined? They speak English, pay taxes, go to school, and contribute to society as much as anyone else — while getting treated as a 2nd-class citizen.

McCain has a much clearer policy and it is towards an legalizing the integration of the immigrants (currently there’s no such thing!)

“The program will … ensure that all undocumented aliens either leave of follow the path to legal residence. American cannot permit a permanent category of individuals that do not have recognized status — a permanent second class”

McCain, I wish you the best of luck!

view comments
 

For a Rails/SQLServer application I’m working on, I had to deal with pagination with custom queries because of the different joins. The mislav-will_paginate plugin works great for MySQL, but for SQL Server, the paginated query generated by the current SQL Server Adapter (I’m using activerecord-sqlserver-adapter-1.0.0.9250) does not work very well. The current implementation is targetted really for SQL Server 2000 and older versions since these versions do not have support for ROW_NUMBER() method. It is a major pain in the butt to do pagination with these databases. With the newer SQL Sever 2005, the job is a bit easier. Microsoft implemented the ROW_NUMBER() method with a convoluted syntax to have better support for pagination, but it is still a drag because of the weird syntax.

Semergence wrote in his blog about patching the SQLServerAdapter to support pagination. Based on his post, I improved ActiveRecord::ConnectionAdapters::SQLServerAdapter::add_limit_offset! to make the query work in a more general way with free-form queries, e.g. queries ran with the paginate_by_sql() method provided by mislav-will_paginate

Include this script in your environment.rb file, or an external file and “require” the file within environment.rb.

  # monkey-patching SQLServerAdapter to support SQL Server 2005-style pagination
  module ActiveRecord
    module ConnectionAdapters
      class SQLServerAdapter
        def add_limit_offset!(sql, options)
          puts sql
          options[:offset] ||= 0
          options_limit = options[:limit] ? "TOP #{options[:limit]}" : ""
          options[:order] ||= if order_by = sql.match(/ORDER BY(.*$)/i)
                                order_by[1]
                              else
                                sql.match('FROM (.+?)\b')[1] + '.id'
                              end
          sql.sub!(/ORDER BY.*$/i, '')
          sql.sub!(/SELECT/i, "SELECT #{options_limit} * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY #{options[:order] } ) AS row_num, ")
          sql << ") AS t WHERE row_num > #{options[:offset]}”
          puts sql
          sql
        end
      end
    end
  end

The method above monkey-patches the SQLServerAdapter by overwriting the add_limit_offset! method.

Here’s a custom query that I used and the transformed result:

Resource.paginate_by_sql([
      %!SELECT  resources.*
        	,skills_count.skill_count
        FROM resources
        	,(
        		SELECT resource_id
        			, COUNT(*) AS skill_count
        		FROM resource_skills
            WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10)
        		GROUP BY resource_id
        	) AS skills_count
        WHERE resources.is_active = ?
          AND resources.id = skills_count.resource_id
        ORDER BY skill_count DESC
      !, true ], :page => page, :per_page => per_page

With :page = 1, :per_page = 2, the resulted SQL is:

SELECT TOP 2 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY skill_count DESC ) AS row_num, resources.*
 	,skills_count.skill_count
 FROM resources
 	,(
 		SELECT resource_id
 			, COUNT(*) AS skill_count
 		FROM resource_skills
 WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10)
 		GROUP BY resource_id
 	) AS skills_count
 WHERE resources.is_active = 1
 AND resources.id = skills_count.resource_id

 ) AS t WHERE row_num > 0

The will_pagination’s COUNT query is

SELECT COUNT(*) FROM (
 SELECT resources.*
 	,skills_count.skill_count
 FROM resources
 	,(
 		SELECT resource_id
 			, COUNT(*) AS skill_count
 		FROM resource_skills
 WHERE meta_skill_id IN (21,22)
 		GROUP BY resource_id
 	) AS skills_count
 WHERE resources.is_active = 1
 AND resources.id = skills_count.resource_id
 ) AS count_table

The ORDER BY part is automatically removed from the main query (which becomes a sub-select) by the plugin to speed up the query. This in turns sanatizes the sql so that SQL Server doesn’t not complain about nested “ORDER BY” within a sub-select. Neat!

The only catch with the current add_limit_offset! is that it does not support ALIAS-ing, because the aliasing confuses the reqex to parse out the ORDER BY condition in the OVER() part of the query.

For regular find() queries, here’s a sample result

Resource.find(:first)
# original query:  SELECT * FROM resources
# transformed:   SELECT TOP 1 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY resources.id ) AS row_number, * FROM resources ) AS t WHERE row_num > 0

Hope this helps and cheers!

view comments
 

Here is a quick guide to configure log4net for general asp.net 2.0 and asp.net 3.5 web projects. While you can find general guides for setting up log4net, more advanced configuration will require further digging through Log4Net documentations. My intention is to provide a quick, 5 minutes overview of configuring log4net for a web project with a more complicated, nested logging configuration.

My goal is to have 2 independent loggers working in parallel. One logs application errors (such as exceptions or serious errors that need to be reviewed later) to the Event log of Windows, and the other will log all general messages to a text file. The reason why 2 loggers are needed is because the Windows server is configured to send out email notifications on “Error” events to notify whoever in charge of maintaining the application. The general logging file containing all logging messages will provide more application-level debugging info.

Installing and Setting up Log4Net
Log4Net is a drop-in DLL file that you can download from the Apache project site. The binary is compatible with ASP.NET 3.5 and ASP.NET 2.0, as well as the Mono platform. After adding the reference to the DLL to your project, create the Global.asax file and add one line to the Application_Start method to initialize log4net at application start up.

void Application_Start(object sender, EventArgs e)
{
  log4net.Config.XmlConfigurator.Configure();
}

In the web.config file, add the log4net section handler to the configSections block



  
    

Basically we have 2 “appender” loggers and their above configurations are quite self-explanatory. The first appender, EventLogAppender, has a level range filter, which will filter out only FATAL or ERROR log entry. Then it has 2 mappings to map the different log levels to the correct Window’s Event message type. Finally, the layout section defines how the log text should look like.

For the 2nd logger, we use a RollingFile appender, which will automatically rotate the files based on the log file size. We define where to log the files to, what the maximum size is, and how many files we would like to keep. We don’t need to configure a filter for this logger since we are collecting all entries for debugging purposes, but you can add a range filter or a pattern filter without any problem.

Finally, we define the root logger. This root-level logger is the one logger receiving our log message and it will distribute these messages to all of the configured referenced appenders.

To use log4net in the program, all you need to do is add “using log4net;” to the top of the file, and initilize a static logger as follow:

public class PageBase : System.Web.UI.Page
{
  public static readonly ILog log = LogManager.GetLogger("Logger");
  public PageBase()
  {
     log.Debug("This is a debug message");
     log.Info("This is an info message");
     log.Error("This is an error message");
  }
}

Notes

  • Log4net is meant to be un-obtrusive and it won’t throw any error whatsoever, even in the case of invalid configuration. This means that you won’t know why it fails if log4net happens to fail. As the manual says, log4net is not meant to be a reliable logging mechanism. So watch out there
  • For the file appender, if you log into a sub-folder within the web project, you should take extra precaution steps to protect the one folder from public eyes. For example, denying all anonymous users from accessing the “log” folder by adding the following section into the web.config

  
    
      
        
      
    
  

References

view comments
 

I randomly ran into Steven Levithan’s blog while searching to get an idea of how JavaScript handles Unicode. Steven is a JavaScript - Unicode and Regular Expression expert. He has a cool section called “Code Challenge” with some good food-for-thoughts challenges. It’s really JavaScript being pushed to the max, in terms of brevity, creativity, and obscurity. Check out Stephen’s “Roman Numeral Convert” challenge for example.

Reading through the comments, I picked out a nugget explaining a JavaScript behavior which actually caused me some unexpected issues with TubeCaption’s Captionizer. Steven explained best in his original comment

… you might have already realized this, but the unary + operator and parseInt are not equivalent. + can convert strings to numbers, and returns NaN if the element cannot be converted. parseInt (which takes an optional second argument for the radix) does the same thing, but also extracts leading numbers from strings. E.g., parseInt(”12x”) returns 12, while +”12x” returns NaN. Additionally, parseInt and + make different assumptions about the radix when there’s a leading zero. +”012″ returns 12, but parseInt(”012″) returns 10. The leading zero causes parseInt to treat it as an octal number in probably all browsers, despite octals being summarily deprecated in ES3. Of course, you can use parseInt(”012″,10) to get around that.

Here is a quick demo of how parseInt() behaves.

For the SRT import feature of TubeCaption’s Captionizer, I heavily relied on parseInt() to get the different time values. I was caught by surprise when a user notified me that his SRT file could not be imported into the timeline. After some debugging, it turned out that some values had padding values and the parseInt() returned incorrect results in octal instead of decimal. I wish I had known about the “+” trick and the subtlety of JavaScript at the time.

view comments
 

It’s a mouthful for the title, but it is true.  I just installed IE8 Beta 2 to try out the new features and see if IE’s javascipt engine gets any speed bump.  So far IE8 is running only as fast as IE7 in my tests, while Firefox 3 runs screamingly fast (read 500% faster, seriously).

I’m impressed with IE8b2’s new Developer tool, a Firebug-like tool.  Surprisingly, the JavaScript debugger actually WORKS!  And it feels much smoother to debug JavaScript than Firebug (Firebug still suffers from some reading/refreshing glitches and doesn’t feel as responsive).

ie8b2 debugging

I was able to step into an object created using prototype’s Class.create() with no problem using F10 step over, F11 step-in keys work as expected.  Nice!  There is also a Profiler which would be totally awesome and for sure I’ll be using it extensively to optimize the JavaScript for my upcoming application.   Finally there’s some light at the end of the tunnel, which Microsoft actually give us developers what we’ve been dreaming for:  making IE sucks less.

I also managed to crash IE8b2.

I was actually running the debugger and stepping in my code.  Then I noticed and clicked clicked on “Browser Mode IE7″.  The entire application froze up.  My explanation is that the main browser process or thread is attached to the debugger, which is running in line-debugging.  Switching “Browser Mode” to a different setting requires the main browser to switch to a different view.  However, since the main process is stuck with the debugger, and the debugger is waiting for the main process to switch browsing mode, we have a deadlock situation.

I had to end-task the sucker.

And suddenly, my Firefox 3.1 started to constantly crash, even in safe-mode (running Firefox.exe using -safe-mode flag).  I chose to permanently disable all add-ons and Firefox managed to get to my favorite homepage, the about:blank page.   However, whenever I went to Yahoo Mail, the browser just crashed without even a trace in the Windows Event Application log.  All I got is the Mozilla Crash Reporter to play with.   With all my add-ons disabled, I was determined to find out what was killing Firefox, in safemode.  Apparently, something was almost as determined as I to come get Firefox, even when it’s waving the “-safe-mode” flag.

First, I went to Firefox Options, trying to find out what happened.  Clicking on the “Applications” tab would automatically crashed Firefox, weird. I went into Options, then Managed Add-ons.  I disabled all plugins, then restarted Firefox and try Yahoo Mail.  It worked.

Since IE8 was the only application I installed before Firefox started to crash, I proceed to enable all plugins but Microsoft’s.  Yahoo Mail worked still.  I then enabled the Windows Presentation Foundation (WPF) plugin and, bam, Firefox crashed again.  The culprit is the new WPF plugin that IE8 beta 2 secretly installed into Firefox.  It’s very nice of you, IE8 installer.  Should I call you mal-ware now since you just make one of my main application mal-functioned?  You just make me almost lose anything trust (left) that I have for installing Microsoft’s stuff.  Good thing trying out the new IE8 (which proves IE7 is that horrible for developer still!) is compelling enough so I let you go easy this time.

So IE8 Beta 2 testers, disable Windows Presentation Foundation plug-in for Firefox will help you stop IE8 from sabotaging your Firefox browsing experience with blazingly fast javascript execution, which IE can only cry foul by crashing itself.

view comments