Christoffer Kjølbæk I have a blog, therefore I am…

11Apr/100

Making “backtrace” in MySQL with “recursive” call

Having items mapped in a tree structure is often used in database design, using "child" and "parent" ids or "nodes" and "leaf nodes". Often when presenting data, it is nice to make a "backtrace" by showing all nodes from the leaf to the root of the tree.

In this case I have a system with a lot of users, each of which is mapped to a group. The groups are defined as a tree structure, but because of the roles of the system, the leaf node can never be more than three nodes away from the root. I need to show a list of, say all users not logged in the last 2 months, and the list should show in which groups the users is located.

I could find all users, get the group id from each users and with a recursive function in the application, find the parent group id of the group, get this group from the database, find the parent group id of the group, get this group from the database..... This would require a lot of database activity. It is also possible to make nested sets or stored procedures, but if the depth of the three is relative low (and known), I believe the below is simpler.

The table of groups is defined as:

CREATE TABLE `groups` (
	`gid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	`pgid` INT NOT NULL ,
	`title` VARCHAR( 100 ) NOT NULL 
) ENGINE = MYISAM ;

With these groups:

mysql> select * from groups;
+-----+------+---------+
| gid | pgid | title   |
+-----+------+---------+
|   1 |    0 | Level 1 |
|   2 |    1 | Level 2 |
|   3 |    2 | Level 3 |
|   4 |    3 | Level 4 |
|   5 |    4 | Level 5 |
+-----+------+---------+
5 rows in set (0.00 sec)

With the below query, it is possible to make the backtrace using joins. Line 6 will find the record holding the parent group of the selected group as the temporary record bt1. Line 7 will then use the parent group id from bt1 to find this group as bt2, and so on.

1
2
3
4
5
6
7
8
9
SELECT groups.gid, groups.title, 
	bt1.gid AS 'gid-1', bt1.title AS 'title-1', 
	bt2.gid AS 'gid-2', bt2.title AS 'title-2', 
	bt3.gid AS 'gid-3', bt3.title AS 'title-3'
FROM groups
	LEFT JOIN groups AS bt1 ON bt1.gid = groups.pgid
	LEFT JOIN groups AS bt2 ON bt2.gid = bt1.pgid
	LEFT JOIN groups AS bt3 ON bt3.gid = bt2.pgid
WHERE groups.gid IN (1,2,3,4,5);

The result is

+-----+---------+-------+---------+-------+---------+-------+---------+
| gid | title   | gid-1 | title-1 | gid-2 | title-2 | gid-3 | title-3 |
+-----+---------+-------+---------+-------+---------+-------+---------+
|   1 | Level 1 |  NULL | NULL    |  NULL | NULL    |  NULL | NULL    |
|   2 | Level 2 |     1 | Level 1 |  NULL | NULL    |  NULL | NULL    |
|   3 | Level 3 |     2 | Level 2 |     1 | Level 1 |  NULL | NULL    |
|   4 | Level 4 |     3 | Level 3 |     2 | Level 2 |     1 | Level 1 |
|   5 | Level 5 |     4 | Level 4 |     3 | Level 3 |     2 | Level 2 |
+-----+---------+-------+---------+-------+---------+-------+---------+
5 rows in set (0.00 sec)

This is not really a recursive call as the title says, but the result is the same, though much faster :)

21Feb/103

Installing Xilinx ISE/EDK 11.5 under Ubuntu 9.10

This guide tell how-to install Xilinx ISE and EDK under Ubuntu 9.10, using a 30-days evaluation license acquired at http://www.xilinx.com/ise_eval/index.htm.

Required tools
The EDK needs make, which must be called gmake. The easiest way is to install build essential and make a symlink from make to gmake. Futhermore fxload is needed for firmware upload and motif for the GUI.

sudo apt-get install fxload build-essential motif-clients
sudo ln -s /usr/bin/make /usr/bin/gmake

If you have purchased a development board from Xilinx, use the included DVD:

cd /media/cdrom
sudo ./xsetup

If not, follow the download link at the above URL, and download the ISE Design Suite: Full DVD, which is roughly 5.6 GB and untar it:

mkdir ise
cd ise
tar -cvf Xilinx_11.1_ISE_DS_SFD.tar
cd Xilinx_11.1_ISE_DS_SFD
sudo ./xsetup

You do not need to use sudo, but if all users on the system should use EDK, it will be installed in /opt/Xilinx/ using sudo. If sudo is not used, it will be installed in /home/username/.

The installation process is straight forward, and will guide you to setting up a license in the last step. If you choose to use the 30-day trial, a browser will open the Xilinx license system, which will generate the license, download it and install it.

Start up scripts

In order to start xps the environment variable $XILINX must hold the path to ISE. To start xps type

export XILINX=/opt/Xilinx/11.1/ISE/ && /opt/Xilinx/11.1/EDK/bin/lin/xps
Tagged som: , , 3 Kommentarer
21Feb/100

Cleaning up /boot by removing unused kernels (Ubuntu)

At some point /boot will be full of old kernels (if /boot is a separate partition), which makes it impossible to make a dist-upgrade and it is therefore necessary to remove some old kernels.

Start by finding you current kernel

uname -r

which will give something like

2.6.24-25-server

Now, find all installed kernels

aptitude search linux|grep linux-image | grep "i   "

which will give something like

i linux-image-2.6.24-12-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-14-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-15-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-16-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-17-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-18-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-19-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-21-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-22-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-23-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-24-server - Linux kernel image for version 2.6.24 on x
i linux-image-2.6.24-25-server - Linux kernel image for version 2.6.24 on x
i linux-image-server - Linux kernel image on Server Equipment.

Purge all of these, except the one found with uname -r and linux-image-server, which is a meta package

sudo apt-get purge linux-image-2.6.24-14-server linux-image-2.6.24-15-server linux-image-2.6.24-16-server linux-image-2.6.24-17-server linux-image-2.6.24-18-server linux-image-2.6.24-19-server linux-image-2.6.24-21-server linux-image-2.6.24-22-server linux-image-2.6.24-23-server linux-image-2.6.24-24-server

The above freed 250 MB from /boot and 550 MB from / at my system.