Merge sort in Coldfusion

For far too long I’ve been using Bubble sorts, they’re very basic and very slow but mainly I’ve been sorting tiny little arrays or lists and the server just don’t care about it.

But that’s not the proper way of doing things. Now I don’t really understand sorting algorithms all that much so I use this site to work out which one I want to use, plus a little bit of wiki:

Sorting-algorithms.com

I’ve decided to use merge sort from now on mainly because it’s quite fast and although it generally seems to be a tiny bit slower than a shell or heap sort, it is stable which shell and heap are not. I don’t really know what that means but it sounds safe! Also it works much better on large sets than any of the simple algorithms.

The algorithm is more complex than a Bubble sort and although I understand the gist of it I decided to find someone elses code and use that. So I did.

Simon Horwith’s blog entry on search algorithms

This guy has made a fantastic little demo app, very comprehensive, with 6 different sortin algorithms in it. You can download the zip and drop it into a server and test the speeds of each algorithm. Then the code is there for you to play with, or as Simon put it: I look forward to hearing how people put them to use as well as any other findings with them.

I’ve tweaked his code a bit, so it will take an array of structures/objects, and sort by any publically accessible field of the that structure. Here’s how I’ve used it in the test page:

sort = new mergesort();

a = {};
a.name = 'Pete';
a.age = 27;

b = {};
b.name = 'Holly';
b.age = 22;

c = {};
c.name = 'Jesus';
c.age = 2012;

peeps = [a,b,c];

