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.

SimpleIndex.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright (c) 2008 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.io.IOException;
  24. import java.util.List;
  25. /**
  26. * Simple implementation of an Access table index
  27. * @author Tim McCune
  28. */
  29. public class SimpleIndex extends Index {
  30. /** data for the single index page. if this data came from multiple pages,
  31. the index is read-only. */
  32. private SimpleDataPage _dataPage;
  33. public SimpleIndex(Table table, int uniqueEntryCount,
  34. int uniqueEntryCountOffset)
  35. {
  36. super(table, uniqueEntryCount, uniqueEntryCountOffset);
  37. }
  38. @Override
  39. protected void updateImpl() throws IOException {
  40. writeDataPage(_dataPage);
  41. }
  42. @Override
  43. protected void readIndexEntries()
  44. throws IOException
  45. {
  46. // find first leaf page
  47. int nextPageNumber = getRootPageNumber();
  48. SimpleDataPage indexPage = null;
  49. while(true) {
  50. indexPage = new SimpleDataPage(nextPageNumber);
  51. readDataPage(indexPage);
  52. if(!indexPage.isLeaf()) {
  53. // FIXME we can't modify this index at this point in time
  54. setReadOnly();
  55. // found another node page
  56. if(!indexPage.getEntries().isEmpty()) {
  57. nextPageNumber = indexPage.getEntries().get(0).getSubPageNumber();
  58. } else {
  59. // try tail page
  60. nextPageNumber = indexPage.getChildTailPageNumber();
  61. }
  62. indexPage = null;
  63. } else {
  64. // found first leaf
  65. break;
  66. }
  67. }
  68. // save the first leaf page
  69. _dataPage = indexPage;
  70. nextPageNumber = indexPage.getNextPageNumber();
  71. _dataPage.setNextPageNumber(INVALID_INDEX_PAGE_NUMBER);
  72. indexPage = null;
  73. // read all leaf pages.
  74. while(nextPageNumber != INVALID_INDEX_PAGE_NUMBER) {
  75. // FIXME we can't modify this index at this point in time
  76. setReadOnly();
  77. // found another one
  78. indexPage = new SimpleDataPage(nextPageNumber);
  79. readDataPage(indexPage);
  80. // since we read all the entries in sort order, we can insert them
  81. // directly into the entries list
  82. _dataPage.getEntries().addAll(indexPage.getEntries());
  83. int totalSize = (_dataPage.getTotalEntrySize() +
  84. indexPage.getTotalEntrySize());
  85. _dataPage.setTotalEntrySize(totalSize);
  86. nextPageNumber = indexPage.getNextPageNumber();
  87. }
  88. // check the entry order, just to be safe
  89. List<Entry> entries = _dataPage.getEntries();
  90. for(int i = 0; i < (entries.size() - 1); ++i) {
  91. Entry e1 = entries.get(i);
  92. Entry e2 = entries.get(i + 1);
  93. if(e1.compareTo(e2) > 0) {
  94. throw new IOException("Unexpected order in index entries, " +
  95. e1 + " is greater than " + e2);
  96. }
  97. }
  98. }
  99. @Override
  100. protected DataPage findDataPage(Entry entry)
  101. throws IOException
  102. {
  103. return _dataPage;
  104. }
  105. @Override
  106. protected DataPage getDataPage(int pageNumber)
  107. throws IOException
  108. {
  109. throw new UnsupportedOperationException();
  110. }
  111. /**
  112. * Simple implementation of a DataPage
  113. */
  114. private static final class SimpleDataPage extends DataPage {
  115. private final int _pageNumber;
  116. private boolean _leaf;
  117. private int _nextPageNumber;
  118. private int _totalEntrySize;
  119. private int _childTailPageNumber;
  120. private List<Entry> _entries;
  121. private SimpleDataPage(int pageNumber) {
  122. _pageNumber = pageNumber;
  123. }
  124. @Override
  125. public int getPageNumber() {
  126. return _pageNumber;
  127. }
  128. @Override
  129. public boolean isLeaf() {
  130. return _leaf;
  131. }
  132. @Override
  133. public void setLeaf(boolean isLeaf) {
  134. _leaf = isLeaf;
  135. }
  136. @Override
  137. public int getPrevPageNumber() { return 0; }
  138. @Override
  139. public void setPrevPageNumber(int pageNumber) {
  140. // ignored
  141. }
  142. @Override
  143. public int getNextPageNumber() {
  144. return _nextPageNumber;
  145. }
  146. @Override
  147. public void setNextPageNumber(int pageNumber) {
  148. _nextPageNumber = pageNumber;
  149. }
  150. @Override
  151. public int getChildTailPageNumber() {
  152. return _childTailPageNumber;
  153. }
  154. @Override
  155. public void setChildTailPageNumber(int pageNumber) {
  156. _childTailPageNumber = pageNumber;
  157. }
  158. @Override
  159. public int getTotalEntrySize() {
  160. return _totalEntrySize;
  161. }
  162. @Override
  163. public void setTotalEntrySize(int totalSize) {
  164. _totalEntrySize = totalSize;
  165. }
  166. @Override
  167. public byte[] getEntryPrefix() {
  168. return EMPTY_PREFIX;
  169. }
  170. @Override
  171. public void setEntryPrefix(byte[] entryPrefix) {
  172. // ignored
  173. }
  174. @Override
  175. public List<Entry> getEntries() {
  176. return _entries;
  177. }
  178. @Override
  179. public void setEntries(List<Entry> entries) {
  180. _entries = entries;
  181. }
  182. @Override
  183. public void addEntry(int idx, Entry entry) {
  184. _entries.add(idx, entry);
  185. _totalEntrySize += entry.size();
  186. }
  187. @Override
  188. public void removeEntry(int idx) {
  189. Entry oldEntry = _entries.remove(idx);
  190. _totalEntrySize -= oldEntry.size();
  191. }
  192. }
  193. }