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.

343 lines
8.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991 - 1992.
  5. //
  6. // File: SORT.HXX
  7. //
  8. // Contents: Key sorting
  9. //
  10. // Classes: CDirectory, CSortChunk, CChunkCursor
  11. //
  12. // History: 12-Jun-91 BartoszM Created
  13. // 19-Jun-91 reviewed
  14. //
  15. //----------------------------------------------------------------------------
  16. #pragma once
  17. #include <keycur.hxx>
  18. #include <dirtree.hxx>
  19. #include <curstk.hxx>
  20. #include <widtab.hxx>
  21. #include "compress.hxx"
  22. #include "occtable.hxx"
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Class: CDirectory
  26. //
  27. // Purpose: Directory of blocks within sort chunk
  28. //
  29. // History: 12-Jun-91 BartoszM Created stub
  30. // 14-Aug-91 BartoszM Implemented
  31. //
  32. //----------------------------------------------------------------------------
  33. class CDirectory
  34. {
  35. public:
  36. inline CDirectory ();
  37. inline ~CDirectory ();
  38. void Init ( unsigned count )
  39. {
  40. _blocks = new CBlock * [count];
  41. _tree.Init ( count );
  42. }
  43. void AddEntry ( CBlock* pBlock, CKeyBuf& key );
  44. CBlock* Seek ( const CKey& key );
  45. void Done() { _tree.Done ( _counter ); }
  46. #ifdef CIEXTMODE
  47. void CiExtDump(void *ciExtSelf);
  48. #endif
  49. private:
  50. unsigned _counter;
  51. CDirTree _tree;
  52. CBlock** _blocks;
  53. };
  54. inline CDirectory::CDirectory () : _counter (0), _blocks (0) {}
  55. inline CDirectory::~CDirectory () { delete _blocks; }
  56. inline CBlock* CDirectory::Seek ( const CKey& key )
  57. {
  58. unsigned i = _tree.Seek ( key );
  59. ciAssert ( i < _counter );
  60. return _blocks [ i ];
  61. }
  62. //+---------------------------------------------------------------------------
  63. //
  64. // Class: CSortChunk
  65. //
  66. // Purpose: sort chunk: A block of memory that contains compressed sorted
  67. // <key, workid, offset> tuples. Keys are prefix compressed,
  68. // property ids are compressed using small number representation,
  69. // workids are represented as a single byte, and offsets are
  70. // compressed using small number representation. The sort chunk
  71. // is divided into cbBlockSize chunks, each of which begins with
  72. // a single byte indicating if this is a continuation key or new
  73. // key, and a full key value. Continuation keys are those that
  74. // were in the previous block with a different workid or offset.
  75. //
  76. // History: 23-May-91: Brianb Created
  77. // 12-Jun-91 BartoszM Rewrote
  78. // 14-Feb-92 AmyA moved CreateRange() in from
  79. // CWordList.
  80. //
  81. //----------------------------------------------------------------------------
  82. class CChunkCursor;
  83. class CKeyCurStack;
  84. class CSortChunk
  85. {
  86. public:
  87. CSortChunk *next;
  88. CSortChunk( CMaxOccTable& maxOccTable );
  89. void Init( const BYTE* pBuf, ULONG cb, WORKID widMax );
  90. ~CSortChunk ();
  91. CChunkCursor* QueryCursor ( INDEXID iid,
  92. const CWidTable& widTable,
  93. WORKID widMax );
  94. CChunkCursor* QueryCursor ( INDEXID iid,
  95. const CWidTable& widTable,
  96. const CKey * pkey,
  97. WORKID widMax );
  98. CBlock * GetBlock() const { return _blocks; }
  99. CDirectory& GetDir() { return _dir; }
  100. unsigned BlockCount() const { return _cBlocks; }
  101. void CreateRange (
  102. COccCurStack & curStk,
  103. const CKey * pkey,
  104. const CKey * pkeyEnd,
  105. INDEXID iid,
  106. const CWidTable& widTable,
  107. WORKID widMax );
  108. #ifdef CIEXTMODE
  109. void CiExtDump(void *ciExtSelf);
  110. #endif
  111. private:
  112. CBlock* _blocks;
  113. unsigned _cBlocks;
  114. //
  115. // _widSingle is set to a specific WorkId if that is the
  116. // *only* workid that shows up in this SortChunk. Otherwise
  117. // it is set to widInvalid;
  118. //
  119. WORKID _widSingle;
  120. CMaxOccTable & _maxOccTable;
  121. CDirectory _dir;
  122. };
  123. //+---------------------------------------------------------------------------
  124. //
  125. // Class: CChunkCursor (scc)
  126. //
  127. // Purpose: Base chunk cursor class.
  128. //
  129. // History: 27-May-92 KyleP Created
  130. //
  131. //----------------------------------------------------------------------------
  132. class CChunkCursor : public CKeyCursor
  133. {
  134. public:
  135. inline CChunkCursor( INDEXID iid, WORKID widMax, CSortChunk * pChunk );
  136. virtual const CKeyBuf *SeekKey( const CKey * pkey ) = 0;
  137. virtual void Init() = 0;
  138. protected:
  139. CSortChunk* _pChunk; // sort chunk
  140. };
  141. //+---------------------------------------------------------------------------
  142. //
  143. // Class: CManyWidChunkCursor (scc)
  144. //
  145. // Purpose: Cursor for extracting information from a chunk
  146. //
  147. // History: 23-May-91 Brianb Created
  148. // 12-Jun-91 BartoszM Rewrote
  149. // 27-Jan-92 AmyA Added Copy Constructor
  150. // 27-Feb-92 AmyA Added HitCount.
  151. // 28-May-92 KyleP Added Wid mapping (compression)
  152. //
  153. //----------------------------------------------------------------------------
  154. class CManyWidChunkCursor : public CChunkCursor
  155. {
  156. friend class CSortChunk;
  157. public:
  158. void Init();
  159. void Init( CBlock * pBlock );
  160. const CKeyBuf *GetKey();
  161. const CKeyBuf *GetNextKey();
  162. const CKeyBuf *SeekKey(const CKey * pkey );
  163. WORKID WorkId();
  164. WORKID NextWorkId();
  165. OCCURRENCE Occurrence();
  166. OCCURRENCE NextOccurrence();
  167. OCCURRENCE MaxOccurrence();
  168. ULONG WorkIdCount();
  169. ULONG OccurrenceCount();
  170. ULONG HitCount();
  171. void RatioFinished ( ULONG& denom, ULONG& num );
  172. #ifdef CIEXTMODE
  173. void CiExtDump(void *ciExtSelf);
  174. #endif
  175. protected:
  176. CManyWidChunkCursor( INDEXID iid,
  177. const CWidTable& widTable,
  178. CSortChunk *pChunk,
  179. WORKID widMax,
  180. CMaxOccTable& maxOccTable );
  181. friend class CWordList;
  182. const CWidTable& _widTable; // wid transl table
  183. WORKID _curFakeWid; // Current fake wid
  184. CMaxOccTable& _maxOccTable; // Max occurrences of wids
  185. CDecompress _decomp;
  186. };
  187. //+---------------------------------------------------------------------------
  188. //
  189. // Class: COneWidChunkCursor (scc)
  190. //
  191. // Purpose: Cursor for extracting information from a chunk
  192. //
  193. // History: 21-May-92 KyleP Created
  194. //
  195. // Notes: This is identical to CManyWidChunkCursor except that it is
  196. // used for chunks which contain only 1 WorkId.
  197. //
  198. //----------------------------------------------------------------------------
  199. class COneWidChunkCursor : public CChunkCursor
  200. {
  201. friend class CSortChunk;
  202. public:
  203. void Init();
  204. const CKeyBuf *GetKey();
  205. const CKeyBuf *GetNextKey();
  206. const CKeyBuf *SeekKey( const CKey * pkey );
  207. WORKID WorkId();
  208. WORKID NextWorkId();
  209. OCCURRENCE Occurrence();
  210. OCCURRENCE NextOccurrence();
  211. OCCURRENCE MaxOccurrence();
  212. ULONG WorkIdCount();
  213. ULONG OccurrenceCount();
  214. ULONG HitCount();
  215. void RatioFinished ( ULONG& denom, ULONG& num );
  216. #ifdef CIEXTMODE
  217. void CiExtDump(void *ciExtSelf);
  218. #endif
  219. protected:
  220. COneWidChunkCursor( INDEXID iid,
  221. const CWidTable& widTable,
  222. WORKID wid,
  223. CSortChunk *pChunk,
  224. WORKID widMax,
  225. CMaxOccTable& maxOccTable );
  226. WORKID _wid;
  227. WORKID _widReal; // _wid is Set to widInvalid when
  228. // NextWorkId() is called and must
  229. // be reset.
  230. WORKID _fakeWid;
  231. CMaxOccTable& _maxOccTable;
  232. COneWidDecompress _decomp;
  233. };
  234. //
  235. // Inline methods
  236. //
  237. //+-------------------------------------------------------------------------
  238. //
  239. // Member: CChunkCursor::CChunkCursor, public
  240. //
  241. // Synopsis: Creates chunk cursor.
  242. //
  243. // Arguments: [iid] -- Index Id
  244. // [pChunk] -- Chunk
  245. //
  246. // History: 20-May-92 KyleP Created
  247. //
  248. //--------------------------------------------------------------------------
  249. inline CChunkCursor::CChunkCursor(
  250. INDEXID iid, WORKID widMax, CSortChunk * pChunk )
  251. : CKeyCursor( iid, widMax ),
  252. _pChunk( pChunk )
  253. {
  254. }