Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
8.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: ColCompr.hxx
  7. //
  8. // Contents: Column compressor definitions
  9. //
  10. // Classes: CCompressedCol
  11. // CCompressedColHash
  12. // XCompressFreeVariant
  13. //
  14. // History: 22 Mar 1994 Alanw Created
  15. //
  16. //--------------------------------------------------------------------------
  17. #pragma once
  18. #ifdef DISPLAY_INCLUDES
  19. #pragma message( "#include <" __FILE__ ">..." )
  20. #endif
  21. #include <objcur.hxx>
  22. class CPathStore;
  23. //+-------------------------------------------------------------------------
  24. //
  25. // Class: CCompressedCol
  26. //
  27. // Purpose: An encapsulation of a compressed column
  28. //
  29. // Interface:
  30. //
  31. // Notes:
  32. //
  33. //--------------------------------------------------------------------------
  34. class CCompressedCol
  35. {
  36. public:
  37. enum CompType {
  38. FixedHash=1, // fixed data width hash table
  39. VarHash, // variable data width hash table
  40. PathCompr, // file path name compression
  41. StringVecCompr, // string vector compression
  42. };
  43. CCompressedCol(
  44. VARTYPE vtData,
  45. unsigned cbKey,
  46. CompType ComprType
  47. ) :
  48. DataType( vtData ),
  49. _cbKeyWidth( cbKey ),
  50. _CompressionType( ComprType ) { }
  51. CCompressedCol( ) :
  52. DataType( VT_NULL ),
  53. _cbKeyWidth( sizeof ULONG ),
  54. _CompressionType( (CompType) 0 ) { }
  55. virtual ~CCompressedCol( ) { }
  56. virtual void AddData(PROPVARIANT const * const pvarnt,
  57. ULONG * pKey,
  58. GetValueResult& reIndicator) = 0;
  59. virtual GetValueResult GetData(PROPVARIANT * pvarnt,
  60. VARTYPE PreferredType,
  61. ULONG key = 0,
  62. PROPID prop = 0) = 0;
  63. virtual void FreeVariant(PROPVARIANT * pvarnt) = 0;
  64. virtual ULONG MemUsed(void) = 0;
  65. VARTYPE DataType;
  66. virtual CPathStore * GetPathStore()
  67. {
  68. return 0;
  69. }
  70. virtual BOOL FindData(PROPVARIANT const * const pvarnt,
  71. ULONG & rKey )
  72. {
  73. Win4Assert(!"FindData should not be called!");
  74. return FALSE;
  75. }
  76. protected:
  77. unsigned _cbKeyWidth; // width in row data (0 if compressed and no
  78. // key needed
  79. CompType _CompressionType;
  80. };
  81. //+-------------------------------------------------------------------------
  82. //
  83. // Class: CCompressedColHash
  84. //
  85. // Purpose: A compressed column which uses a hash table for
  86. // redundant value elimination.
  87. //
  88. // Interface: CCompressedCol
  89. //
  90. // Notes: The hash table code is generic and intended for
  91. // any fixed sized data. A hash table may be parameterized
  92. // for any particular data type by supplying the data
  93. // item width, an initial hash table size and optionally
  94. // a hash function. Items in the hash table are uniquely
  95. // identified by their offset in the data area.
  96. //
  97. // Entries are only added to the table, never removed.
  98. // This reduces storage requirements since reference
  99. // counts do not need to be stored.
  100. //
  101. //--------------------------------------------------------------------------
  102. typedef ULONG HASHKEY;
  103. const HASHKEY MAX_HASHKEY = 0xFFFFFFFF;
  104. class CCompressedColHash: public CCompressedCol
  105. {
  106. public:
  107. typedef ULONG (* PFNHASH) (BYTE *, USHORT);
  108. static ULONG DefaultHash(BYTE *pbData, USHORT cbData);
  109. CCompressedColHash(
  110. VARTYPE vtData,
  111. USHORT cbDataWidth,
  112. PFNHASH pfnHashFunction = DefaultHash);
  113. ~CCompressedColHash();
  114. void AddData(PROPVARIANT const * const pvarnt,
  115. ULONG * pKey,
  116. GetValueResult& reIndicator);
  117. GetValueResult GetData(PROPVARIANT * pvarnt,
  118. VARTYPE PreferredType,
  119. ULONG key = 0,
  120. PROPID prop = 0);
  121. void FreeVariant(PROPVARIANT * pvarnt);
  122. ULONG MemUsed(void) {
  123. // return (_pAlloc->MemUsed());
  124. return _cbData;
  125. }
  126. protected:
  127. VOID _AddData(BYTE *pbData, USHORT cbDataSize, ULONG* pKey);
  128. VOID _Rehash(HASHKEY iKey, BYTE *pbData);
  129. VOID _GrowHashTable( void );
  130. HASHKEY* _IndexHashkey( HASHKEY iKey );
  131. ULONG _GetHashIndex( BYTE *pbData, USHORT cbData )
  132. {
  133. ULONG ulHash = _pfnHash( pbData, cbData );
  134. return ulHash%_cHashEntries;
  135. }
  136. void _ClearAll();
  137. HASHKEY* _pHashTable; // pointer to hash table
  138. PBYTE _pDataItems; // pointer to data items
  139. HASHKEY _cDataItems; // number of entries in pDataItems
  140. const USHORT _cbDataWidth; // width of each data item
  141. private:
  142. USHORT _NextHashSize( HASHKEY cItems, USHORT cHash );
  143. PFNHASH _pfnHash; // hash function
  144. USHORT _cHashEntries; // size of hash table
  145. USHORT _maxChain; // maximum hash chain length
  146. USHORT _fGrowthInProgress; // TRUE if _GrowHashTable active
  147. // CLEANCODE - the class should be changed to use CFixedAllocator
  148. ULONG _cbData; // size of allocation for compressed data
  149. PVOID _pData; // pointer to compression data
  150. ULONG _ulMemCounter; // Memory allocation counter
  151. //
  152. // Serialization.
  153. //
  154. CMutexSem _mtxSerialize; // Serialize Table access
  155. };
  156. //+-------------------------------------------------------------------------
  157. //
  158. // Method: CCompressedColHash::_IndexHashkey, protected inline
  159. //
  160. // Synopsis: Index a hash key and return a pointer to the hash
  161. // chain. The data will be found at the return value
  162. // plus sizeof (HASHKEY)
  163. //
  164. // Arguments: [iKey] - lookup key value
  165. //
  166. // Returns: HASHKEY*, pointer to the indexed item
  167. //
  168. // Notes:
  169. //
  170. //--------------------------------------------------------------------------
  171. inline HASHKEY* CCompressedColHash::_IndexHashkey(
  172. HASHKEY iKey
  173. ) {
  174. Win4Assert(iKey > 0 && iKey <= _cDataItems);
  175. return (HASHKEY *) ((BYTE *)_pDataItems +
  176. (iKey - 1) * (sizeof (HASHKEY) + _cbDataWidth));
  177. }
  178. ULONG GuidHash(BYTE *pbData, USHORT cbData);
  179. //+-------------------------------------------------------------------------
  180. //
  181. // Class: XCompressFreeVariant
  182. //
  183. // Purpose: Safe pointer for variants created by compressors
  184. //
  185. // Interface:
  186. //
  187. // Notes: This class assures that variant structures are properly freed
  188. // in case of exception unwinds.
  189. //
  190. //--------------------------------------------------------------------------
  191. class XCompressFreeVariant
  192. {
  193. CCompressedCol * _pCompr;
  194. PROPVARIANT * _pVarnt;
  195. public:
  196. XCompressFreeVariant() :
  197. _pCompr( 0 ),
  198. _pVarnt( 0 )
  199. {
  200. END_CONSTRUCTION( XCompressFreeVariant );
  201. }
  202. ~XCompressFreeVariant( )
  203. {
  204. if (_pCompr)
  205. _pCompr->FreeVariant(_pVarnt);
  206. }
  207. void Set( CCompressedCol * pCompr,
  208. PROPVARIANT * pVarnt
  209. ) {
  210. _pCompr = pCompr;
  211. _pVarnt = pVarnt;
  212. }
  213. };