You could very well replace the implementation of the getShortString function below with code which calls hashCode() on a HashMap containing your name/value pairs, and in fact, I think it is an excellent approach. I only opted for the hash() function in my own implementation for the following reasons:
1. In general, calling hashCode on two distinct object instances is not actually guaranteed to return a unique number for each object instance. From the documentation: "It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables." Since we are only talking about strings here, and hopefully all implementations of Java return unique integers for unique strings (though they are not technically required to), this is really a non-issue. My gut reaction, though, is not to trust hashCode to always return a unique number. 2. Since calling hashCode() on a HashMap returns the sum of all its entries' hash codes, isn't it possible (though unlikely) that the following statement could be true? "a".hashCode() + "b".hashCode() == "c".hashCode() 3. Hash codes are not guaranteed to be consistent from one execution of a Java application to the next. From the documentation: "Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application." In other words, if you had to restart your server, or if you were load balancing between multiple CF servers (especially if they were running different versions or implementations of Java), your hash codes could technically end up being inconsistent. The implementation I provided would need to store the struct of hashes and values persistently in order to avoid this issue, but I believe in the end, your hashes would be consistent. These are really very small points which are very unlikely to occur, so in general, I agree with Matt that his implementation is a good approach. And I also agree with him that calculating a hash code should be (though is not guaranteed to be) less processor-intensive than the MD5 algorithm. I only meant to suggest an alternative. Cantrell Matt Liotta wrote: > This technique seems like a waste of processing time when the underlying > Java object already provides the same thing. From the documentation... > > hashCode > public int hashCode() > > Returns the hash code value for this map. The hash code of a map is > defined to be the sum of the hash codes of each entry in the map's > entrySet() view. This ensures that t1.equals(t2) implies that > t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by > the general contract of Object.hashCode. > > This implementation iterates over entrySet(), calling hashCode on each > element (entry) in the Collection, and adding up the results. > > See my earlier response to the question for an example of using the > hashCode method with a Map. > > Matt Liotta > President & CEO > Montara Software, Inc. > http://www.montarasoftware.com/ > 888-408-0900 x901 > > >>-----Original Message----- >>From: Christian Cantrell [mailto:qantrell@;yahoo.com] >>Sent: Tuesday, October 29, 2002 10:08 PM >>To: CF-Talk >>Subject: Re: Shorter urls? >> >>Here's an interesting solution. It's not perfect (just threw the code >>together), but it works. It has the additional benefit of adding a >>level of security to your application because it hashes your query >>string in a way that cannot be reversed (essentially obfuscates it in > > a > >>non-reversible manner). It also assumes that once you have created a >>shorter query string, you will eventually want to recover the original >>query string, probably in the page processing the request that > > contains > >>the query string. >> >>Start by including these two functions: >> >><cfscript> >> function getShortString(str) { >> var key = ""; >> if (false is isDefined("application._strMap")) { >> application._strMap = structNew(); >> } >> key = hash(str); >> application._strMap[key] = str; >> return key; >> } >> >> function getLongString(key) { >> if (false is isDefined("application._strMap")) { >> return ""; >> } >> >> if (false is structKeyExists(application._strMap, key)) { >> return ""; >> } >> return application._strMap[key]; >> } >></cfscript> >> >>Then create your querystrings like this: >> >><cfset qs = "a=b&b=c&c=a" /> >><a >> > > href="someProcessingPage.cfm?q=<cfoutput>#getShortString(qs)#</cfoutput> > "> > >>click >>here</a> >> >>In the processing page, retrieve the original querystring like this: >> >><cfset qs = "#getLongString(url.q)#" /> >>The full querystring is <cfoutput>#qs#</cfoutput>. >> >>Of course, you could do something a little more clever like creating a >>function that returns a struct or parses the querystring and puts the >>values in the URL scope so that you could retrieve them just as though >>they were literally passed in the URL as opposed to actually getting >>back a query string, but you get the idea. >> >>Your query string has to be over 32 characters before this technique >>starts to pay for itself in terms of length since the hash() function >>returns a 32 byte string. As I mentioned earlier, it has the added >>benefit of completely obscruing the data you pass in your query > > string, > >>too, since the MD5 hash is not reversable. >> >>If you decide to implement something like this, consider variable > > locking. > >>Hope this helps. >> >>Cantrell >> >>Ian Lurie wrote: >> >>>Hi all, >>> >>>I've searched the devcenter, google, etc. but can't seem to find any >>>discussion of how to generate shorter URL strings. I want to take: >>> >>>http://www.site.com?action=blah&brand=1&name=blahblah&.... >>> >>>And convert it to >>> >>>http://www.site.com/asdfwer234123 >>> >>>I've seen it done but just can't remember where. Can someone send me >> > a > >>url? >> >>>Thanks in advance, >>> >>>Ian >> >> >>-- >>Christian Cantrell >>[EMAIL PROTECTED] >>(571) 220-8659 (mobile) >> >> > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Get the mailserver that powers this list at http://www.coolfusion.com

