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.

LongValueColumnImpl.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. Copyright (c) 2014 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 java.nio.ByteBuffer;
  16. import java.nio.ByteOrder;
  17. /**
  18. * ColumnImpl subclass which is used for long value data types.
  19. *
  20. * @author James Ahlborn
  21. * @usage _advanced_class_
  22. */
  23. class LongValueColumnImpl extends ColumnImpl
  24. {
  25. /**
  26. * Long value (LVAL) type that indicates that the value is stored on the
  27. * same page
  28. */
  29. private static final byte LONG_VALUE_TYPE_THIS_PAGE = (byte) 0x80;
  30. /**
  31. * Long value (LVAL) type that indicates that the value is stored on another
  32. * page
  33. */
  34. private static final byte LONG_VALUE_TYPE_OTHER_PAGE = (byte) 0x40;
  35. /**
  36. * Long value (LVAL) type that indicates that the value is stored on
  37. * multiple other pages
  38. */
  39. private static final byte LONG_VALUE_TYPE_OTHER_PAGES = (byte) 0x00;
  40. /**
  41. * Mask to apply the long length in order to get the flag bits (only the
  42. * first 2 bits are type flags).
  43. */
  44. private static final int LONG_VALUE_TYPE_MASK = 0xC0000000;
  45. /** Holds additional info for writing long values */
  46. private LongValueBufferHolder _lvalBufferH;
  47. LongValueColumnImpl(InitArgs args) throws IOException
  48. {
  49. super(args);
  50. }
  51. @Override
  52. public int getOwnedPageCount() {
  53. return ((_lvalBufferH == null) ? 0 : _lvalBufferH.getOwnedPageCount());
  54. }
  55. @Override
  56. void setUsageMaps(UsageMap ownedPages, UsageMap freeSpacePages) {
  57. _lvalBufferH = new UmapLongValueBufferHolder(ownedPages, freeSpacePages);
  58. }
  59. @Override
  60. void postTableLoadInit() throws IOException {
  61. if(_lvalBufferH == null) {
  62. _lvalBufferH = new LegacyLongValueBufferHolder();
  63. }
  64. super.postTableLoadInit();
  65. }
  66. protected int getMaxLengthInUnits() {
  67. return getType().toUnitSize(getType().getMaxSize());
  68. }
  69. @Override
  70. public Object read(byte[] data, ByteOrder order) throws IOException {
  71. switch(getType()) {
  72. case OLE:
  73. if (data.length > 0) {
  74. return readLongValue(data);
  75. }
  76. return null;
  77. case MEMO:
  78. if (data.length > 0) {
  79. return readLongStringValue(data);
  80. }
  81. return null;
  82. default:
  83. throw new RuntimeException(withErrorContext(
  84. "unexpected var length, long value type: " + getType()));
  85. }
  86. }
  87. @Override
  88. protected ByteBuffer writeRealData(Object obj, int remainingRowLength,
  89. ByteOrder order)
  90. throws IOException
  91. {
  92. switch(getType()) {
  93. case OLE:
  94. // should already be "encoded"
  95. break;
  96. case MEMO:
  97. obj = encodeTextValue(obj, 0, getMaxLengthInUnits(), false).array();
  98. break;
  99. default:
  100. throw new RuntimeException(withErrorContext(
  101. "unexpected var length, long value type: " + getType()));
  102. }
  103. // create long value buffer
  104. return writeLongValue(toByteArray(obj), remainingRowLength);
  105. }
  106. /**
  107. * @param lvalDefinition Column value that points to an LVAL record
  108. * @return The LVAL data
  109. */
  110. protected byte[] readLongValue(byte[] lvalDefinition)
  111. throws IOException
  112. {
  113. ByteBuffer def = PageChannel.wrap(lvalDefinition);
  114. int lengthWithFlags = def.getInt();
  115. int length = lengthWithFlags & (~LONG_VALUE_TYPE_MASK);
  116. byte[] rtn = new byte[length];
  117. byte type = (byte)((lengthWithFlags & LONG_VALUE_TYPE_MASK) >>> 24);
  118. if(type == LONG_VALUE_TYPE_THIS_PAGE) {
  119. // inline long value
  120. def.getInt(); //Skip over lval_dp
  121. def.getInt(); //Skip over unknown
  122. int rowLen = def.remaining();
  123. if(rowLen < length) {
  124. // warn the caller, but return whatever we can
  125. LOG.warn(withErrorContext(
  126. "Value may be truncated: expected length " +
  127. length + " found " + rowLen));
  128. rtn = new byte[rowLen];
  129. }
  130. def.get(rtn);
  131. } else {
  132. // long value on other page(s)
  133. if (lvalDefinition.length != getFormat().SIZE_LONG_VALUE_DEF) {
  134. throw new IOException(withErrorContext(
  135. "Expected " + getFormat().SIZE_LONG_VALUE_DEF +
  136. " bytes in long value definition, but found " +
  137. lvalDefinition.length));
  138. }
  139. int rowNum = ByteUtil.getUnsignedByte(def);
  140. int pageNum = ByteUtil.get3ByteInt(def, def.position());
  141. ByteBuffer lvalPage = getPageChannel().createPageBuffer();
  142. switch (type) {
  143. case LONG_VALUE_TYPE_OTHER_PAGE:
  144. {
  145. getPageChannel().readPage(lvalPage, pageNum);
  146. short rowStart = TableImpl.findRowStart(lvalPage, rowNum, getFormat());
  147. short rowEnd = TableImpl.findRowEnd(lvalPage, rowNum, getFormat());
  148. int rowLen = rowEnd - rowStart;
  149. if(rowLen < length) {
  150. // warn the caller, but return whatever we can
  151. LOG.warn(withErrorContext(
  152. "Value may be truncated: expected length " +
  153. length + " found " + rowLen));
  154. rtn = new byte[rowLen];
  155. }
  156. lvalPage.position(rowStart);
  157. lvalPage.get(rtn);
  158. }
  159. break;
  160. case LONG_VALUE_TYPE_OTHER_PAGES:
  161. ByteBuffer rtnBuf = ByteBuffer.wrap(rtn);
  162. int remainingLen = length;
  163. while(remainingLen > 0) {
  164. lvalPage.clear();
  165. getPageChannel().readPage(lvalPage, pageNum);
  166. short rowStart = TableImpl.findRowStart(lvalPage, rowNum, getFormat());
  167. short rowEnd = TableImpl.findRowEnd(lvalPage, rowNum, getFormat());
  168. // read next page information
  169. lvalPage.position(rowStart);
  170. rowNum = ByteUtil.getUnsignedByte(lvalPage);
  171. pageNum = ByteUtil.get3ByteInt(lvalPage);
  172. // update rowEnd and remainingLen based on chunkLength
  173. int chunkLength = (rowEnd - rowStart) - 4;
  174. if(chunkLength > remainingLen) {
  175. rowEnd = (short)(rowEnd - (chunkLength - remainingLen));
  176. chunkLength = remainingLen;
  177. }
  178. remainingLen -= chunkLength;
  179. lvalPage.limit(rowEnd);
  180. rtnBuf.put(lvalPage);
  181. }
  182. break;
  183. default:
  184. throw new IOException(withErrorContext(
  185. "Unrecognized long value type: " + type));
  186. }
  187. }
  188. return rtn;
  189. }
  190. /**
  191. * @param lvalDefinition Column value that points to an LVAL record
  192. * @return The LVAL data
  193. */
  194. private String readLongStringValue(byte[] lvalDefinition)
  195. throws IOException
  196. {
  197. byte[] binData = readLongValue(lvalDefinition);
  198. if(binData == null) {
  199. return null;
  200. }
  201. if(binData.length == 0) {
  202. return "";
  203. }
  204. return decodeTextValue(binData);
  205. }
  206. /**
  207. * Write an LVAL column into a ByteBuffer inline if it fits, otherwise in
  208. * other data page(s).
  209. * @param value Value of the LVAL column
  210. * @return A buffer containing the LVAL definition and (possibly) the column
  211. * value (unless written to other pages)
  212. * @usage _advanced_method_
  213. */
  214. protected ByteBuffer writeLongValue(byte[] value, int remainingRowLength)
  215. throws IOException
  216. {
  217. if(value.length > getType().getMaxSize()) {
  218. throw new IOException(withErrorContext(
  219. "value too big for column, max " +
  220. getType().getMaxSize() + ", got " + value.length));
  221. }
  222. // determine which type to write
  223. byte type = 0;
  224. int lvalDefLen = getFormat().SIZE_LONG_VALUE_DEF;
  225. if(((getFormat().SIZE_LONG_VALUE_DEF + value.length) <= remainingRowLength)
  226. && (value.length <= getFormat().MAX_INLINE_LONG_VALUE_SIZE)) {
  227. type = LONG_VALUE_TYPE_THIS_PAGE;
  228. lvalDefLen += value.length;
  229. } else if(value.length <= getFormat().MAX_LONG_VALUE_ROW_SIZE) {
  230. type = LONG_VALUE_TYPE_OTHER_PAGE;
  231. } else {
  232. type = LONG_VALUE_TYPE_OTHER_PAGES;
  233. }
  234. ByteBuffer def = PageChannel.createBuffer(lvalDefLen);
  235. // take length and apply type to first byte
  236. int lengthWithFlags = value.length | (type << 24);
  237. def.putInt(lengthWithFlags);
  238. if(type == LONG_VALUE_TYPE_THIS_PAGE) {
  239. // write long value inline
  240. def.putInt(0);
  241. def.putInt(0); //Unknown
  242. def.put(value);
  243. } else {
  244. ByteBuffer lvalPage = null;
  245. int firstLvalPageNum = PageChannel.INVALID_PAGE_NUMBER;
  246. byte firstLvalRow = 0;
  247. // write other page(s)
  248. switch(type) {
  249. case LONG_VALUE_TYPE_OTHER_PAGE:
  250. lvalPage = _lvalBufferH.getLongValuePage(value.length);
  251. firstLvalPageNum = _lvalBufferH.getPageNumber();
  252. firstLvalRow = (byte)TableImpl.addDataPageRow(lvalPage, value.length,
  253. getFormat(), 0);
  254. lvalPage.put(value);
  255. getPageChannel().writePage(lvalPage, firstLvalPageNum);
  256. break;
  257. case LONG_VALUE_TYPE_OTHER_PAGES:
  258. ByteBuffer buffer = ByteBuffer.wrap(value);
  259. int remainingLen = buffer.remaining();
  260. buffer.limit(0);
  261. lvalPage = _lvalBufferH.getLongValuePage(remainingLen);
  262. firstLvalPageNum = _lvalBufferH.getPageNumber();
  263. firstLvalRow = (byte)TableImpl.getRowsOnDataPage(lvalPage, getFormat());
  264. int lvalPageNum = firstLvalPageNum;
  265. ByteBuffer nextLvalPage = null;
  266. int nextLvalPageNum = 0;
  267. int nextLvalRowNum = 0;
  268. while(remainingLen > 0) {
  269. lvalPage.clear();
  270. // figure out how much we will put in this page (we need 4 bytes for
  271. // the next page pointer)
  272. int chunkLength = Math.min(getFormat().MAX_LONG_VALUE_ROW_SIZE - 4,
  273. remainingLen);
  274. // figure out if we will need another page, and if so, allocate it
  275. if(chunkLength < remainingLen) {
  276. // force a new page to be allocated for the chunk after this
  277. _lvalBufferH.clear();
  278. nextLvalPage = _lvalBufferH.getLongValuePage(
  279. (remainingLen - chunkLength) + 4);
  280. nextLvalPageNum = _lvalBufferH.getPageNumber();
  281. nextLvalRowNum = TableImpl.getRowsOnDataPage(nextLvalPage,
  282. getFormat());
  283. } else {
  284. nextLvalPage = null;
  285. nextLvalPageNum = 0;
  286. nextLvalRowNum = 0;
  287. }
  288. // add row to this page
  289. TableImpl.addDataPageRow(lvalPage, chunkLength + 4, getFormat(), 0);
  290. // write next page info
  291. lvalPage.put((byte)nextLvalRowNum); // row number
  292. ByteUtil.put3ByteInt(lvalPage, nextLvalPageNum); // page number
  293. // write this page's chunk of data
  294. buffer.limit(buffer.limit() + chunkLength);
  295. lvalPage.put(buffer);
  296. remainingLen -= chunkLength;
  297. // write new page to database
  298. getPageChannel().writePage(lvalPage, lvalPageNum);
  299. // move to next page
  300. lvalPage = nextLvalPage;
  301. lvalPageNum = nextLvalPageNum;
  302. }
  303. break;
  304. default:
  305. throw new IOException(withErrorContext(
  306. "Unrecognized long value type: " + type));
  307. }
  308. // update def
  309. def.put(firstLvalRow);
  310. ByteUtil.put3ByteInt(def, firstLvalPageNum);
  311. def.putInt(0); //Unknown
  312. }
  313. def.flip();
  314. return def;
  315. }
  316. /**
  317. * Writes the header info for a long value page.
  318. */
  319. private void writeLongValueHeader(ByteBuffer lvalPage)
  320. {
  321. lvalPage.put(PageTypes.DATA); //Page type
  322. lvalPage.put((byte) 1); //Unknown
  323. lvalPage.putShort((short)getFormat().DATA_PAGE_INITIAL_FREE_SPACE); //Free space
  324. lvalPage.put((byte) 'L');
  325. lvalPage.put((byte) 'V');
  326. lvalPage.put((byte) 'A');
  327. lvalPage.put((byte) 'L');
  328. lvalPage.putInt(0); //unknown
  329. lvalPage.putShort((short)0); // num rows in page
  330. }
  331. /**
  332. * Manages secondary page buffers for long value writing.
  333. */
  334. private abstract class LongValueBufferHolder
  335. {
  336. /**
  337. * Returns a long value data page with space for data of the given length.
  338. */
  339. public ByteBuffer getLongValuePage(int dataLength) throws IOException {
  340. TempPageHolder lvalBufferH = getBufferHolder();
  341. dataLength = Math.min(dataLength, getFormat().MAX_LONG_VALUE_ROW_SIZE);
  342. ByteBuffer lvalPage = null;
  343. if(lvalBufferH.getPageNumber() != PageChannel.INVALID_PAGE_NUMBER) {
  344. lvalPage = lvalBufferH.getPage(getPageChannel());
  345. if(TableImpl.rowFitsOnDataPage(dataLength, lvalPage, getFormat())) {
  346. // the current page has space
  347. return lvalPage;
  348. }
  349. }
  350. // need new page
  351. return findNewPage(dataLength);
  352. }
  353. protected ByteBuffer findNewPage(int dataLength) throws IOException {
  354. ByteBuffer lvalPage = getBufferHolder().setNewPage(getPageChannel());
  355. writeLongValueHeader(lvalPage);
  356. return lvalPage;
  357. }
  358. public int getOwnedPageCount() {
  359. return 0;
  360. }
  361. /**
  362. * Returns the page number of the current long value data page.
  363. */
  364. public int getPageNumber() {
  365. return getBufferHolder().getPageNumber();
  366. }
  367. /**
  368. * Discards the current the current long value data page.
  369. */
  370. public void clear() throws IOException {
  371. getBufferHolder().clear();
  372. }
  373. protected abstract TempPageHolder getBufferHolder();
  374. }
  375. /**
  376. * Manages a common, shared extra page for long values. This is legacy
  377. * behavior from before it was understood that there were additional usage
  378. * maps for each columns.
  379. */
  380. private final class LegacyLongValueBufferHolder extends LongValueBufferHolder
  381. {
  382. @Override
  383. protected TempPageHolder getBufferHolder() {
  384. return getTable().getLongValueBuffer();
  385. }
  386. }
  387. /**
  388. * Manages the column usage maps for long values.
  389. */
  390. private final class UmapLongValueBufferHolder extends LongValueBufferHolder
  391. {
  392. /** Usage map of pages that this column owns */
  393. private final UsageMap _ownedPages;
  394. /** Usage map of pages that this column owns with free space on them */
  395. private final UsageMap _freeSpacePages;
  396. /** page buffer used to write "long value" data */
  397. private final TempPageHolder _longValueBufferH =
  398. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  399. private UmapLongValueBufferHolder(UsageMap ownedPages,
  400. UsageMap freeSpacePages) {
  401. _ownedPages = ownedPages;
  402. _freeSpacePages = freeSpacePages;
  403. }
  404. @Override
  405. protected TempPageHolder getBufferHolder() {
  406. return _longValueBufferH;
  407. }
  408. @Override
  409. public int getOwnedPageCount() {
  410. return _ownedPages.getPageCount();
  411. }
  412. @Override
  413. protected ByteBuffer findNewPage(int dataLength) throws IOException {
  414. // grab last owned page and check for free space.
  415. ByteBuffer newPage = TableImpl.findFreeRowSpace(
  416. _ownedPages, _freeSpacePages, _longValueBufferH);
  417. if(newPage != null) {
  418. if(TableImpl.rowFitsOnDataPage(dataLength, newPage, getFormat())) {
  419. return newPage;
  420. }
  421. // discard this page and allocate a new one
  422. clear();
  423. }
  424. // nothing found on current pages, need new page
  425. newPage = super.findNewPage(dataLength);
  426. int pageNumber = getPageNumber();
  427. _ownedPages.addPageNumber(pageNumber);
  428. _freeSpacePages.addPageNumber(pageNumber);
  429. return newPage;
  430. }
  431. @Override
  432. public void clear() throws IOException {
  433. int pageNumber = getPageNumber();
  434. if(pageNumber != PageChannel.INVALID_PAGE_NUMBER) {
  435. _freeSpacePages.removePageNumber(pageNumber, true);
  436. }
  437. super.clear();
  438. }
  439. }
  440. }