google checkout is pretty cool

seems pretty easy to get hooked.

Posted in ecommerce | Leave a comment

Sort HashMap by Values

Let’s say you have a hash map that stores the frequencies of words. So you are collecting (by incrementing the counters for) word frequency while you see them. After you are done, you want get the list of words ordered by frequency – essentially you want to sort the hashmap by values.

Some code here:

            List keys = new ArrayList();
            keys.addAll(map.keySet());
            Collections.sort(keys, new Comparator() {
                    public int compare(Object obj1, Object obj2) {
                        Integer v1 = map.get(obj1);
                        Integer v2 = map.get(obj2);
                        return v2.compareTo(v1);
                    }
                });
            Iterator words = keys.iterator();

            while (words.hasNext())
            {
                String word = (String) words.next();
                int freq = map.get(word).intValue();
                System.out.println("Word " + word + " : " + freq);
             }

We do

return v2.compareTo(v1);

since we want to sort the list in descending order. If you need sort it in ascending order, you would swap v1 and v2.

Posted in java | Tagged , , | Leave a comment

Run remote command through ssh and pass in local input

There are times that you want to run some command/script on a remote machine. It’s pretty straightforward, you can just do:

ssh remote_host remote_command

But there are also times you have some input that’s only available on local machine that you want to pass it as input to the remote command. In that case, you can do this:

ssh remote_host “remote_command ‘$(cat local_file.txt)’”

Several things to notice:

  1. you need the double quote since the entire command has arguments
  2. you need the dollar sign and parentheses to enclose the local command to pass the value of the output of it
  3. you need th single quote to enclose the output above

Alternatively, you could also do

cat local_file.txt | ssh remote_host “remote_command”

Posted in shell magic | Tagged , , | Leave a comment

bash for loop

Here’s a simple shell trick that does a loop through some numbers:

for i in {0..10}; do echo $i; done

For example, sometimes you want to test some concurrent read/write performance on database or whatever, you can do:

for i in {1..10}; do (echo $i; ./read_db.sh $i &); done

This will generate 10 processes that execute the read_db.sh script concurrently.

Posted in shell magic | Tagged , | Leave a comment

Simple examples of using screen command

To start a named screen session:

screen -S screen_name

To detach from current screen:

ctrl-a d

To list screens:

screen -ls

To reattach to a screen:

screen -r screen_name

To reattch to a screen and detach the existing attached screen:

screen -dr screen_name

To create a window inside a screen

ctrl-a c

To go to next or previous window

ctrl-a n
ctrl-a p

Some reference:

http://www.soulcast.com/post/show/55079/An-introduction-to-the-linux-screen-command

Posted in linux | Tagged , | Leave a comment

WordPress pretty permalink not working?

After install wordpress, usually you want to change your default permalink settings so that URL for you post will look like this

http://www.mysite.com/wordpress/fun/my-story-here

instead of this

http://www.mysite.com/wordpress?p=32

But after you change your settings in “Settings” -> “Permalinks”, when you click on the post title, it complains about the page does not exist? That’s what happened to me.

Turns out the reason was the “mod_rewrite” is not enabled on my web server (Apache). mod_rewrite is the thing behind the scene that does this magic of translating the pretty permalink to the ugly “?p=32″ – the one WordPress code can understand. So it has to be enabled.

In my case, my web server is Apache on Ubuntu, the mod_rewrite is installed but not enabled. This is what I did to enable it:

cd /etc/apache2/mods-enabled
ln -s ../mods-available/rewrite.load

Hope this is helpful to someone who has the same permalink problem.

Posted in web | Tagged , , , | Leave a comment