XrdCmsNash.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                         X r d C m s N a s h . h h                          */
00004 /*                                                                            */
00005 /* (c) 2007 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 //         $Id: XrdCmsNash.cc 30949 2009-11-02 16:37:58Z ganis $
00012 
00013 const char *XrdCmsNashCVSID = "$Id: XrdCmsNash.cc 30949 2009-11-02 16:37:58Z ganis $";
00014 
00015 #include <stdlib.h>
00016 
00017 #include "XrdCms/XrdCmsNash.hh"
00018 
00019 /******************************************************************************/
00020 /*                           C o n s t r u c t o r                            */
00021 /******************************************************************************/
00022   
00023 XrdCmsNash::XrdCmsNash(int psize, int csize)
00024 {
00025      prevtablesize = psize;
00026      nashtablesize = csize;
00027      Threshold     = (csize * LoadMax) / 100;
00028      nashnum       = 0;
00029      nashtable     = (XrdCmsKeyItem **)
00030                      malloc( (size_t)(csize*sizeof(XrdCmsKeyItem *)) );
00031      memset((void *)nashtable, 0, (size_t)(csize*sizeof(XrdCmsKeyItem *)));
00032 }
00033 
00034 /******************************************************************************/
00035 /* public                            A d d                                    */
00036 /******************************************************************************/
00037   
00038 XrdCmsKeyItem *XrdCmsNash::Add(XrdCmsKey &Key)
00039 {
00040    XrdCmsKeyItem *hip;
00041    unsigned int kent;
00042 
00043 // Allocate the entry
00044 //
00045    if (!(hip = XrdCmsKeyItem::Alloc(Key.TOD))) return (XrdCmsKeyItem *)0;
00046 
00047 // Check if we should expand the table
00048 //
00049    if (++nashnum > Threshold) Expand();
00050 
00051 // Fill out the key data
00052 //
00053    if (!Key.Hash) Key.setHash();
00054    hip->Key = Key;
00055 
00056 // Add the entry to the table
00057 //
00058    kent = Key.Hash % nashtablesize;
00059    hip->Next = nashtable[kent];
00060    nashtable[kent] = hip;
00061    return hip;
00062 }
00063   
00064 /******************************************************************************/
00065 /* private                        E x p a n d                                 */
00066 /******************************************************************************/
00067   
00068 void XrdCmsNash::Expand()
00069 {
00070    int newsize, newent, i;
00071    size_t memlen;
00072    XrdCmsKeyItem **newtab, *nip, *nextnip;
00073 
00074 // Compute new size for table using a fibonacci series
00075 //
00076    newsize = prevtablesize + nashtablesize;
00077 
00078 // Allocate the new table
00079 //
00080    memlen = (size_t)(newsize*sizeof(XrdCmsKeyItem *));
00081    if (!(newtab = (XrdCmsKeyItem **) malloc(memlen))) return;
00082    memset((void *)newtab, 0, memlen);
00083 
00084 // Redistribute all of the current items
00085 //
00086    for (i = 0; i < nashtablesize; i++)
00087        {nip = nashtable[i];
00088         while(nip)
00089              {nextnip = nip->Next;
00090               newent  = nip->Key.Hash % newsize;
00091               nip->Next = newtab[newent];
00092               newtab[newent] = nip;
00093               nip = nextnip;
00094              }
00095        }
00096 
00097 // Free the old table and plug in the new table
00098 //
00099    free((void *)nashtable);
00100    nashtable     = newtab;
00101    prevtablesize = nashtablesize;
00102    nashtablesize = newsize;
00103 
00104 // Compute new expansion threshold
00105 //
00106    Threshold = static_cast<int>((static_cast<long long>(newsize)*LoadMax)/100);
00107 }
00108 
00109 /******************************************************************************/
00110 /* public                           F i n d                                   */
00111 /******************************************************************************/
00112   
00113 XrdCmsKeyItem *XrdCmsNash::Find(XrdCmsKey &Key)
00114 {
00115   XrdCmsKeyItem *nip;
00116   unsigned int kent;
00117 
00118 // Check if we already have a hash value and get one if not
00119 //
00120    if (!Key.Hash) Key.setHash();
00121 
00122 // Compute position of the hash table entry
00123 //
00124    kent = Key.Hash%nashtablesize;
00125 
00126 // Find the entry
00127 //
00128    nip = nashtable[kent];
00129    while(nip && nip->Key != Key) nip = nip->Next;
00130    return nip;
00131 }
00132 
00133 /******************************************************************************/
00134 /* public                        R e c y c l e                                */
00135 /******************************************************************************/
00136   
00137 // The item must have been previously unload which will place the original
00138 // hash value in Loc.HashSave. Yes, not very OO but very fast.
00139 //
00140 int XrdCmsNash::Recycle(XrdCmsKeyItem *rip)
00141 {
00142    XrdCmsKeyItem *nip, *pip = 0;
00143    unsigned int kent;
00144 
00145 // Compute position of the hash table entry
00146 //
00147    kent = rip->Loc.HashSave%nashtablesize;
00148 
00149 // Find the entry
00150 //
00151    nip = nashtable[kent];
00152    while(nip && nip != rip) {pip = nip; nip = nip->Next;}
00153 
00154 // Remove and recycle if found
00155 //
00156    if (nip)
00157       {if (pip) pip->Next = nip->Next;
00158           else nashtable[kent] = nip->Next;
00159           rip->Recycle();
00160           nashnum--;
00161       }
00162    return nip != 0;
00163 }

Generated on Tue Jul 5 14:46:30 2011 for ROOT_528-00b_version by  doxygen 1.5.1