Back to index

lightning-sunbird  0.9+nobinonly
morkAtomMap.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _MDB_
00039 #include "mdb.h"
00040 #endif
00041 
00042 #ifndef _MORK_
00043 #include "mork.h"
00044 #endif
00045 
00046 #ifndef _MORKNODE_
00047 #include "morkNode.h"
00048 #endif
00049 
00050 #ifndef _MORKENV_
00051 #include "morkEnv.h"
00052 #endif
00053 
00054 #ifndef _MORKMAP_
00055 #include "morkMap.h"
00056 #endif
00057 
00058 #ifndef _MORKATOMMAP_
00059 #include "morkAtomMap.h"
00060 #endif
00061 
00062 #ifndef _MORKATOM_
00063 #include "morkAtom.h"
00064 #endif
00065 
00066 #ifndef _MORKINTMAP_
00067 #include "morkIntMap.h"
00068 #endif
00069 
00070 #ifndef _MORKROW_
00071 #include "morkRow.h"
00072 #endif
00073 
00074 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00075 
00076 // ````` ````` ````` ````` ````` 
00077 // { ===== begin morkNode interface =====
00078 
00079 /*public virtual*/ void
00080 morkAtomAidMap::CloseMorkNode(morkEnv* ev) // CloseAtomAidMap() only if open
00081 {
00082   if ( this->IsOpenNode() )
00083   {
00084     this->MarkClosing();
00085     this->CloseAtomAidMap(ev);
00086     this->MarkShut();
00087   }
00088 }
00089 
00090 /*public virtual*/
00091 morkAtomAidMap::~morkAtomAidMap() // assert CloseAtomAidMap() executed earlier
00092 {
00093   MORK_ASSERT(this->IsShutNode());
00094 }
00095 
00096 
00097 /*public non-poly*/
00098 morkAtomAidMap::morkAtomAidMap(morkEnv* ev, const morkUsage& inUsage,
00099     nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap)
00100 #ifdef MORK_ENABLE_PROBE_MAPS
00101 : morkProbeMap(ev, inUsage,  ioHeap,
00102   /*inKeySize*/ sizeof(morkBookAtom*), /*inValSize*/ 0,
00103   ioSlotHeap, morkAtomAidMap_kStartSlotCount, 
00104   /*inZeroIsClearKey*/ morkBool_kTrue)
00105 #else /*MORK_ENABLE_PROBE_MAPS*/
00106 : morkMap(ev, inUsage,  ioHeap,
00107   /*inKeySize*/ sizeof(morkBookAtom*), /*inValSize*/ 0,
00108   morkAtomAidMap_kStartSlotCount, ioSlotHeap,
00109   /*inHoldChanges*/ morkBool_kFalse)
00110 #endif /*MORK_ENABLE_PROBE_MAPS*/
00111 {
00112   if ( ev->Good() )
00113     mNode_Derived = morkDerived_kAtomAidMap;
00114 }
00115 
00116 /*public non-poly*/ void
00117 morkAtomAidMap::CloseAtomAidMap(morkEnv* ev) // called by CloseMorkNode();
00118 {
00119   if ( this )
00120   {
00121     if ( this->IsNode() )
00122     {
00123 #ifdef MORK_ENABLE_PROBE_MAPS
00124       this->CloseProbeMap(ev);
00125 #else /*MORK_ENABLE_PROBE_MAPS*/
00126       this->CloseMap(ev);
00127 #endif /*MORK_ENABLE_PROBE_MAPS*/
00128       this->MarkShut();
00129     }
00130     else
00131       this->NonNodeError(ev);
00132   }
00133   else
00134     ev->NilPointerError();
00135 }
00136 
00137 // } ===== end morkNode methods =====
00138 // ````` ````` ````` ````` ````` 
00139 
00140 #ifdef MORK_ENABLE_PROBE_MAPS
00141 
00142   /*virtual*/ mork_test // hit(a,b) implies hash(a) == hash(b)
00143   morkAtomAidMap::MapTest(morkEnv* ev, const void* inMapKey,
00144     const void* inAppKey) const
00145   {
00146     MORK_USED_1(ev);
00147     const morkBookAtom* key = *(const morkBookAtom**) inMapKey;
00148     if ( key )
00149     {
00150       mork_bool hit = key->EqualAid(*(const morkBookAtom**) inAppKey);
00151       return ( hit ) ? morkTest_kHit : morkTest_kMiss;
00152     }
00153     else
00154       return morkTest_kVoid;
00155   }
00156 
00157   /*virtual*/ mork_u4 // hit(a,b) implies hash(a) == hash(b)
00158   morkAtomAidMap::MapHash(morkEnv* ev, const void* inAppKey) const
00159   {
00160     const morkBookAtom* key = *(const morkBookAtom**) inAppKey;
00161     if ( key )
00162       return key->HashAid();
00163     else
00164     {
00165       ev->NilPointerWarning();
00166       return 0;
00167     }
00168   }
00169 
00170   /*virtual*/ mork_u4 
00171   morkAtomAidMap::ProbeMapHashMapKey(morkEnv* ev,
00172     const void* inMapKey) const
00173   {
00174     const morkBookAtom* key = *(const morkBookAtom**) inMapKey;
00175     if ( key )
00176       return key->HashAid();
00177     else
00178     {
00179       ev->NilPointerWarning();
00180       return 0;
00181     }
00182   }
00183 #else /*MORK_ENABLE_PROBE_MAPS*/
00184   // { ===== begin morkMap poly interface =====
00185   /*virtual*/ mork_bool // 
00186   morkAtomAidMap::Equal(morkEnv* ev, const void* inKeyA,
00187     const void* inKeyB) const
00188   {
00189     MORK_USED_1(ev);
00190     return (*(const morkBookAtom**) inKeyA)->EqualAid(
00191       *(const morkBookAtom**) inKeyB);
00192   }
00193 
00194   /*virtual*/ mork_u4 // 
00195   morkAtomAidMap::Hash(morkEnv* ev, const void* inKey) const
00196   {
00197     MORK_USED_1(ev);
00198     return (*(const morkBookAtom**) inKey)->HashAid();
00199   }
00200   // } ===== end morkMap poly interface =====
00201 #endif /*MORK_ENABLE_PROBE_MAPS*/
00202 
00203 
00204 mork_bool
00205 morkAtomAidMap::AddAtom(morkEnv* ev, morkBookAtom* ioAtom)
00206 {
00207   if ( ev->Good() )
00208   {
00209 #ifdef MORK_ENABLE_PROBE_MAPS
00210     this->MapAtPut(ev, &ioAtom, /*val*/ (void*) 0, 
00211       /*key*/ (void*) 0, /*val*/ (void*) 0);
00212 #else /*MORK_ENABLE_PROBE_MAPS*/
00213     this->Put(ev, &ioAtom, /*val*/ (void*) 0, 
00214       /*key*/ (void*) 0, /*val*/ (void*) 0, (mork_change**) 0);
00215 #endif /*MORK_ENABLE_PROBE_MAPS*/
00216   }
00217   return ev->Good();
00218 }
00219 
00220 morkBookAtom*
00221 morkAtomAidMap::CutAtom(morkEnv* ev, const morkBookAtom* inAtom)
00222 {
00223   morkBookAtom* oldKey = 0;
00224   
00225 #ifdef MORK_ENABLE_PROBE_MAPS
00226   MORK_USED_1(inAtom);
00227   morkProbeMap::ProbeMapCutError(ev);
00228 #else /*MORK_ENABLE_PROBE_MAPS*/
00229   this->Cut(ev, &inAtom, &oldKey, /*val*/ (void*) 0,
00230     (mork_change**) 0);
00231 #endif /*MORK_ENABLE_PROBE_MAPS*/
00232     
00233   return oldKey;
00234 }
00235 
00236 morkBookAtom*
00237 morkAtomAidMap::GetAtom(morkEnv* ev, const morkBookAtom* inAtom)
00238 {
00239   morkBookAtom* key = 0; // old val in the map
00240 
00241 #ifdef MORK_ENABLE_PROBE_MAPS
00242   this->MapAt(ev, &inAtom, &key, /*val*/ (void*) 0);
00243 #else /*MORK_ENABLE_PROBE_MAPS*/
00244   this->Get(ev, &inAtom, &key, /*val*/ (void*) 0, (mork_change**) 0);
00245 #endif /*MORK_ENABLE_PROBE_MAPS*/
00246   
00247   return key;
00248 }
00249 
00250 morkBookAtom*
00251 morkAtomAidMap::GetAid(morkEnv* ev, mork_aid inAid)
00252 {
00253   morkWeeBookAtom weeAtom(inAid);
00254   morkBookAtom* key = &weeAtom; // we need a pointer
00255   morkBookAtom* oldKey = 0; // old key in the map
00256 
00257 #ifdef MORK_ENABLE_PROBE_MAPS
00258   this->MapAt(ev, &key, &oldKey, /*val*/ (void*) 0);
00259 #else /*MORK_ENABLE_PROBE_MAPS*/
00260   this->Get(ev, &key, &oldKey, /*val*/ (void*) 0, (mork_change**) 0);
00261 #endif /*MORK_ENABLE_PROBE_MAPS*/
00262   
00263   return oldKey;
00264 }
00265 
00266 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00267 
00268 
00269 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00270 
00271 // ````` ````` ````` ````` ````` 
00272 // { ===== begin morkNode interface =====
00273 
00274 /*public virtual*/ void
00275 morkAtomBodyMap::CloseMorkNode(morkEnv* ev) // CloseAtomBodyMap() only if open
00276 {
00277   if ( this->IsOpenNode() )
00278   {
00279     this->MarkClosing();
00280     this->CloseAtomBodyMap(ev);
00281     this->MarkShut();
00282   }
00283 }
00284 
00285 /*public virtual*/
00286 morkAtomBodyMap::~morkAtomBodyMap() // assert CloseAtomBodyMap() executed earlier
00287 {
00288   MORK_ASSERT(this->IsShutNode());
00289 }
00290 
00291 
00292 /*public non-poly*/
00293 morkAtomBodyMap::morkAtomBodyMap(morkEnv* ev, const morkUsage& inUsage,
00294     nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap)
00295 #ifdef MORK_ENABLE_PROBE_MAPS
00296 : morkProbeMap(ev, inUsage,  ioHeap,
00297   /*inKeySize*/ sizeof(morkBookAtom*), /*inValSize*/ 0,
00298   ioSlotHeap, morkAtomBodyMap_kStartSlotCount, 
00299   /*inZeroIsClearKey*/ morkBool_kTrue)
00300 #else /*MORK_ENABLE_PROBE_MAPS*/
00301 : morkMap(ev, inUsage,  ioHeap,
00302   /*inKeySize*/ sizeof(morkBookAtom*), /*inValSize*/ 0,
00303   morkAtomBodyMap_kStartSlotCount, ioSlotHeap,
00304   /*inHoldChanges*/ morkBool_kFalse)
00305 #endif /*MORK_ENABLE_PROBE_MAPS*/
00306 {
00307   if ( ev->Good() )
00308     mNode_Derived = morkDerived_kAtomBodyMap;
00309 }
00310 
00311 /*public non-poly*/ void
00312 morkAtomBodyMap::CloseAtomBodyMap(morkEnv* ev) // called by CloseMorkNode();
00313 {
00314   if ( this )
00315   {
00316     if ( this->IsNode() )
00317     {
00318 #ifdef MORK_ENABLE_PROBE_MAPS
00319       this->CloseProbeMap(ev);
00320 #else /*MORK_ENABLE_PROBE_MAPS*/
00321       this->CloseMap(ev);
00322 #endif /*MORK_ENABLE_PROBE_MAPS*/
00323       this->MarkShut();
00324     }
00325     else
00326       this->NonNodeError(ev);
00327   }
00328   else
00329     ev->NilPointerError();
00330 }
00331 
00332 // } ===== end morkNode methods =====
00333 // ````` ````` ````` ````` ````` 
00334 #ifdef MORK_ENABLE_PROBE_MAPS
00335 
00336   /*virtual*/ mork_test // hit(a,b) implies hash(a) == hash(b)
00337   morkAtomBodyMap::MapTest(morkEnv* ev, const void* inMapKey,
00338     const void* inAppKey) const
00339   {
00340     const morkBookAtom* key = *(const morkBookAtom**) inMapKey;
00341     if ( key )
00342     {
00343       return ( key->EqualFormAndBody(ev, *(const morkBookAtom**) inAppKey) ) ?
00344         morkTest_kHit : morkTest_kMiss;
00345     }
00346     else
00347       return morkTest_kVoid;
00348   }
00349 
00350   /*virtual*/ mork_u4 // hit(a,b) implies hash(a) == hash(b)
00351   morkAtomBodyMap::MapHash(morkEnv* ev, const void* inAppKey) const
00352   {
00353     const morkBookAtom* key = *(const morkBookAtom**) inAppKey;
00354     if ( key )
00355       return key->HashFormAndBody(ev);
00356     else
00357       return 0;
00358   }
00359 
00360   /*virtual*/ mork_u4 
00361   morkAtomBodyMap::ProbeMapHashMapKey(morkEnv* ev, const void* inMapKey) const
00362   {
00363     const morkBookAtom* key = *(const morkBookAtom**) inMapKey;
00364     if ( key )
00365       return key->HashFormAndBody(ev);
00366     else
00367       return 0;
00368   }
00369 #else /*MORK_ENABLE_PROBE_MAPS*/
00370   // { ===== begin morkMap poly interface =====
00371   /*virtual*/ mork_bool // 
00372   morkAtomBodyMap::Equal(morkEnv* ev, const void* inKeyA,
00373     const void* inKeyB) const
00374   {
00375     return (*(const morkBookAtom**) inKeyA)->EqualFormAndBody(ev,
00376       *(const morkBookAtom**) inKeyB);
00377   }
00378 
00379   /*virtual*/ mork_u4 // 
00380   morkAtomBodyMap::Hash(morkEnv* ev, const void* inKey) const
00381   {
00382     return (*(const morkBookAtom**) inKey)->HashFormAndBody(ev);
00383   }
00384   // } ===== end morkMap poly interface =====
00385 #endif /*MORK_ENABLE_PROBE_MAPS*/
00386 
00387 
00388 mork_bool
00389 morkAtomBodyMap::AddAtom(morkEnv* ev, morkBookAtom* ioAtom)
00390 {
00391   if ( ev->Good() )
00392   {
00393 #ifdef MORK_ENABLE_PROBE_MAPS
00394     this->MapAtPut(ev, &ioAtom, /*val*/ (void*) 0, 
00395       /*key*/ (void*) 0, /*val*/ (void*) 0);
00396 #else /*MORK_ENABLE_PROBE_MAPS*/
00397     this->Put(ev, &ioAtom, /*val*/ (void*) 0, 
00398       /*key*/ (void*) 0, /*val*/ (void*) 0, (mork_change**) 0);
00399 #endif /*MORK_ENABLE_PROBE_MAPS*/
00400   }
00401   return ev->Good();
00402 }
00403 
00404 morkBookAtom*
00405 morkAtomBodyMap::CutAtom(morkEnv* ev, const morkBookAtom* inAtom)
00406 {
00407   morkBookAtom* oldKey = 0;
00408 
00409 #ifdef MORK_ENABLE_PROBE_MAPS
00410   MORK_USED_1(inAtom);
00411   morkProbeMap::ProbeMapCutError(ev);
00412 #else /*MORK_ENABLE_PROBE_MAPS*/
00413   this->Cut(ev, &inAtom, &oldKey, /*val*/ (void*) 0,
00414     (mork_change**) 0);
00415 #endif /*MORK_ENABLE_PROBE_MAPS*/
00416     
00417   return oldKey;
00418 }
00419 
00420 morkBookAtom*
00421 morkAtomBodyMap::GetAtom(morkEnv* ev, const morkBookAtom* inAtom)
00422 {
00423   morkBookAtom* key = 0; // old val in the map
00424 #ifdef MORK_ENABLE_PROBE_MAPS
00425   this->MapAt(ev, &inAtom, &key, /*val*/ (void*) 0);
00426 #else /*MORK_ENABLE_PROBE_MAPS*/
00427   this->Get(ev, &inAtom, &key, /*val*/ (void*) 0, (mork_change**) 0);
00428 #endif /*MORK_ENABLE_PROBE_MAPS*/
00429   
00430   return key;
00431 }
00432 
00433 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00434 
00435 morkAtomRowMap::~morkAtomRowMap()
00436 {
00437 }
00438 
00439 // I changed to sizeof(mork_ip) from sizeof(mork_aid) to fix a crash on
00440 // 64 bit machines.  I am not sure it was the right way to fix the problem,
00441 // but it does stop the crash.  Perhaps we should be using the
00442 // morkPointerMap instead?
00443 morkAtomRowMap::morkAtomRowMap(morkEnv* ev, const morkUsage& inUsage,
00444   nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap, mork_column inIndexColumn)
00445   : morkIntMap(ev, inUsage, sizeof(mork_ip), ioHeap, ioSlotHeap,
00446     /*inHoldChanges*/ morkBool_kFalse)
00447 , mAtomRowMap_IndexColumn( inIndexColumn )
00448 {
00449   if ( ev->Good() )
00450     mNode_Derived = morkDerived_kAtomRowMap;
00451 }
00452 
00453 void morkAtomRowMap::AddRow(morkEnv* ev, morkRow* ioRow)
00454 // add ioRow only if it contains a cell in mAtomRowMap_IndexColumn. 
00455 {
00456   mork_aid aid = ioRow->GetCellAtomAid(ev, mAtomRowMap_IndexColumn);
00457   if ( aid )
00458     this->AddAid(ev, aid, ioRow);
00459 }
00460 
00461 void morkAtomRowMap::CutRow(morkEnv* ev, morkRow* ioRow)
00462 // cut ioRow only if it contains a cell in mAtomRowMap_IndexColumn. 
00463 {
00464   mork_aid aid = ioRow->GetCellAtomAid(ev, mAtomRowMap_IndexColumn);
00465   if ( aid )
00466     this->CutAid(ev, aid);
00467 }
00468 
00469 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789