00001 #ifndef CDBBDBSHARED_PAGED_VARRAY_P_HH 00002 #define CDBBDBSHARED_PAGED_VARRAY_P_HH 00003 00004 // File and Version Information: 00005 // $Id: CdbBdbSPagedVarrayP.ddl,v 1.7 2004/08/06 05:54:25 bartoldu Exp $ 00006 00007 #include "BdbUtil/Bdb.hh" 00008 00009 #include "CdbBdbShared/CdbBdbSLeafNodeP.hh" 00010 #include "CdbBdbShared/CdbBdbSDirectoryNodeP.hh" 00011 00012 #include <iostream> 00013 00014 /// Paged variable size array 00015 /** 00016 * This is a persistent ordered collection of elements of the same type, which 00017 * is specified through the template parameter.. 00018 * 00019 * ATTENTION: Objectivity/DB requires to use explicit template instamtiation 00020 * for each actual type this collection is instantiated with. 00021 * Use convenience macros at the end of thsi file. 00022 */ 00023 template< typename E > 00024 class CdbBdbSPagedVarrayP : public BdbPersObj { 00025 00026 public: 00027 00028 typedef E ElementType; 00029 00030 public: 00031 00032 /// Constructors 00033 /** 00034 * The optional parameters list of this constructr includes: 00035 * 00036 * - the initial number of elements to allocate 00037 * - the number of elements (of type E) per leaf node. 00038 * - the number of links to other nodes per directory node. 00039 * 00040 * If default 0 is passed instead of any of last two paremeters then 00041 * the corresponding default value will be used. The default value is 00042 * calculated from the size of the Objectivity/DB page (see the BOOT 00043 * file of the federation) and the size of elements stored on that 00044 * page. 00045 */ 00046 CdbBdbSPagedVarrayP( d_ULong theInitialSize = 0, 00047 d_ULong theNumPerLeaf = 0, 00048 d_ULong theNumPerDirectory = 0 ); 00049 00050 /// Destructor 00051 00052 virtual ~CdbBdbSPagedVarrayP( ); 00053 00054 /// Get the current size of the array 00055 /** 00056 * Returns the actual size of the array as it was specified either through 00057 * the parameter of the constructor or as a results of subsequent calles 00058 * to "resize" method. 00059 */ 00060 d_ULong size( ) const; 00061 00062 /// Resize the array 00063 /** 00064 * As usually, if this number is less than the current size than the tail 00065 * will be destroyed. If bigger - then default constructor for the remaining 00066 * elements will be called. 00067 * 00068 * @return the completion status 00069 */ 00070 BdbStatus resize( d_ULong theNewSize ); 00071 00072 /// Array subscript operator 00073 /** 00074 * Returns a reference onto an element. Given this reference the referred 00075 * element can be modified/replaced. 00076 * 00077 * WARNING: The result is unpredictable if the elements exceeds 00078 * the array's bounds. 00079 */ 00080 /* 00081 * ATTENTION: Using virtual memory references is unsafe in persistent world 00082 * since the (Objectivity) cache is out of our control. 00083 * It's true for both read and write operations. 00084 * 00085 * This is why we use two explicit (read/write) operations with values 00086 * instead of this subscript operator. The only reason we still have 00087 * this operator here is to attract attention to this problem. 00088 * 00089 * E& operator[]( d_ULong theIndex ); 00090 */ 00091 00092 /// Set a value of an element at specified location 00093 /** 00094 * This method is meant to be used when the element at specified 00095 * location is going to be modified/replaced. 00096 * 00097 * WARNING: The result is unpredictable if the elements exceeds 00098 * the array's bounds. 00099 */ 00100 void setElementAt( d_ULong theIndex, 00101 const E& value ); 00102 00103 /// Get a reference onto an element (const) 00104 /** 00105 * Returns a copy (!) of an element. The returned element is only available 00106 * for read. The copy is returned because there is no guarantee that a page 00107 * where the element is located at will be locked in the virtual memory. 00108 * 00109 * WARNING: The result is unpredictable if the elements exceeds 00110 * the array's bounds. 00111 */ 00112 E elementAt( d_ULong theIndex ) const; 00113 00114 /// Dump the structure and (optionaly) the contents of the array 00115 /** 00116 * The contents of the array is only dumped if specified glag is set up. 00117 */ 00118 void dump( std::ostream& o, 00119 bool dumpContentsFlag = false ) const; 00120 00121 private: 00122 00123 /// Calculate the number of levels for specified size 00124 /** 00125 * The culculation takes into account the parameters of paging 00126 * (number of elements per lef and directory nodes). 00127 */ 00128 d_ULong calculateNumLevels( d_ULong theSize ) const; 00129 00130 /// Get default number of elements per leaf node 00131 /** 00132 */ 00133 d_ULong defaultNumPerLeaf( ) const; 00134 00135 /// Get default number of elements per directory node 00136 /** 00137 */ 00138 d_ULong defaultNumPerDirectory( ) const; 00139 00140 private: 00141 00142 // These two parameters are calculated at the object construction 00143 // and never get changed during the object's lifetime. 00144 00145 d_ULong _numPerLeaf; // Number of elements of type E per LEAF node 00146 d_ULong _numPerDirectory; // Number of links to other nodes from a LEAF node 00147 00148 // The following parameters change depending on the size of the array 00149 // and the values of the above given parameters. 00150 00151 d_ULong _numLevels; // The depth of the indexing tree (0..n) 00152 d_ULong _size; // The actual size of the array. 00153 00154 // The initial data node is only used when the current size of the array 00155 // does not exceed the value of above given "_numPerLeaf" parameter. Otherwise 00156 // its reference is set to 0. 00157 // 00158 // The initial directory node is used in an opposite case. Otherwise its 00159 // reference is set to 0. 00160 00161 BdbRef(CdbBdbSLeafNodeP<E>) _leafNode; // Initial leaf node (optional) 00162 BdbRef(CdbBdbSDirectoryNodeP<E>) _directoryNode; // Initial directory node (optional) 00163 }; 00164 00165 /// Macros for the explicit template instantiation any types of elements. 00166 /** 00167 * The macro will provide all the necessary declarations for arbitrary 00168 * type of elements (including the predefined "primitive" ones). This instantiation 00169 * is required by Objectivity/DDL compilation system to put the new classes into 00170 * the federation's schema. 00171 * 00172 * The actual instantiation for primitive types is done in separate files. 00173 * 00174 * GENERAL NOTE: Be carefull when doing explicit template instantiations 00175 * in Objectivity/DDL API to avoid potential library conflicts 00176 * when the same template is defined in two or more packages 00177 * (libraries)!!! 00178 * 00179 * As a solution put a common instance of a template into one package 00180 * and use it from there. 00181 * 00182 * There are two forms of macros. The first parameter (or group of thoses if 00183 * a value of teh parameter is template itself) of both forms is a type of elements. 00184 * 00185 * The second 'TN' parameter will also introduce new type as required by a user. 00186 * 00187 * NOTE: This macro can't be used to instantiate an array of "ooBoolean" type 00188 * because this type may conflicts with "ooInt8" on some platforms. 00189 */ 00190 #define instantiate_CdbBdbSPagedVarrayP( T , TN ) \ 00191 template class CdbBdbSNodeP < T >; \ 00192 template class CdbBdbSLeafNodeP < T >; \ 00193 template class CdbBdbSDirectoryNodeP< T >; \ 00194 template class CdbBdbSPagedVarrayP < T >; \ 00195 typedef CdbBdbSPagedVarrayP < T > TN 00196 00197 #define EXPLICIT_INSTANTIATE_CdbBdbSPagedVarrayP_1( T ) \ 00198 template class CdbBdbSNodeP < T >; \ 00199 template class CdbBdbSLeafNodeP < T >; \ 00200 template class CdbBdbSDirectoryNodeP< T >; \ 00201 template class CdbBdbSPagedVarrayP < T > 00202 00203 #define EXPLICIT_INSTANTIATE_CdbBdbSPagedVarrayP_2( T1 , T2 ) \ 00204 template class CdbBdbSNodeP < T1 , T2 >; \ 00205 template class CdbBdbSLeafNodeP < T1 , T2 >; \ 00206 template class CdbBdbSDirectoryNodeP< T1 , T2 >; \ 00207 template class CdbBdbSPagedVarrayP < T1 , T2 > 00208 00209 #define EXPLICIT_INSTANTIATE_CdbBdbSPagedVarrayP_3( T1, T2 , T3 ) \ 00210 template class CdbBdbSNodeP < T1 , T2 , T3 >; \ 00211 template class CdbBdbSLeafNodeP < T1 , T2 , T3 >; \ 00212 template class CdbBdbSDirectoryNodeP< T1 , T2 , T3 >; \ 00213 template class CdbBdbSPagedVarrayP < T1 , T2 , T3 > 00214 00215 // Template class implementation 00216 00217 #ifdef BABAR_COMP_INST 00218 #include "CdbBdbShared/CdbBdbSPagedVarrayP.cc" 00219 #endif /* BABAR_COMP_INST */ 00220 00221 #endif /* CDBBDBSHARED_PAGED_VARRAY_P_HH */
1.3-rc3