<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eric Hynds &#187; ColdFusion</title>
	<atom:link href="http://www.erichynds.com/category/coldfusion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.erichynds.com</link>
	<description>Web Developer&#039;s Ramblings on JavaScript, jQuery, ColdFusion, MySQL, and other technologies.</description>
	<lastBuildDate>Mon, 02 Jan 2012 22:55:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Quick Gotcha With CFML &amp; JSON Serialization</title>
		<link>http://www.erichynds.com/coldfusion/a-quick-gotcha-with-cfml-and-json-serialization/</link>
		<comments>http://www.erichynds.com/coldfusion/a-quick-gotcha-with-cfml-and-json-serialization/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 12:39:19 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[autocomplete]]></category>
		<category><![CDATA[jquery ui]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=473</guid>
		<description><![CDATA[ColdFusion internally represents structure keys in uppercase when the keys are created using dot notation. Dot notation is typically how programmers write their code and is considered to be best practice when working with structures. To the same effect, ColdFusion&#8217;s implementation of serializeJSON() serializes your object with respect to case; therefore, we might end up [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Fa-quick-gotcha-with-cfml-and-json-serialization%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Fa-quick-gotcha-with-cfml-and-json-serialization%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>ColdFusion internally represents structure keys in uppercase <em>when the keys are created using dot notation</em>.  Dot notation is typically how programmers write their code and is considered to be best practice when working with structures.  To the same effect, ColdFusion&#8217;s implementation of <code>serializeJSON()</code> serializes your object with respect to case; therefore, we might end up with something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> struct <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span><span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> struct.foo <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;bar&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#serializeJSON(struct)#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!---</span>
<span style="color: #808080; font-style: italic;">	Result:  {&quot;FOO&quot;:&quot;bar&quot;}</span>
<span style="color: #808080; font-style: italic;">---&gt;</span></pre></div></div>

<p>Bummer.  You&#8217;d probably expect &#8220;FOO&#8221; to be lowercase since that&#8217;s how you wrote it, but unfortunately it&#8217;s uppercase according to CF. Serializing a query forces uppercase JSON keys as well because queries act and function similarly to structures.  This is rather annoying seeing how JavaScript is case sensitive, and <code>serializeJSON</code> is <em>the</em> way to push data from CF to JS.  jQuery UI&#8217;s <a href="http://jqueryui.com/demos/autocomplete" class="blank">Autocomplete</a> is a fine example of a widget that was not designed with case in mind; your JSON is expected to be returned with either a label or value key or both&#8230; in lowercase.<span id="more-473"></span></p>
<p>If you&#8217;re trying to serialize a query, there isn&#8217;t much you can do other than converting it to a structure and avoiding dot notation.  Use bracket syntax, structInsert(), or create your structure literally:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--- </span>
<span style="color: #808080; font-style: italic;">	three different ways to add values to a structure </span>
<span style="color: #808080; font-style: italic;">	while forcing lowercase keys </span>
<span style="color: #808080; font-style: italic;">---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> struct1 <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span><span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> struct1<span style="color: #0000FF;">&#91;</span><span style="color: #009900;">&quot;foo&quot;</span><span style="color: #0000FF;">&#93;</span> <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;bar&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> struct2 <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span> <span style="color: #009900;">&quot;foo&quot;</span> <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;bar&quot;</span> <span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> struct3 <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span><span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #0000FF;">structInsert</span><span style="color: #0000FF;">&#40;</span>struct3, <span style="color: #009900;">&quot;foo&quot;</span>, <span style="color: #009900;">&quot;bar&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#serializeJSON(struct1)#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span> <span style="color: #808080; font-style: italic;">&lt;!--- Result:  {&quot;foo&quot;:&quot;bar&quot;} ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#serializeJSON(struct2)#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span> <span style="color: #808080; font-style: italic;">&lt;!--- Result:  {&quot;foo&quot;:&quot;bar&quot;} ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdump</span> <span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#serializeJSON(struct3)#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span> <span style="color: #808080; font-style: italic;">&lt;!--- Result:  {&quot;foo&quot;:&quot;bar&quot;} ---&gt;</span></pre></div></div>

<p>If you&#8217;re using a remote URL for your source parameter with the jQuery UI Autocomplete widget, most likely you&#8217;re using a query to retrieve matches from a database.  Converting a CF query to JSON has it&#8217;s own set of problems because you&#8217;re stuck with extraneous data you probably don&#8217;t want: &#8220;COLUMNS&#8221;, &#8220;DATA&#8221;, and &#8220;ROWCOUNT&#8221; depending on how you serialize it.  Either way, you&#8217;ll never be able to pass a <code>serializeJSON(query)</code> directly to the widget, so let&#8217;s kill two birds with one stone by converting it to a structure and forcing the keys to a lowercase string:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--- get the matches ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfquery</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;q&quot;</span> <span style="color: #0000FF;">datasource</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#request.ds#&quot;</span><span style="color: #0000FF;">&gt;</span></span>
select product_id as value, product_title as label
from products
where product_title like <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfqueryparam</span> <span style="color: #0000FF;">value</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;%#url.term#%&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfquery</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--- where we'll hold the data to convert to JSON ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #0000FF;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#91;</span><span style="color: #0000FF;">&#93;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfoutput</span> <span style="color: #0000FF;">query</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;q&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- create a new structure on each iteration, forcing lowercase keys ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> obj <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span></span>
<span style="color: #333333;">		<span style="color: #009900;">&quot;label&quot;</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">label</span>,</span>
<span style="color: #333333;">		<span style="color: #009900;">&quot;value&quot;</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">value</span></span>
<span style="color: #333333;">	<span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- push this match onto the array ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #0000FF;">arrayappend</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">data</span>, obj<span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfoutput</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--- serialize the new structure ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfoutput</span><span style="color: #0000FF;">&gt;</span></span><span style="color: #0000FF;">#serializeJSON<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">data</span><span style="color: #0000FF;">&#41;</span>#</span><span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfoutput</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>Easy peasy, nice and hackish.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/coldfusion/a-quick-gotcha-with-cfml-and-json-serialization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FamFamFam Silk Icon Set in Base64 Encoding</title>
		<link>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/</link>
		<comments>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 15:05:26 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[icons]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=348</guid>
		<description><![CDATA[This one can be filed under both &#8220;total impractical&#8221; and &#8220;somewhat useful&#8221;. For those who haven&#8217;t heard of this trick before, base64 encoding enables you to embed binary image data directly into your HTML or CSS source. Therefore, instead of the browser requesting two files from the server &#8211; the CSS file and the image [...]


Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/test/' rel='bookmark' title='CFIMAGE resize after CFFILE upload'>CFIMAGE resize after CFFILE upload</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Ffamfamfam-silk-icon-set-in-base64-encoding%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Ffamfamfam-silk-icon-set-in-base64-encoding%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This one can be filed under both &#8220;total impractical&#8221; and &#8220;somewhat useful&#8221;.  For those who haven&#8217;t heard of this trick before, base64 encoding enables you to embed binary image data directly into your HTML or CSS source.  Therefore, instead of the browser requesting two files from the server &#8211; the CSS file and the image file &#8211; only the CSS file need be requested.  There are a number of drawbacks/pros/cons to this approach: IE doesn&#8217;t support it, file size limitations, larger CSS files, inability to separately cache CSS vs. images, etc. etc., but if you feel the need to use FamFamFam Silk icon&#8217;s in base64, look no further.</p>
<ul>
<li><a href="/examples/famfamfam/icons.css">The CSS file</a>.  It&#8217;s 2.1mb, so be careful.</li>
<li><a href="/examples/famfamfam/">Examples</a></li>
</ul>
<p>I use icons very liberally on all my projects; they&#8217;re classy and aid in usability.  I typically start with a &#8220;.icon&#8221; class that configures the correct padding/background details for the element, followed by a &#8220;.icon-*&#8221; class, which sets the background-image property to that of the desired icon.  For example, this CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">.<span style="color: #993333;">icon</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span><span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">3px</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">3px</span> <span style="color: #933;">19px</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.icon-pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">/images/icons/pdf.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Applied to this anchor element:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;icon icon-pdf&quot;</span>&gt;</span>Download PDF<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>Produces:  &nbsp;<a class="icon pdf" href="#">Download PDF</a></p>
<p>Can you dig it?<span id="more-348"></span>  The same .icon-pdf class in base64 looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.icon-pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>data<span style="color: #00AA00;">:</span>image/png<span style="color: #00AA00;">;</span>base64<span style="color: #00AA00;">,</span>iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHhSURBVDjLjZPLSxtRFIfVZRdWi0oFBf<span style="color: #00AA00;">+</span>BrhRx5dKVYKG4tLhRqlgXPmIVJQiC60JCCZYqFHQh7rrQlUK7aVUUfCBRG5RkJpNkkswrM5NEf73n6gxpHujAB/fOvefjnHM5VQCqCPa1MNoZnU/Qxqhx4woE7ZZlpXO53F0<span style="color: #00AA00;">+</span>n0c52Dl8Pt/nQkmhoJOCdUWBsvQJ2u4ODMOAwvapVAqSJHGJKIrw<span style="color: #00AA00;">+</span>/2uxAmuJgFdMDUVincSxvEBTNOEpmlIp9OIxWJckMlkoOs6AoHAg6RYYNs2kp4RqOvfuIACVFVFPB4vKYn3pFjAykDSOwVta52vqW6nlEQiwTMRBKGygIh9GEDCMwZH6EgoE<span style="color: #00AA00;">+</span>qHLMuVBdbfKwjv3yE6Ogjz/PQ/CZVDPSFRRYE4/RHy1y8wry8RGWGSqyC/nM1meX9IQpQV2JKIUH8vrEgYmeAFwuPDCHa9QehtD26HBhCZnYC8ucGzKSsIL8wgsjiH1PYPxL<span style="color: #00AA00;">+</span>vQvm5B/3sBMLyIm7GhhCe90BaWykV/Gp<span style="color: #00AA00;">+</span>VR9oqPVe9vfBTsruM1HtBKVPmFIUNusBrV3B4ev6bsbyXlPdkbr/u<span style="color: #00AA00;">+</span>StHUkxruBPY<span style="color: #00AA00;">+</span>0KY8f38oWX/byvNAdluHNLeOxDB<span style="color: #00AA00;">+</span>uyQQfPCWZ3NT69BYJWkjxjnB1o9Fv/ASQ5s<span style="color: #00AA00;">+</span>ABz8i2AAAAAElFTkSuQmCC<span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>With this strategy in mind, I took each Silk icon and created a <code>.icon-*</code> class, where <code>*</code> is the name of the icon (underscores replaced with a hyphen).  Each icon has a background-image property of the base64 encoded image.</p>
<h2>The Code</h2>
<p>The code to convert a directory of icons into base64 representation is very straightforward and revolves around ColdFusion&#8217;s fileReadBinary() and toBase64():</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffunction</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;base64CSS&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span> returntype<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;void&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;imgdir&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the directory of images to base64 convert&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;destination&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the path and name to dump the css file&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;examples&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the path and name to dump the example html&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> images <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> icon <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span><span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- read the image directory ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdirectory</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;images&quot;</span> directory<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#arguments.imgdir#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- write our CSS and example files ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> filewrite<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">expandpath</span><span style="color: #0000FF;">&#40;</span>arguments.destination<span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">&quot;&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">structkeyexists</span><span style="color: #0000FF;">&#40;</span>arguments, <span style="color: #009900;">&quot;examples&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> filewrite<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">expandpath</span><span style="color: #0000FF;">&#40;</span>arguments.examples<span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">&quot;&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- loop through each image ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfloop</span> <span style="color: #0000FF;">query</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;images&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- read the image and convert it to base64 ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.<span style="color: #0000FF;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">toBase64</span><span style="color: #0000FF;">&#40;</span>fileReadBinary<span style="color: #0000FF;">&#40;</span>directory <span style="color: #0000FF;">&amp;</span><span style="color: #009900;">'/'</span><span style="color: #0000FF;">&amp;</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- famfamfam icons separate words with an underscore.  hyphens look better in CSS ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.<span style="color: #0000FF;">name</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">replace</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">replace</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">name</span>, <span style="color: #009900;">&quot;_&quot;</span>, <span style="color: #009900;">&quot;-&quot;</span>, <span style="color: #009900;">&quot;all&quot;</span><span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">'.png'</span>, <span style="color: #009900;">''</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- create the actual CSS and HTML ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.css <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;.icon-&quot;</span> <span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">name</span><span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">&quot; { background-image:url(data:image/png;base64,&quot;</span> <span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">data</span><span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">&quot;) }&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.html <span style="color: #0000FF;">=</span> <span style="color: #009900;">'&lt;div&gt;&lt;span class=&quot;icon icon-'</span><span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">name</span><span style="color: #0000FF;">&amp;</span><span style="color: #009900;">'&quot;&gt;'</span> <span style="color: #0000FF;">&amp;</span> icon.<span style="color: #0000FF;">name</span> <span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">'&lt;/span&gt;'</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- append to the css file ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;append&quot;</span> file<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(arguments.destination)#&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#icon.css#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- append to the examples file ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">structkeyexists</span><span style="color: #0000FF;">&#40;</span>arguments, <span style="color: #009900;">&quot;examples&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
			<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;append&quot;</span> file<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(arguments.examples)#&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#icon.html#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfset</span><span style="color: #0000FF;">&gt;</span></span><span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfloop</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cffunction</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> base64CSS<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;/home/ehynds/Assets/icons/silk/&quot;</span>, <span style="color: #009900;">&quot;icons.css&quot;</span>, <span style="color: #009900;">&quot;examples.htm&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span></pre></div></div>

<p>I started by creating a &#8220;base64CSS&#8221; function which accepts three arguments: a path to a directory of images, the CSS file to generate, and the examples HTML file to generate.  cfdirectory reads the image directory, the two CSS/example files are created, and then I loop through each image in the directory.  Each image is read as binary data and converted to base64, given a name (separated with hyphens instead of underscores), and the CSS and HTML is generated.  Finally, the CSS is appended to the CSS file and the HTML to the examples file.</p>
<p>Simple, right?  Does anyone else use some kind of icon class?  I&#8217;d like to see other approaches!</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/test/' rel='bookmark' title='CFIMAGE resize after CFFILE upload'>CFIMAGE resize after CFFILE upload</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Paginating large datasets with MySQL</title>
		<link>http://www.erichynds.com/coldfusion/paginating-large-datasets-with-mysql/</link>
		<comments>http://www.erichynds.com/coldfusion/paginating-large-datasets-with-mysql/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 00:33:41 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/blog/?p=27</guid>
		<description><![CDATA[Web developers typically choose one of two ways to paginate data: Select every row in one query and use server-side programming to handle the pagination and output. Use the LIMIT clause in the query to only select the amount of rows necessary for that page of results. Which ever you choose, each can have a [...]


Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/large-loops-out-of-memory/' rel='bookmark' title='Large loops = out of memory?'>Large loops = out of memory?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Fpaginating-large-datasets-with-mysql%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Fpaginating-large-datasets-with-mysql%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Web developers typically choose one of two ways to paginate data:</p>
<ol>
<li>Select every row in one query and use server-side programming to handle the pagination and output.</li>
<li>Use the <code>LIMIT</code> clause in the query to only select the amount of rows necessary for that page of results.</li>
</ol>
<p>Which ever you choose, each can have a huge performance implication particularly when the data set is in the thousands of rows, or if the query to select the data is not optimized.</p>
<p>The performance hit in option #1 is obvious: you&#8217;re selecting ALL the rows when only a fraction of them are going to be displayed to the user at one time.  Depending on the situation and if the query can be cached, I suppose this is a fine option, but in my opinion, pagination is a job for your database layer.</p>
<p>In option #2 the query is only going to return XX number of rows out of the entire recordset.  We still need to know how many total rows exist in order to figure out how many pages there will be, and to display other meta data to users (showing records 1-100 of 4000, for example). Therefore, a second query still needs to be issued using a <code>COUNT()</code> statement, or the exact same query again without the <code>LIMIT</code> clause&#8230; not very efficient.</p>
<p>Although it doesn&#8217;t seem well known in the CF community, this scenario is the perfect fit for MySQL&#8217;s <code>SQL_CALC_FOUND_ROWS</code> and <code>FOUND_ROWS()</code>.  Basically, this statement and function used together gives us the ability to retrieve the total number of rows that would have been returned had the <code>LIMIT</code> statement not been used. Two queries still have to be issued, but this time without the performance hit.  Assuming our <code>start_record</code> and <code>records_per_page</code> variables are already set, it works like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cftransaction</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfquery</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;getData&quot;</span> <span style="color: #0000FF;">datasource</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;mydatasource&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	SELECT SQL_CALC_FOUND_ROWS field1, field2, field3
	FROM ...
	WHERE ...
	LIMIT <span style="color: #0000FF;">#start_record#</span>, <span style="color: #0000FF;">#records_per_page#</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfquery</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfquery</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;getDataCount&quot;</span> <span style="color: #0000FF;">datasource</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;mydatasource&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	SELECT FOUND_ROWS() AS totalrecords
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfquery</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cftransaction</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>The query getData retrieves only the rows we&#8217;re going to output, whereas query getDataCount retrieves the number of rows getData would have returned without the <code>LIMIT</code> restriction.  Note that you can issue a <code>SELECT FOUND_ROWS()</code> statement at any time whether you&#8217;re paginating or not, and it&#8217;ll return the recordcount for whatever the last <code>SELECT</code> was.  Therefore, in the example above, I&#8217;m wrapping both queries in a <code>cftransaction</code> tag so the <code>FOUND_ROWS()</code> will always reference the getData query.  If another query is issued at the same time by another user of your website, selecting <code>FOUND_ROWS()</code> will return the recordcount of that query, not from getData. Here&#8217;s a full example of how to build the pagination variables (untested!):</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--- the current page we're on ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfparam</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;url.page&quot;</span> <span style="color: #0000FF;">default</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;1&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--- number of records to display per page / retrieve in the query ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> records_per_page <span style="color: #0000FF;">=</span> <span style="color: #FF0000;">45</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--- row to start on during our DB retrieve ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> start_record <span style="color: #0000FF;">=</span> url.page * records_per_page<span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--- get our data ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cftransaction</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfquery</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;getData&quot;</span> <span style="color: #0000FF;">datasource</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;mydatasource&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	SELECT SQL_CALC_FOUND_ROWS field1, field2, field3
	FROM ...
	WHERE ...
	LIMIT <span style="color: #0000FF;">#start_record#</span>, <span style="color: #0000FF;">#records_per_page#</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfquery</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- get total number of rows ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfquery</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;getDataCount&quot;</span> <span style="color: #0000FF;">datasource</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;mydatasource&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	SELECT FOUND_ROWS() AS totalrecords
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfquery</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cftransaction</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--- total number of pages ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> total_pages <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">ceiling</span><span style="color: #0000FF;">&#40;</span>getDataCount.totalrecords <span style="color: #0000FF;">/</span> records_per_page<span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>In future versions of ColdFusion it would be nice to see some kind of implementation in the &#8220;result&#8221; attribute of CFQUERY.  If your result variable is &#8220;r&#8221;, for example, it would be cool to be able to reference <code>#r.found_rows#</code>, much like you can with <code>#r.generated_key#</code> during an INSERT.  I&#8217;m sure MSSQL and other DBMS&#8217;s have something similar, but I&#8217;ve only had to use this with MySQL.  Happy paginating!</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/large-loops-out-of-memory/' rel='bookmark' title='Large loops = out of memory?'>Large loops = out of memory?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/coldfusion/paginating-large-datasets-with-mysql/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Large loops = out of memory?</title>
		<link>http://www.erichynds.com/coldfusion/large-loops-out-of-memory/</link>
		<comments>http://www.erichynds.com/coldfusion/large-loops-out-of-memory/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 15:10:16 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/blog/?p=24</guid>
		<description><![CDATA[I have a script that loops over a 6MB text file and takes each line to do a large amount of processing: creating structs, lots of queries, calling other CFCs, etc. The problem is this loop will eventually take up 100% of its alloted memory and eventually timeout with an &#8220;out of memory&#8221; error. I [...]


Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/paginating-large-datasets-with-mysql/' rel='bookmark' title='Paginating large datasets with MySQL'>Paginating large datasets with MySQL</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Flarge-loops-out-of-memory%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Flarge-loops-out-of-memory%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I have a script that loops over a 6MB text file and takes each line to do a large amount of processing: creating structs, lots of queries, calling other CFCs, etc.  The problem is this loop will eventually take up 100% of its alloted memory and eventually timeout with an &#8220;out of memory&#8221; error.  I cannot figure out if it is something I am doing wrong or if it is the nature of CF.  From what I&#8217;ve read on the web, CF is notoriously poor at garbage collection and will not flush its memory until after the request is complete, which leads me to believe there is nothing I can do.</p>
<p>I have looked over the code with others and can 100% confirm that all variables in a function are var scoped, each structure is cleared at the head of the loop, and there are no open loops or areas where memory would obviously be leaking from.</p>
<p>I can also confirm with the following code (<a href="http://stackoverflow.com/questions/855066/is-there-a-solution-to-this-cfqueryparam-memory-leak">found here</a>) that with each 1000 iterations through the loop, 20MB of memory is being used, which eventually builds up until it&#8217;s maxed out.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> runtime <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">CreateObject</span><span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;java&quot;</span>,<span style="color: #009900;">&quot;java.lang.Runtime&quot;</span><span style="color: #0000FF;">&#41;</span>.getRuntime<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> memoryUsed <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#40;</span>runtime.totalMemory<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span> - runtime.freeMemory<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/</span> <span style="color: #FF0000;">1024</span> <span style="color: #0000FF;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>It was my understanding that with each loop iteration, recreating the structures that get built within the loop clear them from memory, but this apparently isn&#8217;t the case.  I have also tried tapping into Java to manually force garbage collection, which also doesn&#8217;t do a thing.  My only solution at the moment is to increase the Java heap size to 2500MB &#8211; enough for the damn thing to run &#8211; but once it has finished and other requests are made, memory is never flushed.  I then force kill CF and restart it. This is fine for my development box, but is not going to fly in production, and this script will probably be run a couple times a month.</p>
<p>So, aside from posting ~500 lines of code across a handful of files, any quick ideas I can investigate?  I&#8217;m running CF8 on Ubuntu 9.04.</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/paginating-large-datasets-with-mysql/' rel='bookmark' title='Paginating large datasets with MySQL'>Paginating large datasets with MySQL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/coldfusion/large-loops-out-of-memory/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>CFIMAGE resize after CFFILE upload</title>
		<link>http://www.erichynds.com/coldfusion/test/</link>
		<comments>http://www.erichynds.com/coldfusion/test/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 18:29:40 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[ColdFusion]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/blog/?p=5</guid>
		<description><![CDATA[I ran into an issue today while trying to resize an image with CFIMAGE that was just uploaded with CFFILE. Apparently there are (were) a number of bugs when these two tags are used in succession. In my case, the image would be uploaded fine, and then magically be deleted by the CFIMAGE tag, at [...]


Related posts:<ol><li><a href='http://www.erichynds.com/ubuntulinux/computer-janitor-corrupting-air-installs-on-ubuntu-8-04/' rel='bookmark' title='Computer Janitor corrupting AIR installs on Ubuntu 8.04'>Computer Janitor corrupting AIR installs on Ubuntu 8.04</a></li>
<li><a href='http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/' rel='bookmark' title='FamFamFam Silk Icon Set in Base64 Encoding'>FamFamFam Silk Icon Set in Base64 Encoding</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Ftest%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Ftest%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I ran into an issue today while trying to resize an image with CFIMAGE that was just uploaded with CFFILE.  Apparently there are (were) a number of bugs when these two tags are used in succession.  In my case, the image would be uploaded fine, and then magically be deleted by the CFIMAGE tag, at which point an error would be thrown because the file could not be found.</p>
<p>Code:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--- upload image ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;upload&quot;</span> </span>
<span style="color: #333333;">	destination<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(&quot;</span><span style="color: #0000FF;">/</span>images<span style="color: #0000FF;">/</span>sales<span style="color: #0000FF;">/</span><span style="color: #009900;">&quot;)#&quot;</span></span>
<span style="color: #333333;">	filefield<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;image&quot;</span></span>
<span style="color: #333333;">	nameconflict<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;makeunique&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!--- resize it ---&gt;</span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span>cfimage <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;resize&quot;</span> </span>
<span style="color: #333333;">	<span style="color: #0000FF;">width</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;640&quot;</span> </span>
<span style="color: #333333;">	<span style="color: #0000FF;">height</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;480&quot;</span> </span>
<span style="color: #333333;">	source<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(&quot;</span><span style="color: #0000FF;">/</span>images<span style="color: #0000FF;">/</span>sales<span style="color: #0000FF;">/</span>#file.serverfile#<span style="color: #009900;">&quot;)#&quot;</span></span>
<span style="color: #333333;">	destination<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(&quot;</span><span style="color: #0000FF;">/</span>images<span style="color: #0000FF;">/</span>sales<span style="color: #0000FF;">/</span>#file.serverfile#<span style="color: #009900;">&quot;)#&quot;</span></span>
<span style="color: #333333;">	overwrite<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;yes&quot;</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>Turns out there is a simple (hot)fix for this: <a href="http://kb2.adobe.com/cps/403/kb403411.html">http://kb2.adobe.com/cps/403/kb403411.html</a></p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/ubuntulinux/computer-janitor-corrupting-air-installs-on-ubuntu-8-04/' rel='bookmark' title='Computer Janitor corrupting AIR installs on Ubuntu 8.04'>Computer Janitor corrupting AIR installs on Ubuntu 8.04</a></li>
<li><a href='http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/' rel='bookmark' title='FamFamFam Silk Icon Set in Base64 Encoding'>FamFamFam Silk Icon Set in Base64 Encoding</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/coldfusion/test/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

