Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LongValueColumnImpl.java 16KB

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