Source code of Windows XP (NT5)
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.

220 lines
6.0 KiB

  1. /*****************************************************************************
  2. * *
  3. * BTMAPWR.C *
  4. * *
  5. * Copyright (C) Microsoft Corporation 1989 - 1994. *
  6. * All Rights reserved. *
  7. * *
  8. ******************************************************************************
  9. * *
  10. * Module Intent *
  11. * *
  12. * Routines to write btree map files. *
  13. * *
  14. ******************************************************************************
  15. * *
  16. * Current Owner: UNDONE *
  17. * *
  18. *****************************************************************************/
  19. /*****************************************************************************
  20. *
  21. * Revision History: Created 10/20/89 by KevynCT
  22. *
  23. * 08/21/90 JohnSc autodocified
  24. * 3/05/97 erinfox Change errors to HRESULTS
  25. *
  26. *****************************************************************************/
  27. static char s_aszModule[] = __FILE__; /* For error report */
  28. #include <mvopsys.h>
  29. #include <orkin.h>
  30. #include <iterror.h>
  31. #include <misc.h>
  32. #include <wrapstor.h>
  33. #include <_mvutil.h>
  34. /*----------------------------------------------------------------------------*
  35. | Private functions |
  36. *----------------------------------------------------------------------------*/
  37. /***************************************************************************
  38. *
  39. - Function: HmapbtCreateHbt(hbt)
  40. -
  41. * Purpose: Create a HMAPBT index struct of a btree.
  42. *
  43. * ASSUMES
  44. * args IN: hbt - the btree to map
  45. *
  46. * PROMISES
  47. * returns: the map struct
  48. * +++
  49. *
  50. * Method: Traverse leaf nodes of the btree. Store BK and running
  51. * total count of previous keys in the map array.
  52. *
  53. ***************************************************************************/
  54. HMAPBT HmapbtCreateHbt(HBT hbt, PHRESULT phr)
  55. {
  56. QBTHR qbthr;
  57. BK bk;
  58. QCB qcb;
  59. WORD wLevel, cBk;
  60. LONG cKeys;
  61. QMAPBT qb;
  62. QMAPREC qbT;
  63. HANDLE gh;
  64. if ((qbthr = _GLOBALLOCK(hbt)) == NULL)
  65. {
  66. SetErrCode (phr, E_INVALIDARG);
  67. return(NULL);
  68. }
  69. /* If the btree exists but is empty, return an empty map */
  70. if((wLevel = qbthr->bth.cLevels) == 0)
  71. {
  72. gh = _GLOBALALLOC(GMEM_ZEROINIT|GMEM_SHARE| GMEM_MOVEABLE,
  73. LcbFromBk(0));
  74. qb = (QMAPBT) _GLOBALLOCK(gh);
  75. qb->cTotalBk = 0;
  76. _GLOBALUNLOCK(gh);
  77. _GLOBALUNLOCK(hbt);
  78. return gh;
  79. }
  80. --wLevel;
  81. if (qbthr->ghCache == NULL && RcMakeCache( qbthr) != S_OK )
  82. {
  83. exit0:
  84. _GLOBALUNLOCK(hbt);
  85. return NULL;
  86. }
  87. qbthr->qCache = _GLOBALLOCK(qbthr->ghCache);
  88. if ((gh = _GLOBALALLOC(GMEM_ZEROINIT|GMEM_SHARE| GMEM_MOVEABLE,
  89. LcbFromBk(qbthr->bth.bkEOF))) == NULL)
  90. {
  91. exit1:
  92. _GLOBALUNLOCK (qbthr->ghCache);
  93. goto exit0;
  94. }
  95. qb = (QMAPBT) _GLOBALLOCK(gh);
  96. qbT = qb->table;
  97. cBk = 0;
  98. cKeys = 0;
  99. for (bk = qbthr->bth.bkFirst ; ; bk = BkNext( qcb))
  100. {
  101. if (bk == bkNil)
  102. break;
  103. if ((qcb = QFromBk( bk, wLevel, qbthr, phr)) == NULL )
  104. {
  105. _GLOBALUNLOCK(gh);
  106. _GLOBALFREE(gh);
  107. goto exit1;
  108. }
  109. cBk++;
  110. qbT->cPreviousKeys = cKeys;
  111. qbT->bk = bk;
  112. qbT++;
  113. cKeys += qcb->db.cKeys;
  114. }
  115. qb->cTotalBk = cBk;
  116. _GLOBALUNLOCK(gh);
  117. if ((gh = _GLOBALREALLOC(gh, LcbFromBk( cBk), 0)) == NULL)
  118. {
  119. SetErrCode (phr, E_OUTOFMEMORY);
  120. _GLOBALFREE(gh);
  121. goto exit1;
  122. }
  123. _GLOBALUNLOCK(hbt);
  124. return gh;
  125. }
  126. void DestroyHmapbt(HMAPBT hmapbt)
  127. {
  128. if(hmapbt != NULL)
  129. _GLOBALFREE(hmapbt);
  130. }
  131. /*--------------------------------------------------------------------------*
  132. | Public functions |
  133. *--------------------------------------------------------------------------*/
  134. /***************************************************************************
  135. *
  136. - Function: RcCreateBTMapHfs(hfs, hbt, szName)
  137. -
  138. * Purpose: Create and store a btmap index of the btree hbt, putting
  139. * it into a file called szName in the file system hfs.
  140. *
  141. * ASSUMES
  142. * args IN: hfs - file system where lies the btree
  143. * hbt - handle of btree to map
  144. * szName - name of file to store map file in
  145. *
  146. * PROMISES
  147. * returns: rc
  148. * args OUT: hfs - map file is stored in this file system
  149. *
  150. ***************************************************************************/
  151. PUBLIC HRESULT PASCAL FAR RcCreateBTMapHfs(HFS hfs, HBT hbt, LPSTR szName)
  152. {
  153. HF hf;
  154. HMAPBT hmapbt;
  155. QMAPBT qmapbt;
  156. BOOL fSuccess;
  157. LONG lcb;
  158. HRESULT errb;
  159. if((hfs == NULL) || (hbt == NULL))
  160. return (E_INVALIDARG);
  161. if ((hmapbt = HmapbtCreateHbt(hbt, &errb)) == NULL)
  162. return (errb);
  163. if ((hf = HfCreateFileHfs(hfs, szName, fFSOpenReadWrite, &errb)) == hfNil)
  164. {
  165. exit0:
  166. DestroyHmapbt(hmapbt);
  167. return(errb);
  168. }
  169. qmapbt = (QMAPBT) _GLOBALLOCK(hmapbt);
  170. lcb = LcbFromBk(qmapbt->cTotalBk);
  171. FoSeekHf(hf, foNil, wFSSeekSet, &errb);
  172. fSuccess = (LcbWriteHf(hf, (QV)qmapbt, lcb, &errb) == lcb);
  173. _GLOBALUNLOCK(hmapbt);
  174. if(!fSuccess)
  175. {
  176. RcAbandonHf(hf);
  177. goto exit0;
  178. }
  179. if((fSuccess = RcCloseHf (hf)) != S_OK )
  180. {
  181. RcUnlinkFileHfs(hfs, szName);
  182. SetErrCode (&errb, (HRESULT) fSuccess);
  183. goto exit0;
  184. }
  185. DestroyHmapbt(hmapbt);
  186. return (S_OK);
  187. }