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.

PageChannel.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. Copyright (c) 2005 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.Flushable;
  15. import java.io.IOException;
  16. import java.nio.ByteBuffer;
  17. import java.nio.ByteOrder;
  18. import java.nio.channels.Channel;
  19. import java.nio.channels.FileChannel;
  20. /**
  21. * Reads and writes individual pages in a database file
  22. * @author Tim McCune
  23. */
  24. public class PageChannel implements Channel, Flushable {
  25. static final int INVALID_PAGE_NUMBER = -1;
  26. /** default byte order of access mdb files */
  27. public static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
  28. /** invalid page header, used when deallocating old pages. data pages
  29. generally have 4 interesting bytes at the beginning which we want to
  30. reset. */
  31. private static final byte[] INVALID_PAGE_BYTE_HEADER =
  32. new byte[]{PageTypes.INVALID, (byte)0, (byte)0, (byte)0};
  33. /** Global usage map always lives on page 1 */
  34. static final int PAGE_GLOBAL_USAGE_MAP = 1;
  35. /** Global usage map always lives at row 0 */
  36. static final int ROW_GLOBAL_USAGE_MAP = 0;
  37. /** Channel containing the database */
  38. private final FileChannel _channel;
  39. /** whether or not the _channel should be closed by this class */
  40. private final boolean _closeChannel;
  41. /** Format of the database in the channel */
  42. private final JetFormat _format;
  43. /** whether or not to force all writes to disk immediately */
  44. private final boolean _autoSync;
  45. /** buffer used when deallocating old pages. data pages generally have 4
  46. interesting bytes at the beginning which we want to reset. */
  47. private final ByteBuffer _invalidPageBytes =
  48. ByteBuffer.wrap(INVALID_PAGE_BYTE_HEADER);
  49. /** dummy buffer used when allocating new pages */
  50. private final ByteBuffer _forceBytes = ByteBuffer.allocate(1);
  51. /** Tracks free pages in the database. */
  52. private UsageMap _globalUsageMap;
  53. /** handler for the current database encoding type */
  54. private CodecHandler _codecHandler = DefaultCodecProvider.DUMMY_HANDLER;
  55. /** temp page buffer used when pages cannot be partially encoded */
  56. private TempPageHolder _fullPageEncodeBufferH;
  57. private TempBufferHolder _tempDecodeBufferH;
  58. private int _writeCount;
  59. /**
  60. * Only used by unit tests
  61. */
  62. protected PageChannel(boolean testing) {
  63. if(!testing) {
  64. throw new IllegalArgumentException();
  65. }
  66. _channel = null;
  67. _closeChannel = false;
  68. _format = JetFormat.VERSION_4;
  69. _autoSync = false;
  70. }
  71. /**
  72. * @param channel Channel containing the database
  73. * @param format Format of the database in the channel
  74. */
  75. public PageChannel(FileChannel channel, boolean closeChannel,
  76. JetFormat format, boolean autoSync)
  77. throws IOException
  78. {
  79. _channel = channel;
  80. _closeChannel = closeChannel;
  81. _format = format;
  82. _autoSync = autoSync;
  83. }
  84. /**
  85. * Does second-stage initialization, must be called after construction.
  86. */
  87. public void initialize(DatabaseImpl database, CodecProvider codecProvider)
  88. throws IOException
  89. {
  90. // initialize page en/decoding support
  91. _codecHandler = codecProvider.createHandler(this, database.getCharset());
  92. if(!_codecHandler.canEncodePartialPage()) {
  93. _fullPageEncodeBufferH =
  94. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  95. }
  96. if(!_codecHandler.canDecodeInline()) {
  97. _tempDecodeBufferH = TempBufferHolder.newHolder(
  98. TempBufferHolder.Type.SOFT, true);
  99. }
  100. // note the global usage map is a special map where any page outside of
  101. // the current range is assumed to be "on"
  102. _globalUsageMap = UsageMap.read(database, PAGE_GLOBAL_USAGE_MAP,
  103. ROW_GLOBAL_USAGE_MAP, true);
  104. }
  105. public JetFormat getFormat() {
  106. return _format;
  107. }
  108. public boolean isAutoSync() {
  109. return _autoSync;
  110. }
  111. /**
  112. * Begins a "logical" write operation. See {@link #finishWrite} for more
  113. * details.
  114. */
  115. public void startWrite() {
  116. ++_writeCount;
  117. }
  118. /**
  119. * Begins an exclusive "logical" write operation (throws an exception if
  120. * another write operation is outstanding). See {@link #finishWrite} for
  121. * more details.
  122. */
  123. public void startExclusiveWrite() {
  124. if(_writeCount != 0) {
  125. throw new IllegalArgumentException(
  126. "Another write operation is currently in progress");
  127. }
  128. startWrite();
  129. }
  130. /**
  131. * Completes a "logical" write operation. This method should be called in
  132. * finally block which wraps a logical write operation (which is preceded by
  133. * a {@link #startWrite} call). Logical write operations may be nested. If
  134. * the database is configured for "auto-sync", the channel will be flushed
  135. * when the outermost operation is complete,
  136. */
  137. public void finishWrite() throws IOException {
  138. assertWriting();
  139. if((--_writeCount == 0) && _autoSync) {
  140. flush();
  141. }
  142. }
  143. /**
  144. * Returns {@code true} if a logical write operation is in progress, {@code
  145. * false} otherwise.
  146. */
  147. public boolean isWriting() {
  148. return(_writeCount > 0);
  149. }
  150. /**
  151. * Asserts that a write operation is in progress.
  152. */
  153. private void assertWriting() {
  154. if(!isWriting()) {
  155. throw new IllegalStateException("No write operation in progress");
  156. }
  157. }
  158. /**
  159. * Returns the next page number based on the given file size.
  160. */
  161. private int getNextPageNumber(long size) {
  162. return (int)(size / getFormat().PAGE_SIZE);
  163. }
  164. /**
  165. * Returns the offset for a page within the file.
  166. */
  167. private long getPageOffset(int pageNumber) {
  168. return((long) pageNumber * (long) getFormat().PAGE_SIZE);
  169. }
  170. /**
  171. * Validates that the given pageNumber is valid for this database.
  172. */
  173. private void validatePageNumber(int pageNumber)
  174. throws IOException
  175. {
  176. int nextPageNumber = getNextPageNumber(_channel.size());
  177. if((pageNumber <= INVALID_PAGE_NUMBER) || (pageNumber >= nextPageNumber)) {
  178. throw new IllegalStateException("invalid page number " + pageNumber);
  179. }
  180. }
  181. /**
  182. * @param buffer Buffer to read the page into
  183. * @param pageNumber Number of the page to read in (starting at 0)
  184. */
  185. public void readPage(ByteBuffer buffer, int pageNumber)
  186. throws IOException
  187. {
  188. if(pageNumber == 0) {
  189. readRootPage(buffer);
  190. return;
  191. }
  192. validatePageNumber(pageNumber);
  193. ByteBuffer inPage = buffer;
  194. ByteBuffer outPage = buffer;
  195. if(!_codecHandler.canDecodeInline()) {
  196. inPage = _tempDecodeBufferH.getPageBuffer(this);
  197. outPage.clear();
  198. }
  199. inPage.clear();
  200. int bytesRead = _channel.read(
  201. inPage, (long) pageNumber * (long) getFormat().PAGE_SIZE);
  202. inPage.flip();
  203. if(bytesRead != getFormat().PAGE_SIZE) {
  204. throw new IOException("Failed attempting to read " +
  205. getFormat().PAGE_SIZE + " bytes from page " +
  206. pageNumber + ", only read " + bytesRead);
  207. }
  208. _codecHandler.decodePage(inPage, outPage, pageNumber);
  209. }
  210. /**
  211. * @param buffer Buffer to read the root page into
  212. */
  213. public void readRootPage(ByteBuffer buffer)
  214. throws IOException
  215. {
  216. // special method for reading root page, can be done before PageChannel is
  217. // fully initialized
  218. buffer.clear();
  219. int bytesRead = _channel.read(buffer, 0L);
  220. buffer.flip();
  221. if(bytesRead != getFormat().PAGE_SIZE) {
  222. throw new IOException("Failed attempting to read " +
  223. getFormat().PAGE_SIZE + " bytes from page " +
  224. 0 + ", only read " + bytesRead);
  225. }
  226. // de-mask header (note, page 0 never has additional encoding)
  227. applyHeaderMask(buffer);
  228. }
  229. /**
  230. * Write a page to disk
  231. * @param page Page to write
  232. * @param pageNumber Page number to write the page to
  233. */
  234. public void writePage(ByteBuffer page, int pageNumber) throws IOException {
  235. writePage(page, pageNumber, 0);
  236. }
  237. /**
  238. * Write a page (or part of a page) to disk
  239. * @param page Page to write
  240. * @param pageNumber Page number to write the page to
  241. * @param pageOffset offset within the page at which to start writing the
  242. * page data
  243. */
  244. public void writePage(ByteBuffer page, int pageNumber, int pageOffset)
  245. throws IOException
  246. {
  247. assertWriting();
  248. validatePageNumber(pageNumber);
  249. page.rewind().position(pageOffset);
  250. int writeLen = page.remaining();
  251. if((writeLen + pageOffset) > getFormat().PAGE_SIZE) {
  252. throw new IllegalArgumentException(
  253. "Page buffer is too large, size " + (writeLen + pageOffset));
  254. }
  255. ByteBuffer encodedPage = page;
  256. if(pageNumber == 0) {
  257. // re-mask header
  258. applyHeaderMask(page);
  259. } else {
  260. if(!_codecHandler.canEncodePartialPage()) {
  261. if((pageOffset > 0) && (writeLen < getFormat().PAGE_SIZE)) {
  262. // current codec handler cannot encode part of a page, so need to
  263. // copy the modified part into the current page contents in a temp
  264. // buffer so that we can encode the entire page
  265. ByteBuffer fullPage = _fullPageEncodeBufferH.setPage(
  266. this, pageNumber);
  267. // copy the modified part to the full page
  268. fullPage.position(pageOffset);
  269. fullPage.put(page);
  270. fullPage.rewind();
  271. // reset so we can write the whole page
  272. page = fullPage;
  273. pageOffset = 0;
  274. writeLen = getFormat().PAGE_SIZE;
  275. } else {
  276. _fullPageEncodeBufferH.possiblyInvalidate(pageNumber, null);
  277. }
  278. }
  279. // re-encode page
  280. encodedPage = _codecHandler.encodePage(page, pageNumber, pageOffset);
  281. // reset position/limit in case they were affected by encoding
  282. encodedPage.position(pageOffset).limit(pageOffset + writeLen);
  283. }
  284. try {
  285. _channel.write(encodedPage, (getPageOffset(pageNumber) + pageOffset));
  286. } finally {
  287. if(pageNumber == 0) {
  288. // de-mask header
  289. applyHeaderMask(page);
  290. }
  291. }
  292. }
  293. /**
  294. * Allocates a new page in the database. Data in the page is undefined
  295. * until it is written in a call to {@link #writePage(ByteBuffer,int)}.
  296. */
  297. public int allocateNewPage() throws IOException {
  298. assertWriting();
  299. // this will force the file to be extended with mostly undefined bytes
  300. long size = _channel.size();
  301. if(size >= getFormat().MAX_DATABASE_SIZE) {
  302. throw new IOException("Database is at maximum size " +
  303. getFormat().MAX_DATABASE_SIZE);
  304. }
  305. if((size % getFormat().PAGE_SIZE) != 0L) {
  306. throw new IOException("Database corrupted, file size " + size +
  307. " is not multiple of page size " +
  308. getFormat().PAGE_SIZE);
  309. }
  310. _forceBytes.rewind();
  311. // push the buffer to the end of the page, so that a full page's worth of
  312. // data is written
  313. int pageOffset = (getFormat().PAGE_SIZE - _forceBytes.remaining());
  314. long offset = size + pageOffset;
  315. int pageNumber = getNextPageNumber(size);
  316. // since we are just allocating page space at this point and not writing
  317. // meaningful data, we do _not_ encode the page.
  318. _channel.write(_forceBytes, offset);
  319. _globalUsageMap.removePageNumber(pageNumber);
  320. return pageNumber;
  321. }
  322. /**
  323. * Deallocate a previously used page in the database.
  324. */
  325. public void deallocatePage(int pageNumber) throws IOException {
  326. assertWriting();
  327. validatePageNumber(pageNumber);
  328. // don't write the whole page, just wipe out the header (which should be
  329. // enough to let us know if we accidentally try to use an invalid page)
  330. _invalidPageBytes.rewind();
  331. _channel.write(_invalidPageBytes, getPageOffset(pageNumber));
  332. _globalUsageMap.addPageNumber(pageNumber); //force is done here
  333. }
  334. /**
  335. * @return A newly-allocated buffer that can be passed to readPage
  336. */
  337. public ByteBuffer createPageBuffer() {
  338. return createBuffer(getFormat().PAGE_SIZE);
  339. }
  340. /**
  341. * @return A newly-allocated buffer of the given size and DEFAULT_BYTE_ORDER
  342. * byte order
  343. */
  344. public static ByteBuffer createBuffer(int size) {
  345. return createBuffer(size, DEFAULT_BYTE_ORDER);
  346. }
  347. /**
  348. * @return A newly-allocated buffer of the given size and byte order
  349. */
  350. public static ByteBuffer createBuffer(int size, ByteOrder order) {
  351. return ByteBuffer.allocate(size).order(order);
  352. }
  353. @Override
  354. public void flush() throws IOException {
  355. _channel.force(true);
  356. }
  357. @Override
  358. public void close() throws IOException {
  359. flush();
  360. if(_closeChannel) {
  361. _channel.close();
  362. }
  363. }
  364. @Override
  365. public boolean isOpen() {
  366. return _channel.isOpen();
  367. }
  368. /**
  369. * Applies the XOR mask to the database header in the given buffer.
  370. */
  371. private void applyHeaderMask(ByteBuffer buffer) {
  372. // de/re-obfuscate the header
  373. byte[] headerMask = _format.HEADER_MASK;
  374. for(int idx = 0; idx < headerMask.length; ++idx) {
  375. int pos = idx + _format.OFFSET_MASKED_HEADER;
  376. byte b = (byte)(buffer.get(pos) ^ headerMask[idx]);
  377. buffer.put(pos, b);
  378. }
  379. }
  380. /**
  381. * @return a duplicate of the current buffer narrowed to the given position
  382. * and limit. mark will be set at the current position.
  383. */
  384. public static ByteBuffer narrowBuffer(ByteBuffer buffer, int position,
  385. int limit)
  386. {
  387. return (ByteBuffer)buffer.duplicate()
  388. .order(buffer.order())
  389. .clear()
  390. .limit(limit)
  391. .position(position)
  392. .mark();
  393. }
  394. /**
  395. * Returns a ByteBuffer wrapping the given bytes and configured with the
  396. * default byte order.
  397. */
  398. public static ByteBuffer wrap(byte[] bytes) {
  399. return ByteBuffer.wrap(bytes).order(DEFAULT_BYTE_ORDER);
  400. }
  401. }