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.

Table.java 80KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /*
  2. Copyright (c) 2005 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.nio.ByteBuffer;
  25. import java.nio.charset.Charset;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.Collection;
  29. import java.util.Collections;
  30. import java.util.Comparator;
  31. import java.util.HashMap;
  32. import java.util.Iterator;
  33. import java.util.LinkedHashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import org.apache.commons.logging.Log;
  37. import org.apache.commons.logging.LogFactory;
  38. /**
  39. * A single database table
  40. * <p>
  41. * Is not thread-safe.
  42. *
  43. * @author Tim McCune
  44. * @usage _general_class_
  45. */
  46. public class Table
  47. implements Iterable<Map<String, Object>>
  48. {
  49. private static final Log LOG = LogFactory.getLog(Table.class);
  50. private static final short OFFSET_MASK = (short)0x1FFF;
  51. private static final short DELETED_ROW_MASK = (short)0x8000;
  52. private static final short OVERFLOW_ROW_MASK = (short)0x4000;
  53. static final int MAGIC_TABLE_NUMBER = 1625;
  54. private static final int MAX_BYTE = 256;
  55. /**
  56. * Table type code for system tables
  57. * @usage _intermediate_class_
  58. */
  59. public static final byte TYPE_SYSTEM = 0x53;
  60. /**
  61. * Table type code for user tables
  62. * @usage _intermediate_class_
  63. */
  64. public static final byte TYPE_USER = 0x4e;
  65. /**
  66. * enum which controls the ordering of the columns in a table.
  67. * @usage _intermediate_class_
  68. */
  69. public enum ColumnOrder {
  70. /** columns are ordered based on the order of the data in the table (this
  71. order does not change as columns are added to the table). */
  72. DATA,
  73. /** columns are ordered based on the "display" order (this order can be
  74. changed arbitrarily) */
  75. DISPLAY;
  76. }
  77. /** comparator which sorts variable length columns based on their index into
  78. the variable length offset table */
  79. private static final Comparator<Column> VAR_LEN_COLUMN_COMPARATOR =
  80. new Comparator<Column>() {
  81. public int compare(Column c1, Column c2) {
  82. return ((c1.getVarLenTableIndex() < c2.getVarLenTableIndex()) ? -1 :
  83. ((c1.getVarLenTableIndex() > c2.getVarLenTableIndex()) ? 1 :
  84. 0));
  85. }
  86. };
  87. /** comparator which sorts columns based on their display index */
  88. private static final Comparator<Column> DISPLAY_ORDER_COMPARATOR =
  89. new Comparator<Column>() {
  90. public int compare(Column c1, Column c2) {
  91. return ((c1.getDisplayIndex() < c2.getDisplayIndex()) ? -1 :
  92. ((c1.getDisplayIndex() > c2.getDisplayIndex()) ? 1 :
  93. 0));
  94. }
  95. };
  96. /** owning database */
  97. private final Database _database;
  98. /** additional table flags from the catalog entry */
  99. private int _flags;
  100. /** Type of the table (either TYPE_SYSTEM or TYPE_USER) */
  101. private byte _tableType;
  102. /** Number of actual indexes on the table */
  103. private int _indexCount;
  104. /** Number of logical indexes for the table */
  105. private int _logicalIndexCount;
  106. /** Number of rows in the table */
  107. private int _rowCount;
  108. /** last long auto number for the table */
  109. private int _lastLongAutoNumber;
  110. /** last complex type auto number for the table */
  111. private int _lastComplexTypeAutoNumber;
  112. /** page number of the definition of this table */
  113. private final int _tableDefPageNumber;
  114. /** max Number of columns in the table (includes previous deletions) */
  115. private short _maxColumnCount;
  116. /** max Number of variable columns in the table */
  117. private short _maxVarColumnCount;
  118. /** List of columns in this table, ordered by column number */
  119. private List<Column> _columns = new ArrayList<Column>();
  120. /** List of variable length columns in this table, ordered by offset */
  121. private List<Column> _varColumns = new ArrayList<Column>();
  122. /** List of autonumber columns in this table, ordered by column number */
  123. private List<Column> _autoNumColumns;
  124. /** List of indexes on this table (multiple logical indexes may be backed by
  125. the same index data) */
  126. private List<Index> _indexes = new ArrayList<Index>();
  127. /** List of index datas on this table (the actual backing data for an
  128. index) */
  129. private List<IndexData> _indexDatas = new ArrayList<IndexData>();
  130. /** Table name as stored in Database */
  131. private final String _name;
  132. /** Usage map of pages that this table owns */
  133. private UsageMap _ownedPages;
  134. /** Usage map of pages that this table owns with free space on them */
  135. private UsageMap _freeSpacePages;
  136. /** modification count for the table, keeps row-states up-to-date */
  137. private int _modCount;
  138. /** page buffer used to update data pages when adding rows */
  139. private final TempPageHolder _addRowBufferH =
  140. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  141. /** page buffer used to update the table def page */
  142. private final TempPageHolder _tableDefBufferH =
  143. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  144. /** buffer used to writing single rows of data */
  145. private final TempBufferHolder _singleRowBufferH =
  146. TempBufferHolder.newHolder(TempBufferHolder.Type.SOFT, true);
  147. /** "buffer" used to writing multi rows of data (will create new buffer on
  148. every call) */
  149. private final TempBufferHolder _multiRowBufferH =
  150. TempBufferHolder.newHolder(TempBufferHolder.Type.NONE, true);
  151. /** page buffer used to write out-of-line "long value" data */
  152. private final TempPageHolder _longValueBufferH =
  153. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  154. /** "big index support" is optional */
  155. private final boolean _useBigIndex;
  156. /** optional error handler to use when row errors are encountered */
  157. private ErrorHandler _tableErrorHandler;
  158. /** properties for this table */
  159. private PropertyMap _props;
  160. /** properties group for this table (and columns) */
  161. private PropertyMaps _propertyMaps;
  162. /** common cursor for iterating through the table, kept here for historic
  163. reasons */
  164. private Cursor _cursor;
  165. /**
  166. * Only used by unit tests
  167. */
  168. Table(boolean testing, List<Column> columns) throws IOException {
  169. if(!testing) {
  170. throw new IllegalArgumentException();
  171. }
  172. _database = null;
  173. _tableDefPageNumber = PageChannel.INVALID_PAGE_NUMBER;
  174. _name = null;
  175. _useBigIndex = true;
  176. setColumns(columns);
  177. }
  178. /**
  179. * @param database database which owns this table
  180. * @param tableBuffer Buffer to read the table with
  181. * @param pageNumber Page number of the table definition
  182. * @param name Table name
  183. * @param useBigIndex whether or not "big index support" should be enabled
  184. * for the table
  185. */
  186. protected Table(Database database, ByteBuffer tableBuffer,
  187. int pageNumber, String name, int flags, boolean useBigIndex)
  188. throws IOException
  189. {
  190. _database = database;
  191. _tableDefPageNumber = pageNumber;
  192. _name = name;
  193. _flags = flags;
  194. _useBigIndex = useBigIndex;
  195. int nextPage = tableBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
  196. ByteBuffer nextPageBuffer = null;
  197. while (nextPage != 0) {
  198. if (nextPageBuffer == null) {
  199. nextPageBuffer = getPageChannel().createPageBuffer();
  200. }
  201. getPageChannel().readPage(nextPageBuffer, nextPage);
  202. nextPage = nextPageBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
  203. ByteBuffer newBuffer = getPageChannel().createBuffer(
  204. tableBuffer.capacity() + getFormat().PAGE_SIZE - 8);
  205. newBuffer.put(tableBuffer);
  206. newBuffer.put(nextPageBuffer.array(), 8, getFormat().PAGE_SIZE - 8);
  207. tableBuffer = newBuffer;
  208. tableBuffer.flip();
  209. }
  210. readTableDefinition(tableBuffer);
  211. tableBuffer = null;
  212. }
  213. /**
  214. * @return The name of the table
  215. * @usage _general_method_
  216. */
  217. public String getName() {
  218. return _name;
  219. }
  220. /**
  221. * Whether or not this table has been marked as hidden.
  222. * @usage _general_method_
  223. */
  224. public boolean isHidden() {
  225. return((_flags & Database.HIDDEN_OBJECT_FLAG) != 0);
  226. }
  227. /**
  228. * @usage _advanced_method_
  229. */
  230. public boolean doUseBigIndex() {
  231. return _useBigIndex;
  232. }
  233. /**
  234. * @usage _advanced_method_
  235. */
  236. public int getMaxColumnCount() {
  237. return _maxColumnCount;
  238. }
  239. /**
  240. * @usage _general_method_
  241. */
  242. public int getColumnCount() {
  243. return _columns.size();
  244. }
  245. /**
  246. * @usage _general_method_
  247. */
  248. public Database getDatabase() {
  249. return _database;
  250. }
  251. /**
  252. * @usage _advanced_method_
  253. */
  254. public JetFormat getFormat() {
  255. return getDatabase().getFormat();
  256. }
  257. /**
  258. * @usage _advanced_method_
  259. */
  260. public PageChannel getPageChannel() {
  261. return getDatabase().getPageChannel();
  262. }
  263. /**
  264. * Gets the currently configured ErrorHandler (always non-{@code null}).
  265. * This will be used to handle all errors unless overridden at the Cursor
  266. * level.
  267. * @usage _intermediate_method_
  268. */
  269. public ErrorHandler getErrorHandler() {
  270. return((_tableErrorHandler != null) ? _tableErrorHandler :
  271. getDatabase().getErrorHandler());
  272. }
  273. /**
  274. * Sets a new ErrorHandler. If {@code null}, resets to using the
  275. * ErrorHandler configured at the Database level.
  276. * @usage _intermediate_method_
  277. */
  278. public void setErrorHandler(ErrorHandler newErrorHandler) {
  279. _tableErrorHandler = newErrorHandler;
  280. }
  281. public int getTableDefPageNumber() {
  282. return _tableDefPageNumber;
  283. }
  284. /**
  285. * @usage _advanced_method_
  286. */
  287. public RowState createRowState() {
  288. return new RowState(TempBufferHolder.Type.HARD);
  289. }
  290. protected UsageMap.PageCursor getOwnedPagesCursor() {
  291. return _ownedPages.cursor();
  292. }
  293. /**
  294. * Returns the <i>approximate</i> number of database pages owned by this
  295. * table and all related indexes (this number does <i>not</i> take into
  296. * account pages used for large OLE/MEMO fields).
  297. * <p>
  298. * To calculate the approximate number of bytes owned by a table:
  299. * <code>
  300. * int approxTableBytes = (table.getApproximateOwnedPageCount() *
  301. * table.getFormat().PAGE_SIZE);
  302. * </code>
  303. * @usage _intermediate_method_
  304. */
  305. public int getApproximateOwnedPageCount() {
  306. // add a page for the table def (although that might actually be more than
  307. // one page)
  308. int count = _ownedPages.getPageCount() + 1;
  309. // note, we count owned pages from _physical_ indexes, not logical indexes
  310. // (otherwise we could double count pages)
  311. for(IndexData indexData : _indexDatas) {
  312. count += indexData.getOwnedPageCount();
  313. }
  314. return count;
  315. }
  316. protected TempPageHolder getLongValueBuffer() {
  317. return _longValueBufferH;
  318. }
  319. /**
  320. * @return All of the columns in this table (unmodifiable List)
  321. * @usage _general_method_
  322. */
  323. public List<Column> getColumns() {
  324. return Collections.unmodifiableList(_columns);
  325. }
  326. /**
  327. * @return the column with the given name
  328. * @usage _general_method_
  329. */
  330. public Column getColumn(String name) {
  331. for(Column column : _columns) {
  332. if(column.getName().equalsIgnoreCase(name)) {
  333. return column;
  334. }
  335. }
  336. throw new IllegalArgumentException("Column with name " + name +
  337. " does not exist in this table");
  338. }
  339. /**
  340. * Only called by unit tests
  341. */
  342. private void setColumns(List<Column> columns) {
  343. _columns = columns;
  344. int colIdx = 0;
  345. int varLenIdx = 0;
  346. int fixedOffset = 0;
  347. for(Column col : _columns) {
  348. col.setColumnNumber((short)colIdx);
  349. col.setColumnIndex(colIdx++);
  350. if(col.isVariableLength()) {
  351. col.setVarLenTableIndex(varLenIdx++);
  352. _varColumns.add(col);
  353. } else {
  354. col.setFixedDataOffset(fixedOffset);
  355. fixedOffset += col.getType().getFixedSize();
  356. }
  357. }
  358. _maxColumnCount = (short)_columns.size();
  359. _maxVarColumnCount = (short)_varColumns.size();
  360. _autoNumColumns = getAutoNumberColumns(columns);
  361. }
  362. /**
  363. * @return the properties for this table
  364. * @usage _general_method_
  365. */
  366. public PropertyMap getProperties() throws IOException {
  367. if(_props == null) {
  368. _props = getPropertyMaps().getDefault();
  369. }
  370. return _props;
  371. }
  372. /**
  373. * @return all PropertyMaps for this table (and columns)
  374. * @usage _general_method_
  375. */
  376. protected PropertyMaps getPropertyMaps() throws IOException {
  377. if(_propertyMaps == null) {
  378. _propertyMaps = getDatabase().getPropertiesForObject(
  379. _tableDefPageNumber);
  380. }
  381. return _propertyMaps;
  382. }
  383. /**
  384. * @return All of the Indexes on this table (unmodifiable List)
  385. * @usage _intermediate_method_
  386. */
  387. public List<Index> getIndexes() {
  388. return Collections.unmodifiableList(_indexes);
  389. }
  390. /**
  391. * @return the index with the given name
  392. * @throws IllegalArgumentException if there is no index with the given name
  393. * @usage _intermediate_method_
  394. */
  395. public Index getIndex(String name) {
  396. for(Index index : _indexes) {
  397. if(index.getName().equalsIgnoreCase(name)) {
  398. return index;
  399. }
  400. }
  401. throw new IllegalArgumentException("Index with name " + name +
  402. " does not exist on this table");
  403. }
  404. /**
  405. * @return the primary key index for this table
  406. * @throws IllegalArgumentException if there is no primary key index on this
  407. * table
  408. * @usage _intermediate_method_
  409. */
  410. public Index getPrimaryKeyIndex() {
  411. for(Index index : _indexes) {
  412. if(index.isPrimaryKey()) {
  413. return index;
  414. }
  415. }
  416. throw new IllegalArgumentException("Table " + getName() +
  417. " does not have a primary key index");
  418. }
  419. /**
  420. * @return the foreign key index joining this table to the given other table
  421. * @throws IllegalArgumentException if there is no relationship between this
  422. * table and the given table
  423. * @usage _intermediate_method_
  424. */
  425. public Index getForeignKeyIndex(Table otherTable) {
  426. for(Index index : _indexes) {
  427. if(index.isForeignKey() && (index.getReference() != null) &&
  428. (index.getReference().getOtherTablePageNumber() ==
  429. otherTable.getTableDefPageNumber())) {
  430. return index;
  431. }
  432. }
  433. throw new IllegalArgumentException(
  434. "Table " + getName() + " does not have a foreign key reference to " +
  435. otherTable.getName());
  436. }
  437. /**
  438. * @return All of the IndexData on this table (unmodifiable List)
  439. */
  440. List<IndexData> getIndexDatas() {
  441. return Collections.unmodifiableList(_indexDatas);
  442. }
  443. /**
  444. * Only called by unit tests
  445. */
  446. int getLogicalIndexCount() {
  447. return _logicalIndexCount;
  448. }
  449. private Cursor getInternalCursor() {
  450. if(_cursor == null) {
  451. _cursor = Cursor.createCursor(this);
  452. }
  453. return _cursor;
  454. }
  455. /**
  456. * After calling this method, getNextRow will return the first row in the
  457. * table, see {@link Cursor#reset}.
  458. * @usage _general_method_
  459. */
  460. public void reset() {
  461. getInternalCursor().reset();
  462. }
  463. /**
  464. * Delete the current row (retrieved by a call to {@link #getNextRow()}).
  465. * @usage _general_method_
  466. */
  467. public void deleteCurrentRow() throws IOException {
  468. getInternalCursor().deleteCurrentRow();
  469. }
  470. /**
  471. * Delete the row on which the given rowState is currently positioned.
  472. * <p>
  473. * Note, this method is not generally meant to be used directly. You should
  474. * use the {@link #deleteCurrentRow} method or use the Cursor class, which
  475. * allows for more complex table interactions.
  476. * @usage _advanced_method_
  477. */
  478. public void deleteRow(RowState rowState, RowId rowId) throws IOException {
  479. requireValidRowId(rowId);
  480. // ensure that the relevant row state is up-to-date
  481. ByteBuffer rowBuffer = positionAtRowHeader(rowState, rowId);
  482. requireNonDeletedRow(rowState, rowId);
  483. // delete flag always gets set in the "header" row (even if data is on
  484. // overflow row)
  485. int pageNumber = rowState.getHeaderRowId().getPageNumber();
  486. int rowNumber = rowState.getHeaderRowId().getRowNumber();
  487. // use any read rowValues to help update the indexes
  488. Object[] rowValues = (!_indexDatas.isEmpty() ?
  489. rowState.getRowValues() : null);
  490. int rowIndex = getRowStartOffset(rowNumber, getFormat());
  491. rowBuffer.putShort(rowIndex, (short)(rowBuffer.getShort(rowIndex)
  492. | DELETED_ROW_MASK | OVERFLOW_ROW_MASK));
  493. writeDataPage(rowBuffer, pageNumber);
  494. // update the indexes
  495. for(IndexData indexData : _indexDatas) {
  496. indexData.deleteRow(rowValues, rowId);
  497. }
  498. // make sure table def gets updated
  499. updateTableDefinition(-1);
  500. }
  501. /**
  502. * @return The next row in this table (Column name -> Column value)
  503. * @usage _general_method_
  504. */
  505. public Map<String, Object> getNextRow() throws IOException {
  506. return getNextRow(null);
  507. }
  508. /**
  509. * @param columnNames Only column names in this collection will be returned
  510. * @return The next row in this table (Column name -> Column value)
  511. * @usage _general_method_
  512. */
  513. public Map<String, Object> getNextRow(Collection<String> columnNames)
  514. throws IOException
  515. {
  516. return getInternalCursor().getNextRow(columnNames);
  517. }
  518. /**
  519. * Reads a single column from the given row.
  520. * <p>
  521. * Note, this method is not generally meant to be used directly. Instead
  522. * use the Cursor class, which allows for more complex table interactions,
  523. * e.g. {@link Cursor#getCurrentRowValue}.
  524. * @usage _advanced_method_
  525. */
  526. public Object getRowValue(RowState rowState, RowId rowId, Column column)
  527. throws IOException
  528. {
  529. if(this != column.getTable()) {
  530. throw new IllegalArgumentException(
  531. "Given column " + column + " is not from this table");
  532. }
  533. requireValidRowId(rowId);
  534. // position at correct row
  535. ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
  536. requireNonDeletedRow(rowState, rowId);
  537. return getRowColumn(getFormat(), rowBuffer, column, rowState, null);
  538. }
  539. /**
  540. * Reads some columns from the given row.
  541. * @param columnNames Only column names in this collection will be returned
  542. * @usage _advanced_method_
  543. */
  544. public Map<String, Object> getRow(
  545. RowState rowState, RowId rowId, Collection<String> columnNames)
  546. throws IOException
  547. {
  548. requireValidRowId(rowId);
  549. // position at correct row
  550. ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
  551. requireNonDeletedRow(rowState, rowId);
  552. return getRow(getFormat(), rowState, rowBuffer, _columns, columnNames);
  553. }
  554. /**
  555. * Reads the row data from the given row buffer. Leaves limit unchanged.
  556. * Saves parsed row values to the given rowState.
  557. */
  558. private static Map<String, Object> getRow(
  559. JetFormat format,
  560. RowState rowState,
  561. ByteBuffer rowBuffer,
  562. Collection<Column> columns,
  563. Collection<String> columnNames)
  564. throws IOException
  565. {
  566. Map<String, Object> rtn = new LinkedHashMap<String, Object>(
  567. columns.size());
  568. for(Column column : columns) {
  569. if((columnNames == null) || (columnNames.contains(column.getName()))) {
  570. // Add the value to the row data
  571. column.setRowValue(
  572. rtn, getRowColumn(format, rowBuffer, column, rowState, null));
  573. }
  574. }
  575. return rtn;
  576. }
  577. /**
  578. * Reads the column data from the given row buffer. Leaves limit unchanged.
  579. * Caches the returned value in the rowState.
  580. */
  581. private static Object getRowColumn(JetFormat format,
  582. ByteBuffer rowBuffer,
  583. Column column,
  584. RowState rowState,
  585. Map<Column,byte[]> rawVarValues)
  586. throws IOException
  587. {
  588. byte[] columnData = null;
  589. try {
  590. NullMask nullMask = rowState.getNullMask(rowBuffer);
  591. boolean isNull = nullMask.isNull(column);
  592. if(column.getType() == DataType.BOOLEAN) {
  593. // Boolean values are stored in the null mask. see note about
  594. // caching below
  595. return rowState.setRowValue(column.getColumnIndex(),
  596. Boolean.valueOf(!isNull));
  597. } else if(isNull) {
  598. // well, that's easy! (no need to update cache w/ null)
  599. return null;
  600. }
  601. // reset position to row start
  602. rowBuffer.reset();
  603. // locate the column data bytes
  604. int rowStart = rowBuffer.position();
  605. int colDataPos = 0;
  606. int colDataLen = 0;
  607. if(!column.isVariableLength()) {
  608. // read fixed length value (non-boolean at this point)
  609. int dataStart = rowStart + format.OFFSET_COLUMN_FIXED_DATA_ROW_OFFSET;
  610. colDataPos = dataStart + column.getFixedDataOffset();
  611. colDataLen = column.getType().getFixedSize(column.getLength());
  612. } else {
  613. int varDataStart;
  614. int varDataEnd;
  615. if(format.SIZE_ROW_VAR_COL_OFFSET == 2) {
  616. // read simple var length value
  617. int varColumnOffsetPos =
  618. (rowBuffer.limit() - nullMask.byteSize() - 4) -
  619. (column.getVarLenTableIndex() * 2);
  620. varDataStart = rowBuffer.getShort(varColumnOffsetPos);
  621. varDataEnd = rowBuffer.getShort(varColumnOffsetPos - 2);
  622. } else {
  623. // read jump-table based var length values
  624. short[] varColumnOffsets = readJumpTableVarColOffsets(
  625. rowState, rowBuffer, rowStart, nullMask);
  626. varDataStart = varColumnOffsets[column.getVarLenTableIndex()];
  627. varDataEnd = varColumnOffsets[column.getVarLenTableIndex() + 1];
  628. }
  629. colDataPos = rowStart + varDataStart;
  630. colDataLen = varDataEnd - varDataStart;
  631. }
  632. // grab the column data
  633. rowBuffer.position(colDataPos);
  634. columnData = ByteUtil.getBytes(rowBuffer, colDataLen);
  635. if((rawVarValues != null) && column.isVariableLength()) {
  636. // caller wants raw value as well
  637. rawVarValues.put(column, columnData);
  638. }
  639. // parse the column data. we cache the row values in order to be able
  640. // to update the index on row deletion. note, most of the returned
  641. // values are immutable, except for binary data (returned as byte[]),
  642. // but binary data shouldn't be indexed anyway.
  643. return rowState.setRowValue(column.getColumnIndex(),
  644. column.read(columnData));
  645. } catch(Exception e) {
  646. // cache "raw" row value. see note about caching above
  647. rowState.setRowValue(column.getColumnIndex(),
  648. Column.rawDataWrapper(columnData));
  649. return rowState.handleRowError(column, columnData, e);
  650. }
  651. }
  652. private static short[] readJumpTableVarColOffsets(
  653. RowState rowState, ByteBuffer rowBuffer, int rowStart,
  654. NullMask nullMask)
  655. {
  656. short[] varColOffsets = rowState.getVarColOffsets();
  657. if(varColOffsets != null) {
  658. return varColOffsets;
  659. }
  660. // calculate offsets using jump-table info
  661. int nullMaskSize = nullMask.byteSize();
  662. int rowEnd = rowStart + rowBuffer.remaining() - 1;
  663. int numVarCols = ByteUtil.getUnsignedByte(rowBuffer,
  664. rowEnd - nullMaskSize);
  665. varColOffsets = new short[numVarCols + 1];
  666. int rowLen = rowEnd - rowStart + 1;
  667. int numJumps = (rowLen - 1) / MAX_BYTE;
  668. int colOffset = rowEnd - nullMaskSize - numJumps - 1;
  669. // If last jump is a dummy value, ignore it
  670. if(((colOffset - rowStart - numVarCols) / MAX_BYTE) < numJumps) {
  671. numJumps--;
  672. }
  673. int jumpsUsed = 0;
  674. for(int i = 0; i < numVarCols + 1; i++) {
  675. while((jumpsUsed < numJumps) &&
  676. (i == ByteUtil.getUnsignedByte(
  677. rowBuffer, rowEnd - nullMaskSize-jumpsUsed - 1))) {
  678. jumpsUsed++;
  679. }
  680. varColOffsets[i] = (short)
  681. (ByteUtil.getUnsignedByte(rowBuffer, colOffset - i)
  682. + (jumpsUsed * MAX_BYTE));
  683. }
  684. rowState.setVarColOffsets(varColOffsets);
  685. return varColOffsets;
  686. }
  687. /**
  688. * Reads the null mask from the given row buffer. Leaves limit unchanged.
  689. */
  690. private NullMask getRowNullMask(ByteBuffer rowBuffer)
  691. throws IOException
  692. {
  693. // reset position to row start
  694. rowBuffer.reset();
  695. // Number of columns in this row
  696. int columnCount = ByteUtil.getUnsignedVarInt(
  697. rowBuffer, getFormat().SIZE_ROW_COLUMN_COUNT);
  698. // read null mask
  699. NullMask nullMask = new NullMask(columnCount);
  700. rowBuffer.position(rowBuffer.limit() - nullMask.byteSize()); //Null mask at end
  701. nullMask.read(rowBuffer);
  702. return nullMask;
  703. }
  704. /**
  705. * Sets a new buffer to the correct row header page using the given rowState
  706. * according to the given rowId. Deleted state is
  707. * determined, but overflow row pointers are not followed.
  708. *
  709. * @return a ByteBuffer of the relevant page, or null if row was invalid
  710. * @usage _advanced_method_
  711. */
  712. public static ByteBuffer positionAtRowHeader(RowState rowState, RowId rowId)
  713. throws IOException
  714. {
  715. ByteBuffer rowBuffer = rowState.setHeaderRow(rowId);
  716. if(rowState.isAtHeaderRow()) {
  717. // this task has already been accomplished
  718. return rowBuffer;
  719. }
  720. if(!rowState.isValid()) {
  721. // this was an invalid page/row
  722. rowState.setStatus(RowStateStatus.AT_HEADER);
  723. return null;
  724. }
  725. // note, we don't use findRowStart here cause we need the unmasked value
  726. short rowStart = rowBuffer.getShort(
  727. getRowStartOffset(rowId.getRowNumber(),
  728. rowState.getTable().getFormat()));
  729. // check the deleted, overflow flags for the row (the "real" flags are
  730. // always set on the header row)
  731. RowStatus rowStatus = RowStatus.NORMAL;
  732. if(isDeletedRow(rowStart)) {
  733. rowStatus = RowStatus.DELETED;
  734. } else if(isOverflowRow(rowStart)) {
  735. rowStatus = RowStatus.OVERFLOW;
  736. }
  737. rowState.setRowStatus(rowStatus);
  738. rowState.setStatus(RowStateStatus.AT_HEADER);
  739. return rowBuffer;
  740. }
  741. /**
  742. * Sets the position and limit in a new buffer using the given rowState
  743. * according to the given row number and row end, following overflow row
  744. * pointers as necessary.
  745. *
  746. * @return a ByteBuffer narrowed to the actual row data, or null if row was
  747. * invalid or deleted
  748. * @usage _advanced_method_
  749. */
  750. public static ByteBuffer positionAtRowData(RowState rowState, RowId rowId)
  751. throws IOException
  752. {
  753. positionAtRowHeader(rowState, rowId);
  754. if(!rowState.isValid() || rowState.isDeleted()) {
  755. // row is invalid or deleted
  756. rowState.setStatus(RowStateStatus.AT_FINAL);
  757. return null;
  758. }
  759. ByteBuffer rowBuffer = rowState.getFinalPage();
  760. int rowNum = rowState.getFinalRowId().getRowNumber();
  761. JetFormat format = rowState.getTable().getFormat();
  762. if(rowState.isAtFinalRow()) {
  763. // we've already found the final row data
  764. return PageChannel.narrowBuffer(
  765. rowBuffer,
  766. findRowStart(rowBuffer, rowNum, format),
  767. findRowEnd(rowBuffer, rowNum, format));
  768. }
  769. while(true) {
  770. // note, we don't use findRowStart here cause we need the unmasked value
  771. short rowStart = rowBuffer.getShort(getRowStartOffset(rowNum, format));
  772. short rowEnd = findRowEnd(rowBuffer, rowNum, format);
  773. // note, at this point we know the row is not deleted, so ignore any
  774. // subsequent deleted flags (as overflow rows are always marked deleted
  775. // anyway)
  776. boolean overflowRow = isOverflowRow(rowStart);
  777. // now, strip flags from rowStart offset
  778. rowStart = (short)(rowStart & OFFSET_MASK);
  779. if (overflowRow) {
  780. if((rowEnd - rowStart) < 4) {
  781. throw new IOException("invalid overflow row info");
  782. }
  783. // Overflow page. the "row" data in the current page points to
  784. // another page/row
  785. int overflowRowNum = ByteUtil.getUnsignedByte(rowBuffer, rowStart);
  786. int overflowPageNum = ByteUtil.get3ByteInt(rowBuffer, rowStart + 1);
  787. rowBuffer = rowState.setOverflowRow(
  788. new RowId(overflowPageNum, overflowRowNum));
  789. rowNum = overflowRowNum;
  790. } else {
  791. rowState.setStatus(RowStateStatus.AT_FINAL);
  792. return PageChannel.narrowBuffer(rowBuffer, rowStart, rowEnd);
  793. }
  794. }
  795. }
  796. /**
  797. * Calls <code>reset</code> on this table and returns a modifiable
  798. * Iterator which will iterate through all the rows of this table. Use of
  799. * the Iterator follows the same restrictions as a call to
  800. * <code>getNextRow</code>.
  801. * @throws IllegalStateException if an IOException is thrown by one of the
  802. * operations, the actual exception will be contained within
  803. * @usage _general_method_
  804. */
  805. public Iterator<Map<String, Object>> iterator()
  806. {
  807. return iterator(null);
  808. }
  809. /**
  810. * Calls <code>reset</code> on this table and returns a modifiable
  811. * Iterator which will iterate through all the rows of this table, returning
  812. * only the given columns. Use of the Iterator follows the same
  813. * restrictions as a call to <code>getNextRow</code>.
  814. * @throws IllegalStateException if an IOException is thrown by one of the
  815. * operations, the actual exception will be contained within
  816. * @usage _general_method_
  817. */
  818. public Iterator<Map<String, Object>> iterator(Collection<String> columnNames)
  819. {
  820. reset();
  821. return getInternalCursor().iterator(columnNames);
  822. }
  823. /**
  824. * Writes a new table defined by the given TableCreator to the database.
  825. * @usage _advanced_method_
  826. */
  827. protected static void writeTableDefinition(TableCreator creator)
  828. throws IOException
  829. {
  830. // first, create the usage map page
  831. createUsageMapDefinitionBuffer(creator);
  832. // next, determine how big the table def will be (in case it will be more
  833. // than one page)
  834. JetFormat format = creator.getFormat();
  835. int idxDataLen = (creator.getIndexCount() *
  836. (format.SIZE_INDEX_DEFINITION +
  837. format.SIZE_INDEX_COLUMN_BLOCK)) +
  838. (creator.getLogicalIndexCount() * format.SIZE_INDEX_INFO_BLOCK);
  839. int totalTableDefSize = format.SIZE_TDEF_HEADER +
  840. (format.SIZE_COLUMN_DEF_BLOCK * creator.getColumns().size()) +
  841. idxDataLen + format.SIZE_TDEF_TRAILER;
  842. // total up the amount of space used by the column and index names (2
  843. // bytes per char + 2 bytes for the length)
  844. for(Column col : creator.getColumns()) {
  845. int nameByteLen = (col.getName().length() *
  846. JetFormat.TEXT_FIELD_UNIT_SIZE);
  847. totalTableDefSize += nameByteLen + 2;
  848. }
  849. for(IndexBuilder idx : creator.getIndexes()) {
  850. int nameByteLen = (idx.getName().length() *
  851. JetFormat.TEXT_FIELD_UNIT_SIZE);
  852. totalTableDefSize += nameByteLen + 2;
  853. }
  854. // now, create the table definition
  855. PageChannel pageChannel = creator.getPageChannel();
  856. ByteBuffer buffer = pageChannel .createBuffer(Math.max(totalTableDefSize,
  857. format.PAGE_SIZE));
  858. writeTableDefinitionHeader(creator, buffer, totalTableDefSize);
  859. if(creator.hasIndexes()) {
  860. // index row counts
  861. IndexData.writeRowCountDefinitions(creator, buffer);
  862. }
  863. // column definitions
  864. Column.writeDefinitions(creator, buffer);
  865. if(creator.hasIndexes()) {
  866. // index and index data definitions
  867. IndexData.writeDefinitions(creator, buffer);
  868. Index.writeDefinitions(creator, buffer);
  869. }
  870. //End of tabledef
  871. buffer.put((byte) 0xff);
  872. buffer.put((byte) 0xff);
  873. // write table buffer to database
  874. if(totalTableDefSize <= format.PAGE_SIZE) {
  875. // easy case, fits on one page
  876. buffer.putShort(format.OFFSET_FREE_SPACE,
  877. (short)(buffer.remaining() - 8)); // overwrite page free space
  878. // Write the tdef page to disk.
  879. pageChannel.writePage(buffer, creator.getTdefPageNumber());
  880. } else {
  881. // need to split across multiple pages
  882. ByteBuffer partialTdef = pageChannel.createPageBuffer();
  883. buffer.rewind();
  884. int nextTdefPageNumber = PageChannel.INVALID_PAGE_NUMBER;
  885. while(buffer.hasRemaining()) {
  886. // reset for next write
  887. partialTdef.clear();
  888. if(nextTdefPageNumber == PageChannel.INVALID_PAGE_NUMBER) {
  889. // this is the first page. note, the first page already has the
  890. // page header, so no need to write it here
  891. nextTdefPageNumber = creator.getTdefPageNumber();
  892. } else {
  893. // write page header
  894. writeTablePageHeader(partialTdef);
  895. }
  896. // copy the next page of tdef bytes
  897. int curTdefPageNumber = nextTdefPageNumber;
  898. int writeLen = Math.min(partialTdef.remaining(), buffer.remaining());
  899. partialTdef.put(buffer.array(), buffer.position(), writeLen);
  900. ByteUtil.forward(buffer, writeLen);
  901. if(buffer.hasRemaining()) {
  902. // need a next page
  903. nextTdefPageNumber = pageChannel.allocateNewPage();
  904. partialTdef.putInt(format.OFFSET_NEXT_TABLE_DEF_PAGE,
  905. nextTdefPageNumber);
  906. }
  907. // update page free space
  908. partialTdef.putShort(format.OFFSET_FREE_SPACE,
  909. (short)(partialTdef.remaining() - 8)); // overwrite page free space
  910. // write partial page to disk
  911. pageChannel.writePage(partialTdef, curTdefPageNumber);
  912. }
  913. }
  914. }
  915. /**
  916. * @param buffer Buffer to write to
  917. * @param columns List of Columns in the table
  918. */
  919. private static void writeTableDefinitionHeader(
  920. TableCreator creator, ByteBuffer buffer, int totalTableDefSize)
  921. throws IOException
  922. {
  923. List<Column> columns = creator.getColumns();
  924. //Start writing the tdef
  925. writeTablePageHeader(buffer);
  926. buffer.putInt(totalTableDefSize); //Length of table def
  927. buffer.putInt(MAGIC_TABLE_NUMBER); // seemingly constant magic value
  928. buffer.putInt(0); //Number of rows
  929. buffer.putInt(0); //Last Autonumber
  930. buffer.put((byte) 1); // this makes autonumbering work in access
  931. for (int i = 0; i < 15; i++) { //Unknown
  932. buffer.put((byte) 0);
  933. }
  934. buffer.put(Table.TYPE_USER); //Table type
  935. buffer.putShort((short) columns.size()); //Max columns a row will have
  936. buffer.putShort(Column.countVariableLength(columns)); //Number of variable columns in table
  937. buffer.putShort((short) columns.size()); //Number of columns in table
  938. buffer.putInt(creator.getLogicalIndexCount()); //Number of logical indexes in table
  939. buffer.putInt(creator.getIndexCount()); //Number of indexes in table
  940. buffer.put((byte) 0); //Usage map row number
  941. ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Usage map page number
  942. buffer.put((byte) 1); //Free map row number
  943. ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Free map page number
  944. if (LOG.isDebugEnabled()) {
  945. int position = buffer.position();
  946. buffer.rewind();
  947. LOG.debug("Creating new table def block:\n" + ByteUtil.toHexString(
  948. buffer, creator.getFormat().SIZE_TDEF_HEADER));
  949. buffer.position(position);
  950. }
  951. }
  952. /**
  953. * Writes the page header for a table definition page
  954. * @param buffer Buffer to write to
  955. */
  956. private static void writeTablePageHeader(ByteBuffer buffer)
  957. {
  958. buffer.put(PageTypes.TABLE_DEF); //Page type
  959. buffer.put((byte) 0x01); //Unknown
  960. buffer.put((byte) 0); //Unknown
  961. buffer.put((byte) 0); //Unknown
  962. buffer.putInt(0); //Next TDEF page pointer
  963. }
  964. /**
  965. * Writes the given name into the given buffer in the format as expected by
  966. * {@link #readName}.
  967. */
  968. static void writeName(ByteBuffer buffer, String name, Charset charset)
  969. {
  970. ByteBuffer encName = Column.encodeUncompressedText(name, charset);
  971. buffer.putShort((short) encName.remaining());
  972. buffer.put(encName);
  973. }
  974. /**
  975. * Create the usage map definition page buffer. The "used pages" map is in
  976. * row 0, the "pages with free space" map is in row 1. Index usage maps are
  977. * in subsequent rows.
  978. */
  979. private static void createUsageMapDefinitionBuffer(TableCreator creator)
  980. throws IOException
  981. {
  982. // 2 table usage maps plus 1 for each index
  983. int umapNum = 2 + creator.getIndexCount();
  984. JetFormat format = creator.getFormat();
  985. int usageMapRowLength = format.OFFSET_USAGE_MAP_START +
  986. format.USAGE_MAP_TABLE_BYTE_LENGTH;
  987. int freeSpace = format.DATA_PAGE_INITIAL_FREE_SPACE
  988. - (umapNum * getRowSpaceUsage(usageMapRowLength, format));
  989. // for now, don't handle writing that many indexes
  990. if(freeSpace < 0) {
  991. throw new IOException("FIXME attempting to write too many indexes");
  992. }
  993. int umapPageNumber = creator.getUmapPageNumber();
  994. PageChannel pageChannel = creator.getPageChannel();
  995. ByteBuffer rtn = pageChannel.createPageBuffer();
  996. rtn.put(PageTypes.DATA);
  997. rtn.put((byte) 0x1); //Unknown
  998. rtn.putShort((short)freeSpace); //Free space in page
  999. rtn.putInt(0); //Table definition
  1000. rtn.putInt(0); //Unknown
  1001. rtn.putShort((short) umapNum); //Number of records on this page
  1002. // write two rows of usage map definitions for the table
  1003. int rowStart = findRowEnd(rtn, 0, format) - usageMapRowLength;
  1004. for(int i = 0; i < 2; ++i) {
  1005. rtn.putShort(getRowStartOffset(i, format), (short)rowStart);
  1006. if(i == 0) {
  1007. // initial "usage pages" map definition
  1008. rtn.put(rowStart, UsageMap.MAP_TYPE_REFERENCE);
  1009. } else {
  1010. // initial "pages with free space" map definition
  1011. rtn.put(rowStart, UsageMap.MAP_TYPE_INLINE);
  1012. }
  1013. rowStart -= usageMapRowLength;
  1014. }
  1015. if(creator.hasIndexes()) {
  1016. for(int i = 0; i < creator.getIndexes().size(); ++i) {
  1017. IndexBuilder idx = creator.getIndexes().get(i);
  1018. // allocate root page for the index
  1019. int rootPageNumber = pageChannel.allocateNewPage();
  1020. int umapRowNum = i + 2;
  1021. // stash info for later use
  1022. TableCreator.IndexState idxState = creator.getIndexState(idx);
  1023. idxState.setRootPageNumber(rootPageNumber);
  1024. idxState.setUmapRowNumber((byte)umapRowNum);
  1025. idxState.setUmapPageNumber(umapPageNumber);
  1026. // index map definition, including initial root page
  1027. rtn.putShort(getRowStartOffset(umapRowNum, format), (short)rowStart);
  1028. rtn.put(rowStart, UsageMap.MAP_TYPE_INLINE);
  1029. rtn.putInt(rowStart + 1, rootPageNumber);
  1030. rtn.put(rowStart + 5, (byte)1);
  1031. rowStart -= usageMapRowLength;
  1032. }
  1033. }
  1034. pageChannel.writePage(rtn, umapPageNumber);
  1035. }
  1036. /**
  1037. * Read the table definition
  1038. */
  1039. private void readTableDefinition(ByteBuffer tableBuffer) throws IOException
  1040. {
  1041. if (LOG.isDebugEnabled()) {
  1042. tableBuffer.rewind();
  1043. LOG.debug("Table def block:\n" + ByteUtil.toHexString(tableBuffer,
  1044. getFormat().SIZE_TDEF_HEADER));
  1045. }
  1046. _rowCount = tableBuffer.getInt(getFormat().OFFSET_NUM_ROWS);
  1047. _lastLongAutoNumber = tableBuffer.getInt(getFormat().OFFSET_NEXT_AUTO_NUMBER);
  1048. if(getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER >= 0) {
  1049. _lastComplexTypeAutoNumber = tableBuffer.getInt(
  1050. getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER);
  1051. }
  1052. _tableType = tableBuffer.get(getFormat().OFFSET_TABLE_TYPE);
  1053. _maxColumnCount = tableBuffer.getShort(getFormat().OFFSET_MAX_COLS);
  1054. _maxVarColumnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_VAR_COLS);
  1055. short columnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_COLS);
  1056. _logicalIndexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEX_SLOTS);
  1057. _indexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEXES);
  1058. int rowNum = ByteUtil.getUnsignedByte(
  1059. tableBuffer, getFormat().OFFSET_OWNED_PAGES);
  1060. int pageNum = ByteUtil.get3ByteInt(tableBuffer, getFormat().OFFSET_OWNED_PAGES + 1);
  1061. _ownedPages = UsageMap.read(getDatabase(), pageNum, rowNum, false);
  1062. rowNum = ByteUtil.getUnsignedByte(
  1063. tableBuffer, getFormat().OFFSET_FREE_SPACE_PAGES);
  1064. pageNum = ByteUtil.get3ByteInt(tableBuffer, getFormat().OFFSET_FREE_SPACE_PAGES + 1);
  1065. _freeSpacePages = UsageMap.read(getDatabase(), pageNum, rowNum, false);
  1066. for (int i = 0; i < _indexCount; i++) {
  1067. _indexDatas.add(IndexData.create(this, tableBuffer, i, getFormat()));
  1068. }
  1069. int colOffset = getFormat().OFFSET_INDEX_DEF_BLOCK +
  1070. _indexCount * getFormat().SIZE_INDEX_DEFINITION;
  1071. int dispIndex = 0;
  1072. for (int i = 0; i < columnCount; i++) {
  1073. Column column = new Column(this, tableBuffer,
  1074. colOffset + (i * getFormat().SIZE_COLUMN_HEADER), dispIndex++);
  1075. _columns.add(column);
  1076. if(column.isVariableLength()) {
  1077. // also shove it in the variable columns list, which is ordered
  1078. // differently from the _columns list
  1079. _varColumns.add(column);
  1080. }
  1081. }
  1082. tableBuffer.position(colOffset +
  1083. (columnCount * getFormat().SIZE_COLUMN_HEADER));
  1084. for (int i = 0; i < columnCount; i++) {
  1085. Column column = _columns.get(i);
  1086. column.setName(readName(tableBuffer));
  1087. }
  1088. Collections.sort(_columns);
  1089. _autoNumColumns = getAutoNumberColumns(_columns);
  1090. // setup the data index for the columns
  1091. int colIdx = 0;
  1092. for(Column col : _columns) {
  1093. col.setColumnIndex(colIdx++);
  1094. }
  1095. // sort variable length columns based on their index into the variable
  1096. // length offset table, because we will write the columns in this order
  1097. Collections.sort(_varColumns, VAR_LEN_COLUMN_COMPARATOR);
  1098. // read index column information
  1099. for (int i = 0; i < _indexCount; i++) {
  1100. _indexDatas.get(i).read(tableBuffer, _columns);
  1101. }
  1102. // read logical index info (may be more logical indexes than index datas)
  1103. for (int i = 0; i < _logicalIndexCount; i++) {
  1104. _indexes.add(new Index(tableBuffer, _indexDatas, getFormat()));
  1105. }
  1106. // read logical index names
  1107. for (int i = 0; i < _logicalIndexCount; i++) {
  1108. _indexes.get(i).setName(readName(tableBuffer));
  1109. }
  1110. Collections.sort(_indexes);
  1111. // re-sort columns if necessary
  1112. if(getDatabase().getColumnOrder() != ColumnOrder.DATA) {
  1113. Collections.sort(_columns, DISPLAY_ORDER_COMPARATOR);
  1114. }
  1115. for(Column col : _columns) {
  1116. // some columns need to do extra work after the table is completely
  1117. // loaded
  1118. col.postTableLoadInit();
  1119. }
  1120. }
  1121. /**
  1122. * Writes the given page data to the given page number, clears any other
  1123. * relevant buffers.
  1124. */
  1125. private void writeDataPage(ByteBuffer pageBuffer, int pageNumber)
  1126. throws IOException
  1127. {
  1128. // write the page data
  1129. getPageChannel().writePage(pageBuffer, pageNumber);
  1130. // possibly invalidate the add row buffer if a different data buffer is
  1131. // being written (e.g. this happens during deleteRow)
  1132. _addRowBufferH.possiblyInvalidate(pageNumber, pageBuffer);
  1133. // update modification count so any active RowStates can keep themselves
  1134. // up-to-date
  1135. ++_modCount;
  1136. }
  1137. /**
  1138. * Returns a name read from the buffer at the current position. The
  1139. * expected name format is the name length followed by the name
  1140. * encoded using the {@link JetFormat#CHARSET}
  1141. */
  1142. private String readName(ByteBuffer buffer) {
  1143. int nameLength = readNameLength(buffer);
  1144. byte[] nameBytes = ByteUtil.getBytes(buffer, nameLength);
  1145. return Column.decodeUncompressedText(nameBytes,
  1146. getDatabase().getCharset());
  1147. }
  1148. /**
  1149. * Returns a name length read from the buffer at the current position.
  1150. */
  1151. private int readNameLength(ByteBuffer buffer) {
  1152. return ByteUtil.getUnsignedVarInt(buffer, getFormat().SIZE_NAME_LENGTH);
  1153. }
  1154. /**
  1155. * Converts a map of columnName -> columnValue to an array of row values
  1156. * appropriate for a call to {@link #addRow(Object...)}.
  1157. * @usage _general_method_
  1158. */
  1159. public Object[] asRow(Map<String,?> rowMap) {
  1160. return asRow(rowMap, null);
  1161. }
  1162. /**
  1163. * Converts a map of columnName -> columnValue to an array of row values
  1164. * appropriate for a call to {@link #updateCurrentRow(Object...)}.
  1165. * @usage _general_method_
  1166. */
  1167. public Object[] asUpdateRow(Map<String,?> rowMap) {
  1168. return asRow(rowMap, Column.KEEP_VALUE);
  1169. }
  1170. /**
  1171. * Converts a map of columnName -> columnValue to an array of row values.
  1172. */
  1173. private Object[] asRow(Map<String,?> rowMap, Object defaultValue)
  1174. {
  1175. Object[] row = new Object[_columns.size()];
  1176. if(defaultValue != null) {
  1177. Arrays.fill(row, defaultValue);
  1178. }
  1179. if(rowMap == null) {
  1180. return row;
  1181. }
  1182. for(Column col : _columns) {
  1183. if(rowMap.containsKey(col.getName())) {
  1184. col.setRowValue(row, col.getRowValue(rowMap));
  1185. }
  1186. }
  1187. return row;
  1188. }
  1189. /**
  1190. * Adds a single row to this table and writes it to disk. The values are
  1191. * expected to be given in the order that the Columns are listed by the
  1192. * {@link #getColumns} method. This is by default the storage order of the
  1193. * Columns in the database, however this order can be influenced by setting
  1194. * the ColumnOrder via {@link Database#setColumnOrder} prior to opening the
  1195. * Table. The {@link #asRow} method can be used to easily convert a row Map into the
  1196. * appropriate row array for this Table.
  1197. * <p>
  1198. * Note, if this table has an auto-number column, the value generated will be
  1199. * put back into the given row array (assuming the given row array is at
  1200. * least as long as the number of Columns in this Table).
  1201. *
  1202. * @param row row values for a single row. the given row array will be
  1203. * modified if this table contains an auto-number column,
  1204. * otherwise it will not be modified.
  1205. * @usage _general_method_
  1206. */
  1207. public void addRow(Object... row) throws IOException {
  1208. addRows(Collections.singletonList(row), _singleRowBufferH);
  1209. }
  1210. /**
  1211. * Add multiple rows to this table, only writing to disk after all
  1212. * rows have been written, and every time a data page is filled. This
  1213. * is much more efficient than calling <code>addRow</code> multiple times.
  1214. * <p>
  1215. * Note, if this table has an auto-number column, the values written will be
  1216. * put back into the given row arrays (assuming the given row array is at
  1217. * least as long as the number of Columns in this Table).
  1218. *
  1219. * @see #addRow(Object...) for more details on row arrays
  1220. *
  1221. * @param rows List of Object[] row values. the rows will be modified if
  1222. * this table contains an auto-number column, otherwise they
  1223. * will not be modified.
  1224. * @usage _general_method_
  1225. */
  1226. public void addRows(List<? extends Object[]> rows) throws IOException {
  1227. addRows(rows, _multiRowBufferH);
  1228. }
  1229. /**
  1230. * Add multiple rows to this table, only writing to disk after all
  1231. * rows have been written, and every time a data page is filled.
  1232. * @param inRows List of Object[] row values
  1233. * @param writeRowBufferH TempBufferHolder used to generate buffers for
  1234. * writing the row data
  1235. */
  1236. private void addRows(List<? extends Object[]> inRows,
  1237. TempBufferHolder writeRowBufferH)
  1238. throws IOException
  1239. {
  1240. if(inRows.isEmpty()) {
  1241. return;
  1242. }
  1243. // copy the input rows to a modifiable list so we can update the elements
  1244. List<Object[]> rows = new ArrayList<Object[]>(inRows);
  1245. ByteBuffer[] rowData = new ByteBuffer[rows.size()];
  1246. for (int i = 0; i < rows.size(); i++) {
  1247. // we need to make sure the row is the right length and is an Object[]
  1248. // (fill with null if too short). note, if the row is copied the caller
  1249. // will not be able to access any generated auto-number value, but if
  1250. // they need that info they should use a row array of the right
  1251. // size/type!
  1252. Object[] row = rows.get(i);
  1253. if((row.length < _columns.size()) || (row.getClass() != Object[].class)) {
  1254. row = dupeRow(row, _columns.size());
  1255. // we copied the row, so put the copy back into the rows list
  1256. rows.set(i, row);
  1257. }
  1258. // fill in autonumbers
  1259. handleAutoNumbersForAdd(row);
  1260. // write the row of data to a temporary buffer
  1261. rowData[i] = createRow(row, writeRowBufferH.getPageBuffer(getPageChannel()));
  1262. if (rowData[i].limit() > getFormat().MAX_ROW_SIZE) {
  1263. throw new IOException("Row size " + rowData[i].limit() +
  1264. " is too large");
  1265. }
  1266. }
  1267. ByteBuffer dataPage = null;
  1268. int pageNumber = PageChannel.INVALID_PAGE_NUMBER;
  1269. for (int i = 0; i < rowData.length; i++) {
  1270. int rowSize = rowData[i].remaining();
  1271. // get page with space
  1272. dataPage = findFreeRowSpace(rowSize, dataPage, pageNumber);
  1273. pageNumber = _addRowBufferH.getPageNumber();
  1274. // write out the row data
  1275. int rowNum = addDataPageRow(dataPage, rowSize, getFormat(), 0);
  1276. dataPage.put(rowData[i]);
  1277. // update the indexes
  1278. RowId rowId = new RowId(pageNumber, rowNum);
  1279. for(IndexData indexData : _indexDatas) {
  1280. indexData.addRow(rows.get(i), rowId);
  1281. }
  1282. }
  1283. writeDataPage(dataPage, pageNumber);
  1284. // Update tdef page
  1285. updateTableDefinition(rows.size());
  1286. }
  1287. /**
  1288. * Updates the current row to the new values.
  1289. * <p>
  1290. * Note, if this table has an auto-number column(s), the existing value(s)
  1291. * will be maintained, unchanged.
  1292. *
  1293. * @param row new row values for the current row.
  1294. * @usage _general_method_
  1295. */
  1296. public void updateCurrentRow(Object... row) throws IOException {
  1297. getInternalCursor().updateCurrentRow(row);
  1298. }
  1299. /**
  1300. * Update the row on which the given rowState is currently positioned.
  1301. * <p>
  1302. * Note, this method is not generally meant to be used directly. You should
  1303. * use the {@link #updateCurrentRow} method or use the Cursor class, which
  1304. * allows for more complex table interactions, e.g.
  1305. * {@link Cursor#setCurrentRowValue} and {@link Cursor#updateCurrentRow}.
  1306. * @usage _advanced_method_
  1307. */
  1308. public void updateRow(RowState rowState, RowId rowId, Object... row)
  1309. throws IOException
  1310. {
  1311. requireValidRowId(rowId);
  1312. // ensure that the relevant row state is up-to-date
  1313. ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
  1314. int oldRowSize = rowBuffer.remaining();
  1315. requireNonDeletedRow(rowState, rowId);
  1316. // we need to make sure the row is the right length & type (fill with
  1317. // null if too short).
  1318. if((row.length < _columns.size()) || (row.getClass() != Object[].class)) {
  1319. row = dupeRow(row, _columns.size());
  1320. }
  1321. // fill in any auto-numbers (we don't allow autonumber values to be
  1322. // modified)
  1323. handleAutoNumbersForUpdate(row, rowBuffer, rowState);
  1324. // hang on to the raw values of var length columns we are "keeping". this
  1325. // will allow us to re-use pre-written var length data, which can save
  1326. // space for things like long value columns.
  1327. Map<Column,byte[]> rawVarValues =
  1328. (!_varColumns.isEmpty() ? new HashMap<Column,byte[]>() : null);
  1329. // fill in any "keep value" fields
  1330. for(Column column : _columns) {
  1331. if(column.getRowValue(row) == Column.KEEP_VALUE) {
  1332. column.setRowValue(
  1333. row, getRowColumn(getFormat(), rowBuffer, column, rowState,
  1334. rawVarValues));
  1335. }
  1336. }
  1337. // generate new row bytes
  1338. ByteBuffer newRowData = createRow(
  1339. row, _singleRowBufferH.getPageBuffer(getPageChannel()), oldRowSize,
  1340. rawVarValues);
  1341. if (newRowData.limit() > getFormat().MAX_ROW_SIZE) {
  1342. throw new IOException("Row size " + newRowData.limit() +
  1343. " is too large");
  1344. }
  1345. if(!_indexDatas.isEmpty()) {
  1346. Object[] oldRowValues = rowState.getRowValues();
  1347. // delete old values from indexes
  1348. for(IndexData indexData : _indexDatas) {
  1349. indexData.deleteRow(oldRowValues, rowId);
  1350. }
  1351. }
  1352. // see if we can squeeze the new row data into the existing row
  1353. rowBuffer.reset();
  1354. int rowSize = newRowData.remaining();
  1355. ByteBuffer dataPage = null;
  1356. int pageNumber = PageChannel.INVALID_PAGE_NUMBER;
  1357. if(oldRowSize >= rowSize) {
  1358. // awesome, slap it in!
  1359. rowBuffer.put(newRowData);
  1360. // grab the page we just updated
  1361. dataPage = rowState.getFinalPage();
  1362. pageNumber = rowState.getFinalRowId().getPageNumber();
  1363. } else {
  1364. // bummer, need to find a new page for the data
  1365. dataPage = findFreeRowSpace(rowSize, null,
  1366. PageChannel.INVALID_PAGE_NUMBER);
  1367. pageNumber = _addRowBufferH.getPageNumber();
  1368. RowId headerRowId = rowState.getHeaderRowId();
  1369. ByteBuffer headerPage = rowState.getHeaderPage();
  1370. if(pageNumber == headerRowId.getPageNumber()) {
  1371. // new row is on the same page as header row, share page
  1372. dataPage = headerPage;
  1373. }
  1374. // write out the new row data (set the deleted flag on the new data row
  1375. // so that it is ignored during normal table traversal)
  1376. int rowNum = addDataPageRow(dataPage, rowSize, getFormat(),
  1377. DELETED_ROW_MASK);
  1378. dataPage.put(newRowData);
  1379. // write the overflow info into the header row and clear out the
  1380. // remaining header data
  1381. rowBuffer = PageChannel.narrowBuffer(
  1382. headerPage,
  1383. findRowStart(headerPage, headerRowId.getRowNumber(), getFormat()),
  1384. findRowEnd(headerPage, headerRowId.getRowNumber(), getFormat()));
  1385. rowBuffer.put((byte)rowNum);
  1386. ByteUtil.put3ByteInt(rowBuffer, pageNumber);
  1387. ByteUtil.clearRemaining(rowBuffer);
  1388. // set the overflow flag on the header row
  1389. int headerRowIndex = getRowStartOffset(headerRowId.getRowNumber(),
  1390. getFormat());
  1391. headerPage.putShort(headerRowIndex,
  1392. (short)(headerPage.getShort(headerRowIndex)
  1393. | OVERFLOW_ROW_MASK));
  1394. if(pageNumber != headerRowId.getPageNumber()) {
  1395. writeDataPage(headerPage, headerRowId.getPageNumber());
  1396. }
  1397. }
  1398. // update the indexes
  1399. for(IndexData indexData : _indexDatas) {
  1400. indexData.addRow(row, rowId);
  1401. }
  1402. writeDataPage(dataPage, pageNumber);
  1403. updateTableDefinition(0);
  1404. }
  1405. private ByteBuffer findFreeRowSpace(int rowSize, ByteBuffer dataPage,
  1406. int pageNumber)
  1407. throws IOException
  1408. {
  1409. if(dataPage == null) {
  1410. // find last data page (Not bothering to check other pages for free
  1411. // space.)
  1412. UsageMap.PageCursor revPageCursor = _ownedPages.cursor();
  1413. revPageCursor.afterLast();
  1414. while(true) {
  1415. int tmpPageNumber = revPageCursor.getPreviousPage();
  1416. if(tmpPageNumber < 0) {
  1417. break;
  1418. }
  1419. dataPage = _addRowBufferH.setPage(getPageChannel(), tmpPageNumber);
  1420. if(dataPage.get() == PageTypes.DATA) {
  1421. // found last data page, only use if actually listed in free space
  1422. // pages
  1423. if(_freeSpacePages.containsPageNumber(tmpPageNumber)) {
  1424. pageNumber = tmpPageNumber;
  1425. }
  1426. break;
  1427. }
  1428. }
  1429. if(pageNumber == PageChannel.INVALID_PAGE_NUMBER) {
  1430. // No data pages exist (with free space). Create a new one.
  1431. return newDataPage();
  1432. }
  1433. }
  1434. if(!rowFitsOnDataPage(rowSize, dataPage, getFormat())) {
  1435. // Last data page is full. Create a new one.
  1436. writeDataPage(dataPage, pageNumber);
  1437. _freeSpacePages.removePageNumber(pageNumber);
  1438. dataPage = newDataPage();
  1439. }
  1440. return dataPage;
  1441. }
  1442. /**
  1443. * Updates the table definition after rows are modified.
  1444. */
  1445. private void updateTableDefinition(int rowCountInc) throws IOException
  1446. {
  1447. // load table definition
  1448. ByteBuffer tdefPage = _tableDefBufferH.setPage(getPageChannel(),
  1449. _tableDefPageNumber);
  1450. // make sure rowcount and autonumber are up-to-date
  1451. _rowCount += rowCountInc;
  1452. tdefPage.putInt(getFormat().OFFSET_NUM_ROWS, _rowCount);
  1453. tdefPage.putInt(getFormat().OFFSET_NEXT_AUTO_NUMBER, _lastLongAutoNumber);
  1454. int ctypeOff = getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER;
  1455. if(ctypeOff >= 0) {
  1456. tdefPage.putInt(ctypeOff, _lastComplexTypeAutoNumber);
  1457. }
  1458. // write any index changes
  1459. for (IndexData indexData : _indexDatas) {
  1460. // write the unique entry count for the index to the table definition
  1461. // page
  1462. tdefPage.putInt(indexData.getUniqueEntryCountOffset(),
  1463. indexData.getUniqueEntryCount());
  1464. // write the entry page for the index
  1465. indexData.update();
  1466. }
  1467. // write modified table definition
  1468. getPageChannel().writePage(tdefPage, _tableDefPageNumber);
  1469. }
  1470. /**
  1471. * Create a new data page
  1472. * @return Page number of the new page
  1473. */
  1474. private ByteBuffer newDataPage() throws IOException {
  1475. if (LOG.isDebugEnabled()) {
  1476. LOG.debug("Creating new data page");
  1477. }
  1478. ByteBuffer dataPage = _addRowBufferH.setNewPage(getPageChannel());
  1479. dataPage.put(PageTypes.DATA); //Page type
  1480. dataPage.put((byte) 1); //Unknown
  1481. dataPage.putShort((short)getFormat().DATA_PAGE_INITIAL_FREE_SPACE); //Free space in this page
  1482. dataPage.putInt(_tableDefPageNumber); //Page pointer to table definition
  1483. dataPage.putInt(0); //Unknown
  1484. dataPage.putShort((short)0); //Number of rows on this page
  1485. int pageNumber = _addRowBufferH.getPageNumber();
  1486. getPageChannel().writePage(dataPage, pageNumber);
  1487. _ownedPages.addPageNumber(pageNumber);
  1488. _freeSpacePages.addPageNumber(pageNumber);
  1489. return dataPage;
  1490. }
  1491. ByteBuffer createRow(Object[] rowArray, ByteBuffer buffer)
  1492. throws IOException
  1493. {
  1494. return createRow(rowArray, buffer, 0, Collections.<Column,byte[]>emptyMap());
  1495. }
  1496. /**
  1497. * Serialize a row of Objects into a byte buffer.
  1498. *
  1499. * @param rowArray row data, expected to be correct length for this table
  1500. * @param buffer buffer to which to write the row data
  1501. * @param minRowSize min size for result row
  1502. * @param rawVarValues optional, pre-written values for var length columns
  1503. * (enables re-use of previously written values).
  1504. * @return the given buffer, filled with the row data
  1505. */
  1506. private ByteBuffer createRow(Object[] rowArray, ByteBuffer buffer,
  1507. int minRowSize, Map<Column,byte[]> rawVarValues)
  1508. throws IOException
  1509. {
  1510. buffer.putShort(_maxColumnCount);
  1511. NullMask nullMask = new NullMask(_maxColumnCount);
  1512. //Fixed length column data comes first
  1513. int fixedDataStart = buffer.position();
  1514. int fixedDataEnd = fixedDataStart;
  1515. for (Column col : _columns) {
  1516. if(col.isVariableLength()) {
  1517. continue;
  1518. }
  1519. Object rowValue = col.getRowValue(rowArray);
  1520. if (col.getType() == DataType.BOOLEAN) {
  1521. if(Column.toBooleanValue(rowValue)) {
  1522. //Booleans are stored in the null mask
  1523. nullMask.markNotNull(col);
  1524. }
  1525. rowValue = null;
  1526. }
  1527. if(rowValue != null) {
  1528. // we have a value to write
  1529. nullMask.markNotNull(col);
  1530. // remainingRowLength is ignored when writing fixed length data
  1531. buffer.position(fixedDataStart + col.getFixedDataOffset());
  1532. buffer.put(col.write(rowValue, 0));
  1533. }
  1534. // always insert space for the entire fixed data column length
  1535. // (including null values), access expects the row to always be at least
  1536. // big enough to hold all fixed values
  1537. buffer.position(fixedDataStart + col.getFixedDataOffset() +
  1538. col.getLength());
  1539. // keep track of the end of fixed data
  1540. if(buffer.position() > fixedDataEnd) {
  1541. fixedDataEnd = buffer.position();
  1542. }
  1543. }
  1544. // reposition at end of fixed data
  1545. buffer.position(fixedDataEnd);
  1546. // only need this info if this table contains any var length data
  1547. if(_maxVarColumnCount > 0) {
  1548. int maxRowSize = getFormat().MAX_ROW_SIZE;
  1549. // figure out how much space remains for var length data. first,
  1550. // account for already written space
  1551. maxRowSize -= buffer.position();
  1552. // now, account for trailer space
  1553. int trailerSize = (nullMask.byteSize() + 4 + (_maxVarColumnCount * 2));
  1554. maxRowSize -= trailerSize;
  1555. // for each non-null long value column we need to reserve a small
  1556. // amount of space so that we don't end up running out of row space
  1557. // later by being too greedy
  1558. for (Column varCol : _varColumns) {
  1559. if((varCol.getType().isLongValue()) &&
  1560. (varCol.getRowValue(rowArray) != null)) {
  1561. maxRowSize -= getFormat().SIZE_LONG_VALUE_DEF;
  1562. }
  1563. }
  1564. //Now write out variable length column data
  1565. short[] varColumnOffsets = new short[_maxVarColumnCount];
  1566. int varColumnOffsetsIndex = 0;
  1567. for (Column varCol : _varColumns) {
  1568. short offset = (short) buffer.position();
  1569. Object rowValue = varCol.getRowValue(rowArray);
  1570. if (rowValue != null) {
  1571. // we have a value
  1572. nullMask.markNotNull(varCol);
  1573. byte[] rawValue = null;
  1574. ByteBuffer varDataBuf = null;
  1575. if(((rawValue = rawVarValues.get(varCol)) != null) &&
  1576. (rawValue.length <= maxRowSize)) {
  1577. // save time and potentially db space, re-use raw value
  1578. varDataBuf = ByteBuffer.wrap(rawValue);
  1579. } else {
  1580. // write column value
  1581. varDataBuf = varCol.write(rowValue, maxRowSize);
  1582. }
  1583. maxRowSize -= varDataBuf.remaining();
  1584. if(varCol.getType().isLongValue()) {
  1585. // we already accounted for some amount of the long value data
  1586. // above. add that space back so we don't double count
  1587. maxRowSize += getFormat().SIZE_LONG_VALUE_DEF;
  1588. }
  1589. buffer.put(varDataBuf);
  1590. }
  1591. // we do a loop here so that we fill in offsets for deleted columns
  1592. while(varColumnOffsetsIndex <= varCol.getVarLenTableIndex()) {
  1593. varColumnOffsets[varColumnOffsetsIndex++] = offset;
  1594. }
  1595. }
  1596. // fill in offsets for any remaining deleted columns
  1597. while(varColumnOffsetsIndex < varColumnOffsets.length) {
  1598. varColumnOffsets[varColumnOffsetsIndex++] = (short) buffer.position();
  1599. }
  1600. // record where we stopped writing
  1601. int eod = buffer.position();
  1602. // insert padding if necessary
  1603. padRowBuffer(buffer, minRowSize, trailerSize);
  1604. buffer.putShort((short) eod); //EOD marker
  1605. //Now write out variable length offsets
  1606. //Offsets are stored in reverse order
  1607. for (int i = _maxVarColumnCount - 1; i >= 0; i--) {
  1608. buffer.putShort(varColumnOffsets[i]);
  1609. }
  1610. buffer.putShort(_maxVarColumnCount); //Number of var length columns
  1611. } else {
  1612. // insert padding for row w/ no var cols
  1613. padRowBuffer(buffer, minRowSize, nullMask.byteSize());
  1614. }
  1615. nullMask.write(buffer); //Null mask
  1616. buffer.flip();
  1617. if (LOG.isDebugEnabled()) {
  1618. LOG.debug("Creating new data block:\n" + ByteUtil.toHexString(buffer, buffer.limit()));
  1619. }
  1620. return buffer;
  1621. }
  1622. /**
  1623. * Autonumber columns may not be modified on update.
  1624. */
  1625. private void handleAutoNumbersForUpdate(
  1626. Object[] row, ByteBuffer rowBuffer, RowState rowState)
  1627. throws IOException
  1628. {
  1629. if(_autoNumColumns.isEmpty()) {
  1630. return;
  1631. }
  1632. for(Column col : _autoNumColumns) {
  1633. col.setRowValue(row, getRowColumn(getFormat(), rowBuffer, col, rowState,
  1634. null));
  1635. }
  1636. }
  1637. /**
  1638. * Fill in all autonumber column values.
  1639. */
  1640. private void handleAutoNumbersForAdd(Object[] row)
  1641. throws IOException
  1642. {
  1643. if(_autoNumColumns.isEmpty()) {
  1644. return;
  1645. }
  1646. Object complexAutoNumber = null;
  1647. for(Column col : _autoNumColumns) {
  1648. // ignore given row value, use next autonumber
  1649. Column.AutoNumberGenerator autoNumGen = col.getAutoNumberGenerator();
  1650. Object rowValue = null;
  1651. if(autoNumGen.getType() != DataType.COMPLEX_TYPE) {
  1652. rowValue = autoNumGen.getNext(null);
  1653. } else {
  1654. // complex type auto numbers are shared across all complex columns
  1655. // in the row
  1656. complexAutoNumber = autoNumGen.getNext(complexAutoNumber);
  1657. rowValue = complexAutoNumber;
  1658. }
  1659. col.setRowValue(row, rowValue);
  1660. }
  1661. }
  1662. private static void padRowBuffer(ByteBuffer buffer, int minRowSize,
  1663. int trailerSize)
  1664. {
  1665. int pos = buffer.position();
  1666. if((pos + trailerSize) < minRowSize) {
  1667. // pad the row to get to the min byte size
  1668. int padSize = minRowSize - (pos + trailerSize);
  1669. ByteUtil.clearRange(buffer, pos, pos + padSize);
  1670. ByteUtil.forward(buffer, padSize);
  1671. }
  1672. }
  1673. /**
  1674. * @usage _general_method_
  1675. */
  1676. public int getRowCount() {
  1677. return _rowCount;
  1678. }
  1679. int getNextLongAutoNumber() {
  1680. // note, the saved value is the last one handed out, so pre-increment
  1681. return ++_lastLongAutoNumber;
  1682. }
  1683. int getLastLongAutoNumber() {
  1684. // gets the last used auto number (does not modify)
  1685. return _lastLongAutoNumber;
  1686. }
  1687. int getNextComplexTypeAutoNumber() {
  1688. // note, the saved value is the last one handed out, so pre-increment
  1689. return ++_lastComplexTypeAutoNumber;
  1690. }
  1691. int getLastComplexTypeAutoNumber() {
  1692. // gets the last used auto number (does not modify)
  1693. return _lastComplexTypeAutoNumber;
  1694. }
  1695. @Override
  1696. public String toString() {
  1697. StringBuilder rtn = new StringBuilder();
  1698. rtn.append("Type: " + _tableType +
  1699. ((_tableType == TYPE_USER) ? " (USER)" : " (SYSTEM)"));
  1700. rtn.append("\nName: " + _name);
  1701. rtn.append("\nRow count: " + _rowCount);
  1702. rtn.append("\nColumn count: " + _columns.size());
  1703. rtn.append("\nIndex (data) count: " + _indexCount);
  1704. rtn.append("\nLogical Index count: " + _logicalIndexCount);
  1705. rtn.append("\nColumns:\n");
  1706. for(Column col : _columns) {
  1707. rtn.append(col);
  1708. }
  1709. rtn.append("\nIndexes:\n");
  1710. for(Index index : _indexes) {
  1711. rtn.append(index);
  1712. }
  1713. rtn.append("\nOwned pages: " + _ownedPages + "\n");
  1714. return rtn.toString();
  1715. }
  1716. /**
  1717. * @return A simple String representation of the entire table in
  1718. * tab-delimited format
  1719. * @usage _general_method_
  1720. */
  1721. public String display() throws IOException {
  1722. return display(Long.MAX_VALUE);
  1723. }
  1724. /**
  1725. * @param limit Maximum number of rows to display
  1726. * @return A simple String representation of the entire table in
  1727. * tab-delimited format
  1728. * @usage _general_method_
  1729. */
  1730. public String display(long limit) throws IOException {
  1731. reset();
  1732. StringBuilder rtn = new StringBuilder();
  1733. for(Iterator<Column> iter = _columns.iterator(); iter.hasNext(); ) {
  1734. Column col = iter.next();
  1735. rtn.append(col.getName());
  1736. if (iter.hasNext()) {
  1737. rtn.append("\t");
  1738. }
  1739. }
  1740. rtn.append("\n");
  1741. Map<String, Object> row;
  1742. int rowCount = 0;
  1743. while ((rowCount++ < limit) && (row = getNextRow()) != null) {
  1744. for(Iterator<Object> iter = row.values().iterator(); iter.hasNext(); ) {
  1745. Object obj = iter.next();
  1746. if (obj instanceof byte[]) {
  1747. byte[] b = (byte[]) obj;
  1748. rtn.append(ByteUtil.toHexString(b));
  1749. //This block can be used to easily dump a binary column to a file
  1750. /*java.io.File f = java.io.File.createTempFile("ole", ".bin");
  1751. java.io.FileOutputStream out = new java.io.FileOutputStream(f);
  1752. out.write(b);
  1753. out.flush();
  1754. out.close();*/
  1755. } else {
  1756. rtn.append(String.valueOf(obj));
  1757. }
  1758. if (iter.hasNext()) {
  1759. rtn.append("\t");
  1760. }
  1761. }
  1762. rtn.append("\n");
  1763. }
  1764. return rtn.toString();
  1765. }
  1766. /**
  1767. * Updates free space and row info for a new row of the given size in the
  1768. * given data page. Positions the page for writing the row data.
  1769. * @return the row number of the new row
  1770. * @usage _advanced_method_
  1771. */
  1772. public static int addDataPageRow(ByteBuffer dataPage,
  1773. int rowSize,
  1774. JetFormat format,
  1775. int rowFlags)
  1776. {
  1777. int rowSpaceUsage = getRowSpaceUsage(rowSize, format);
  1778. // Decrease free space record.
  1779. short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
  1780. dataPage.putShort(format.OFFSET_FREE_SPACE, (short) (freeSpaceInPage -
  1781. rowSpaceUsage));
  1782. // Increment row count record.
  1783. short rowCount = dataPage.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
  1784. dataPage.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE,
  1785. (short) (rowCount + 1));
  1786. // determine row position
  1787. short rowLocation = findRowEnd(dataPage, rowCount, format);
  1788. rowLocation -= rowSize;
  1789. // write row position
  1790. dataPage.putShort(getRowStartOffset(rowCount, format),
  1791. (short)(rowLocation | rowFlags));
  1792. // set position for row data
  1793. dataPage.position(rowLocation);
  1794. return rowCount;
  1795. }
  1796. /**
  1797. * Returns the row count for the current page. If the page is invalid
  1798. * ({@code null}) or the page is not a DATA page, 0 is returned.
  1799. */
  1800. private static int getRowsOnDataPage(ByteBuffer rowBuffer, JetFormat format)
  1801. throws IOException
  1802. {
  1803. int rowsOnPage = 0;
  1804. if((rowBuffer != null) && (rowBuffer.get(0) == PageTypes.DATA)) {
  1805. rowsOnPage = rowBuffer.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
  1806. }
  1807. return rowsOnPage;
  1808. }
  1809. /**
  1810. * @throws IllegalStateException if the given rowId is invalid
  1811. */
  1812. private static void requireValidRowId(RowId rowId) {
  1813. if(!rowId.isValid()) {
  1814. throw new IllegalArgumentException("Given rowId is invalid: " + rowId);
  1815. }
  1816. }
  1817. /**
  1818. * @throws IllegalStateException if the given row is invalid or deleted
  1819. */
  1820. private static void requireNonDeletedRow(RowState rowState, RowId rowId) {
  1821. if(!rowState.isValid()) {
  1822. throw new IllegalArgumentException(
  1823. "Given rowId is invalid for this table: " + rowId);
  1824. }
  1825. if(rowState.isDeleted()) {
  1826. throw new IllegalStateException("Row is deleted: " + rowId);
  1827. }
  1828. }
  1829. /**
  1830. * @usage _advanced_method_
  1831. */
  1832. public static boolean isDeletedRow(short rowStart) {
  1833. return ((rowStart & DELETED_ROW_MASK) != 0);
  1834. }
  1835. /**
  1836. * @usage _advanced_method_
  1837. */
  1838. public static boolean isOverflowRow(short rowStart) {
  1839. return ((rowStart & OVERFLOW_ROW_MASK) != 0);
  1840. }
  1841. /**
  1842. * @usage _advanced_method_
  1843. */
  1844. public static short cleanRowStart(short rowStart) {
  1845. return (short)(rowStart & OFFSET_MASK);
  1846. }
  1847. /**
  1848. * @usage _advanced_method_
  1849. */
  1850. public static short findRowStart(ByteBuffer buffer, int rowNum,
  1851. JetFormat format)
  1852. {
  1853. return cleanRowStart(
  1854. buffer.getShort(getRowStartOffset(rowNum, format)));
  1855. }
  1856. /**
  1857. * @usage _advanced_method_
  1858. */
  1859. public static int getRowStartOffset(int rowNum, JetFormat format)
  1860. {
  1861. return format.OFFSET_ROW_START + (format.SIZE_ROW_LOCATION * rowNum);
  1862. }
  1863. /**
  1864. * @usage _advanced_method_
  1865. */
  1866. public static short findRowEnd(ByteBuffer buffer, int rowNum,
  1867. JetFormat format)
  1868. {
  1869. return (short)((rowNum == 0) ?
  1870. format.PAGE_SIZE :
  1871. cleanRowStart(
  1872. buffer.getShort(getRowEndOffset(rowNum, format))));
  1873. }
  1874. /**
  1875. * @usage _advanced_method_
  1876. */
  1877. public static int getRowEndOffset(int rowNum, JetFormat format)
  1878. {
  1879. return format.OFFSET_ROW_START + (format.SIZE_ROW_LOCATION * (rowNum - 1));
  1880. }
  1881. /**
  1882. * @usage _advanced_method_
  1883. */
  1884. public static int getRowSpaceUsage(int rowSize, JetFormat format)
  1885. {
  1886. return rowSize + format.SIZE_ROW_LOCATION;
  1887. }
  1888. /**
  1889. * @return the "AutoNumber" columns in the given collection of columns.
  1890. * @usage _advanced_method_
  1891. */
  1892. public static List<Column> getAutoNumberColumns(Collection<Column> columns) {
  1893. List<Column> autoCols = new ArrayList<Column>(1);
  1894. for(Column c : columns) {
  1895. if(c.isAutoNumber()) {
  1896. autoCols.add(c);
  1897. }
  1898. }
  1899. return autoCols;
  1900. }
  1901. /**
  1902. * Returns {@code true} if a row of the given size will fit on the given
  1903. * data page, {@code false} otherwise.
  1904. * @usage _advanced_method_
  1905. */
  1906. public static boolean rowFitsOnDataPage(
  1907. int rowLength, ByteBuffer dataPage, JetFormat format)
  1908. throws IOException
  1909. {
  1910. int rowSpaceUsage = getRowSpaceUsage(rowLength, format);
  1911. short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
  1912. int rowsOnPage = getRowsOnDataPage(dataPage, format);
  1913. return ((rowSpaceUsage <= freeSpaceInPage) &&
  1914. (rowsOnPage < format.MAX_NUM_ROWS_ON_DATA_PAGE));
  1915. }
  1916. /**
  1917. * Duplicates and returns a row of data, optionally with a longer length
  1918. * filled with {@code null}.
  1919. */
  1920. static Object[] dupeRow(Object[] row, int newRowLength) {
  1921. Object[] copy = new Object[newRowLength];
  1922. System.arraycopy(row, 0, copy, 0, Math.min(row.length, newRowLength));
  1923. return copy;
  1924. }
  1925. /** various statuses for the row data */
  1926. private enum RowStatus {
  1927. INIT, INVALID_PAGE, INVALID_ROW, VALID, DELETED, NORMAL, OVERFLOW;
  1928. }
  1929. /** the phases the RowState moves through as the data is parsed */
  1930. private enum RowStateStatus {
  1931. INIT, AT_HEADER, AT_FINAL;
  1932. }
  1933. /**
  1934. * Maintains the state of reading a row of data.
  1935. * @usage _advanced_class_
  1936. */
  1937. public final class RowState
  1938. {
  1939. /** Buffer used for reading the header row data pages */
  1940. private final TempPageHolder _headerRowBufferH;
  1941. /** the header rowId */
  1942. private RowId _headerRowId = RowId.FIRST_ROW_ID;
  1943. /** the number of rows on the header page */
  1944. private int _rowsOnHeaderPage;
  1945. /** the rowState status */
  1946. private RowStateStatus _status = RowStateStatus.INIT;
  1947. /** the row status */
  1948. private RowStatus _rowStatus = RowStatus.INIT;
  1949. /** buffer used for reading overflow pages */
  1950. private final TempPageHolder _overflowRowBufferH =
  1951. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  1952. /** the row buffer which contains the final data (after following any
  1953. overflow pointers) */
  1954. private ByteBuffer _finalRowBuffer;
  1955. /** the rowId which contains the final data (after following any overflow
  1956. pointers) */
  1957. private RowId _finalRowId = null;
  1958. /** true if the row values array has data */
  1959. private boolean _haveRowValues;
  1960. /** values read from the last row */
  1961. private final Object[] _rowValues;
  1962. /** null mask for the last row */
  1963. private NullMask _nullMask;
  1964. /** last modification count seen on the table we track this so that the
  1965. rowState can detect updates to the table and re-read any buffered
  1966. data */
  1967. private int _lastModCount;
  1968. /** optional error handler to use when row errors are encountered */
  1969. private ErrorHandler _errorHandler;
  1970. /** cached variable column offsets for jump-table based rows */
  1971. private short[] _varColOffsets;
  1972. private RowState(TempBufferHolder.Type headerType) {
  1973. _headerRowBufferH = TempPageHolder.newHolder(headerType);
  1974. _rowValues = new Object[Table.this.getColumnCount()];
  1975. _lastModCount = Table.this._modCount;
  1976. }
  1977. public Table getTable() {
  1978. return Table.this;
  1979. }
  1980. public ErrorHandler getErrorHandler() {
  1981. return((_errorHandler != null) ? _errorHandler :
  1982. getTable().getErrorHandler());
  1983. }
  1984. public void setErrorHandler(ErrorHandler newErrorHandler) {
  1985. _errorHandler = newErrorHandler;
  1986. }
  1987. public void reset() {
  1988. _finalRowId = null;
  1989. _finalRowBuffer = null;
  1990. _rowsOnHeaderPage = 0;
  1991. _status = RowStateStatus.INIT;
  1992. _rowStatus = RowStatus.INIT;
  1993. _varColOffsets = null;
  1994. _nullMask = null;
  1995. if(_haveRowValues) {
  1996. Arrays.fill(_rowValues, null);
  1997. _haveRowValues = false;
  1998. }
  1999. }
  2000. public boolean isUpToDate() {
  2001. return(Table.this._modCount == _lastModCount);
  2002. }
  2003. private void checkForModification() {
  2004. if(!isUpToDate()) {
  2005. reset();
  2006. _headerRowBufferH.invalidate();
  2007. _overflowRowBufferH.invalidate();
  2008. _lastModCount = Table.this._modCount;
  2009. }
  2010. }
  2011. private ByteBuffer getFinalPage()
  2012. throws IOException
  2013. {
  2014. if(_finalRowBuffer == null) {
  2015. // (re)load current page
  2016. _finalRowBuffer = getHeaderPage();
  2017. }
  2018. return _finalRowBuffer;
  2019. }
  2020. public RowId getFinalRowId() {
  2021. if(_finalRowId == null) {
  2022. _finalRowId = getHeaderRowId();
  2023. }
  2024. return _finalRowId;
  2025. }
  2026. private void setRowStatus(RowStatus rowStatus) {
  2027. _rowStatus = rowStatus;
  2028. }
  2029. public boolean isValid() {
  2030. return(_rowStatus.ordinal() >= RowStatus.VALID.ordinal());
  2031. }
  2032. public boolean isDeleted() {
  2033. return(_rowStatus == RowStatus.DELETED);
  2034. }
  2035. public boolean isOverflow() {
  2036. return(_rowStatus == RowStatus.OVERFLOW);
  2037. }
  2038. public boolean isHeaderPageNumberValid() {
  2039. return(_rowStatus.ordinal() > RowStatus.INVALID_PAGE.ordinal());
  2040. }
  2041. public boolean isHeaderRowNumberValid() {
  2042. return(_rowStatus.ordinal() > RowStatus.INVALID_ROW.ordinal());
  2043. }
  2044. private void setStatus(RowStateStatus status) {
  2045. _status = status;
  2046. }
  2047. public boolean isAtHeaderRow() {
  2048. return(_status.ordinal() >= RowStateStatus.AT_HEADER.ordinal());
  2049. }
  2050. public boolean isAtFinalRow() {
  2051. return(_status.ordinal() >= RowStateStatus.AT_FINAL.ordinal());
  2052. }
  2053. private Object setRowValue(int idx, Object value) {
  2054. _haveRowValues = true;
  2055. _rowValues[idx] = value;
  2056. return value;
  2057. }
  2058. public Object[] getRowValues() {
  2059. return dupeRow(_rowValues, _rowValues.length);
  2060. }
  2061. public NullMask getNullMask(ByteBuffer rowBuffer) throws IOException {
  2062. if(_nullMask == null) {
  2063. _nullMask = getRowNullMask(rowBuffer);
  2064. }
  2065. return _nullMask;
  2066. }
  2067. private short[] getVarColOffsets() {
  2068. return _varColOffsets;
  2069. }
  2070. private void setVarColOffsets(short[] varColOffsets) {
  2071. _varColOffsets = varColOffsets;
  2072. }
  2073. public RowId getHeaderRowId() {
  2074. return _headerRowId;
  2075. }
  2076. public int getRowsOnHeaderPage() {
  2077. return _rowsOnHeaderPage;
  2078. }
  2079. private ByteBuffer getHeaderPage()
  2080. throws IOException
  2081. {
  2082. checkForModification();
  2083. return _headerRowBufferH.getPage(getPageChannel());
  2084. }
  2085. private ByteBuffer setHeaderRow(RowId rowId)
  2086. throws IOException
  2087. {
  2088. checkForModification();
  2089. // don't do any work if we are already positioned correctly
  2090. if(isAtHeaderRow() && (getHeaderRowId().equals(rowId))) {
  2091. return(isValid() ? getHeaderPage() : null);
  2092. }
  2093. // rejigger everything
  2094. reset();
  2095. _headerRowId = rowId;
  2096. _finalRowId = rowId;
  2097. int pageNumber = rowId.getPageNumber();
  2098. int rowNumber = rowId.getRowNumber();
  2099. if((pageNumber < 0) || !_ownedPages.containsPageNumber(pageNumber)) {
  2100. setRowStatus(RowStatus.INVALID_PAGE);
  2101. return null;
  2102. }
  2103. _finalRowBuffer = _headerRowBufferH.setPage(getPageChannel(),
  2104. pageNumber);
  2105. _rowsOnHeaderPage = getRowsOnDataPage(_finalRowBuffer, getFormat());
  2106. if((rowNumber < 0) || (rowNumber >= _rowsOnHeaderPage)) {
  2107. setRowStatus(RowStatus.INVALID_ROW);
  2108. return null;
  2109. }
  2110. setRowStatus(RowStatus.VALID);
  2111. return _finalRowBuffer;
  2112. }
  2113. private ByteBuffer setOverflowRow(RowId rowId)
  2114. throws IOException
  2115. {
  2116. // this should never see modifications because it only happens within
  2117. // the positionAtRowData method
  2118. if(!isUpToDate()) {
  2119. throw new IllegalStateException("Table modified while searching?");
  2120. }
  2121. if(_rowStatus != RowStatus.OVERFLOW) {
  2122. throw new IllegalStateException("Row is not an overflow row?");
  2123. }
  2124. _finalRowId = rowId;
  2125. _finalRowBuffer = _overflowRowBufferH.setPage(getPageChannel(),
  2126. rowId.getPageNumber());
  2127. return _finalRowBuffer;
  2128. }
  2129. private Object handleRowError(Column column,
  2130. byte[] columnData,
  2131. Exception error)
  2132. throws IOException
  2133. {
  2134. return getErrorHandler().handleRowError(column, columnData,
  2135. this, error);
  2136. }
  2137. @Override
  2138. public String toString()
  2139. {
  2140. return "RowState: headerRowId = " + _headerRowId + ", finalRowId = " +
  2141. _finalRowId;
  2142. }
  2143. }
  2144. }