<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Usage Pitfalls" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Persistent Hashtables </title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../Resources/Code/prettify.js"> </script> <script src="../Resources/Code/lang-vb.js"> </script> <script src="../Resources/Code/jquery.min.js"> </script> <script src="../Resources/Code/ZeroClipboard.js"> </script> <script src="../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../index_CSH.html#usage_pitfalls/persistent_hashtables.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../usage_pitfalls.htm">Usage Pitfalls</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Persistent Hashtables</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1>Persistent Hashtables</h1> <p>Hashtable or Hashmap is a data structure that associates keys to values. Hashtable uses a hash function to quickly navigate to a specific value. Hash function returns an integer value based on a specific algorithm whichis based on the contents of the object. Different hash algorithms can be used to produce hash codes for different objects. The general requirements for hash code are the following: </p> <ul> <li value="1">Hash function must return the same result for the same object during the lifetime of the application.</li> <li value="2">Hash function must produce the same results for the objects that are equal according to the equals(object) function</li> <li value="3">If 2 objects are unequal according to the equals(object) function it is not required that the hash function produce distinct results</li> </ul> <p>As you can see from the last point there can be more than one distinct key object in a hashtable that have the same hash code. Special methods called collision resolution are used to find the correct value for the specific key. Usually a separate storage - a bucket - is used for all keys with the same hash code. In this case a bucket is located by the hash code and then the right key is searched within the bucket, which allows to get a good enough performance. This works good enough for an in-memory hashtable as the hash values are not changed during application lifetime. However, it gets more difficult with a persistent hashtable.</p> <p>When a hashtable is stored to a database - the hash values are not stored. As we know from the definition, the hash value of an object is only required to stay the same during the application lifetime, which means that if the hashtable will be loaded into memory from the database in another application or in another session, the hash values of the keys can differ from their initial value. We will still be able to retrieve values by their key objects if equals and hashCode functions are based on the object contents. However the consistency of the hashtable can potentially be broken. This can happen if the key objects from different buckets will obtain the same hash value as the result of re-instantiation from the database. </p> <p>The simplest way to avoid the inconsistency of the persisted hash table use object content-based hash code functions for your key objects.</p> <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>