mike-obrien.net Resume Blog Labs
Tuesday, April 15, 2008

I thought this line from a CNN story on the petition to keep XP around was telling:

"Others used the comments section to rail against the very idea that Microsoft has the power to enforce the phase-out from a stable, decent product to one that many consider worse, while profiting from the move."

Yeah, I totally agree. I'm the first to embrace new versions of software but aside from the new Server, .NET and Visual Studio releases I'm not at all hyped about this next generation of Windows or Office. It is pretty rotten that no matter how a new version release turns out, the masses are forced to move to it while MS makes money. It seems like instead of taking their time to produce a quality release, products are rushed to market so that there can be a profit turned (Sharepoint & Commerce Server???). So instead of getting higher quality releases less frequently we get frequent low quality releases and we're paying for it. Things like performance, documentation and general user acceptance are tossed out the window. No wonder Apples' market share has increased so rapidly in the last few years (With the help of the iPhone and iPod of course).

Monday, April 14, 2008 11:55:17 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [2]  | 
Monday, April 07, 2008

I keep seeing this term thrown around a lot lately with all these beta releases and have had a hard time pinning down an exact definition of it, at least in layman speak anyway's. I feel a little stupid as if I should just know what it means; 'ya know a GO LIVE license, DUH'! Here is the only reference I have found (For Beta IIS 7) describing it in simple terms:

IIS 7.0 Go-Live FAQ

2. What exactly is a Go Live license? How is it different from a normal license?

A Go Live license is a license to use a Beta release of a Microsoft product in a live production environment, before the actual release of the product. It is important to also note that the Go Live license transfers from Microsoft to the licensee, liability for any unanticipated consequences, financial or otherwise, directly or indirectly caused by using the Beta technology before its official release.

Obviously license details could be different for each product but 2 things stand out in this description; 1) You're allowed to use it in production in its beta form and 2) MS is not liable for any problems the beta version may cause.

I wish there was more of a definitive source within MS for explaining licenses. I just went through this with an MSDN subscription purchase. It's almost impossible to find license details spelled out in one central place and in a way that's easy to understand without being a lawyer. And there are many licenses for the same product, so it's really hard to know what your getting into and which one is right for you.

Monday, April 07, 2008 7:19:37 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
Tuesday, March 04, 2008

