Wednesday, November 25, 2009

Resize a VirtualBox Disk Image

VirtualBox V3.0.8
Host: Windows XP
Guest: CentOS 5.4
  1. Create new virtal disk at desired size
  2. VBoxManage.exe clonehd --existing old.vdi new.vdi
  3. Use resize tool like GParted to create new partition for unused space as ext3 in the new.vdi
  4. use system-config-lvm to expand LogicVolumn with the new partition . (Command line is preferred)
   basic command in case the url is bad
   # lvm pvcreate /dev/sda5
   # lvm vgextend "MindbenderGroup" /dev/sda5
      Volume group "MindbenderGroup" successfully extended
   # lvm lvresize -l 2261 /dev/MindbenderGroup/MindbenderHome 
     ;;; 2261 is calculated according to Free PE / Current LE show by lvm vgdiaply & lvm lvdisplay
     Extending logical volume MindbenderHome to 70.66 GB
     Logical volume MindbenderHome successfully resized
   # resize2fs /dev/MindbenderGroup/MindbenderHome
   
   # swapoff -v /dev/VolGroup00/LogVol01 
   # lvm lvresize /dev/VolGroup00/LogVol01 -L +256M 
   # mkswap /dev/VolGroup00/LogVol01 
   # swapon -va 
   # cat /proc/swaps # free 

Saturday, September 26, 2009

Crawl Chinese web site with Scrapy

The response return by Scrapy is a Python repr() object thus Chinese character 'display' in Unicode.

Quick fix is like this:
  item['name'][0].encode('utf-8')
Though some characters are still display incorrectly.
Find source code here.

More about Unicode in Python:
  http://evanjones.ca/python-utf8.html
  http://www.b-list.org/weblog/2007/nov/10/unicode/

Friday, September 18, 2009

Trac quick setup

Came across Trac while checking information of Scrapy and had a quick try.

Operation System is Ubuntu.

Operation performed:
  • Download package from official site
  • Follow INSTALL doc in the package to complete installation
  • Start Trac and visit Guide
  • Auth
    • basic auth
    • TODO other auth of adding register feature
  • Subversion
    • python Subversion module (ubuntu install script: apt-get install python-subversion)
    • "svnadmin create" Subversion repos and update trac.ini accordingly
  • Permission (Guide section TracPermissions)
    • Add permission TRAC_ADMIN to user in order to show the WebAdmin tab
    • WebAdmin is an Admin Web interface to perform admin operations
  • Roadmap
  • Component
  • TODO etc ...

Thursday, September 03, 2009

Verify Binary Search result with duplicate target in the array

Testing scenario: If testArray contains target at position n, then binarySearch (testArray, target) must return n

public void assertTheory4(int[] testArray, int target, int returnValue) {
  assertEquals(getTargetPosition(testArray, target), returnValue);
}

public int getTargetPosition(int[] testArray, int target) {
  for (int i = 0; i < testArray.length; i++)
    if (testArray[i] == target)
      return i;
  return -1;
}

Array=[2, 11, 36, 66, 104, 108, 108, 108, 122, 155, 159, 161, 191]
target=108

Binary Search 108 will return 6 while getTargetPosition return 5.

Possible fix: Update the function getTargetPosition to return a int array, which contains all position of the value.
  Then assert this return int array must contain binary search result.


Any others?

REFERENCE: SECTION 7.2 OF <BEAUTIFUL CODE>

Tuesday, September 01, 2009

NameCheck of XML Validation

The beautiful part is build up a array with size 2^16, each value is 8bits, each bit indicate something like whether current character could be start of name, valid name.
For instance, number 4 is at 0x34 (decimal 52), so flags[52] = 0x0b (00001100), those 8bits 0/1 are indication.

Chapter 5 of <beautiful code> has the detail.

Patterns: Factory method,Template method and Strategy

Basically, Factory method is a method that is defined in Parent class and is implemented in Child class, which usually is abstract method.

By contract, Template method is a method that implements an "algorithm". This implementation is usually defined in a "Parent" class and leveraging other methods including Factory methods. (See sample below)

public abstract class PizzaStore {
        /**
         * #templateMethod(String type) is an Template Method.
         * <p>
         * #factoryMethodOfInit() AND #factoryMethodOfCreate(String type) are both
         * Factory Method.
         */
        public Pizza templateMethod(String type) {

                factoryMethodOfInit();

                Pizza pizza = factoryMethodOfCreate(type);

                System.out.println("--- Making a " + pizza.getName() + " ---");
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
                return pizza;
        }

        protected abstract void factoryMethodOfInit();
        protected abstract Pizza factoryMethodOfCreate(String type);
}

Here is a definition:
Factory Method:
   Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Template Method:
   Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
   (Here "lets subclasses redefine certain steps" could be done by leveraging Factory method)

Relationship: Factory Methods are often called by template methods.

Then the Strategy Pattern, Spring DI uses this pattern IMO somehow.

Check the updated PizzaStore version below, londonPizzaCreator and nYorkPizzaCreator are "Strategies" of PizzaCreator.
Then PizzaStore can change strategies at runtime regardless implementation detail by injecting various strategies. (via method setPizzaCreator)

public abstract class PizzaStore {

        private PizzaCreator pizzaCreator;

        public Pizza templateMethod(String type) {

                factoryMethodOfInit();

                Pizza pizza = pizzaCreator.createPizza(type);

                System.out.println("--- Making a " + pizza.getName() + " ---");
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
                return pizza;
        }
        protected abstract void factoryMethodOfInit();
        public void setPizzaCreator(PizzaCreator pizzaCreator) {
                this.pizzaCreator = pizzaCreator;
        }
}

// ---------------------------------------------------------------------------------- "Strategies"

public interface PizzaCreator {
        public Pizza createPizza(String type);
}

public class londonPizzaCreator implements PizzaCreator {
        public Pizza createPizza(String type) {
                // TODO add creat detail
                return null;
        }
}

public class nYorkPizzaCreator implements PizzaCreator {
        public Pizza createPizza(String type) {
                // TODO add creat detail
                return null;
        }
}


Strategy:
  Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Related to Template Method:
  Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

What's more?
Those patterns are actually in different category in the book by GoF, trying to understand the reason therefore to understand their diff more clearly.


Wednesday, August 19, 2009

Tail Call Optimization

A tail call is a subroutine call which is followed by a return to the calling code.

Tail calls are often optimized by interpreters and compilers of functional programming languages to more efficient forms of iteration.

See here and here.

Hello-Macro in Clojure

If define the unless as function like this,
  (defn unless [expr form] (if expr nil form))

Does that work?
(unless false (println "this should print"))
  => this should print
(unless true (println "this should not print"))
  => this should not print

Obviously the answer is no.

The reason is Clojure evaluates all the arguments before passing them to a function,
so the println is called before unless ever sees it.
So it seems Clojure is applicative order evaluation, but not normal order evaluation. (SICP exe. 1.5)

Therefore Macro.

(defmacro unless [expr form] (list 'if expr nil form))

Thursday, March 05, 2009

Uninstall Oracle DB in Windows XP

This article shows the detail.

But problem is the OUI failed to show all installed components...
After read this post, I got chance to read the inventory.xml by finding out that my Oracle DB was not appeared in the HOME LIST...

Everything went well once fix the inventory.xml manualy.