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.

TableScanCursor.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright (c) 2013 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.IOException;
  15. import com.healthmarketscience.jackcess.impl.TableImpl.RowState;
  16. /**
  17. * Simple un-indexed cursor.
  18. *
  19. * @author James Ahlborn
  20. */
  21. public class TableScanCursor extends CursorImpl
  22. {
  23. /** first position for the TableScanCursor */
  24. private static final ScanPosition FIRST_SCAN_POSITION =
  25. new ScanPosition(RowIdImpl.FIRST_ROW_ID);
  26. /** last position for the TableScanCursor */
  27. private static final ScanPosition LAST_SCAN_POSITION =
  28. new ScanPosition(RowIdImpl.LAST_ROW_ID);
  29. /** ScanDirHandler for forward traversal */
  30. private final ScanDirHandler _forwardDirHandler =
  31. new ForwardScanDirHandler();
  32. /** ScanDirHandler for backward traversal */
  33. private final ScanDirHandler _reverseDirHandler =
  34. new ReverseScanDirHandler();
  35. /** Cursor over the pages that this table owns */
  36. private final UsageMap.PageCursor _ownedPagesCursor;
  37. public TableScanCursor(TableImpl table) {
  38. super(new IdImpl(table, null), table,
  39. FIRST_SCAN_POSITION, LAST_SCAN_POSITION);
  40. _ownedPagesCursor = table.getOwnedPagesCursor();
  41. }
  42. @Override
  43. protected ScanDirHandler getDirHandler(boolean moveForward) {
  44. return (moveForward ? _forwardDirHandler : _reverseDirHandler);
  45. }
  46. @Override
  47. protected boolean isUpToDate() {
  48. return(super.isUpToDate() && _ownedPagesCursor.isUpToDate());
  49. }
  50. @Override
  51. protected void reset(boolean moveForward) {
  52. _ownedPagesCursor.reset(moveForward);
  53. super.reset(moveForward);
  54. }
  55. @Override
  56. protected void restorePositionImpl(PositionImpl curPos, PositionImpl prevPos)
  57. throws IOException
  58. {
  59. if(!(curPos instanceof ScanPosition) ||
  60. !(prevPos instanceof ScanPosition)) {
  61. throw new IllegalArgumentException(
  62. "Restored positions must be scan positions");
  63. }
  64. _ownedPagesCursor.restorePosition(curPos.getRowId().getPageNumber(),
  65. prevPos.getRowId().getPageNumber());
  66. super.restorePositionImpl(curPos, prevPos);
  67. }
  68. @Override
  69. protected PositionImpl getRowPosition(RowIdImpl rowId) throws IOException
  70. {
  71. return new ScanPosition(rowId);
  72. }
  73. @Override
  74. protected PositionImpl findAnotherPosition(
  75. RowState rowState, PositionImpl curPos, boolean moveForward)
  76. throws IOException
  77. {
  78. ScanDirHandler handler = getDirHandler(moveForward);
  79. // figure out how many rows are left on this page so we can find the
  80. // next row
  81. RowIdImpl curRowId = curPos.getRowId();
  82. TableImpl.positionAtRowHeader(rowState, curRowId);
  83. int currentRowNumber = curRowId.getRowNumber();
  84. // loop until we find the next valid row or run out of pages
  85. while(true) {
  86. currentRowNumber = handler.getAnotherRowNumber(currentRowNumber);
  87. curRowId = new RowIdImpl(curRowId.getPageNumber(), currentRowNumber);
  88. TableImpl.positionAtRowHeader(rowState, curRowId);
  89. if(!rowState.isValid()) {
  90. // load next page
  91. curRowId = new RowIdImpl(handler.getAnotherPageNumber(),
  92. RowIdImpl.INVALID_ROW_NUMBER);
  93. TableImpl.positionAtRowHeader(rowState, curRowId);
  94. if(!rowState.isHeaderPageNumberValid()) {
  95. //No more owned pages. No more rows.
  96. return handler.getEndPosition();
  97. }
  98. // update row count and initial row number
  99. currentRowNumber = handler.getInitialRowNumber(
  100. rowState.getRowsOnHeaderPage());
  101. } else if(!rowState.isDeleted()) {
  102. // we found a valid, non-deleted row, return it
  103. return new ScanPosition(curRowId);
  104. }
  105. }
  106. }
  107. /**
  108. * Handles moving the table scan cursor in a given direction. Separates
  109. * cursor logic from value storage.
  110. */
  111. private abstract class ScanDirHandler extends DirHandler {
  112. public abstract int getAnotherRowNumber(int curRowNumber);
  113. public abstract int getAnotherPageNumber();
  114. public abstract int getInitialRowNumber(int rowsOnPage);
  115. }
  116. /**
  117. * Handles moving the table scan cursor forward.
  118. */
  119. private final class ForwardScanDirHandler extends ScanDirHandler {
  120. @Override
  121. public PositionImpl getBeginningPosition() {
  122. return getFirstPosition();
  123. }
  124. @Override
  125. public PositionImpl getEndPosition() {
  126. return getLastPosition();
  127. }
  128. @Override
  129. public int getAnotherRowNumber(int curRowNumber) {
  130. return curRowNumber + 1;
  131. }
  132. @Override
  133. public int getAnotherPageNumber() {
  134. return _ownedPagesCursor.getNextPage();
  135. }
  136. @Override
  137. public int getInitialRowNumber(int rowsOnPage) {
  138. return -1;
  139. }
  140. }
  141. /**
  142. * Handles moving the table scan cursor backward.
  143. */
  144. private final class ReverseScanDirHandler extends ScanDirHandler {
  145. @Override
  146. public PositionImpl getBeginningPosition() {
  147. return getLastPosition();
  148. }
  149. @Override
  150. public PositionImpl getEndPosition() {
  151. return getFirstPosition();
  152. }
  153. @Override
  154. public int getAnotherRowNumber(int curRowNumber) {
  155. return curRowNumber - 1;
  156. }
  157. @Override
  158. public int getAnotherPageNumber() {
  159. return _ownedPagesCursor.getPreviousPage();
  160. }
  161. @Override
  162. public int getInitialRowNumber(int rowsOnPage) {
  163. return rowsOnPage;
  164. }
  165. }
  166. /**
  167. * Value object which maintains the current position of a TableScanCursor.
  168. */
  169. private static final class ScanPosition extends PositionImpl
  170. {
  171. private final RowIdImpl _rowId;
  172. private ScanPosition(RowIdImpl rowId) {
  173. _rowId = rowId;
  174. }
  175. @Override
  176. public RowIdImpl getRowId() {
  177. return _rowId;
  178. }
  179. @Override
  180. protected boolean equalsImpl(Object o) {
  181. return getRowId().equals(((ScanPosition)o).getRowId());
  182. }
  183. @Override
  184. public String toString() {
  185. return "RowId = " + getRowId();
  186. }
  187. }
  188. }