Common Table Expressions (CTE's) are a handy feature introduced in SQL Server 2005. Making them more handy is the fact that they can be used recursively. This opens up a number of possibilities; one of which being a set based way to build a virtual table out of a comma separated list and filter off of that virtual table. Lets say for example we have a comma separated list of ID's we want to use to filter the results of a query.

DECLARE @Filter varchar(1000)

SET @Filter = '4,8,23,56,72';

You'll notice the semicolon at the end of the SET statement. The next statement in this example will be the CTE and TSQL requires the CTE and the previous statement to be separated by a semicolon. Now for the the first rendition of the CTE.

WITH Filter(FilterId, Position) AS
(
    SELECT 
    CAST(SUBSTRING(@Filter, 1, 
        CASE CHARINDEX(',', @Filter) 
        WHEN 0 THEN LEN(@Filter) 
        ELSE CHARINDEX(',', @Filter) - 1 END
    ) AS int) AS FilterId,
    CHARINDEX(',', @Filter) AS Position
    WHERE @Filter IS NOT NULL AND LEN(@Filter) > 0
)

SELECT * FROM Filter

The CTE, so far, only contains one query which will serve as the "anchor". If you run this it only returns one result, the very first id and the current position in the string:

image

Next we will add in the recursive query. This query must come immediatly after the anchor and be separated from it by the UNION ALL operator. You can have multiple anchors and multiple recursive queries which can make use of other combination operators, but anchors must be grouped before the recursive queries and the two groups must be separated by the UNION ALL operator.

WITH Filter(FilterId, Position) AS
(
    SELECT 
    CAST(SUBSTRING(@Filter, 1, 
        CASE CHARINDEX(',', @Filter) 
        WHEN 0 THEN LEN(@Filter) 
        ELSE CHARINDEX(',', @Filter) - 1 END
    ) AS int) AS FilterId,
    CHARINDEX(',', @Filter) AS Position
    WHERE @Filter IS NOT NULL AND LEN(@Filter) > 0

    UNION ALL

    SELECT 
    CAST(SUBSTRING(@Filter, 
        Position + 1, 
        CASE CHARINDEX(',', @Filter, Position + 1) 
        WHEN 0 THEN LEN(@Filter) - Position 
        ELSE CHARINDEX(',', @Filter, Position + 1) - Position - 1 END
    ) AS int) AS FilterId,
    CHARINDEX(',', @Filter, Position + 1) AS Position
    FROM Filter WHERE Position > 0
)

SELECT * FROM Filter

The recursive query makes use of the last position to "move" to the next id in the string. When it has hit the end of the string the last position is set to zero which terminates the recursion. The results are as follows:

image

You'll also notice that the query that selects from the CTE immediately follows it; this is another requirement for CTE's. Now lets select some employees from the AdventureWorks database:

DECLARE @Filter varchar(1000)

SET @Filter = '4,8,23,56,72';

WITH Filter(FilterId, Position) AS
(
    SELECT 
    CAST(SUBSTRING(@Filter, 
        1, 
        CASE CHARINDEX(',', @Filter) 
        WHEN 0 THEN LEN(@Filter) 
        ELSE CHARINDEX(',', @Filter) - 1 END
    ) AS int) AS FilterId,
    CHARINDEX(',', @Filter) AS Position
    WHERE @Filter IS NOT NULL AND LEN(@Filter) > 0

    UNION ALL

    SELECT 
    CAST(SUBSTRING(@Filter, 
        Position + 1, 
        CASE CHARINDEX(',', @Filter, Position + 1) 
        WHEN 0 THEN LEN(@Filter) - Position 
        ELSE CHARINDEX(',', @Filter, Position + 1) - Position - 1 END
    ) AS int) AS FilterId,
    CHARINDEX(',', @Filter, Position + 1) AS Position
    FROM Filter WHERE Position > 0
)

SELECT EmployeeID, LoginID, Title 
FROM HumanResources.Employee
WHERE EmployeeID IN (SELECT FilterId FROM Filter)

And here are the results:

image

Tuesday, March 04, 2008 4:33:58 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
Monday, February 11, 2008

I cant believe I wasn't aware of this cool little feature but with the release of C# 2.0 came the yield keyword. Basically it allows you to create an enumerator that "yields" when it has the current value ready. It's much simpler and elegant than enumerator code you would normally have to write. As an example we will create an enumerable class that returns numbers in the Fibonacci sequence.

public class FibonacciSeries : IEnumerable
{
    int size;

    internal FibonacciSeries(int size)
    { this.size = size; }

    public IEnumerator GetEnumerator()
    {
        int termA = -1;
        int termB = 1;

        for (int index = 0; index < size; index++)
        {
            int current = termA + termB;
            termA = termB;
            termB = current;
            yield return current;
        }
    }
}

Instead of creating a separate class that implements IEnumerable and keeps track of iterator state, we simply put our enumerator code in the GetEnumerator() method and call yield return ...; when we have a value ready. This allows the iterator to be written as if we were pushing the value out, much cleaner and simpler. Behind the scenes, when our code is compiled, the compiler builds what we would have normally had to write . Below is the compiled version of the above code in Reflector (Albeit cleaned it up a bit):

public class FibonacciSeries : IEnumerable
{
    private int size;

    internal FibonacciSeries(int size)
    {
        this.size = size;
    }

    public IEnumerator GetEnumerator()
    {
        Enumerator enumerator = new Enumerator(0);
        enumerator.parent = this;
        return enumerator;
    }

    private sealed class Enumerator : IEnumerator<object>, IEnumerator, IDisposable
    {
        private int state;
        private object current;
        public FibonacciSeries parent;
        public int currentValue;
        public int index;
        private int termA = -1;
        private int termB = 1;

        public Enumerator(int state)
        {
            this.state = state;
        }

        public bool MoveNext()
        {
            switch (this.state)
            {
                case 0:
                    this.index = 0;
                    break;

                case 1:
                    this.index++;
                    break;
            }

            this.state = -1;

            while (this.index < this.parent.size)
            {
                this.currentValue = this.termA + this.termB;
                this.termA = this.termB;
                this.termB = this.currentValue;
                this.current = this.currentValue;
                this.state = 1;
                return true;
            }

            return false;
        }

        void IEnumerator.Reset()
        { throw new NotSupportedException(); }

        void IDisposable.Dispose() { }

        object IEnumerator<object>.Current
        { get { return this.current; } }

        object IEnumerator.Current
        { get { return this.current; } }
    }
}
C#
Monday, February 11, 2008 1:43:21 AM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
Tuesday, February 05, 2008

There are much better explanations by people who really know about functional programming so the next few posts on F# are more an exercise for my brain than anything else. But maybe you will find them useful...

So I haven't heard of the tuple since watching boring educational videos on database theory 10 years ago. But alas the tuple appears again as I'm learning F# and also digging into some mathematical concepts again. It's actually been there all along as I declared a singleton or joined database tables. A tuple is basically a finite sequence of objects that are in a particular order and can contain the same object more than once. It is also immutable (Aka, cant be modified). An example of a tuple could be a first name, last name and age. In C# we may define it as a generic triple (a tuple with three items) class as follows:

public class Triple<A,B,C>
{
    private A value1;
    private B value2;
    private C value3;

    public Person(
        A value1,
        B value2,
        C value3)
    {
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;
    }

    public A Value1 { get { return this.value1; } }
    public B Value2 { get { return this.value2; } }
    public C Value3 { get { return this.value3; } }
}

Then define our person as this triple:

Triple<string, string, int> person = 
    new Triple<string, string, int>("Richard", "Nixon", 61);

We could then set other values to the contents of the triple if we wanted:

string firstName = person.ValueA;
string lastName = person.ValueB;
int age = person.ValueC;

Easy enough right? Well lets see how we could do the same thing in F#. First off we don't have to create or instantiate a tuple class (This is actually done under the covers as we will see in a moment). We simply set the variable equal to a comma separated list of objects as follows:

let person = "Richard", "Nixon", 61

And voila! We have a tuple (A triple to be precise)... Now to set the contents of the tuple to other variables we simply do the following:

let firstName, lastName, age = person

Here we just set the individual firstName, lastName and age variables to the corresponding values in the tuple. On the other hand if we just want the first name we can block out the last name and age with the underscore placeholder ("_") and just get the first name:

let firstName, _, _ = person

or we can just get the last name and the age by blocking out the first name. You get the point...

let _, lastName, age = person

F# uses "pattern matching" to match up the variables listed, with the values in the tuple. Now as mentioned before, under the covers F# does actually use generic tuple types as seen when working with a tuple from F#...

in C#...

image

In reflector we can see that there are tuple definitions for up to 7 items:

image

Edgar Sanchez has a nice blog entry on tuples here.

F#
Tuesday, February 05, 2008 2:46:15 AM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
Monday, January 28, 2008

Mike Jones has posted an analysis of an adaptation of the Concurrent Life app (That ships with the F# distro) here. I haven't gotten through the entire article yet but so far it is great. The sample app has a C# interface and consumes an F# library. He suggests that C#'ers not familiar with functional programming use F# imperatively to get comfortable with the language syntax then tackle the functional style.

Also, I came across these articles by Thomas Petricek that are proving very helpfull:

  • F# Overview (I.) - Introduction
  • F# Overview (II.) - Functional Programming
  • F# Overview (III.) - Object Oriented and Imperative Programming
  • F# Overview (IV.) - Language Oriented Programming
  • And also a nice intro by The Flying Frog Consultancy...

    C# | F#
    Monday, January 28, 2008 4:38:04 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 

    Most software, protocols and hardware are not compatible with their older versions, not to mention the fact that these versions change frequently. What if you had 50 year old hardware that was compatible with your brand new MacBook Pro? Well on this 50th anniversary of the patent for the original Lego brick we see compatibility at its best; the original Lego brick patented on January 28, 1958 is still compatible with bricks purchased today.

    Not surprisingly, years earlier and only 150 miles away in Copenhagen, Niels Bohr and his crew formulated many of the principles of Quantum Mechanics. This is appropriate as the connectivity of the Lego brick is in fact quantized...

    Monday, January 28, 2008 2:51:02 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
    Wednesday, January 09, 2008

    Factoring binomials raised to a particular power can be done easily when you follow a few patterns. We'll use the following binomial to demonstrate these patterns: (2a - b)3. I'm going to do this the long way to illustrate the mechanics.

    The first pattern has to do with the power the binomial is raised to. The factored form will first have the original terms multiplied by each other to form a new term. This new term will be added to itself the same number of times as the power of the unfactored binomial plus one; in this example it will be 4 (3 + 1). Since the operation in the unfactored binomial is subtraction the second term, b, will be negative.

    (2a)(-b) + (2a)(-b) + (2a)(-b) + (2a)(-b)

    The next pattern involves the coefficients. The coefficients for each term in the factored binomial match a row in Pascal's Triangle. The row number we want in Pascal's Triangle is the power the unfactored binomial was raised too (With the first row in the triangle being zero). So in this example the binomial is raised to the 3rd power and the coefficients will match the values in row 3 of Pascal's Triangle: 1, 3, 3 and 1 in that order.

    image

    1(2a)(-b) + 3(2a)(-b) + 3(2a)(-b) + 1(2a)(-b)

    The next pattern involves the powers of the terms. The first term in the original unfactored binomial, 2a, starts with the power of the original unfactored binomial and decreases by one, in the factored form. The second term in the original unfactored binomial, b, starts with a power of zero and increases by one, in the factored form.

    1(2a)3(-b)0 + 3(2a)2(-b)1 + 3(2a)1(-b)2 + 1(2a)0(-b)3

    Now the factored form can be reduced. First, if the terms in the unfactored binomial are subtracted, every other operation in the factored binomial will be subtraction. This occurs because the power determines the sign of the term. Negative terms raised to an even number will be positive (IE: -22 == -2 * -2 == 4) and negative terms raised to an odd power will remain negative (IE: -23 == -2 * -2 * -2 == -8). The pattern ends up being the first first operation is subtraction, the second is addition and so on.

    1(2a)3(b)0 - 3(2a)2(b)1 + 3(2a)1(b)2 - 1(2a)0(b)3

    Now reduce the terms:

    8a3 - 12a2b + 6ab2 - b3

    So a binomial raised to the third power, (a + b)3, will follow this pattern:

    a3 + 3a2b + 3ab2 + b3

    The following script will factor out binomials (at least to a certain degree):

    ( )  


    Unfactored
    Factored
    Reduced


    Wednesday, January 09, 2008 2:44:03 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
    Thursday, December 27, 2007

    The following is a step by step installation of Subversion over Apache and SSL authenticating through an Active Directory server or local server accounts. BTW, I'm by no means an Apache guru so please leave a comment if I'm missing anything. And thanks to a bunch of people who I cant remember who posted info on the web that helped in compiling these steps! :)

    1. Install the latest CollabNet Win32 distribution found here.
      1. Make sure that only the Apache (MOD_DAV_SVN) component is checked.
        image
      2. Set the Apache configuration. You can set an arbitrary http port for now; it will change when SSL is setup.  Also remember to check the “Install Apache … as a Windows Service” checkbox. The other two options should be set accordingly.
        image
    2. Create a test repository
      1. Open a command prompt and run the following command from the CollabNet installation folder (C:\Program Files\CollabNet Subversion Server\) to create a test repository:
        svnadmin create d:\temp\Repos\mysweetapp
    3. Test Connectivity
      1. Start the Apache service; it should be called Apache2.
      2. Browse to the test repository at http://localhost:1984/mysweetapp with a Subversion client and create a folder to verify that everything is setup correctly.
    4. Install and Configure the SSPI module
      1. Download the SSPI module from here. You will want to match the major and minor Apache build with the version number trailing the SSPI module version number. For example mod_auth_sspi-1.0.4-2.0.58.zip would be for Apache 2.0.x and mod_auth_sspi-1.0.4-2.2.2.zip would be for Apache 2.2.x (Thanks to Dan Switzer for pointing this out, I totally missed that!). After unzipping the contents if the zip, copy the mod_auth_sspi.so (In the bin folder) into the Apache modules folder (C:\Program Files\CollabNet Subversion Server\httpd\modules).
      2. Open the httpd.conf file in the Apache configuration folder (C:\Program Files\CollabNet Subversion Server\httpd\conf)
      3. Add the following line to (Or uncomment it in) the Apache configuration file (httpd.conf) in the LoadModule section:
        LoadModule sspi_auth_module modules/mod_auth_sspi.so
      4. Add the following settings, under “# Active Directory Auth”, to the location section. Be sure to specify the SSPIDomain which can be an AD domain or the local server name. If it is the local server name the local user accounts will be used to authenticate. You can use this option if there is no AD server.
             <Location />
                  DAV svn
                  SVNParentPath D:/Temp/Repos
         
                  # Active Directory Auth
                  AuthName "SVN Server"
                  AuthType SSPI
                  SSPIAuth On
                  SSPIAuthoritative On
                  SSPIDomain localhost
                  SSPIOfferBasic on
                  Require valid-user
             </Location>
      5. Restart the Apache2 service after the httpd.conf file has been saved.
      6. Perform the test noted in step #3 to test connectivity, this time logging in with a user from the domain specified above.
      7. Note that in TortoiseSVN  you can check the “Save Authentication” checkbox to avoid having to repeatedly enter your credentials:
        image
    5. Configure SSL
      1. Create the Certificate
        1. Create an OpenSSL configuration file under the Apache bin folder (C:\Program Files\CollabNet Subversion Server\httpd\bin) called openssl.conf and set its contents as follows:
               [ v3_ca ]
               subjectKeyIdentifier = hash
               authorityKeyIdentifier = keyid:always,issuer:always
               basicConstraints = CA:true
               [ req ]
               default_bits  = 1024
               default_keyfile  = svnserver.key
               distinguished_name = req_distinguished_name
               attributes  = req_attributes
               x509_extensions = v3_ca 
               string_mask  = nombstr
               [ req_distinguished_name ] 
               commonName  = Common Name
               commonName_default = My Server Name
               [ req_attributes ]
        2. Open up a command prompt in the Apache bin folder (C:\Program Files\CollabNet Subversion Server\httpd\bin).
        3. Run the following command to generate the private key and certificate request files. Be sure to enter the ip address or DNS name of the server when prompted for the common name. Also remember the pass phrase you entered as it will be required for the following step. This will create a svnserver.csr and svnserver.key file in the Apache bin folder.
                openssl req -config openssl.conf -new -out svnserver.csr
          image
        4. Remove the passphrase from the private key with the following command. Enter the passphrase you specified in the last step.
               openssl rsa -in svnserver.key -out svnserver.key
          image
        5. Create the self signed certificate with the following command. The following command sets the certificate expiration to 20 years.
               openssl x509 -in svnserver.csr -out svnserver.cert -req -signkey svnserver.key -days 7300
          image
        6. Delete the svnserver.csr in the Apache bin folder.
        7. Copy the svnserver.key and svnserver.cert from the Apache bin folder to the Apache conf folder.
      2. Open the httpd.conf file in the Apache configuration folder (C:\Program Files\CollabNet Subversion Server\httpd\conf).
      3. Change the listen port to 443:
             Listen 443
      4. Change the server name to include the SSL port, 443:
             ServerName localhost:443
      5. Uncomment or add the load module directive for mod_ssl:
             LoadModule ssl_module modules/mod_ssl.so
      6. Create or overwrite the following IfModule section so that it appears as follows:
             <IfModule mod_ssl.c>
                    SSLEngine on
         
                    SSLRandomSeed startup   builtin
                    SSLRandomSeed connect   builtin
                    SSLPassPhraseDialog     builtin
                    SSLSessionCache         dbm:logs/ssl_scache
                    SSLSessionCacheTimeout  300
                    SSLMutex                default
                    SSLCertificateFile      conf\svnserver.cert
                    SSLCertificateKeyFile   conf\svnserver.key 
              </IfModule>
      7. Restart the Apache2 service.
      8. Browse to https://localhost/mysweetapp and create a folder to test the configuration.
      9. Note that in TortoiseSVN you can permanently accept the certificate when this dialog appears. It is warning you that the issuer is not a trusted root authority.
        image
    Thursday, December 27, 2007 2:27:11 AM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  |