Earlier today, Sammy Larbi posted about how he created an algorithm in Java for sorting a really BIG file by breaking it up into multiple small files and then using an "external merge sort" to join those files into a final sorted file. This sounded cool, more because it was high-level than anything else. Sounded like fun to try and duplicate this kind of functionality in ColdFusion.
First off, why would we do this? Simple - memory usage. Sammy's scenario was that you had to read in and sort a massive file whose entirety could not be read into the server's memory without causing a crash. Now, ColdFusion automatically reads in complete file data when you use the CFFile tag. Therefore, we cannot use that. We are going to need to harness the buffering input and output power of Java's I/O library.
As it turns out, there is a really great convenience class in Java for reading in file data one line at a time. This is the java.io.LineNumberReader and I have done some experimentation with it before. As far as writing files to disk, if this file is MASSIVE, then we want to be careful about how often we go to the file system (as file I/O is a very costly action). Therefore, we can use one of Java's buffered output writers to help manage the efficiency with which we write the file (as opposed to calling CFFile Action="Append" for each line of data).
This is a proof of concept; we are not writing massive files - we are just using 30 rows of data. I have no idea how this scales, but it seems to work on a small level. I was not sure how to best decide how many lines of data could go in each small file, so I set a variable that could be altered to change the size of the file. This variable, and the size of the original file, determines how many small files will be created.
Without any further delay, the ColdFusion code that pulls this off (sorry that the Java object creation code lines are not formatted / indented as nicely as they could be - they're just too long to do in this space):
Launch code in new window » Download code as text file »
The first part of the above process actually generates a random data file. This is the content of that file:
GTUJXMEHPKICBQYZDAWLRSVFON
WHMTQVKZFULDOYGCRXEAJNSPIB
SMDPXWKYTNZFCOHALIJBRGEUVQ
VYWLUIFGBRXNKECTAPMSZJHDOQ
NODBUJHCZQAPIMKRELXGTSVWYF
TINGSFBJPXHDWZMCAEOQLRVUYK
WDBJIETYRHXGOSVZUFLCKAMPQN
PWBEKMJOGAYISVTFXDRZUQHNCL
ZSPAEILTWVXYMOQBJDNCKGHFUR
KWDMCPZBTYAEOQVGNJUFRIHSXL
DFIXZQUGLPCVEYSBONTHWKARJM
TBPJZWYMAKQLRXUDNCSEGOHVIF
ICLJWBTPFVQUZOGYRESXHKMNDA
GDKIBVWJTYRCPZEOUALFHSQNXM
AGLFKOQRNTZPBWUVEHYMIXJSCD
EHWMBZJAKICFSQLTRNYDXOPVUG
MNWUALEQIRPGTKHBFVYOJDZXCS
CXASBFOEJQGZWKHUTRVMDLNPYI
YXMGQIHSOKDPVTLWJBAUFRZCNE
RGWBEMOHQYZFSCVTAUNKPJXLDI
JLYCGZHKDSWRBPUIOTEFMXQVAN
EZXVPSDIQCUTRNKLAYOJHWGMBF
PRWBMJSETZUYNAFQGLOKXIHVCD
FMKPYCGANZQXEUROSVDIJTWBLH
IRNKWEXCUBYMVOQLHZSGDJPFAT
VPTNQYLBEWZGCAXJKOMSHFUIDR
HUTRAJPVBXCDGYSIZWLFOQEKNM
HRFEGUDQANMTXYKPCZSWVLIBOJ
ESKXZUWALQBYCPJVDINRTMGOHF
NBVGXZUISFCHKPELMWJDAYROTQ
As the page starts to process, it splits the unsorted file into several smaller files. Each of these files is sorted before it is written to disk. Here are the 4 sub-sorted files (notice that they don't all have to have the same number of data rows):
sorted_1.txt
GTUJXMEHPKICBQYZDAWLRSVFON
NODBUJHCZQAPIMKRELXGTSVWYF
PWBEKMJOGAYISVTFXDRZUQHNCL
SMDPXWKYTNZFCOHALIJBRGEUVQ
TINGSFBJPXHDWZMCAEOQLRVUYK
VYWLUIFGBRXNKECTAPMSZJHDOQ
WDBJIETYRHXGOSVZUFLCKAMPQN
WHMTQVKZFULDOYGCRXEAJNSPIB
sorted_2.txt
AGLFKOQRNTZPBWUVEHYMIXJSCD
DFIXZQUGLPCVEYSBONTHWKARJM
EHWMBZJAKICFSQLTRNYDXOPVUG
GDKIBVWJTYRCPZEOUALFHSQNXM
ICLJWBTPFVQUZOGYRESXHKMNDA
KWDMCPZBTYAEOQVGNJUFRIHSXL
TBPJZWYMAKQLRXUDNCSEGOHVIF
ZSPAEILTWVXYMOQBJDNCKGHFUR
sorted_3.txt
CXASBFOEJQGZWKHUTRVMDLNPYI
EZXVPSDIQCUTRNKLAYOJHWGMBF
FMKPYCGANZQXEUROSVDIJTWBLH
JLYCGZHKDSWRBPUIOTEFMXQVAN
MNWUALEQIRPGTKHBFVYOJDZXCS
PRWBMJSETZUYNAFQGLOKXIHVCD
RGWBEMOHQYZFSCVTAUNKPJXLDI
YXMGQIHSOKDPVTLWJBAUFRZCNE
sorted_4.txt
ESKXZUWALQBYCPJVDINRTMGOHF
HRFEGUDQANMTXYKPCZSWVLIBOJ
HUTRAJPVBXCDGYSIZWLFOQEKNM
IRNKWEXCUBYMVOQLHZSGDJPFAT
NBVGXZUISFCHKPELMWJDAYROTQ
VPTNQYLBEWZGCAXJKOMSHFUIDR
Once we have each of those files sorted and written on their own, we read them back in, one line at a time. For each read, the smallest of the top-rows was written to the buffered output (resultant) file stream. The final, sorted data was:
AGLFKOQRNTZPBWUVEHYMIXJSCD
CXASBFOEJQGZWKHUTRVMDLNPYI
DFIXZQUGLPCVEYSBONTHWKARJM
EHWMBZJAKICFSQLTRNYDXOPVUG
ESKXZUWALQBYCPJVDINRTMGOHF
EZXVPSDIQCUTRNKLAYOJHWGMBF
FMKPYCGANZQXEUROSVDIJTWBLH
GDKIBVWJTYRCPZEOUALFHSQNXM
GTUJXMEHPKICBQYZDAWLRSVFON
HRFEGUDQANMTXYKPCZSWVLIBOJ
HUTRAJPVBXCDGYSIZWLFOQEKNM
ICLJWBTPFVQUZOGYRESXHKMNDA
IRNKWEXCUBYMVOQLHZSGDJPFAT
JLYCGZHKDSWRBPUIOTEFMXQVAN
KWDMCPZBTYAEOQVGNJUFRIHSXL
MNWUALEQIRPGTKHBFVYOJDZXCS
NBVGXZUISFCHKPELMWJDAYROTQ
NODBUJHCZQAPIMKRELXGTSVWYF
PRWBMJSETZUYNAFQGLOKXIHVCD
PWBEKMJOGAYISVTFXDRZUQHNCL
RGWBEMOHQYZFSCVTAUNKPJXLDI
SMDPXWKYTNZFCOHALIJBRGEUVQ
TBPJZWYMAKQLRXUDNCSEGOHVIF
TINGSFBJPXHDWZMCAEOQLRVUYK
VPTNQYLBEWZGCAXJKOMSHFUIDR
VYWLUIFGBRXNKECTAPMSZJHDOQ
WDBJIETYRHXGOSVZUFLCKAMPQN
WHMTQVKZFULDOYGCRXEAJNSPIB
YXMGQIHSOKDPVTLWJBAUFRZCNE
ZSPAEILTWVXYMOQBJDNCKGHFUR
Now, while it may be hard to look at that and say "Oh, yeah, that's totally sorted," I pasted it into excel and did a data sort. Indeed, none of the rows shifted meaning, this bad boy has been sorted.
This was a very fun exercise to do. Now, I know the code is a bit long, and you probably didn't read through it, but you should really take a look at how I used the ColdFusion query object - it's just bananas.
Download Code Snippet ZIP File
Comments (7) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
New Incredible Hulk ColdFusion eCard Available
Javascript Exec() Method For Regular Expression Matching
Good stuff Ben, and thanks for flushing it out! I should point out that I wouldn't really say I "created" the algorithm. I'm sure its well known enough, but I didn't find it as easily as I thought I would have on the internet (what I did find didn't seem to work for me). In any case, I didn't want to implicitly take credit for it by not saying anything =).
What you did basically does follow what I did ... and I DO like how you used the CF query to store the file reader information. I had a lot of boilerplate code to deal with it in a couple of Java arrays or ArrayLists (can't remember at the moment), and having a query object like you used would have really cut down on the crap. =)
The only thing I would have done differently is when you said "Check to see if we need to write one final file." I would have put that into a function so we didn't need 2 blocks of code that perform the same basic operation (DRY, you know). However, I know this is only an example, so I shouldn't get nitpicky =).
Posted by Sam on May 10, 2007 at 10:30 PM
Thats some insane code ;)
Posted by Shuns on May 10, 2007 at 11:22 PM
@Sam,
Yeah, I know that you didn't invent the algorithm, you merely put it into practice. All this stuff was created long ago it seems. I took an algorithms course in college that covered an enormous book of stuff just like this... merge sorts, binary sorts, bubble sorts, tree traversal, big O notation, orders of magnitude.... Naturally I have long since forgotten everything from that class (except for the D+ on my first test) :)
But the matter still stands - it was fun to do in ColdFusion as I have never done anything like this before.
As far as the UDF stuff for DRY programming, I honestly tend to avoid doing that in examples. The reason for it is that as you are reading down through the demo, if you hit a UDF reference, then you have to scroll away from it to see what it does. By putting the code inline (most of the time), I figure it is easier to follow.
And, ideally, this entire algorithm would probably be wrapped up in some UDF or CFC: ExternalMergeSort( strFilePathIn, strFilePathOut ).
@Shuns,
Thanks... nothing like a little insanity to get me through the week.
Posted by Ben Nadel on May 11, 2007 at 8:45 AM
Ben, just had another idea:
Replace the "Launch code in new window ยป" with a link that will turn the code block into a textbox (with the code included) so people can copy the code without going to a new page. Not sure how easy it is to do, but might be worth doing. Either that, or check out lightWindow to just popup a overlay window with the code without going to a new page.
Posted by Boyan on Jun 6, 2007 at 9:28 AM
@Boyan,
That could be pretty cool. I don't think it would be that difficult, but I will evaluate. Consider it on the "List." Thanks.
Posted by Ben Nadel on Jun 6, 2007 at 10:36 AM
Great! You the man!
Posted by Boyan on Jun 6, 2007 at 11:11 AM
To sort big files you can use our External Multi Way Merge Sort, open source and available at
http://brie.di.unipi.it/smalltext
To know more, read the section "Documentation | Small Text Utils" tutorial
Enjoy!
Posted by Claudio Corsi on Jul 10, 2008 at 7:54 AM