Back to index

lightning-sunbird  0.9+nobinonly
morkAtom.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 _MORKATOM_
00039 #define _MORKATOM_ 1
00040 
00041 #ifndef _MORK_
00042 #include "mork.h"
00043 #endif
00044 
00045 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00046 
00047 
00048 #define morkAtom_kMaxByteSize 255 /* max for 8-bit integer */
00049 #define morkAtom_kForeverCellUses 0x0FF /* max for 8-bit integer */
00050 #define morkAtom_kMaxCellUses 0x07F /* max for 7-bit integer */
00051 
00052 #define morkAtom_kKindWeeAnon  'a'  /* means morkWeeAnonAtom subclass */
00053 #define morkAtom_kKindBigAnon  'A'  /* means morkBigAnonAtom subclass */
00054 #define morkAtom_kKindWeeBook  'b'  /* means morkWeeBookAtom subclass */
00055 #define morkAtom_kKindBigBook  'B'  /* means morkBigBookAtom subclass */
00056 #define morkAtom_kKindFarBook  'f'  /* means morkFarBookAtom subclass */
00057 #define morkAtom_kKindRowOid   'r'  /* means morkOidAtom subclass */
00058 #define morkAtom_kKindTableOid 't'  /* means morkOidAtom subclass */
00059 
00060 /*| Atom: .
00061 |*/
00062 class morkAtom { //
00063  
00064 public: 
00065 
00066   mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00067   mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00068   mork_change   mAtom_Change;    // how has this atom been changed?
00069   mork_u1       mAtom_Size;      // only for atoms smaller than 256 bytes
00070 
00071 public: 
00072   morkAtom(mork_aid inAid, mork_u1 inKind);
00073   
00074   mork_bool IsWeeAnon() const { return mAtom_Kind == morkAtom_kKindWeeAnon; }
00075   mork_bool IsBigAnon() const { return mAtom_Kind == morkAtom_kKindBigAnon; }
00076   mork_bool IsWeeBook() const { return mAtom_Kind == morkAtom_kKindWeeBook; }
00077   mork_bool IsBigBook() const { return mAtom_Kind == morkAtom_kKindBigBook; }
00078   mork_bool IsFarBook() const { return mAtom_Kind == morkAtom_kKindFarBook; }
00079   mork_bool IsRowOid() const { return mAtom_Kind == morkAtom_kKindRowOid; }
00080   mork_bool IsTableOid() const { return mAtom_Kind == morkAtom_kKindTableOid; }
00081 
00082   mork_bool IsBook() const { return this->IsWeeBook() || this->IsBigBook(); }
00083 
00084 public: // clean vs dirty
00085 
00086   void SetAtomClean() { mAtom_Change = morkChange_kNil; }
00087   void SetAtomDirty() { mAtom_Change = morkChange_kAdd; }
00088   
00089   mork_bool IsAtomClean() const { return mAtom_Change == morkChange_kNil; }
00090   mork_bool IsAtomDirty() const { return mAtom_Change == morkChange_kAdd; }
00091 
00092 public: // atom space scope if IsBook() is true, or else zero:
00093 
00094   mork_scope GetBookAtomSpaceScope(morkEnv* ev) const;
00095   // zero or book's space's scope
00096 
00097   mork_aid   GetBookAtomAid() const;
00098   // zero or book atom's ID
00099  
00100 public: // empty construction does nothing
00101   morkAtom() { }
00102 
00103 public: // one-byte refcounting, freezing at maximum
00104   void       MakeCellUseForever(morkEnv* ev);
00105   mork_u1    AddCellUse(morkEnv* ev);
00106   mork_u1    CutCellUse(morkEnv* ev);
00107   
00108   mork_bool  IsCellUseForever() const 
00109   { return mAtom_CellUses == morkAtom_kForeverCellUses; }
00110   
00111 private: // warnings
00112 
00113   static void CellUsesUnderflowWarning(morkEnv* ev);
00114 
00115 public: // errors
00116 
00117   static void BadAtomKindError(morkEnv* ev);
00118   static void ZeroAidError(morkEnv* ev);
00119   static void AtomSizeOverflowError(morkEnv* ev);
00120 
00121 public: // yarns
00122   
00123   mork_bool   AsBuf(morkBuf& outBuf) const;
00124   mork_bool   AliasYarn(mdbYarn* outYarn) const;
00125   mork_bool   GetYarn(mdbYarn* outYarn) const;
00126 
00127 private: // copying is not allowed
00128   morkAtom(const morkAtom& other);
00129   morkAtom& operator=(const morkAtom& other);
00130 };
00131 
00132 /*| OidAtom: an atom that references a row or table by identity.
00133 |*/
00134 class morkOidAtom : public morkAtom { //
00135 
00136   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00137   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00138   // mork_change   mAtom_Change;    // how has this atom been changed?
00139   // mork_u1       mAtom_Size;      // NOT USED IN "BIG" format atoms
00140  
00141 public:
00142   mdbOid           mOidAtom_Oid;       // identity of referenced object
00143 
00144 public: // empty construction does nothing
00145   morkOidAtom() { }
00146   void InitRowOidAtom(morkEnv* ev, const mdbOid& inOid);
00147   void InitTableOidAtom(morkEnv* ev, const mdbOid& inOid);
00148 
00149 private: // copying is not allowed
00150   morkOidAtom(const morkOidAtom& other);
00151   morkOidAtom& operator=(const morkOidAtom& other);
00152 };
00153 
00154 /*| WeeAnonAtom: an atom whose content immediately follows morkAtom slots
00155 **| in an inline fashion, so that morkWeeAnonAtom contains both leading
00156 **| atom slots and then the content bytes without further overhead.  Note
00157 **| that charset encoding is not indicated, so zero is implied for Latin1.
00158 **| (Non-Latin1 content must be stored in a morkBigAnonAtom with a charset.)
00159 **|
00160 **|| An anon (anonymous) atom has no identity, with no associated bookkeeping
00161 **| for lookup needed for sharing like a book atom.
00162 **|
00163 **|| A wee anon atom is immediate but not shared with any other users of this
00164 **| atom, so no bookkeeping for sharing is needed.  This means the atom has
00165 **| no ID, because the atom has no identity other than this immediate content,
00166 **| and no hash table is needed to look up this particular atom.  This also
00167 **| applies to the larger format morkBigAnonAtom, which has more slots.
00168 |*/
00169 class morkWeeAnonAtom : public morkAtom { //
00170 
00171   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00172   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00173   // mork_change   mAtom_Change;    // how has this atom been changed?
00174   // mork_u1       mAtom_Size;      // only for atoms smaller than 256 bytes
00175   
00176 public:
00177   mork_u1 mWeeAnonAtom_Body[ 1 ]; // 1st byte of immediate content vector
00178 
00179 public: // empty construction does nothing
00180   morkWeeAnonAtom() { }
00181   void InitWeeAnonAtom(morkEnv* ev, const morkBuf& inBuf);
00182   
00183   // allow extra trailing byte for a null byte:
00184   static mork_size SizeForFill(mork_fill inFill)
00185   { return sizeof(morkWeeAnonAtom) + inFill; }
00186 
00187 private: // copying is not allowed
00188   morkWeeAnonAtom(const morkWeeAnonAtom& other);
00189   morkWeeAnonAtom& operator=(const morkWeeAnonAtom& other);
00190 };
00191 
00192 /*| BigAnonAtom: another immediate atom that cannot be encoded as the smaller
00193 **| morkWeeAnonAtom format because either the size is too great, and/or the
00194 **| charset is not the default zero for Latin1 and must be explicitly noted.
00195 **|
00196 **|| An anon (anonymous) atom has no identity, with no associated bookkeeping
00197 **| for lookup needed for sharing like a book atom.
00198 |*/
00199 class morkBigAnonAtom : public morkAtom { //
00200 
00201   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00202   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00203   // mork_change   mAtom_Change;    // how has this atom been changed?
00204   // mork_u1       mAtom_Size;      // NOT USED IN "BIG" format atoms
00205  
00206 public:
00207   mork_cscode   mBigAnonAtom_Form;      // charset format encoding
00208   mork_size     mBigAnonAtom_Size;      // size of content vector
00209   mork_u1       mBigAnonAtom_Body[ 1 ]; // 1st byte of immed content vector
00210 
00211 public: // empty construction does nothing
00212   morkBigAnonAtom() { }
00213   void InitBigAnonAtom(morkEnv* ev, const morkBuf& inBuf, mork_cscode inForm);
00214   
00215   // allow extra trailing byte for a null byte:
00216   static mork_size SizeForFill(mork_fill inFill)
00217   { return sizeof(morkBigAnonAtom) + inFill; }
00218 
00219 private: // copying is not allowed
00220   morkBigAnonAtom(const morkBigAnonAtom& other);
00221   morkBigAnonAtom& operator=(const morkBigAnonAtom& other);
00222 };
00223 
00224 #define morkBookAtom_kMaxBodySize 1024 /* if larger, cannot be shared */
00225 
00226 /*| BookAtom: the common subportion of wee book atoms and big book atoms that
00227 **| includes the atom ID and the pointer to the space referencing this atom
00228 **| through a hash table.
00229 |*/
00230 class morkBookAtom : public morkAtom { //
00231   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00232   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00233   // mork_change   mAtom_Change;    // how has this atom been changed?
00234   // mork_u1       mAtom_Size;      // only for atoms smaller than 256 bytes
00235   
00236 public:
00237   morkAtomSpace* mBookAtom_Space; // mBookAtom_Space->SpaceScope() is atom scope 
00238   mork_aid       mBookAtom_Id;    // identity token for this shared atom
00239 
00240 public: // empty construction does nothing
00241   morkBookAtom() { }
00242 
00243   static void NonBookAtomTypeError(morkEnv* ev);
00244 
00245 public: // Hash() and Equal() for atom ID maps are same for all subclasses:
00246 
00247   mork_u4 HashAid() const { return mBookAtom_Id; }
00248   mork_bool EqualAid(const morkBookAtom* inAtom) const
00249   { return ( mBookAtom_Id == inAtom->mBookAtom_Id); }
00250 
00251 public: // Hash() and Equal() for atom body maps know about subclasses:
00252   
00253   // YOU CANNOT SUBCLASS morkBookAtom WITHOUT FIXING Hash and Equal METHODS:
00254 
00255   mork_u4 HashFormAndBody(morkEnv* ev) const;
00256   mork_bool EqualFormAndBody(morkEnv* ev, const morkBookAtom* inAtom) const;
00257   
00258 public: // separation from containing space
00259 
00260   void CutBookAtomFromSpace(morkEnv* ev);
00261 
00262 private: // copying is not allowed
00263   morkBookAtom(const morkBookAtom& other);
00264   morkBookAtom& operator=(const morkBookAtom& other);
00265 };
00266 
00267 /*| FarBookAtom: this alternative format for book atoms was introduced
00268 **| in May 2000 in order to support finding atoms in hash tables without
00269 **| first copying the strings from original parsing buffers into a new
00270 **| atom format.  This was consuming too much time.  However, we can
00271 **| use morkFarBookAtom to stage a hash table query, as long as we then
00272 **| fix HashFormAndBody() and EqualFormAndBody() to use morkFarBookAtom
00273 **| correctly.
00274 **|
00275 **|| Note we do NOT intend that instances of morkFarBookAtom will ever
00276 **| be installed in hash tables, because this is not space efficient.
00277 **| We only expect to create temp instances for table lookups.
00278 |*/
00279 class morkFarBookAtom : public morkBookAtom { //
00280   
00281   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00282   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00283   // mork_change   mAtom_Change;    // how has this atom been changed?
00284   // mork_u1       mAtom_Size;      // NOT USED IN "BIG" format atoms
00285 
00286   // morkAtomSpace* mBookAtom_Space; // mBookAtom_Space->SpaceScope() is scope 
00287   // mork_aid       mBookAtom_Id;    // identity token for this shared atom
00288   
00289 public:
00290   mork_cscode   mFarBookAtom_Form;      // charset format encoding
00291   mork_size     mFarBookAtom_Size;      // size of content vector
00292   mork_u1*      mFarBookAtom_Body;      // bytes are elsewere, out of line
00293 
00294 public: // empty construction does nothing
00295   morkFarBookAtom() { }
00296   void InitFarBookAtom(morkEnv* ev, const morkBuf& inBuf,
00297     mork_cscode inForm, morkAtomSpace* ioSpace, mork_aid inAid);
00298   
00299 private: // copying is not allowed
00300   morkFarBookAtom(const morkFarBookAtom& other);
00301   morkFarBookAtom& operator=(const morkFarBookAtom& other);
00302 };
00303 
00304 /*| WeeBookAtom: .
00305 |*/
00306 class morkWeeBookAtom : public morkBookAtom { //
00307   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00308   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00309   // mork_change   mAtom_Change;    // how has this atom been changed?
00310   // mork_u1       mAtom_Size;      // only for atoms smaller than 256 bytes
00311 
00312   // morkAtomSpace* mBookAtom_Space; // mBookAtom_Space->SpaceScope() is scope 
00313   // mork_aid       mBookAtom_Id;    // identity token for this shared atom
00314   
00315 public:
00316   mork_u1     mWeeBookAtom_Body[ 1 ]; // 1st byte of immed content vector
00317 
00318 public: // empty construction does nothing
00319   morkWeeBookAtom() { }
00320   morkWeeBookAtom(mork_aid inAid);
00321   
00322   void InitWeeBookAtom(morkEnv* ev, const morkBuf& inBuf,
00323     morkAtomSpace* ioSpace, mork_aid inAid);
00324   
00325   // allow extra trailing byte for a null byte:
00326   static mork_size SizeForFill(mork_fill inFill)
00327   { return sizeof(morkWeeBookAtom) + inFill; }
00328 
00329 private: // copying is not allowed
00330   morkWeeBookAtom(const morkWeeBookAtom& other);
00331   morkWeeBookAtom& operator=(const morkWeeBookAtom& other);
00332 };
00333 
00334 /*| BigBookAtom: .
00335 |*/
00336 class morkBigBookAtom : public morkBookAtom { //
00337   
00338   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00339   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00340   // mork_change   mAtom_Change;    // how has this atom been changed?
00341   // mork_u1       mAtom_Size;      // NOT USED IN "BIG" format atoms
00342 
00343   // morkAtomSpace* mBookAtom_Space; // mBookAtom_Space->SpaceScope() is scope 
00344   // mork_aid       mBookAtom_Id;    // identity token for this shared atom
00345   
00346 public:
00347   mork_cscode   mBigBookAtom_Form;      // charset format encoding
00348   mork_size     mBigBookAtom_Size;      // size of content vector
00349   mork_u1       mBigBookAtom_Body[ 1 ]; // 1st byte of immed content vector
00350 
00351 public: // empty construction does nothing
00352   morkBigBookAtom() { }
00353   void InitBigBookAtom(morkEnv* ev, const morkBuf& inBuf,
00354     mork_cscode inForm, morkAtomSpace* ioSpace, mork_aid inAid);
00355   
00356   // allow extra trailing byte for a null byte:
00357   static mork_size SizeForFill(mork_fill inFill)
00358   { return sizeof(morkBigBookAtom) + inFill; }
00359 
00360 private: // copying is not allowed
00361   morkBigBookAtom(const morkBigBookAtom& other);
00362   morkBigBookAtom& operator=(const morkBigBookAtom& other);
00363 };
00364 
00365 /*| MaxBookAtom: .
00366 |*/
00367 class morkMaxBookAtom : public morkBigBookAtom { //
00368   
00369   // mork_u1       mAtom_Kind;      // identifies a specific atom subclass
00370   // mork_u1       mAtom_CellUses;  // number of persistent uses in a cell
00371   // mork_change   mAtom_Change;    // how has this atom been changed?
00372   // mork_u1       mAtom_Size;      // NOT USED IN "BIG" format atoms
00373 
00374   // morkAtomSpace* mBookAtom_Space; // mBookAtom_Space->SpaceScope() is scope 
00375   // mork_aid       mBookAtom_Id;    // identity token for this shared atom
00376 
00377   // mork_cscode   mBigBookAtom_Form;      // charset format encoding
00378   // mork_size     mBigBookAtom_Size;      // size of content vector
00379   // mork_u1       mBigBookAtom_Body[ 1 ]; // 1st byte of immed content vector
00380   
00381 public:
00382   mork_u1 mMaxBookAtom_Body[ morkBookAtom_kMaxBodySize + 3 ]; // max bytes
00383 
00384 public: // empty construction does nothing
00385   morkMaxBookAtom() { }
00386   void InitMaxBookAtom(morkEnv* ev, const morkBuf& inBuf,
00387     mork_cscode inForm, morkAtomSpace* ioSpace, mork_aid inAid)
00388   { this->InitBigBookAtom(ev, inBuf, inForm, ioSpace, inAid); }
00389 
00390 private: // copying is not allowed
00391   morkMaxBookAtom(const morkMaxBookAtom& other);
00392   morkMaxBookAtom& operator=(const morkMaxBookAtom& other);
00393 };
00394 
00395 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00396 
00397 #endif /* _MORKATOM_ */
00398