Archive for October, 2005

PostgreSQL8.0 supports XML!

Monday, October 17th, 2005

Cool. It looks that PostgreSQL8.0 supports XML as an additional extension (contrib driectory). In gentoo you can compile it specifying +xml2 option.

Now how to use it: 1. you need to create xml functions in a database using a supplied script, most typically /usr/share/postgresql/contrib/pgxml.sql. Note that this script has been created (customized) by autoconf and contains a hard-coded location of your pgxml.so library, typically: /usr/lib/postgresql/pgxml.so.

  1. If you get no error…. well that’s it ;-)

See this link and the documentation contrib/xml2 for more details.

BTW: If your database stores XML as binary strings (bytea) you must convert them to normal strings using encode(<byteacolumn>, ‘escape’);

Slow aggergate queries in PostgreSQL

Monday, October 17th, 2005

Postgres is strange. If you make a query

select min(column_with_index) from table;

the database performs a sequential scan – you can only imagine how long it takes on a 10M record database. This seems to be a feature (!@!#!@), but indices can will used if you use the following workaround:

select column_with_index from table order by 1 (desc) limit 1;

This trick is explained here.

Blog is going public, some WP patches

Sunday, October 16th, 2005

After almost a day of editing and reorganizing, I think the blog is ready to go on-line. Many things are really chaotic, I nonetheless hope that someone might find something useful here.

I also did some minor patching of WP, making future updates will be close to impossible. Hope it will not be that bad…

To summarize today’s changes: 1. “linky” category, inspired by Diego’s blog, which was inspired by Fernando Graphicos, inspired by…. ;-)

I basically followed Fernando’s suggestions, shamelessly stealing some of Diego’s CSS (not too much though). The changes I had to make included editing of all the files containing a loop “while(have_posts()…”.

  1. Displaying hierarchical categories, instead of the flat strucutre. The change was to change “hierarchical=0″ to “hierarchical=1″ in sidebar.php.

  2. Displaying proper categories, depending on the current user. This was a bit tricky one: Depending on the user logged in, I wanted to have the correct list of categories on the main page. The patch is one line in template-functions-category.php”:

Replacing the line (somewhere around line 290): WHERE post_status = ‘publish’ with WHERE ( post_status = ‘publish’”. (($user_ID)?” or (post_status=’private’ AND post_author = $user_ID)”:”") . “)

Also $user_ID has to be declared as global somewhere before.

  1. (FAILED) Correct counting of categories. With a hierarchical system, I wanted to have a correct number of categories, e.g. if category A (5) has two sub categories A1 (2) and A2 (7), I would like A to be displayed as (14) or (5/9), as this is the number of posts you’d see if you click on it. … or is it? ;-)

I implemented is as follows: - storing a hash table of parent nodes while reading the categories (line 275) - updating the total number of posts by summing it up the chain (while ($id = $parent[$id])) {} (line 295).

This would have worked, but it has a BIG problem: a post can have both categories A and A1 set and thus would be counted twice. To avoid this, one would have to change the entire logic of a query: A single SQL query would have to be replaced by 2*n queries (for each category, querying the child categories -> using get_category_children() from classes.php) and then querying the number of posts in each of the categories. While it would have been feasible, I thought it’s not worth it and I gave up.

Non-recursively generating permutations

Saturday, October 15th, 2005

A cool link: http://www.dogma.net/markn/articles/Permutations/

In essence the algorithm works as follows:

  1. start looking from the end of the sequence for two consecutive values i and ii such that i < ii.
  2. start looking from the end of the sequence for the value j greater that i (the worst case getting ii or earlier).
  3. swap i and j
  4. reverse the sequence ii to last.

Examples:

123 -> i  = 2, ii = 3, j = 3  -> 132
132 -> i=1,ii=2,j=3 -> 213
213 -> i=2;ii=3;j=3 -> 231
231 -> i=1;ii=2;j=2 -> 312
312 -> i=2;ii=3;j=3 -> 321
321 - END

It also generates only unique permutations if there are some duplicates in the sequence (e.g. AAABBB) Cool!

Multiple matches in one line – perl one-liner

Friday, October 14th, 2005

(thanks to Diego): perl -ne ‘while ((/condition/g)) { print “something”; }’

Converting newlines DOS <-> Unix

Thursday, October 13th, 2005

Use flip:

flip -u <file_name> flip -m <file_name>

apt-get install flip