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.

TempPageHolder.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  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 java.nio.ByteBuffer;
  16. /**
  17. * Manages a reference to a page buffer.
  18. *
  19. * @author James Ahlborn
  20. */
  21. public final class TempPageHolder {
  22. private int _pageNumber = PageChannel.INVALID_PAGE_NUMBER;
  23. private final TempBufferHolder _buffer;
  24. /** the last "modification" count of the buffer that this holder observed.
  25. this is tracked so that the page data can be re-read if the underlying
  26. buffer has been discarded since the last page read */
  27. private int _bufferModCount;
  28. private TempPageHolder(TempBufferHolder.Type type) {
  29. _buffer = TempBufferHolder.newHolder(type, false);
  30. _bufferModCount = _buffer.getModCount();
  31. }
  32. /**
  33. * Creates a new TempPageHolder.
  34. * @param type the type of reference desired for any create page buffers
  35. */
  36. public static TempPageHolder newHolder(TempBufferHolder.Type type) {
  37. return new TempPageHolder(type);
  38. }
  39. /**
  40. * @return the currently set page number
  41. */
  42. public int getPageNumber() {
  43. return _pageNumber;
  44. }
  45. /**
  46. * @return the page for the current page number, reading as necessary,
  47. * position and limit are unchanged
  48. */
  49. public ByteBuffer getPage(PageChannel pageChannel)
  50. throws IOException
  51. {
  52. return setPage(pageChannel, _pageNumber, false);
  53. }
  54. /**
  55. * Sets the current page number and returns that page
  56. * @return the page for the new page number, reading as necessary, resets
  57. * position
  58. */
  59. public ByteBuffer setPage(PageChannel pageChannel, int pageNumber)
  60. throws IOException
  61. {
  62. return setPage(pageChannel, pageNumber, true);
  63. }
  64. private ByteBuffer setPage(PageChannel pageChannel, int pageNumber,
  65. boolean rewind)
  66. throws IOException
  67. {
  68. ByteBuffer buffer = _buffer.getPageBuffer(pageChannel);
  69. int modCount = _buffer.getModCount();
  70. if((pageNumber != _pageNumber) || (_bufferModCount != modCount)) {
  71. _pageNumber = pageNumber;
  72. _bufferModCount = modCount;
  73. pageChannel.readPage(buffer, _pageNumber);
  74. } else if(rewind) {
  75. buffer.rewind();
  76. }
  77. return buffer;
  78. }
  79. /**
  80. * Allocates a new buffer in the database (with undefined data) and returns
  81. * a new empty buffer.
  82. */
  83. public ByteBuffer setNewPage(PageChannel pageChannel)
  84. throws IOException
  85. {
  86. // ditch any current data
  87. clear();
  88. // allocate a new page in the database
  89. _pageNumber = pageChannel.allocateNewPage();
  90. // return a new buffer
  91. return _buffer.getPageBuffer(pageChannel);
  92. }
  93. /**
  94. * Forces any current page data to be disregarded (any
  95. * <code>getPage</code>/<code>setPage</code> call must reload page data).
  96. * Does not necessarily release any memory.
  97. */
  98. public void invalidate() {
  99. possiblyInvalidate(_pageNumber, null);
  100. }
  101. /**
  102. * Forces any current page data to be disregarded if it matches the given
  103. * page number (any <code>getPage</code>/<code>setPage</code> call must
  104. * reload page data) and is not the given buffer. Does not necessarily
  105. * release any memory.
  106. */
  107. public void possiblyInvalidate(int modifiedPageNumber,
  108. ByteBuffer modifiedBuffer) {
  109. if(modifiedBuffer == _buffer.getExistingBuffer()) {
  110. // no worries, our buffer was the one modified (or is null, either way
  111. // we'll need to reload)
  112. return;
  113. }
  114. if(modifiedPageNumber == _pageNumber) {
  115. _pageNumber = PageChannel.INVALID_PAGE_NUMBER;
  116. }
  117. }
  118. /**
  119. * Forces any current page data to be disregarded (any
  120. * <code>getPage</code>/<code>setPage</code> call must reload page data) and
  121. * releases any referenced memory.
  122. */
  123. public void clear() {
  124. invalidate();
  125. _buffer.clear();
  126. }
  127. }