Archive for the 'Programming' Category

I’ve been trying to write a bunch of scripts that perform some database administration tasks in Groovy. Groovy offers a several advantages over most of my other options. It runs practically everywhere. There are JDBC drivers available for every database that I have to work with. Most importantly to me though, SQL programming in Groovy is really simple.

At first I thought it was a Groovy limitation that was causing my scripts to run out of heap space when I ran them against large tables. It turned out to be the default behavior of the MySQL JDBC driver though.

By default, ResultSets are completely retrieved and stored in memory. In most cases this is the most efficient way to operate, and due to the design of the MySQL network protocol is easier to implement. If you are working with ResultSets that have a large number of rows or large values, and can not allocate heap space in your JVM for the memory required, you can tell the driver to stream the results back one row at a time.

The solution is to override three default settings. The first two override the default settings of the ResultSets that will be returned by the statement.
sql.setResultSetType( java.sql.ResultSet.TYPE_FORWARD_ONLY )
sql.setResultSetConcurrency( java.sql.ResultSet.CONCUR_READ_ONLY )

This one adds a closure that gets called every time a statement gets created.
sql.withStatement{ stmt -> stmt.setFetchSize( Integer.MIN_VALUE ) }

Smarter Filtering

At work I have a bunch of scripts that tail logs with STDOUT being redirected to some grep statements to filter out just the parts that I am interested in. Well, it would be nice to do the same thing but have the filter be smart enough to discover interesting lines.

Read the rest of this entry »

Playing With Ruby

A couple of weeks ago I was bitten by the Ruby bug. I’m not entirely certain why. From my perspective the language isn’t terribly intuitive. The default library that comes with it is huge, and there’s alot of stuff that can be done pretty quickly using it. For example, here’s a really simple script that can be used to calculate when your mount point will be full:

#!/usr/bin/ruby  require "time"  startdate = Time.parse( ARGV[0] )
startsize = ARGV[1].to_i
enddate = Time.parse( ARGV[2] )
endsize = ARGV[3].to_i
maxsize = ARGV[4].to_i

growthrate = (( endsize - startsize ) / ( enddate - startdate ))

maxdate = (( maxsize - endsize ) / growthrate )

p ( enddate + maxdate )

Here’s I’m running it with arguments that indicate the total size of my mount point was 123456 bytes on Jan 1st and 234567 bytes on Jan 31st. The maximum size for this mount point is 678900 bytes.

$ ./diskspace.rb "2006-01-01" 123456 "2006-01-31" 234567 678900
Wed May 31 00:16:50 -0600 2006

According to the output I’m either going to have to grow my volume or remove some data by May 31st.

Google Maps API

I spent most of this afternoon playing around with the Google Maps API. I created a page that highlighted many of the local coffee resources. It was a little more difficult than I was expecting, but mostly I think that was due to my relative unfamiliarity (is that even a word?) with Javascript. It’s been years since I’ve used it.

If anyone takes a look at the code, which was hacked together from a couple of the examples on Google, you might question why I created the addMarker function. I think that I might have found a tiny little bug in Firefox, even though it might just be an issue with Javascript. Initially I had all that code embedded in the for loop a couple of lines above. No matter which marker you clicked, the Information Window would always open over the last one that was created. For some reason throwing everything into a seperate function got it working.

Finally, one of the things that is seriously great about this API is that GXml object. That simplified everything a great deal. At this point it would be a very simple matter for me to just create a PHP page that generates that XML and then I could store everything in a database.

Making Gallery2 accept photos via email.

Gallery2 doesn’t support emailing photos to it by default, so I had to make alot of changes to get it working. Look behind the cut if you’re interested in how I did it.
Read the rest of this entry »

AutoJim

Jim is one of my bosses. He’s been giving a co-worker a hard time by opening a program like notepad and leaving messages for him whenever he finds that my co-worker’s computer has been left unlocked and unattended. The problem is, Jim is a busy guy and can’t always be on the lookout for unlocked computers.

I’ve spent the last two nights writing this program to help him out. I took some sample code for writing a program to put an icon in the taskbar, modified it so that every 5 minutes it checks to see if the mouse cursor has been moved. If the cursor hasn’t been moved then it opens a text file for write, leaves a random message in the text file, and then has notepad open the text file so that the user can see it.

Read the rest of this entry »