writeoutput('

Original array

');
writedump(peeps);

// sort peeps array, by oject.name into Descending (default) order
peeps = sort.sortArrayByFieldAscending(peeps,'name');

writeoutput('

Sorted by name in descending (default) order

');
writedump(peeps);

// sort peeps array, by oject.age into Ascencing order
peeps = sort.sortArrayByFieldAscending(peeps,'age',true);

writeoutput('

Sorted by age in ascending order

');
writedump(peeps);

You can view the live demo of it working here http://skeater.co.uk/samples/mergesort.cfm and I’ve also attached a .zip of the .cfm and .cfc.

The ZIP link above doesn’t work at the moment, I’ll have it fixed shortly!

Thanks to Simon Horwith, and hopefully someone else will find this useful!

Precisely the same Railo mistake as last time

In the past I’ve had problems with my blog working on mobiles. Something to do with the way it’s set up for Coldfusion but I’m running Railo.

I blogged about it here: My very few BlogCFC woes

So when I switched across to my new server recently, I forgot to make the changes to get it running on my new server. I have in fact done something slightly differently this time though, I’ve changed the /railo/tomcat/conf/web.xml file instead of the one in the instance of Skeater.co.uk, so the changes are global.


GlobalCFMLServlet
*.cfm
*.cfml
*.cfc

/index.cfm/ *
/default.cfm/ *
/post.cfm/ *
/archive.cfm/ *
/blog.cfm/ *
/page.cfm/ *

/mobile/index.cfm/ *
/rewrite.cfm/ *

Note: once again, the white space between the slash, /, and the star, *, is just
to stop the blog trying to use it as a comment.

So thanks Sparky for pointing out the error, and no thanks to you Pete, for being an idiot and not correcting the problem that you’ve had before so you should have known! Dumb ass.

Editing all the links in a page

I’m not sure if this is a duplicate post of not… I couldn’t find the original one, so here goes anyway.

We had the situation at work where we wanted to find all the links in a news article, in the back end editing system, and send them all to something like bit.ly to shorten them, so make it less likely that the article hit the maximum word count.

This might have been a forum post actually, anyway, that’s besides the point.

So I came up with a quick example to work out how to do it. Instead of using a URL shortening API for the demo I’ve just reversed the hrefs. But it would be easy to swap them that code out for bit.ly or something similar.

Edit HREF's demo

This is totally copied from Ben Nadel's blog, an idea which he got in turn from A Book Apart: Mobile First, but I thought it was worth mentioning again.


#longText#

// find the a tags
arrHref = reMatchNoCase(']*>',longText);

// get the array length and loop over
cntHref = arrayLen(arrHref);
for(h = 1; h LTE cntHref; h++) {

// get the individual href,
thisHref = arrHref[h];
thisHref = reReplaceNoCase(thisHref,'
','1');

// reverse it, bit.ly it, whatever takes you're fancy
reverseHref = reverse(thisHref);

// replace the original href with the new one
longText = replaceNoCase(longText,thisHref,reverseHref);
}



#longText#

You can see the live sample here: http://skeater.co.uk/samples/hrefEdit.cfm

It might not be completely robust, it might need updating for production use but the concept is there and it works.

Railo and MySQL column alias

This seems like a very odd situation, but Railo has stopped supporting column aliases by default – well it’s the JDBC database driver I think… I’m not too good at all this back end java/database stuff.

But basically it all worked fine, now I’ve upgraded Railo, and things like this will throw an error.

SELECT col1 AS colOne FROM table

I don’t see why, this is such a common thing to do… Anyway, some one else has had the exact same problem not that long ago, and Google very kindly found me their blog post so I could fix it.

You just have to set “Alias Handling” to “true” for your datasources in the Railo Server Administrator. Then every thing works fine again.

I think the point of it is something to do with being able to grab the true column name and get hold of better metadata, but at the moment that’s no use to me!

I really hope Adobe don’t start doing this with Coldfusion!

Credit has to go to Anuj Gakhar for this fix: http://www.anujgakhar.com/2012/01/11/railo-mysql-and-column-aliases/

MySQL update/insert/delete rows affected in CFScript

There’s a way of getting the rows affected by an update, insert or delete MySQL query in CFScript.

I didn’t realise this until yesterday, and I’ve been doing this for a while now. I really should read more release notes.

I’ll update my http://skeater.co.uk/page.cfm/CFScript-Snippets page to show this, but I thought it was worth posting about anyway

// create query string
strSelectRows = 'DELETE FROM table WHERE col = "potato"';

// create query, assign datasource, assign query string, execute and capture returned result
qrySelectRows= new Query();
qrySelectRows.setDataSource(APPLICATION.dsn);
qrySelectRows.setSQL(strSelectRows);
qrySelectRows= qrySelectRows.Execute().getResult();

// dump results out
writeDump(qrySelectRows.getPrefix().recordcount);
writeDump(qrySelectRows.getPrefix());

Check the demo here to see the tasty results. I’ve also shown the whole prefix component in the demo.

Installing a local Railo site on MAMP

I’ve just set up my local development environment on my Mac… something I’ve never done before.

I’ve done it many times on Ubuntu and Windows, and most of it’s the same, but I thought I’d record the process, mainly for my own future reference.

Installing MAMP is easy, you just download the installer/zip thing from their site: http://www.mamp.info/en/index.html

After that, I installed Railo, which was a fairly similar process, it only took a few minutes using their installer: http://www.getrailo.org/index.cfm/download/

Then after that it gets a bit awkward. I’m building my sites using Coldfusion Builder, and they’re hosted on github as well. So first thing you’ve got to do is make your local directory, it doesn’t really matter where you put it but I used the default htdocs folder of the MAMP install: /Applications/MAMP/htdocs/SiteName

I used git to clone down my repo, and that made the folder structure for me:


cd /Applications/MAMP/htdocs/
git clone git@github.com:Willshaw/SiteName.git

In order to do that you’ve got to have you .ssh keys setup, but you don’t need to use git, you just have to have your local website files somewhere on your hard drive.

Once you’ve got some site files to use you’ve got to do 3 things:

  • Tell the Railo install where they are
  • Create a VirtualHost element in your apache config files
  • Tell Mac OS X that you have a local site

To get railo working, you need to edit the /Library/Railo/tomcat/conf/server.xml file. At the bottom there will be some commented out code telling you how to add a new railo context. Basically you just have to give it the path to your site files, and the name of your local domain… you have to make this up now. It can be anything, like localDevSite, or fatPigsSmell, anything at all.

<!--

-->

You should see it down at the end of the server.xml file.

Once you’ve done that, you need to restart railo. You can do this by simply going to the administrator and clicking the restart link on the left hand side, under the services category in the menu.

When railo has restarted you should see a WEB-INF folder in your local site files that railo has created, and you’ll also see your site files listed as a context at the bottom of the page when the administrator loads.

If you’re not with me at this point… then sorry. Email me I might be able to help.

If you’ve got the WEB-INF file, then woohoo. Almost there.

Next you have to tell apache that you’re running a whole new website on your server. So you edit /Applications/MAMP/conf/apache/httpd.conf and add lines similar to the following at the very end:

DocumentRoot "/Applications/MAMP/htdocs/SiteName"
ServerName localSite

You basically telling apache exactly the same things you told railo. I don’t know why railo can’t just copy apace, it’s annoying.

The last thing to do is tell mac os x that when you type http://localSite you want to look at your local development website, you don’t want to go hunting for it on the internet.

So you edit /etc/hosts and simply add the new local host name to the end of the list after 127.0.0.1, so it looks something like this


##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost localNPT

And that’s all I did. Now when I visit http://localSite, I get a copy of my website, pulled down from github.

If this doesn’t work for you, or it breaks anything, well sorry but web servers are a pain in the arse. Restart everything. That usually fixes it.

I have to give credit to these 2 blog posts that helped me out:

http://kisdigital.wordpress.com/2011/11/16/adding-new-sites-to-railo-demystified/

http://wordpress.org/support/topic/wordpress-3-network-multisite-on-a-local-development-site?replies=49

On that second one it was the replies from keesiemeijer that really helped