Back to index

lightning-sunbird  0.9+nobinonly
morkAtomSpace.h
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 _MORKATOMSPACE_
00039 #define _MORKATOMSPACE_ 1
00040 
00041 #ifndef _MORK_
00042 #include "mork.h"
00043 #endif
00044 
00045 #ifndef _MORKNODE_
00046 #include "morkNode.h"
00047 #endif
00048 
00049 #ifndef _MORKSPACE_
00050 #include "morkSpace.h"
00051 #endif
00052 
00053 #ifndef _MORKATOMMAP_
00054 #include "morkAtomMap.h"
00055 #endif
00056 
00057 #ifndef _MORKNODEMAP_
00058 #include "morkNodeMap.h"
00059 #endif
00060 
00061 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00062 
00063 /*| kMinUnderId: the smallest ID we auto-assign to the 'under' namespace
00064 **| reserved for tokens expected to occur very frequently, such as the names
00065 **| of columns.  We reserve single byte ids in the ASCII range to correspond
00066 **| one-to-one to those tokens consisting single ASCII characters (so that
00067 **| this assignment is always known and constant).  So we start at 0x80, and
00068 **| then reserve the upper half of two hex digit ids and all the three hex
00069 **| digit IDs for the 'under' namespace for common tokens.
00070 |*/
00071 #define morkAtomSpace_kMinUnderId 0x80   /* low 7 bits mean byte tokens */
00072 
00073 #define morkAtomSpace_kMaxSevenBitAid 0x7F  /* low seven bit integer ID */
00074 
00075 /*| kMinOverId: the smallest ID we auto-assign to the 'over' namespace that
00076 **| might include very large numbers of tokens that are used infrequently,
00077 **| so that we care less whether the shortest hex representation is used.
00078 **| So we start all IDs for 'over' category tokens at a value range that
00079 **| needs at least four hex digits, so we can reserve three hex digits and
00080 **| shorter for more commonly occuring tokens in the 'under' category.
00081 |*/
00082 #define morkAtomSpace_kMinOverId 0x1000  /* using at least four hex bytes */
00083 
00084 #define morkDerived_kAtomSpace  /*i*/ 0x6153 /* ascii 'aS' */
00085 
00086 #define morkAtomSpace_kColumnScope ((mork_scope) 'c') /* column scope is forever */
00087 
00088 /*| morkAtomSpace:
00089 |*/
00090 class morkAtomSpace : public morkSpace { // 
00091 
00092 // public: // slots inherited from morkSpace (meant to inform only)
00093   // nsIMdbHeap*    mNode_Heap;
00094 
00095   // mork_base      mNode_Base;     // must equal morkBase_kNode
00096   // mork_derived   mNode_Derived;  // depends on specific node subclass
00097   
00098   // mork_access    mNode_Access;   // kOpen, kClosing, kShut, or kDead
00099   // mork_usage     mNode_Usage;    // kHeap, kStack, kMember, kGlobal, kNone
00100   // mork_able      mNode_Mutable;  // can this node be modified?
00101   // mork_load      mNode_Load;     // is this node clean or dirty?
00102   
00103   // mork_uses      mNode_Uses;     // refcount for strong refs
00104   // mork_refs      mNode_Refs;     // refcount for strong refs + weak refs
00105   
00106   // morkStore*  mSpace_Store; // weak ref to containing store
00107   
00108   // mork_bool   mSpace_DoAutoIDs;    // whether db should assign member IDs
00109   // mork_bool   mSpace_HaveDoneAutoIDs; // whether actually auto assigned IDs
00110   // mork_u1     mSpace_Pad[ 2 ];     // pad to u4 alignment
00111 
00112 public: // state is public because the entire Mork system is private
00113 
00114   mork_aid         mAtomSpace_HighUnderId; // high ID in 'under' range
00115   mork_aid         mAtomSpace_HighOverId;  // high ID in 'over' range
00116   
00117   morkAtomAidMap   mAtomSpace_AtomAids; // all atoms in space by ID
00118   morkAtomBodyMap  mAtomSpace_AtomBodies; // all atoms in space by body
00119 
00120 public: // more specific dirty methods for atom space:
00121   void SetAtomSpaceDirty() { this->SetNodeDirty(); }
00122   void SetAtomSpaceClean() { this->SetNodeClean(); }
00123   
00124   mork_bool IsAtomSpaceClean() const { return this->IsNodeClean(); }
00125   mork_bool IsAtomSpaceDirty() const { return this->IsNodeDirty(); }
00126 
00127 // { ===== begin morkNode interface =====
00128 public: // morkNode virtual methods
00129   virtual void CloseMorkNode(morkEnv* ev); // CloseAtomSpace() only if open
00130   virtual ~morkAtomSpace(); // assert that CloseAtomSpace() executed earlier
00131   
00132 public: // morkMap construction & destruction
00133   morkAtomSpace(morkEnv* ev, const morkUsage& inUsage, mork_scope inScope, 
00134     morkStore* ioStore, nsIMdbHeap* ioNodeHeap, nsIMdbHeap* ioSlotHeap);
00135   void CloseAtomSpace(morkEnv* ev); // called by CloseMorkNode();
00136 
00137 public: // dynamic type identification
00138   mork_bool IsAtomSpace() const
00139   { return IsNode() && mNode_Derived == morkDerived_kAtomSpace; }
00140 // } ===== end morkNode methods =====
00141 
00142 public: // typing
00143   void NonAtomSpaceTypeError(morkEnv* ev);
00144 
00145 public: // setup
00146 
00147   mork_bool MarkAllAtomSpaceContentDirty(morkEnv* ev);
00148   // MarkAllAtomSpaceContentDirty() visits every space object and marks 
00149   // them dirty, including every table, row, cell, and atom.  The return
00150   // equals ev->Good(), to show whether any error happened.  This method is
00151   // intended for use in the beginning of a "compress commit" which writes
00152   // all store content, whether dirty or not.  We dirty everything first so
00153   // that later iterations over content can mark things clean as they are
00154   // written, and organize the process of serialization so that objects are
00155   // written only at need (because of being dirty).
00156 
00157 public: // other space methods
00158 
00159   // void ReserveColumnAidCount(mork_count inCount)
00160   // {
00161   //   mAtomSpace_HighUnderId = morkAtomSpace_kMinUnderId + inCount;
00162   //   mAtomSpace_HighOverId = morkAtomSpace_kMinOverId + inCount;
00163   // }
00164 
00165   mork_num CutAllAtoms(morkEnv* ev, morkPool* ioPool);
00166   // CutAllAtoms() puts all the atoms back in the pool.
00167   
00168   morkBookAtom* MakeBookAtomCopyWithAid(morkEnv* ev,
00169      const morkFarBookAtom& inAtom,  mork_aid inAid);
00170   // Make copy of inAtom and put it in both maps, using specified ID.
00171   
00172   morkBookAtom* MakeBookAtomCopy(morkEnv* ev, const morkFarBookAtom& inAtom);
00173   // Make copy of inAtom and put it in both maps, using a new ID as needed.
00174 
00175   mork_aid MakeNewAtomId(morkEnv* ev, morkBookAtom* ioAtom);
00176   // generate an unused atom id.
00177 
00178 public: // typesafe refcounting inlines calling inherited morkNode methods
00179   static void SlotWeakAtomSpace(morkAtomSpace* me,
00180     morkEnv* ev, morkAtomSpace** ioSlot)
00181   { morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00182   
00183   static void SlotStrongAtomSpace(morkAtomSpace* me,
00184     morkEnv* ev, morkAtomSpace** ioSlot)
00185   { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
00186 };
00187 
00188 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00189 
00190 #define morkDerived_kAtomSpaceMap  /*i*/ 0x615A /* ascii 'aZ' */
00191 
00192 /*| morkAtomSpaceMap: maps mork_scope -> morkAtomSpace
00193 |*/
00194 class morkAtomSpaceMap : public morkNodeMap { // for mapping tokens to tables
00195 
00196 public:
00197 
00198   virtual ~morkAtomSpaceMap();
00199   morkAtomSpaceMap(morkEnv* ev, const morkUsage& inUsage,
00200     nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap);
00201 
00202 public: // other map methods
00203 
00204   mork_bool  AddAtomSpace(morkEnv* ev, morkAtomSpace* ioAtomSpace)
00205   { return this->AddNode(ev, ioAtomSpace->SpaceScope(), ioAtomSpace); }
00206   // the AddAtomSpace() boolean return equals ev->Good().
00207 
00208   mork_bool  CutAtomSpace(morkEnv* ev, mork_scope inScope)
00209   { return this->CutNode(ev, inScope); }
00210   // The CutAtomSpace() boolean return indicates whether removal happened. 
00211   
00212   morkAtomSpace*  GetAtomSpace(morkEnv* ev, mork_scope inScope)
00213   { return (morkAtomSpace*) this->GetNode(ev, inScope); }
00214   // Note the returned space does NOT have an increase in refcount for this.
00215 
00216   mork_num CutAllAtomSpaces(morkEnv* ev)
00217   { return this->CutAllNodes(ev); }
00218   // CutAllAtomSpaces() releases all the referenced table values.
00219 };
00220 
00221 class morkAtomSpaceMapIter: public morkMapIter{ // typesafe wrapper class
00222 
00223 public:
00224   morkAtomSpaceMapIter(morkEnv* ev, morkAtomSpaceMap* ioMap)
00225   : morkMapIter(ev, ioMap) { }
00226  
00227   morkAtomSpaceMapIter( ) : morkMapIter()  { }
00228   void InitAtomSpaceMapIter(morkEnv* ev, morkAtomSpaceMap* ioMap)
00229   { this->InitMapIter(ev, ioMap); }
00230    
00231   mork_change*
00232   FirstAtomSpace(morkEnv* ev, mork_scope* outScope, morkAtomSpace** outAtomSpace)
00233   { return this->First(ev, outScope, outAtomSpace); }
00234   
00235   mork_change*
00236   NextAtomSpace(morkEnv* ev, mork_scope* outScope, morkAtomSpace** outAtomSpace)
00237   { return this->Next(ev, outScope, outAtomSpace); }
00238   
00239   mork_change*
00240   HereAtomSpace(morkEnv* ev, mork_scope* outScope, morkAtomSpace** outAtomSpace)
00241   { return this->Here(ev, outScope, outAtomSpace); }
00242   
00243   mork_change*
00244   CutHereAtomSpace(morkEnv* ev, mork_scope* outScope, morkAtomSpace** outAtomSpace)
00245   { return this->CutHere(ev, outScope, outAtomSpace); }
00246 };
00247 
00248 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00249 
00250 #endif /* _MORKATOMSPACE_ */
00251