You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TableImpl.java 116KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683
  1. /*
  2. Copyright (c) 2005 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.BufferedWriter;
  15. import java.io.IOException;
  16. import java.io.StringWriter;
  17. import java.nio.BufferOverflowException;
  18. import java.nio.ByteBuffer;
  19. import java.nio.charset.Charset;
  20. import java.util.AbstractMap;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.Comparator;
  26. import java.util.HashMap;
  27. import java.util.Iterator;
  28. import java.util.LinkedHashSet;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import java.util.TreeSet;
  33. import com.healthmarketscience.jackcess.BatchUpdateException;
  34. import com.healthmarketscience.jackcess.Column;
  35. import com.healthmarketscience.jackcess.ColumnBuilder;
  36. import com.healthmarketscience.jackcess.ConstraintViolationException;
  37. import com.healthmarketscience.jackcess.CursorBuilder;
  38. import com.healthmarketscience.jackcess.Index;
  39. import com.healthmarketscience.jackcess.IndexBuilder;
  40. import com.healthmarketscience.jackcess.InvalidValueException;
  41. import com.healthmarketscience.jackcess.JackcessException;
  42. import com.healthmarketscience.jackcess.PropertyMap;
  43. import com.healthmarketscience.jackcess.Row;
  44. import com.healthmarketscience.jackcess.RowId;
  45. import com.healthmarketscience.jackcess.Table;
  46. import com.healthmarketscience.jackcess.expr.Identifier;
  47. import com.healthmarketscience.jackcess.util.ErrorHandler;
  48. import com.healthmarketscience.jackcess.util.ExportUtil;
  49. import org.apache.commons.lang3.builder.ToStringBuilder;
  50. import org.apache.commons.logging.Log;
  51. import org.apache.commons.logging.LogFactory;
  52. /**
  53. * A single database table
  54. * <p>
  55. * Is not thread-safe.
  56. *
  57. * @author Tim McCune
  58. * @usage _intermediate_class_
  59. */
  60. public class TableImpl implements Table, PropertyMaps.Owner
  61. {
  62. private static final Log LOG = LogFactory.getLog(TableImpl.class);
  63. private static final short OFFSET_MASK = (short)0x1FFF;
  64. private static final short DELETED_ROW_MASK = (short)0x8000;
  65. private static final short OVERFLOW_ROW_MASK = (short)0x4000;
  66. static final int MAGIC_TABLE_NUMBER = 1625;
  67. private static final int MAX_BYTE = 256;
  68. /**
  69. * Table type code for system tables
  70. * @usage _intermediate_class_
  71. */
  72. public static final byte TYPE_SYSTEM = 0x53;
  73. /**
  74. * Table type code for user tables
  75. * @usage _intermediate_class_
  76. */
  77. public static final byte TYPE_USER = 0x4e;
  78. public enum IndexFeature {
  79. EXACT_MATCH, EXACT_UNIQUE_ONLY, ANY_MATCH;
  80. }
  81. /** comparator which sorts variable length columns based on their index into
  82. the variable length offset table */
  83. private static final Comparator<ColumnImpl> VAR_LEN_COLUMN_COMPARATOR =
  84. new Comparator<ColumnImpl>() {
  85. @Override
  86. public int compare(ColumnImpl c1, ColumnImpl c2) {
  87. return ((c1.getVarLenTableIndex() < c2.getVarLenTableIndex()) ? -1 :
  88. ((c1.getVarLenTableIndex() > c2.getVarLenTableIndex()) ? 1 :
  89. 0));
  90. }
  91. };
  92. /** comparator which sorts columns based on their display index */
  93. private static final Comparator<ColumnImpl> DISPLAY_ORDER_COMPARATOR =
  94. new Comparator<ColumnImpl>() {
  95. @Override
  96. public int compare(ColumnImpl c1, ColumnImpl c2) {
  97. return ((c1.getDisplayIndex() < c2.getDisplayIndex()) ? -1 :
  98. ((c1.getDisplayIndex() > c2.getDisplayIndex()) ? 1 :
  99. 0));
  100. }
  101. };
  102. /** owning database */
  103. private final DatabaseImpl _database;
  104. /** additional table flags from the catalog entry */
  105. private final int _flags;
  106. /** Type of the table (either TYPE_SYSTEM or TYPE_USER) */
  107. private final byte _tableType;
  108. /** Number of actual indexes on the table */
  109. private int _indexCount;
  110. /** Number of logical indexes for the table */
  111. private int _logicalIndexCount;
  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 final List<ColumnImpl> _columns = new ArrayList<ColumnImpl>();
  120. /** List of variable length columns in this table, ordered by offset */
  121. private final List<ColumnImpl> _varColumns = new ArrayList<ColumnImpl>();
  122. /** List of autonumber columns in this table, ordered by column number */
  123. private final List<ColumnImpl> _autoNumColumns = new ArrayList<ColumnImpl>(1);
  124. /** handler for calculated columns */
  125. private final CalcColEvaluator _calcColEval = new CalcColEvaluator();
  126. /** List of indexes on this table (multiple logical indexes may be backed by
  127. the same index data) */
  128. private final List<IndexImpl> _indexes = new ArrayList<IndexImpl>();
  129. /** List of index datas on this table (the actual backing data for an
  130. index) */
  131. private final List<IndexData> _indexDatas = new ArrayList<IndexData>();
  132. /** List of columns in this table which are in one or more indexes */
  133. private final Set<ColumnImpl> _indexColumns = new LinkedHashSet<ColumnImpl>();
  134. /** Table name as stored in Database */
  135. private final String _name;
  136. /** Usage map of pages that this table owns */
  137. private final UsageMap _ownedPages;
  138. /** Usage map of pages that this table owns with free space on them */
  139. private final UsageMap _freeSpacePages;
  140. /** Number of rows in the table */
  141. private int _rowCount;
  142. /** last long auto number for the table */
  143. private int _lastLongAutoNumber;
  144. /** last complex type auto number for the table */
  145. private int _lastComplexTypeAutoNumber;
  146. /** modification count for the table, keeps row-states up-to-date */
  147. private int _modCount;
  148. /** page buffer used to update data pages when adding rows */
  149. private final TempPageHolder _addRowBufferH =
  150. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  151. /** page buffer used to update the table def page */
  152. private final TempPageHolder _tableDefBufferH =
  153. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  154. /** buffer used to writing rows of data */
  155. private final TempBufferHolder _writeRowBufferH =
  156. TempBufferHolder.newHolder(TempBufferHolder.Type.SOFT, true);
  157. /** page buffer used to write out-of-row "long value" data */
  158. private final TempPageHolder _longValueBufferH =
  159. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  160. /** optional error handler to use when row errors are encountered */
  161. private ErrorHandler _tableErrorHandler;
  162. /** properties for this table */
  163. private PropertyMap _props;
  164. /** properties group for this table (and columns) */
  165. private PropertyMaps _propertyMaps;
  166. /** optional flag indicating whether or not auto numbers can be directly
  167. inserted by the user */
  168. private Boolean _allowAutoNumInsert;
  169. /** foreign-key enforcer for this table */
  170. private final FKEnforcer _fkEnforcer;
  171. /** table validator if any (and enabled) */
  172. private RowValidatorEvalContext _rowValidator;
  173. /** default cursor for iterating through the table, kept here for basic
  174. table traversal */
  175. private CursorImpl _defaultCursor;
  176. /**
  177. * Only used by unit tests
  178. * @usage _advanced_method_
  179. */
  180. protected TableImpl(boolean testing, List<ColumnImpl> columns)
  181. throws IOException
  182. {
  183. if(!testing) {
  184. throw new IllegalArgumentException();
  185. }
  186. _database = null;
  187. _tableDefPageNumber = PageChannel.INVALID_PAGE_NUMBER;
  188. _name = null;
  189. _columns.addAll(columns);
  190. for(ColumnImpl col : _columns) {
  191. if(col.getType().isVariableLength()) {
  192. _varColumns.add(col);
  193. }
  194. }
  195. _maxColumnCount = (short)_columns.size();
  196. _maxVarColumnCount = (short)_varColumns.size();
  197. initAutoNumberColumns();
  198. _fkEnforcer = null;
  199. _flags = 0;
  200. _tableType = TYPE_USER;
  201. _indexCount = 0;
  202. _logicalIndexCount = 0;
  203. _ownedPages = null;
  204. _freeSpacePages = null;
  205. }
  206. /**
  207. * @param database database which owns this table
  208. * @param tableBuffer Buffer to read the table with
  209. * @param pageNumber Page number of the table definition
  210. * @param name Table name
  211. */
  212. protected TableImpl(DatabaseImpl database, ByteBuffer tableBuffer,
  213. int pageNumber, String name, int flags)
  214. throws IOException
  215. {
  216. _database = database;
  217. _tableDefPageNumber = pageNumber;
  218. _name = name;
  219. _flags = flags;
  220. // read table definition
  221. tableBuffer = loadCompleteTableDefinitionBuffer(tableBuffer, null);
  222. _rowCount = tableBuffer.getInt(getFormat().OFFSET_NUM_ROWS);
  223. _lastLongAutoNumber = tableBuffer.getInt(getFormat().OFFSET_NEXT_AUTO_NUMBER);
  224. if(getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER >= 0) {
  225. _lastComplexTypeAutoNumber = tableBuffer.getInt(
  226. getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER);
  227. }
  228. _tableType = tableBuffer.get(getFormat().OFFSET_TABLE_TYPE);
  229. _maxColumnCount = tableBuffer.getShort(getFormat().OFFSET_MAX_COLS);
  230. _maxVarColumnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_VAR_COLS);
  231. short columnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_COLS);
  232. _logicalIndexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEX_SLOTS);
  233. _indexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEXES);
  234. tableBuffer.position(getFormat().OFFSET_OWNED_PAGES);
  235. _ownedPages = UsageMap.read(getDatabase(), tableBuffer);
  236. tableBuffer.position(getFormat().OFFSET_FREE_SPACE_PAGES);
  237. _freeSpacePages = UsageMap.read(getDatabase(), tableBuffer);
  238. for (int i = 0; i < _indexCount; i++) {
  239. _indexDatas.add(IndexData.create(this, tableBuffer, i, getFormat()));
  240. }
  241. readColumnDefinitions(tableBuffer, columnCount);
  242. readIndexDefinitions(tableBuffer);
  243. // read column usage map info
  244. while((tableBuffer.remaining() >= 2) &&
  245. readColumnUsageMaps(tableBuffer)) {
  246. // keep reading ...
  247. }
  248. // re-sort columns if necessary
  249. if(getDatabase().getColumnOrder() != ColumnOrder.DATA) {
  250. Collections.sort(_columns, DISPLAY_ORDER_COMPARATOR);
  251. }
  252. for(ColumnImpl col : _columns) {
  253. // some columns need to do extra work after the table is completely
  254. // loaded
  255. col.postTableLoadInit();
  256. }
  257. _fkEnforcer = new FKEnforcer(this);
  258. if(!isSystem()) {
  259. // after fully constructed, allow column/row validators to be configured
  260. // (but only for user tables)
  261. for(ColumnImpl col : _columns) {
  262. col.initColumnValidator();
  263. }
  264. reloadRowValidator();
  265. }
  266. }
  267. private void reloadRowValidator() throws IOException {
  268. // reset table row validator before proceeding
  269. _rowValidator = null;
  270. if(!getDatabase().isEvaluateExpressions()) {
  271. return;
  272. }
  273. PropertyMap props = getProperties();
  274. String exprStr = PropertyMaps.getTrimmedStringProperty(
  275. props, PropertyMap.VALIDATION_RULE_PROP);
  276. if(exprStr != null) {
  277. String helpStr = PropertyMaps.getTrimmedStringProperty(
  278. props, PropertyMap.VALIDATION_TEXT_PROP);
  279. _rowValidator = new RowValidatorEvalContext(this)
  280. .setExpr(exprStr, helpStr);
  281. }
  282. }
  283. @Override
  284. public String getName() {
  285. return _name;
  286. }
  287. @Override
  288. public boolean isHidden() {
  289. return((_flags & DatabaseImpl.HIDDEN_OBJECT_FLAG) != 0);
  290. }
  291. @Override
  292. public boolean isSystem() {
  293. return(_tableType != TYPE_USER);
  294. }
  295. /**
  296. * @usage _advanced_method_
  297. */
  298. public int getMaxColumnCount() {
  299. return _maxColumnCount;
  300. }
  301. @Override
  302. public int getColumnCount() {
  303. return _columns.size();
  304. }
  305. @Override
  306. public DatabaseImpl getDatabase() {
  307. return _database;
  308. }
  309. /**
  310. * @usage _advanced_method_
  311. */
  312. public JetFormat getFormat() {
  313. return getDatabase().getFormat();
  314. }
  315. /**
  316. * @usage _advanced_method_
  317. */
  318. public PageChannel getPageChannel() {
  319. return getDatabase().getPageChannel();
  320. }
  321. @Override
  322. public ErrorHandler getErrorHandler() {
  323. return((_tableErrorHandler != null) ? _tableErrorHandler :
  324. getDatabase().getErrorHandler());
  325. }
  326. @Override
  327. public void setErrorHandler(ErrorHandler newErrorHandler) {
  328. _tableErrorHandler = newErrorHandler;
  329. }
  330. public int getTableDefPageNumber() {
  331. return _tableDefPageNumber;
  332. }
  333. @Override
  334. public boolean isAllowAutoNumberInsert() {
  335. return ((_allowAutoNumInsert != null) ? (boolean)_allowAutoNumInsert :
  336. getDatabase().isAllowAutoNumberInsert());
  337. }
  338. @Override
  339. public void setAllowAutoNumberInsert(Boolean allowAutoNumInsert) {
  340. _allowAutoNumInsert = allowAutoNumInsert;
  341. }
  342. /**
  343. * @usage _advanced_method_
  344. */
  345. public RowState createRowState() {
  346. return new RowState(TempBufferHolder.Type.HARD);
  347. }
  348. /**
  349. * @usage _advanced_method_
  350. */
  351. public UsageMap.PageCursor getOwnedPagesCursor() {
  352. return _ownedPages.cursor();
  353. }
  354. /**
  355. * Returns the <i>approximate</i> number of database pages owned by this
  356. * table and all related indexes (this number does <i>not</i> take into
  357. * account pages used for large OLE/MEMO fields).
  358. * <p>
  359. * To calculate the approximate number of bytes owned by a table:
  360. * <code>
  361. * int approxTableBytes = (table.getApproximateOwnedPageCount() *
  362. * table.getFormat().PAGE_SIZE);
  363. * </code>
  364. * @usage _intermediate_method_
  365. */
  366. public int getApproximateOwnedPageCount() {
  367. // add a page for the table def (although that might actually be more than
  368. // one page)
  369. int count = _ownedPages.getPageCount() + 1;
  370. for(ColumnImpl col : _columns) {
  371. count += col.getOwnedPageCount();
  372. }
  373. // note, we count owned pages from _physical_ indexes, not logical indexes
  374. // (otherwise we could double count pages)
  375. for(IndexData indexData : _indexDatas) {
  376. count += indexData.getOwnedPageCount();
  377. }
  378. return count;
  379. }
  380. protected TempPageHolder getLongValueBuffer() {
  381. return _longValueBufferH;
  382. }
  383. @Override
  384. public List<ColumnImpl> getColumns() {
  385. return Collections.unmodifiableList(_columns);
  386. }
  387. @Override
  388. public ColumnImpl getColumn(String name) {
  389. for(ColumnImpl column : _columns) {
  390. if(column.getName().equalsIgnoreCase(name)) {
  391. return column;
  392. }
  393. }
  394. throw new IllegalArgumentException(withErrorContext(
  395. "Column with name " + name + " does not exist in this table"));
  396. }
  397. public boolean hasColumn(String name) {
  398. for(ColumnImpl column : _columns) {
  399. if(column.getName().equalsIgnoreCase(name)) {
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405. @Override
  406. public PropertyMap getProperties() throws IOException {
  407. if(_props == null) {
  408. _props = getPropertyMaps().getDefault();
  409. }
  410. return _props;
  411. }
  412. /**
  413. * @return all PropertyMaps for this table (and columns)
  414. * @usage _advanced_method_
  415. */
  416. public PropertyMaps getPropertyMaps() throws IOException {
  417. if(_propertyMaps == null) {
  418. _propertyMaps = getDatabase().getPropertiesForObject(
  419. _tableDefPageNumber, this);
  420. }
  421. return _propertyMaps;
  422. }
  423. @Override
  424. public void propertiesUpdated() throws IOException {
  425. // propagate update to columns
  426. for(ColumnImpl col : _columns) {
  427. col.propertiesUpdated();
  428. }
  429. reloadRowValidator();
  430. // calculated columns will need to be re-sorted (their expressions may
  431. // have changed when their properties were updated)
  432. _calcColEval.reSort();
  433. }
  434. @Override
  435. public List<IndexImpl> getIndexes() {
  436. return Collections.unmodifiableList(_indexes);
  437. }
  438. @Override
  439. public IndexImpl getIndex(String name) {
  440. for(IndexImpl index : _indexes) {
  441. if(index.getName().equalsIgnoreCase(name)) {
  442. return index;
  443. }
  444. }
  445. throw new IllegalArgumentException(withErrorContext(
  446. "Index with name " + name + " does not exist on this table"));
  447. }
  448. @Override
  449. public IndexImpl getPrimaryKeyIndex() {
  450. for(IndexImpl index : _indexes) {
  451. if(index.isPrimaryKey()) {
  452. return index;
  453. }
  454. }
  455. throw new IllegalArgumentException(withErrorContext(
  456. "No primary key index found"));
  457. }
  458. @Override
  459. public IndexImpl getForeignKeyIndex(Table otherTable) {
  460. for(IndexImpl index : _indexes) {
  461. if(index.isForeignKey() && (index.getReference() != null) &&
  462. (index.getReference().getOtherTablePageNumber() ==
  463. ((TableImpl)otherTable).getTableDefPageNumber())) {
  464. return index;
  465. }
  466. }
  467. throw new IllegalArgumentException(withErrorContext(
  468. "No foreign key reference to " +
  469. otherTable.getName() + " found"));
  470. }
  471. /**
  472. * @return All of the IndexData on this table (unmodifiable List)
  473. * @usage _advanced_method_
  474. */
  475. public List<IndexData> getIndexDatas() {
  476. return Collections.unmodifiableList(_indexDatas);
  477. }
  478. /**
  479. * Only called by unit tests
  480. * @usage _advanced_method_
  481. */
  482. public int getLogicalIndexCount() {
  483. return _logicalIndexCount;
  484. }
  485. int getIndexCount() {
  486. return _indexCount;
  487. }
  488. public IndexImpl findIndexForColumns(Collection<String> searchColumns,
  489. IndexFeature feature) {
  490. IndexImpl partialIndex = null;
  491. for(IndexImpl index : _indexes) {
  492. Collection<? extends Index.Column> indexColumns = index.getColumns();
  493. if(indexColumns.size() < searchColumns.size()) {
  494. continue;
  495. }
  496. boolean exactMatch = (indexColumns.size() == searchColumns.size());
  497. Iterator<String> sIter = searchColumns.iterator();
  498. Iterator<? extends Index.Column> iIter = indexColumns.iterator();
  499. boolean searchMatches = true;
  500. while(sIter.hasNext()) {
  501. String sColName = sIter.next();
  502. String iColName = iIter.next().getName();
  503. if((sColName != iColName) &&
  504. ((sColName == null) || !sColName.equalsIgnoreCase(iColName))) {
  505. searchMatches = false;
  506. break;
  507. }
  508. }
  509. if(searchMatches) {
  510. if(exactMatch && ((feature != IndexFeature.EXACT_UNIQUE_ONLY) ||
  511. index.isUnique())) {
  512. return index;
  513. }
  514. if(!exactMatch && (feature == IndexFeature.ANY_MATCH) &&
  515. ((partialIndex == null) ||
  516. (indexColumns.size() < partialIndex.getColumnCount()))) {
  517. // this is a better partial index match
  518. partialIndex = index;
  519. }
  520. }
  521. }
  522. return partialIndex;
  523. }
  524. List<ColumnImpl> getAutoNumberColumns() {
  525. return _autoNumColumns;
  526. }
  527. @Override
  528. public CursorImpl getDefaultCursor() {
  529. if(_defaultCursor == null) {
  530. _defaultCursor = CursorImpl.createCursor(this);
  531. }
  532. return _defaultCursor;
  533. }
  534. @Override
  535. public CursorBuilder newCursor() {
  536. return new CursorBuilder(this);
  537. }
  538. @Override
  539. public void reset() {
  540. getDefaultCursor().reset();
  541. }
  542. @Override
  543. public Row deleteRow(Row row) throws IOException {
  544. deleteRow(row.getId());
  545. return row;
  546. }
  547. /**
  548. * Delete the row with the given id. Provided RowId must have previously
  549. * been returned from this Table.
  550. * @return the given rowId
  551. * @throws IllegalStateException if the given row is not valid
  552. * @usage _intermediate_method_
  553. */
  554. public RowId deleteRow(RowId rowId) throws IOException {
  555. deleteRow(getDefaultCursor().getRowState(), (RowIdImpl)rowId);
  556. return rowId;
  557. }
  558. /**
  559. * Delete the row for the given rowId.
  560. * @usage _advanced_method_
  561. */
  562. public void deleteRow(RowState rowState, RowIdImpl rowId)
  563. throws IOException
  564. {
  565. requireValidRowId(rowId);
  566. getPageChannel().startWrite();
  567. try {
  568. // ensure that the relevant row state is up-to-date
  569. ByteBuffer rowBuffer = positionAtRowHeader(rowState, rowId);
  570. if(rowState.isDeleted()) {
  571. // don't care about duplicate deletion
  572. return;
  573. }
  574. requireNonDeletedRow(rowState, rowId);
  575. // delete flag always gets set in the "header" row (even if data is on
  576. // overflow row)
  577. int pageNumber = rowState.getHeaderRowId().getPageNumber();
  578. int rowNumber = rowState.getHeaderRowId().getRowNumber();
  579. // attempt to fill in index column values
  580. Object[] rowValues = null;
  581. if(!_indexDatas.isEmpty()) {
  582. // move to row data to get index values
  583. rowBuffer = positionAtRowData(rowState, rowId);
  584. for(ColumnImpl idxCol : _indexColumns) {
  585. getRowColumn(getFormat(), rowBuffer, idxCol, rowState, null);
  586. }
  587. // use any read rowValues to help update the indexes
  588. rowValues = rowState.getRowCacheValues();
  589. // check foreign keys before proceeding w/ deletion
  590. _fkEnforcer.deleteRow(rowValues);
  591. // move back to the header
  592. rowBuffer = positionAtRowHeader(rowState, rowId);
  593. }
  594. // finally, pull the trigger
  595. int rowIndex = getRowStartOffset(rowNumber, getFormat());
  596. rowBuffer.putShort(rowIndex, (short)(rowBuffer.getShort(rowIndex)
  597. | DELETED_ROW_MASK | OVERFLOW_ROW_MASK));
  598. writeDataPage(rowBuffer, pageNumber);
  599. // update the indexes
  600. for(IndexData indexData : _indexDatas) {
  601. indexData.deleteRow(rowValues, rowId);
  602. }
  603. // make sure table def gets updated
  604. updateTableDefinition(-1);
  605. } finally {
  606. getPageChannel().finishWrite();
  607. }
  608. }
  609. @Override
  610. public Row getNextRow() throws IOException {
  611. return getDefaultCursor().getNextRow();
  612. }
  613. /**
  614. * Reads a single column from the given row.
  615. * @usage _advanced_method_
  616. */
  617. public Object getRowValue(RowState rowState, RowIdImpl rowId,
  618. ColumnImpl column)
  619. throws IOException
  620. {
  621. if(this != column.getTable()) {
  622. throw new IllegalArgumentException(withErrorContext(
  623. "Given column " + column + " is not from this table"));
  624. }
  625. requireValidRowId(rowId);
  626. // position at correct row
  627. ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
  628. requireNonDeletedRow(rowState, rowId);
  629. return getRowColumn(getFormat(), rowBuffer, column, rowState, null);
  630. }
  631. /**
  632. * Reads some columns from the given row.
  633. * @param columnNames Only column names in this collection will be returned
  634. * @usage _advanced_method_
  635. */
  636. public RowImpl getRow(
  637. RowState rowState, RowIdImpl rowId, Collection<String> columnNames)
  638. throws IOException
  639. {
  640. requireValidRowId(rowId);
  641. // position at correct row
  642. ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
  643. requireNonDeletedRow(rowState, rowId);
  644. return getRow(getFormat(), rowState, rowBuffer, _columns, columnNames);
  645. }
  646. /**
  647. * Reads the row data from the given row buffer. Leaves limit unchanged.
  648. * Saves parsed row values to the given rowState.
  649. */
  650. private static RowImpl getRow(
  651. JetFormat format,
  652. RowState rowState,
  653. ByteBuffer rowBuffer,
  654. Collection<ColumnImpl> columns,
  655. Collection<String> columnNames)
  656. throws IOException
  657. {
  658. RowImpl rtn = new RowImpl(rowState.getHeaderRowId(), columns.size());
  659. for(ColumnImpl column : columns) {
  660. if((columnNames == null) || (columnNames.contains(column.getName()))) {
  661. // Add the value to the row data
  662. column.setRowValue(
  663. rtn, getRowColumn(format, rowBuffer, column, rowState, null));
  664. }
  665. }
  666. return rtn;
  667. }
  668. /**
  669. * Reads the column data from the given row buffer. Leaves limit unchanged.
  670. * Caches the returned value in the rowState.
  671. */
  672. private static Object getRowColumn(JetFormat format,
  673. ByteBuffer rowBuffer,
  674. ColumnImpl column,
  675. RowState rowState,
  676. Map<ColumnImpl,byte[]> rawVarValues)
  677. throws IOException
  678. {
  679. byte[] columnData = null;
  680. try {
  681. NullMask nullMask = rowState.getNullMask(rowBuffer);
  682. boolean isNull = nullMask.isNull(column);
  683. if(column.storeInNullMask()) {
  684. // Boolean values are stored in the null mask. see note about
  685. // caching below
  686. return rowState.setRowCacheValue(column.getColumnIndex(),
  687. column.readFromNullMask(isNull));
  688. } else if(isNull) {
  689. // well, that's easy! (no need to update cache w/ null)
  690. return null;
  691. }
  692. Object cachedValue = rowState.getRowCacheValue(column.getColumnIndex());
  693. if(cachedValue != null) {
  694. // we already have it, use it
  695. return cachedValue;
  696. }
  697. // reset position to row start
  698. rowBuffer.reset();
  699. // locate the column data bytes
  700. int rowStart = rowBuffer.position();
  701. int colDataPos = 0;
  702. int colDataLen = 0;
  703. if(!column.isVariableLength()) {
  704. // read fixed length value (non-boolean at this point)
  705. int dataStart = rowStart + format.OFFSET_COLUMN_FIXED_DATA_ROW_OFFSET;
  706. colDataPos = dataStart + column.getFixedDataOffset();
  707. colDataLen = column.getType().getFixedSize(column.getLength());
  708. } else {
  709. int varDataStart;
  710. int varDataEnd;
  711. if(format.SIZE_ROW_VAR_COL_OFFSET == 2) {
  712. // read simple var length value
  713. int varColumnOffsetPos =
  714. (rowBuffer.limit() - nullMask.byteSize() - 4) -
  715. (column.getVarLenTableIndex() * 2);
  716. varDataStart = rowBuffer.getShort(varColumnOffsetPos);
  717. varDataEnd = rowBuffer.getShort(varColumnOffsetPos - 2);
  718. } else {
  719. // read jump-table based var length values
  720. short[] varColumnOffsets = readJumpTableVarColOffsets(
  721. rowState, rowBuffer, rowStart, nullMask);
  722. varDataStart = varColumnOffsets[column.getVarLenTableIndex()];
  723. varDataEnd = varColumnOffsets[column.getVarLenTableIndex() + 1];
  724. }
  725. colDataPos = rowStart + varDataStart;
  726. colDataLen = varDataEnd - varDataStart;
  727. }
  728. // grab the column data
  729. rowBuffer.position(colDataPos);
  730. columnData = ByteUtil.getBytes(rowBuffer, colDataLen);
  731. if((rawVarValues != null) && column.isVariableLength()) {
  732. // caller wants raw value as well
  733. rawVarValues.put(column, columnData);
  734. }
  735. // parse the column data. we cache the row values in order to be able
  736. // to update the index on row deletion. note, most of the returned
  737. // values are immutable, except for binary data (returned as byte[]),
  738. // but binary data shouldn't be indexed anyway.
  739. return rowState.setRowCacheValue(column.getColumnIndex(),
  740. column.read(columnData));
  741. } catch(Exception e) {
  742. // cache "raw" row value. see note about caching above
  743. rowState.setRowCacheValue(column.getColumnIndex(),
  744. ColumnImpl.rawDataWrapper(columnData));
  745. return rowState.handleRowError(column, columnData, e);
  746. }
  747. }
  748. private static short[] readJumpTableVarColOffsets(
  749. RowState rowState, ByteBuffer rowBuffer, int rowStart,
  750. NullMask nullMask)
  751. {
  752. short[] varColOffsets = rowState.getVarColOffsets();
  753. if(varColOffsets != null) {
  754. return varColOffsets;
  755. }
  756. // calculate offsets using jump-table info
  757. int nullMaskSize = nullMask.byteSize();
  758. int rowEnd = rowStart + rowBuffer.remaining() - 1;
  759. int numVarCols = ByteUtil.getUnsignedByte(rowBuffer,
  760. rowEnd - nullMaskSize);
  761. varColOffsets = new short[numVarCols + 1];
  762. int rowLen = rowEnd - rowStart + 1;
  763. int numJumps = (rowLen - 1) / MAX_BYTE;
  764. int colOffset = rowEnd - nullMaskSize - numJumps - 1;
  765. // If last jump is a dummy value, ignore it
  766. if(((colOffset - rowStart - numVarCols) / MAX_BYTE) < numJumps) {
  767. numJumps--;
  768. }
  769. int jumpsUsed = 0;
  770. for(int i = 0; i < numVarCols + 1; i++) {
  771. while((jumpsUsed < numJumps) &&
  772. (i == ByteUtil.getUnsignedByte(
  773. rowBuffer, rowEnd - nullMaskSize-jumpsUsed - 1))) {
  774. jumpsUsed++;
  775. }
  776. varColOffsets[i] = (short)
  777. (ByteUtil.getUnsignedByte(rowBuffer, colOffset - i)
  778. + (jumpsUsed * MAX_BYTE));
  779. }
  780. rowState.setVarColOffsets(varColOffsets);
  781. return varColOffsets;
  782. }
  783. /**
  784. * Reads the null mask from the given row buffer. Leaves limit unchanged.
  785. */
  786. private NullMask getRowNullMask(ByteBuffer rowBuffer)
  787. throws IOException
  788. {
  789. // reset position to row start
  790. rowBuffer.reset();
  791. // Number of columns in this row
  792. int columnCount = ByteUtil.getUnsignedVarInt(
  793. rowBuffer, getFormat().SIZE_ROW_COLUMN_COUNT);
  794. // read null mask
  795. NullMask nullMask = new NullMask(columnCount);
  796. rowBuffer.position(rowBuffer.limit() - nullMask.byteSize()); //Null mask at end
  797. nullMask.read(rowBuffer);
  798. return nullMask;
  799. }
  800. /**
  801. * Sets a new buffer to the correct row header page using the given rowState
  802. * according to the given rowId. Deleted state is
  803. * determined, but overflow row pointers are not followed.
  804. *
  805. * @return a ByteBuffer of the relevant page, or null if row was invalid
  806. * @usage _advanced_method_
  807. */
  808. public static ByteBuffer positionAtRowHeader(RowState rowState,
  809. RowIdImpl rowId)
  810. throws IOException
  811. {
  812. ByteBuffer rowBuffer = rowState.setHeaderRow(rowId);
  813. if(rowState.isAtHeaderRow()) {
  814. // this task has already been accomplished
  815. return rowBuffer;
  816. }
  817. if(!rowState.isValid()) {
  818. // this was an invalid page/row
  819. rowState.setStatus(RowStateStatus.AT_HEADER);
  820. return null;
  821. }
  822. // note, we don't use findRowStart here cause we need the unmasked value
  823. short rowStart = rowBuffer.getShort(
  824. getRowStartOffset(rowId.getRowNumber(),
  825. rowState.getTable().getFormat()));
  826. // check the deleted, overflow flags for the row (the "real" flags are
  827. // always set on the header row)
  828. RowStatus rowStatus = RowStatus.NORMAL;
  829. if(isDeletedRow(rowStart)) {
  830. rowStatus = RowStatus.DELETED;
  831. } else if(isOverflowRow(rowStart)) {
  832. rowStatus = RowStatus.OVERFLOW;
  833. }
  834. rowState.setRowStatus(rowStatus);
  835. rowState.setStatus(RowStateStatus.AT_HEADER);
  836. return rowBuffer;
  837. }
  838. /**
  839. * Sets the position and limit in a new buffer using the given rowState
  840. * according to the given row number and row end, following overflow row
  841. * pointers as necessary.
  842. *
  843. * @return a ByteBuffer narrowed to the actual row data, or null if row was
  844. * invalid or deleted
  845. * @usage _advanced_method_
  846. */
  847. public static ByteBuffer positionAtRowData(RowState rowState,
  848. RowIdImpl rowId)
  849. throws IOException
  850. {
  851. positionAtRowHeader(rowState, rowId);
  852. if(!rowState.isValid() || rowState.isDeleted()) {
  853. // row is invalid or deleted
  854. rowState.setStatus(RowStateStatus.AT_FINAL);
  855. return null;
  856. }
  857. ByteBuffer rowBuffer = rowState.getFinalPage();
  858. int rowNum = rowState.getFinalRowId().getRowNumber();
  859. JetFormat format = rowState.getTable().getFormat();
  860. if(rowState.isAtFinalRow()) {
  861. // we've already found the final row data
  862. return PageChannel.narrowBuffer(
  863. rowBuffer,
  864. findRowStart(rowBuffer, rowNum, format),
  865. findRowEnd(rowBuffer, rowNum, format));
  866. }
  867. while(true) {
  868. // note, we don't use findRowStart here cause we need the unmasked value
  869. short rowStart = rowBuffer.getShort(getRowStartOffset(rowNum, format));
  870. short rowEnd = findRowEnd(rowBuffer, rowNum, format);
  871. // note, at this point we know the row is not deleted, so ignore any
  872. // subsequent deleted flags (as overflow rows are always marked deleted
  873. // anyway)
  874. boolean overflowRow = isOverflowRow(rowStart);
  875. // now, strip flags from rowStart offset
  876. rowStart = (short)(rowStart & OFFSET_MASK);
  877. if (overflowRow) {
  878. if((rowEnd - rowStart) < 4) {
  879. throw new IOException(rowState.getTable().withErrorContext(
  880. "invalid overflow row info"));
  881. }
  882. // Overflow page. the "row" data in the current page points to
  883. // another page/row
  884. int overflowRowNum = ByteUtil.getUnsignedByte(rowBuffer, rowStart);
  885. int overflowPageNum = ByteUtil.get3ByteInt(rowBuffer, rowStart + 1);
  886. rowBuffer = rowState.setOverflowRow(
  887. new RowIdImpl(overflowPageNum, overflowRowNum));
  888. rowNum = overflowRowNum;
  889. } else {
  890. rowState.setStatus(RowStateStatus.AT_FINAL);
  891. return PageChannel.narrowBuffer(rowBuffer, rowStart, rowEnd);
  892. }
  893. }
  894. }
  895. @Override
  896. public Iterator<Row> iterator() {
  897. return getDefaultCursor().iterator();
  898. }
  899. /**
  900. * Writes a new table defined by the given TableCreator to the database.
  901. * @usage _advanced_method_
  902. */
  903. protected static void writeTableDefinition(TableCreator creator)
  904. throws IOException
  905. {
  906. // first, create the usage map page
  907. createUsageMapDefinitionBuffer(creator);
  908. // next, determine how big the table def will be (in case it will be more
  909. // than one page)
  910. JetFormat format = creator.getFormat();
  911. int idxDataLen = (creator.getIndexCount() *
  912. (format.SIZE_INDEX_DEFINITION +
  913. format.SIZE_INDEX_COLUMN_BLOCK)) +
  914. (creator.getLogicalIndexCount() * format.SIZE_INDEX_INFO_BLOCK);
  915. int colUmapLen = creator.getLongValueColumns().size() * 10;
  916. int totalTableDefSize = format.SIZE_TDEF_HEADER +
  917. (format.SIZE_COLUMN_DEF_BLOCK * creator.getColumns().size()) +
  918. idxDataLen + colUmapLen + format.SIZE_TDEF_TRAILER;
  919. // total up the amount of space used by the column and index names (2
  920. // bytes per char + 2 bytes for the length)
  921. for(ColumnBuilder col : creator.getColumns()) {
  922. totalTableDefSize += DBMutator.calculateNameLength(col.getName());
  923. }
  924. for(IndexBuilder idx : creator.getIndexes()) {
  925. totalTableDefSize += DBMutator.calculateNameLength(idx.getName());
  926. }
  927. // now, create the table definition
  928. ByteBuffer buffer = PageChannel.createBuffer(Math.max(totalTableDefSize,
  929. format.PAGE_SIZE));
  930. writeTableDefinitionHeader(creator, buffer, totalTableDefSize);
  931. if(creator.hasIndexes()) {
  932. // index row counts
  933. IndexData.writeRowCountDefinitions(creator, buffer);
  934. }
  935. // column definitions
  936. ColumnImpl.writeDefinitions(creator, buffer);
  937. if(creator.hasIndexes()) {
  938. // index and index data definitions
  939. IndexData.writeDefinitions(creator, buffer);
  940. IndexImpl.writeDefinitions(creator, buffer);
  941. }
  942. // column usage map references
  943. ColumnImpl.writeColUsageMapDefinitions(creator, buffer);
  944. //End of tabledef
  945. buffer.put((byte) 0xff);
  946. buffer.put((byte) 0xff);
  947. buffer.flip();
  948. // write table buffer to database
  949. writeTableDefinitionBuffer(buffer, creator.getTdefPageNumber(), creator,
  950. Collections.<Integer>emptyList());
  951. }
  952. private static void writeTableDefinitionBuffer(
  953. ByteBuffer buffer, int tdefPageNumber,
  954. TableMutator mutator, List<Integer> reservedPages)
  955. throws IOException
  956. {
  957. buffer.rewind();
  958. int totalTableDefSize = buffer.remaining();
  959. JetFormat format = mutator.getFormat();
  960. PageChannel pageChannel = mutator.getPageChannel();
  961. // write table buffer to database
  962. if(totalTableDefSize <= format.PAGE_SIZE) {
  963. // easy case, fits on one page
  964. // overwrite page free space
  965. buffer.putShort(format.OFFSET_FREE_SPACE,
  966. (short)(Math.max(
  967. format.PAGE_SIZE - totalTableDefSize - 8, 0)));
  968. // Write the tdef page to disk.
  969. buffer.clear();
  970. pageChannel.writePage(buffer, tdefPageNumber);
  971. } else {
  972. // need to split across multiple pages
  973. ByteBuffer partialTdef = pageChannel.createPageBuffer();
  974. buffer.rewind();
  975. int nextTdefPageNumber = PageChannel.INVALID_PAGE_NUMBER;
  976. while(buffer.hasRemaining()) {
  977. // reset for next write
  978. partialTdef.clear();
  979. if(nextTdefPageNumber == PageChannel.INVALID_PAGE_NUMBER) {
  980. // this is the first page. note, the first page already has the
  981. // page header, so no need to write it here
  982. nextTdefPageNumber = tdefPageNumber;
  983. } else {
  984. // write page header
  985. writeTablePageHeader(partialTdef);
  986. }
  987. // copy the next page of tdef bytes
  988. int curTdefPageNumber = nextTdefPageNumber;
  989. int writeLen = Math.min(partialTdef.remaining(), buffer.remaining());
  990. partialTdef.put(buffer.array(), buffer.position(), writeLen);
  991. ByteUtil.forward(buffer, writeLen);
  992. if(buffer.hasRemaining()) {
  993. // need a next page
  994. if(reservedPages.isEmpty()) {
  995. nextTdefPageNumber = pageChannel.allocateNewPage();
  996. } else {
  997. nextTdefPageNumber = reservedPages.remove(0);
  998. }
  999. partialTdef.putInt(format.OFFSET_NEXT_TABLE_DEF_PAGE,
  1000. nextTdefPageNumber);
  1001. }
  1002. // update page free space
  1003. partialTdef.putShort(format.OFFSET_FREE_SPACE,
  1004. (short)(Math.max(
  1005. partialTdef.remaining() - 8, 0)));
  1006. // write partial page to disk
  1007. pageChannel.writePage(partialTdef, curTdefPageNumber);
  1008. }
  1009. }
  1010. }
  1011. /**
  1012. * Writes a column defined by the given TableUpdater to this table.
  1013. * @usage _advanced_method_
  1014. */
  1015. protected ColumnImpl mutateAddColumn(TableUpdater mutator) throws IOException
  1016. {
  1017. ColumnBuilder column = mutator.getColumn();
  1018. JetFormat format = mutator.getFormat();
  1019. boolean isVarCol = column.isVariableLength();
  1020. boolean isLongVal = column.getType().isLongValue();
  1021. ////
  1022. // calculate how much more space we need in the table def
  1023. if(isLongVal) {
  1024. mutator.addTdefLen(10);
  1025. }
  1026. mutator.addTdefLen(format.SIZE_COLUMN_DEF_BLOCK);
  1027. int nameByteLen = DBMutator.calculateNameLength(column.getName());
  1028. mutator.addTdefLen(nameByteLen);
  1029. ////
  1030. // load current table definition and add space for new info
  1031. ByteBuffer tableBuffer = loadCompleteTableDefinitionBufferForUpdate(
  1032. mutator);
  1033. ColumnImpl newCol = null;
  1034. int umapPos = -1;
  1035. boolean success = false;
  1036. try {
  1037. ////
  1038. // update various bits of the table def
  1039. ByteUtil.forward(tableBuffer, 29);
  1040. tableBuffer.putShort((short)(_maxColumnCount + 1));
  1041. short varColCount = (short)(_varColumns.size() + (isVarCol ? 1 : 0));
  1042. tableBuffer.putShort(varColCount);
  1043. tableBuffer.putShort((short)(_columns.size() + 1));
  1044. // move to end of column def blocks
  1045. tableBuffer.position(format.SIZE_TDEF_HEADER +
  1046. (_indexCount * format.SIZE_INDEX_DEFINITION) +
  1047. (_columns.size() * format.SIZE_COLUMN_DEF_BLOCK));
  1048. // figure out the data offsets for the new column
  1049. int fixedOffset = 0;
  1050. int varOffset = 0;
  1051. if(column.isVariableLength()) {
  1052. // find the variable offset
  1053. for(ColumnImpl col : _varColumns) {
  1054. if(col.getVarLenTableIndex() >= varOffset) {
  1055. varOffset = col.getVarLenTableIndex() + 1;
  1056. }
  1057. }
  1058. } else {
  1059. // find the fixed offset
  1060. for(ColumnImpl col : _columns) {
  1061. if(!col.isVariableLength() &&
  1062. (col.getFixedDataOffset() >= fixedOffset)) {
  1063. fixedOffset = col.getFixedDataOffset() +
  1064. col.getType().getFixedSize(col.getLength());
  1065. }
  1066. }
  1067. }
  1068. mutator.setColumnOffsets(fixedOffset, varOffset, varOffset);
  1069. // insert space for the column definition and write it
  1070. int colDefPos = tableBuffer.position();
  1071. ByteUtil.insertEmptyData(tableBuffer, format.SIZE_COLUMN_DEF_BLOCK);
  1072. ColumnImpl.writeDefinition(mutator, column, tableBuffer);
  1073. // skip existing column names and write new name
  1074. skipNames(tableBuffer, _columns.size());
  1075. ByteUtil.insertEmptyData(tableBuffer, nameByteLen);
  1076. writeName(tableBuffer, column.getName(), mutator.getCharset());
  1077. if(isLongVal) {
  1078. // allocate usage maps for the long value col
  1079. Map.Entry<Integer,Integer> umapInfo = addUsageMaps(2, null);
  1080. TableMutator.ColumnState colState = mutator.getColumnState(column);
  1081. colState.setUmapPageNumber(umapInfo.getKey());
  1082. byte rowNum = umapInfo.getValue().byteValue();
  1083. colState.setUmapOwnedRowNumber(rowNum);
  1084. colState.setUmapFreeRowNumber((byte)(rowNum + 1));
  1085. // skip past index defs
  1086. ByteUtil.forward(tableBuffer, (_indexCount *
  1087. format.SIZE_INDEX_COLUMN_BLOCK));
  1088. ByteUtil.forward(tableBuffer,
  1089. (_logicalIndexCount * format.SIZE_INDEX_INFO_BLOCK));
  1090. skipNames(tableBuffer, _logicalIndexCount);
  1091. // skip existing usage maps
  1092. while(tableBuffer.remaining() >= 2) {
  1093. if(tableBuffer.getShort() == IndexData.COLUMN_UNUSED) {
  1094. // found end of tdef, we want to insert before this
  1095. ByteUtil.forward(tableBuffer, -2);
  1096. break;
  1097. }
  1098. ByteUtil.forward(tableBuffer, 8);
  1099. // keep reading ...
  1100. }
  1101. // write new column usage map info
  1102. umapPos = tableBuffer.position();
  1103. ByteUtil.insertEmptyData(tableBuffer, 10);
  1104. ColumnImpl.writeColUsageMapDefinition(
  1105. mutator, column, tableBuffer);
  1106. }
  1107. // sanity check the updates
  1108. validateTableDefUpdate(mutator, tableBuffer);
  1109. // before writing the new table def, create the column
  1110. newCol = ColumnImpl.create(this, tableBuffer, colDefPos,
  1111. column.getName(), _columns.size());
  1112. newCol.setColumnIndex(_columns.size());
  1113. ////
  1114. // write updated table def back to the database
  1115. writeTableDefinitionBuffer(tableBuffer, _tableDefPageNumber, mutator,
  1116. mutator.getNextPages());
  1117. success = true;
  1118. } finally {
  1119. if(!success) {
  1120. // need to discard modified table buffer
  1121. _tableDefBufferH.invalidate();
  1122. }
  1123. }
  1124. ////
  1125. // now, update current TableImpl
  1126. _columns.add(newCol);
  1127. ++_maxColumnCount;
  1128. if(newCol.isVariableLength()) {
  1129. _varColumns.add(newCol);
  1130. ++_maxVarColumnCount;
  1131. }
  1132. if(newCol.isAutoNumber()) {
  1133. _autoNumColumns.add(newCol);
  1134. }
  1135. if(newCol.isCalculated()) {
  1136. _calcColEval.add(newCol);
  1137. }
  1138. if(umapPos >= 0) {
  1139. // read column usage map
  1140. tableBuffer.position(umapPos);
  1141. readColumnUsageMaps(tableBuffer);
  1142. }
  1143. newCol.postTableLoadInit();
  1144. if(!isSystem()) {
  1145. // after fully constructed, allow column validator to be configured (but
  1146. // only for user tables)
  1147. newCol.initColumnValidator();
  1148. }
  1149. // save any column properties
  1150. Map<String,PropertyMap.Property> colProps = column.getProperties();
  1151. if(colProps != null) {
  1152. newCol.getProperties().putAll(colProps.values());
  1153. getProperties().save();
  1154. }
  1155. completeTableMutation(tableBuffer);
  1156. return newCol;
  1157. }
  1158. /**
  1159. * Writes a index defined by the given TableUpdater to this table.
  1160. * @usage _advanced_method_
  1161. */
  1162. protected IndexData mutateAddIndexData(TableUpdater mutator) throws IOException
  1163. {
  1164. IndexBuilder index = mutator.getIndex();
  1165. JetFormat format = mutator.getFormat();
  1166. ////
  1167. // calculate how much more space we need in the table def
  1168. mutator.addTdefLen(format.SIZE_INDEX_DEFINITION +
  1169. format.SIZE_INDEX_COLUMN_BLOCK);
  1170. ////
  1171. // load current table definition and add space for new info
  1172. ByteBuffer tableBuffer = loadCompleteTableDefinitionBufferForUpdate(
  1173. mutator);
  1174. IndexData newIdxData = null;
  1175. boolean success = false;
  1176. try {
  1177. ////
  1178. // update various bits of the table def
  1179. ByteUtil.forward(tableBuffer, 39);
  1180. tableBuffer.putInt(_indexCount + 1);
  1181. // move to end of index data def blocks
  1182. tableBuffer.position(format.SIZE_TDEF_HEADER +
  1183. (_indexCount * format.SIZE_INDEX_DEFINITION));
  1184. // write index row count definition (empty initially)
  1185. ByteUtil.insertEmptyData(tableBuffer, format.SIZE_INDEX_DEFINITION);
  1186. IndexData.writeRowCountDefinitions(mutator, tableBuffer, 1);
  1187. // skip columns and column names
  1188. ByteUtil.forward(tableBuffer,
  1189. (_columns.size() * format.SIZE_COLUMN_DEF_BLOCK));
  1190. skipNames(tableBuffer, _columns.size());
  1191. // move to end of current index datas
  1192. ByteUtil.forward(tableBuffer, (_indexCount *
  1193. format.SIZE_INDEX_COLUMN_BLOCK));
  1194. // allocate usage maps and root page
  1195. TableMutator.IndexDataState idxDataState = mutator.getIndexDataState(index);
  1196. int rootPageNumber = getPageChannel().allocateNewPage();
  1197. Map.Entry<Integer,Integer> umapInfo = addUsageMaps(1, rootPageNumber);
  1198. idxDataState.setRootPageNumber(rootPageNumber);
  1199. idxDataState.setUmapPageNumber(umapInfo.getKey());
  1200. idxDataState.setUmapRowNumber(umapInfo.getValue().byteValue());
  1201. // write index data def
  1202. int idxDataDefPos = tableBuffer.position();
  1203. ByteUtil.insertEmptyData(tableBuffer, format.SIZE_INDEX_COLUMN_BLOCK);
  1204. IndexData.writeDefinition(mutator, tableBuffer, idxDataState, null);
  1205. // sanity check the updates
  1206. validateTableDefUpdate(mutator, tableBuffer);
  1207. // before writing the new table def, create the index data
  1208. tableBuffer.position(0);
  1209. newIdxData = IndexData.create(
  1210. this, tableBuffer, idxDataState.getIndexDataNumber(), format);
  1211. tableBuffer.position(idxDataDefPos);
  1212. newIdxData.read(tableBuffer, _columns);
  1213. ////
  1214. // write updated table def back to the database
  1215. writeTableDefinitionBuffer(tableBuffer, _tableDefPageNumber, mutator,
  1216. mutator.getNextPages());
  1217. success = true;
  1218. } finally {
  1219. if(!success) {
  1220. // need to discard modified table buffer
  1221. _tableDefBufferH.invalidate();
  1222. }
  1223. }
  1224. ////
  1225. // now, update current TableImpl
  1226. for(IndexData.ColumnDescriptor iCol : newIdxData.getColumns()) {
  1227. _indexColumns.add(iCol.getColumn());
  1228. }
  1229. ++_indexCount;
  1230. _indexDatas.add(newIdxData);
  1231. completeTableMutation(tableBuffer);
  1232. // don't forget to populate the new index
  1233. populateIndexData(newIdxData);
  1234. return newIdxData;
  1235. }
  1236. private void populateIndexData(IndexData idxData)
  1237. throws IOException
  1238. {
  1239. // grab the columns involved in this index
  1240. List<ColumnImpl> idxCols = new ArrayList<ColumnImpl>();
  1241. for(IndexData.ColumnDescriptor col : idxData.getColumns()) {
  1242. idxCols.add(col.getColumn());
  1243. }
  1244. // iterate through all the rows and add them to the index
  1245. Object[] rowVals = new Object[_columns.size()];
  1246. for(Row row : getDefaultCursor().newIterable().addColumns(idxCols)) {
  1247. for(Column col : idxCols) {
  1248. col.setRowValue(rowVals, col.getRowValue(row));
  1249. }
  1250. IndexData.commitAll(
  1251. idxData.prepareAddRow(rowVals, (RowIdImpl)row.getId(), null));
  1252. }
  1253. updateTableDefinition(0);
  1254. }
  1255. /**
  1256. * Writes a index defined by the given TableUpdater to this table.
  1257. * @usage _advanced_method_
  1258. */
  1259. protected IndexImpl mutateAddIndex(TableUpdater mutator) throws IOException
  1260. {
  1261. IndexBuilder index = mutator.getIndex();
  1262. JetFormat format = mutator.getFormat();
  1263. ////
  1264. // calculate how much more space we need in the table def
  1265. mutator.addTdefLen(format.SIZE_INDEX_INFO_BLOCK);
  1266. int nameByteLen = DBMutator.calculateNameLength(index.getName());
  1267. mutator.addTdefLen(nameByteLen);
  1268. ////
  1269. // load current table definition and add space for new info
  1270. ByteBuffer tableBuffer = loadCompleteTableDefinitionBufferForUpdate(
  1271. mutator);
  1272. IndexImpl newIdx = null;
  1273. boolean success = false;
  1274. try {
  1275. ////
  1276. // update various bits of the table def
  1277. ByteUtil.forward(tableBuffer, 35);
  1278. tableBuffer.putInt(_logicalIndexCount + 1);
  1279. // move to end of index data def blocks
  1280. tableBuffer.position(format.SIZE_TDEF_HEADER +
  1281. (_indexCount * format.SIZE_INDEX_DEFINITION));
  1282. // skip columns and column names
  1283. ByteUtil.forward(tableBuffer,
  1284. (_columns.size() * format.SIZE_COLUMN_DEF_BLOCK));
  1285. skipNames(tableBuffer, _columns.size());
  1286. // move to end of current index datas
  1287. ByteUtil.forward(tableBuffer, (_indexCount *
  1288. format.SIZE_INDEX_COLUMN_BLOCK));
  1289. // move to end of current indexes
  1290. ByteUtil.forward(tableBuffer, (_logicalIndexCount *
  1291. format.SIZE_INDEX_INFO_BLOCK));
  1292. int idxDefPos = tableBuffer.position();
  1293. ByteUtil.insertEmptyData(tableBuffer, format.SIZE_INDEX_INFO_BLOCK);
  1294. IndexImpl.writeDefinition(mutator, index, tableBuffer);
  1295. // skip existing index names and write new name
  1296. skipNames(tableBuffer, _logicalIndexCount);
  1297. ByteUtil.insertEmptyData(tableBuffer, nameByteLen);
  1298. writeName(tableBuffer, index.getName(), mutator.getCharset());
  1299. // sanity check the updates
  1300. validateTableDefUpdate(mutator, tableBuffer);
  1301. // before writing the new table def, create the index
  1302. tableBuffer.position(idxDefPos);
  1303. newIdx = new IndexImpl(tableBuffer, _indexDatas, format);
  1304. newIdx.setName(index.getName());
  1305. ////
  1306. // write updated table def back to the database
  1307. writeTableDefinitionBuffer(tableBuffer, _tableDefPageNumber, mutator,
  1308. mutator.getNextPages());
  1309. success = true;
  1310. } finally {
  1311. if(!success) {
  1312. // need to discard modified table buffer
  1313. _tableDefBufferH.invalidate();
  1314. }
  1315. }
  1316. ////
  1317. // now, update current TableImpl
  1318. ++_logicalIndexCount;
  1319. _indexes.add(newIdx);
  1320. completeTableMutation(tableBuffer);
  1321. return newIdx;
  1322. }
  1323. private void validateTableDefUpdate(TableUpdater mutator, ByteBuffer tableBuffer)
  1324. throws IOException
  1325. {
  1326. if(!mutator.validateUpdatedTdef(tableBuffer)) {
  1327. throw new IllegalStateException(
  1328. withErrorContext("Failed updating table definition (unexpected length)"));
  1329. }
  1330. }
  1331. private void completeTableMutation(ByteBuffer tableBuffer) throws IOException
  1332. {
  1333. // lastly, may need to clear table def buffer
  1334. _tableDefBufferH.possiblyInvalidate(_tableDefPageNumber, tableBuffer);
  1335. // update any foreign key enforcing
  1336. _fkEnforcer.reset();
  1337. // update modification count so any active RowStates can keep themselves
  1338. // up-to-date
  1339. ++_modCount;
  1340. }
  1341. /**
  1342. * Skips the given number of names in the table buffer.
  1343. */
  1344. private static void skipNames(ByteBuffer tableBuffer, int count) {
  1345. for(int i = 0; i < count; ++i) {
  1346. ByteUtil.forward(tableBuffer, tableBuffer.getShort());
  1347. }
  1348. }
  1349. private ByteBuffer loadCompleteTableDefinitionBufferForUpdate(
  1350. TableUpdater mutator)
  1351. throws IOException
  1352. {
  1353. // load complete table definition
  1354. ByteBuffer tableBuffer = _tableDefBufferH.setPage(getPageChannel(),
  1355. _tableDefPageNumber);
  1356. tableBuffer = loadCompleteTableDefinitionBuffer(
  1357. tableBuffer, mutator.getNextPages());
  1358. // make sure the table buffer has enough room for the new info
  1359. int addedLen = mutator.getAddedTdefLen();
  1360. int origTdefLen = tableBuffer.getInt(8);
  1361. mutator.setOrigTdefLen(origTdefLen);
  1362. int newTdefLen = origTdefLen + addedLen;
  1363. while(newTdefLen > tableBuffer.capacity()) {
  1364. tableBuffer = expandTableBuffer(tableBuffer);
  1365. tableBuffer.flip();
  1366. }
  1367. tableBuffer.limit(origTdefLen);
  1368. // set new tdef length
  1369. tableBuffer.position(8);
  1370. tableBuffer.putInt(newTdefLen);
  1371. return tableBuffer;
  1372. }
  1373. /**
  1374. * Adds some usage maps for use with this table. This method is expected to
  1375. * be called with a small-ish number of requested usage maps.
  1376. */
  1377. private Map.Entry<Integer,Integer> addUsageMaps(
  1378. int numMaps, Integer firstUsedPage)
  1379. throws IOException
  1380. {
  1381. JetFormat format = getFormat();
  1382. PageChannel pageChannel = getPageChannel();
  1383. int umapRowLength = format.OFFSET_USAGE_MAP_START +
  1384. format.USAGE_MAP_TABLE_BYTE_LENGTH;
  1385. int totalUmapSpaceUsage = getRowSpaceUsage(umapRowLength, format) * numMaps;
  1386. int umapPageNumber = PageChannel.INVALID_PAGE_NUMBER;
  1387. int firstRowNum = -1;
  1388. int freeSpace = 0;
  1389. // search currently known usage map buffers to find one with enough free
  1390. // space (the numMaps should always be small enough to put them all on one
  1391. // page). pages will free space will probaby be newer pages (higher
  1392. // numbers), so we sort in reverse order.
  1393. Set<Integer> knownPages = new TreeSet<Integer>(Collections.reverseOrder());
  1394. collectUsageMapPages(knownPages);
  1395. ByteBuffer umapBuf = pageChannel.createPageBuffer();
  1396. for(Integer pageNum : knownPages) {
  1397. pageChannel.readPage(umapBuf, pageNum);
  1398. freeSpace = umapBuf.getShort(format.OFFSET_FREE_SPACE);
  1399. if(freeSpace >= totalUmapSpaceUsage) {
  1400. // found a page!
  1401. umapPageNumber = pageNum;
  1402. firstRowNum = getRowsOnDataPage(umapBuf, format);
  1403. break;
  1404. }
  1405. }
  1406. if(umapPageNumber == PageChannel.INVALID_PAGE_NUMBER) {
  1407. // didn't find any existing pages, need to create a new one
  1408. umapPageNumber = pageChannel.allocateNewPage();
  1409. freeSpace = format.DATA_PAGE_INITIAL_FREE_SPACE;
  1410. firstRowNum = 0;
  1411. umapBuf = createUsageMapDefPage(pageChannel, freeSpace);
  1412. }
  1413. // write the actual usage map defs
  1414. int rowStart = findRowEnd(umapBuf, firstRowNum, format) - umapRowLength;
  1415. int umapRowNum = firstRowNum;
  1416. for(int i = 0; i < numMaps; ++i) {
  1417. umapBuf.putShort(getRowStartOffset(umapRowNum, format), (short)rowStart);
  1418. umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE);
  1419. int dataOffset = rowStart + 1;
  1420. if(firstUsedPage != null) {
  1421. // fill in the first used page of the usage map
  1422. umapBuf.putInt(dataOffset, firstUsedPage);
  1423. dataOffset += 4;
  1424. umapBuf.put(dataOffset, (byte)1);
  1425. dataOffset++;
  1426. }
  1427. // zero remaining row data
  1428. ByteUtil.clearRange(umapBuf, dataOffset, (rowStart + umapRowLength));
  1429. rowStart -= umapRowLength;
  1430. ++umapRowNum;
  1431. }
  1432. // finish the page
  1433. freeSpace -= totalUmapSpaceUsage;
  1434. umapBuf.putShort(format.OFFSET_FREE_SPACE, (short)freeSpace);
  1435. umapBuf.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE,
  1436. (short)umapRowNum);
  1437. pageChannel.writePage(umapBuf, umapPageNumber);
  1438. return new AbstractMap.SimpleImmutableEntry<Integer,Integer>(
  1439. umapPageNumber, firstRowNum);
  1440. }
  1441. void collectUsageMapPages(Collection<Integer> pages) {
  1442. pages.add(_ownedPages.getTablePageNumber());
  1443. pages.add(_freeSpacePages.getTablePageNumber());
  1444. for(IndexData idx : _indexDatas) {
  1445. idx.collectUsageMapPages(pages);
  1446. }
  1447. for(ColumnImpl col : _columns) {
  1448. col.collectUsageMapPages(pages);
  1449. }
  1450. }
  1451. /**
  1452. * @param buffer Buffer to write to
  1453. */
  1454. private static void writeTableDefinitionHeader(
  1455. TableCreator creator, ByteBuffer buffer, int totalTableDefSize)
  1456. throws IOException
  1457. {
  1458. List<ColumnBuilder> columns = creator.getColumns();
  1459. //Start writing the tdef
  1460. writeTablePageHeader(buffer);
  1461. buffer.putInt(totalTableDefSize); //Length of table def
  1462. buffer.putInt(MAGIC_TABLE_NUMBER); // seemingly constant magic value
  1463. buffer.putInt(0); //Number of rows
  1464. buffer.putInt(0); //Last Autonumber
  1465. buffer.put((byte) 1); // this makes autonumbering work in access
  1466. for (int i = 0; i < 15; i++) { //Unknown
  1467. buffer.put((byte) 0);
  1468. }
  1469. buffer.put(TYPE_USER); //Table type
  1470. buffer.putShort((short) columns.size()); //Max columns a row will have
  1471. buffer.putShort(ColumnImpl.countVariableLength(columns)); //Number of variable columns in table
  1472. buffer.putShort((short) columns.size()); //Number of columns in table
  1473. buffer.putInt(creator.getLogicalIndexCount()); //Number of logical indexes in table
  1474. buffer.putInt(creator.getIndexCount()); //Number of indexes in table
  1475. buffer.put((byte) 0); //Usage map row number
  1476. ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Usage map page number
  1477. buffer.put((byte) 1); //Free map row number
  1478. ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Free map page number
  1479. }
  1480. /**
  1481. * Writes the page header for a table definition page
  1482. * @param buffer Buffer to write to
  1483. */
  1484. private static void writeTablePageHeader(ByteBuffer buffer)
  1485. {
  1486. buffer.put(PageTypes.TABLE_DEF); //Page type
  1487. buffer.put((byte) 0x01); //Unknown
  1488. buffer.put((byte) 0); //Unknown
  1489. buffer.put((byte) 0); //Unknown
  1490. buffer.putInt(0); //Next TDEF page pointer
  1491. }
  1492. /**
  1493. * Writes the given name into the given buffer in the format as expected by
  1494. * {@link #readName}.
  1495. */
  1496. static void writeName(ByteBuffer buffer, String name, Charset charset)
  1497. {
  1498. ByteBuffer encName = ColumnImpl.encodeUncompressedText(name, charset);
  1499. buffer.putShort((short) encName.remaining());
  1500. buffer.put(encName);
  1501. }
  1502. /**
  1503. * Create the usage map definition page buffer. The "used pages" map is in
  1504. * row 0, the "pages with free space" map is in row 1. Index usage maps are
  1505. * in subsequent rows.
  1506. */
  1507. private static void createUsageMapDefinitionBuffer(TableCreator creator)
  1508. throws IOException
  1509. {
  1510. List<ColumnBuilder> lvalCols = creator.getLongValueColumns();
  1511. // 2 table usage maps plus 1 for each index and 2 for each lval col
  1512. int indexUmapEnd = 2 + creator.getIndexCount();
  1513. int umapNum = indexUmapEnd + (lvalCols.size() * 2);
  1514. JetFormat format = creator.getFormat();
  1515. int umapRowLength = format.OFFSET_USAGE_MAP_START +
  1516. format.USAGE_MAP_TABLE_BYTE_LENGTH;
  1517. int umapSpaceUsage = getRowSpaceUsage(umapRowLength, format);
  1518. PageChannel pageChannel = creator.getPageChannel();
  1519. int umapPageNumber = PageChannel.INVALID_PAGE_NUMBER;
  1520. ByteBuffer umapBuf = null;
  1521. int freeSpace = 0;
  1522. int rowStart = 0;
  1523. int umapRowNum = 0;
  1524. for(int i = 0; i < umapNum; ++i) {
  1525. if(umapBuf == null) {
  1526. // need new page for usage maps
  1527. if(umapPageNumber == PageChannel.INVALID_PAGE_NUMBER) {
  1528. // first umap page has already been reserved
  1529. umapPageNumber = creator.getUmapPageNumber();
  1530. } else {
  1531. // need another umap page
  1532. umapPageNumber = creator.reservePageNumber();
  1533. }
  1534. freeSpace = format.DATA_PAGE_INITIAL_FREE_SPACE;
  1535. umapBuf = createUsageMapDefPage(pageChannel, freeSpace);
  1536. rowStart = findRowEnd(umapBuf, 0, format) - umapRowLength;
  1537. umapRowNum = 0;
  1538. }
  1539. umapBuf.putShort(getRowStartOffset(umapRowNum, format), (short)rowStart);
  1540. if(i == 0) {
  1541. // table "owned pages" map definition
  1542. umapBuf.put(rowStart, UsageMap.MAP_TYPE_REFERENCE);
  1543. } else if(i == 1) {
  1544. // table "free space pages" map definition
  1545. umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE);
  1546. } else if(i < indexUmapEnd) {
  1547. // index umap
  1548. int indexIdx = i - 2;
  1549. TableMutator.IndexDataState idxDataState =
  1550. creator.getIndexDataStates().get(indexIdx);
  1551. // allocate root page for the index
  1552. int rootPageNumber = pageChannel.allocateNewPage();
  1553. // stash info for later use
  1554. idxDataState.setRootPageNumber(rootPageNumber);
  1555. idxDataState.setUmapRowNumber((byte)umapRowNum);
  1556. idxDataState.setUmapPageNumber(umapPageNumber);
  1557. // index map definition, including initial root page
  1558. umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE);
  1559. umapBuf.putInt(rowStart + 1, rootPageNumber);
  1560. umapBuf.put(rowStart + 5, (byte)1);
  1561. } else {
  1562. // long value column umaps
  1563. int lvalColIdx = i - indexUmapEnd;
  1564. int umapType = lvalColIdx % 2;
  1565. lvalColIdx /= 2;
  1566. ColumnBuilder lvalCol = lvalCols.get(lvalColIdx);
  1567. TableMutator.ColumnState colState =
  1568. creator.getColumnState(lvalCol);
  1569. umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE);
  1570. if((umapType == 1) &&
  1571. (umapPageNumber != colState.getUmapPageNumber())) {
  1572. // we want to force both usage maps for a column to be on the same
  1573. // data page, so just discard the previous one we wrote
  1574. --i;
  1575. umapType = 0;
  1576. }
  1577. if(umapType == 0) {
  1578. // lval column "owned pages" usage map
  1579. colState.setUmapOwnedRowNumber((byte)umapRowNum);
  1580. colState.setUmapPageNumber(umapPageNumber);
  1581. } else {
  1582. // lval column "free space pages" usage map (always on same page)
  1583. colState.setUmapFreeRowNumber((byte)umapRowNum);
  1584. }
  1585. }
  1586. rowStart -= umapRowLength;
  1587. freeSpace -= umapSpaceUsage;
  1588. ++umapRowNum;
  1589. if((freeSpace <= umapSpaceUsage) || (i == (umapNum - 1))) {
  1590. // finish current page
  1591. umapBuf.putShort(format.OFFSET_FREE_SPACE, (short)freeSpace);
  1592. umapBuf.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE,
  1593. (short)umapRowNum);
  1594. pageChannel.writePage(umapBuf, umapPageNumber);
  1595. umapBuf = null;
  1596. }
  1597. }
  1598. }
  1599. private static ByteBuffer createUsageMapDefPage(
  1600. PageChannel pageChannel, int freeSpace)
  1601. {
  1602. ByteBuffer umapBuf = pageChannel.createPageBuffer();
  1603. umapBuf.put(PageTypes.DATA);
  1604. umapBuf.put((byte) 0x1); //Unknown
  1605. umapBuf.putShort((short)freeSpace); //Free space in page
  1606. umapBuf.putInt(0); //Table definition
  1607. umapBuf.putInt(0); //Unknown
  1608. umapBuf.putShort((short)0); //Number of records on this page
  1609. return umapBuf;
  1610. }
  1611. /**
  1612. * Returns a single ByteBuffer which contains the entire table definition
  1613. * (which may span multiple database pages).
  1614. */
  1615. private ByteBuffer loadCompleteTableDefinitionBuffer(
  1616. ByteBuffer tableBuffer, List<Integer> pages)
  1617. throws IOException
  1618. {
  1619. int nextPage = tableBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
  1620. ByteBuffer nextPageBuffer = null;
  1621. while (nextPage != 0) {
  1622. if(pages != null) {
  1623. pages.add(nextPage);
  1624. }
  1625. if (nextPageBuffer == null) {
  1626. nextPageBuffer = getPageChannel().createPageBuffer();
  1627. }
  1628. getPageChannel().readPage(nextPageBuffer, nextPage);
  1629. nextPage = nextPageBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
  1630. tableBuffer = expandTableBuffer(tableBuffer);
  1631. tableBuffer.put(nextPageBuffer.array(), 8, getFormat().PAGE_SIZE - 8);
  1632. tableBuffer.flip();
  1633. }
  1634. return tableBuffer;
  1635. }
  1636. private ByteBuffer expandTableBuffer(ByteBuffer tableBuffer) {
  1637. ByteBuffer newBuffer = PageChannel.createBuffer(
  1638. tableBuffer.capacity() + getFormat().PAGE_SIZE - 8);
  1639. newBuffer.put(tableBuffer);
  1640. return newBuffer;
  1641. }
  1642. private void readColumnDefinitions(ByteBuffer tableBuffer, short columnCount)
  1643. throws IOException
  1644. {
  1645. int colOffset = getFormat().OFFSET_INDEX_DEF_BLOCK +
  1646. _indexCount * getFormat().SIZE_INDEX_DEFINITION;
  1647. tableBuffer.position(colOffset +
  1648. (columnCount * getFormat().SIZE_COLUMN_HEADER));
  1649. List<String> colNames = new ArrayList<String>(columnCount);
  1650. for (int i = 0; i < columnCount; i++) {
  1651. colNames.add(readName(tableBuffer));
  1652. }
  1653. int dispIndex = 0;
  1654. for (int i = 0; i < columnCount; i++) {
  1655. ColumnImpl column = ColumnImpl.create(this, tableBuffer,
  1656. colOffset + (i * getFormat().SIZE_COLUMN_HEADER), colNames.get(i),
  1657. dispIndex++);
  1658. _columns.add(column);
  1659. if(column.isVariableLength()) {
  1660. // also shove it in the variable columns list, which is ordered
  1661. // differently from the _columns list
  1662. _varColumns.add(column);
  1663. }
  1664. }
  1665. Collections.sort(_columns);
  1666. initAutoNumberColumns();
  1667. initCalculatedColumns();
  1668. // setup the data index for the columns
  1669. int colIdx = 0;
  1670. for(ColumnImpl col : _columns) {
  1671. col.setColumnIndex(colIdx++);
  1672. }
  1673. // sort variable length columns based on their index into the variable
  1674. // length offset table, because we will write the columns in this order
  1675. Collections.sort(_varColumns, VAR_LEN_COLUMN_COMPARATOR);
  1676. }
  1677. private void readIndexDefinitions(ByteBuffer tableBuffer) throws IOException
  1678. {
  1679. // read index column information
  1680. for (int i = 0; i < _indexCount; i++) {
  1681. IndexData idxData = _indexDatas.get(i);
  1682. idxData.read(tableBuffer, _columns);
  1683. // keep track of all columns involved in indexes
  1684. for(IndexData.ColumnDescriptor iCol : idxData.getColumns()) {
  1685. _indexColumns.add(iCol.getColumn());
  1686. }
  1687. }
  1688. // read logical index info (may be more logical indexes than index datas)
  1689. for (int i = 0; i < _logicalIndexCount; i++) {
  1690. _indexes.add(new IndexImpl(tableBuffer, _indexDatas, getFormat()));
  1691. }
  1692. // read logical index names
  1693. for (int i = 0; i < _logicalIndexCount; i++) {
  1694. _indexes.get(i).setName(readName(tableBuffer));
  1695. }
  1696. Collections.sort(_indexes);
  1697. }
  1698. private boolean readColumnUsageMaps(ByteBuffer tableBuffer)
  1699. throws IOException
  1700. {
  1701. short umapColNum = tableBuffer.getShort();
  1702. if(umapColNum == IndexData.COLUMN_UNUSED) {
  1703. return false;
  1704. }
  1705. int pos = tableBuffer.position();
  1706. UsageMap colOwnedPages = null;
  1707. UsageMap colFreeSpacePages = null;
  1708. try {
  1709. colOwnedPages = UsageMap.read(getDatabase(), tableBuffer);
  1710. colFreeSpacePages = UsageMap.read(getDatabase(), tableBuffer);
  1711. } catch(IllegalStateException e) {
  1712. // ignore invalid usage map info
  1713. colOwnedPages = null;
  1714. colFreeSpacePages = null;
  1715. tableBuffer.position(pos + 8);
  1716. LOG.warn(withErrorContext("Invalid column " + umapColNum +
  1717. " usage map definition: " + e));
  1718. }
  1719. for(ColumnImpl col : _columns) {
  1720. if(col.getColumnNumber() == umapColNum) {
  1721. col.setUsageMaps(colOwnedPages, colFreeSpacePages);
  1722. break;
  1723. }
  1724. }
  1725. return true;
  1726. }
  1727. /**
  1728. * Writes the given page data to the given page number, clears any other
  1729. * relevant buffers.
  1730. */
  1731. private void writeDataPage(ByteBuffer pageBuffer, int pageNumber)
  1732. throws IOException
  1733. {
  1734. // write the page data
  1735. getPageChannel().writePage(pageBuffer, pageNumber);
  1736. // possibly invalidate the add row buffer if a different data buffer is
  1737. // being written (e.g. this happens during deleteRow)
  1738. _addRowBufferH.possiblyInvalidate(pageNumber, pageBuffer);
  1739. // update modification count so any active RowStates can keep themselves
  1740. // up-to-date
  1741. ++_modCount;
  1742. }
  1743. /**
  1744. * Returns a name read from the buffer at the current position. The
  1745. * expected name format is the name length followed by the name
  1746. * encoded using the {@link JetFormat#CHARSET}
  1747. */
  1748. private String readName(ByteBuffer buffer) {
  1749. int nameLength = readNameLength(buffer);
  1750. byte[] nameBytes = ByteUtil.getBytes(buffer, nameLength);
  1751. return ColumnImpl.decodeUncompressedText(nameBytes,
  1752. getDatabase().getCharset());
  1753. }
  1754. /**
  1755. * Returns a name length read from the buffer at the current position.
  1756. */
  1757. private int readNameLength(ByteBuffer buffer) {
  1758. return ByteUtil.getUnsignedVarInt(buffer, getFormat().SIZE_NAME_LENGTH);
  1759. }
  1760. @Override
  1761. public Object[] asRow(Map<String,?> rowMap) {
  1762. return asRow(rowMap, null, false);
  1763. }
  1764. /**
  1765. * Converts a map of columnName -&gt; columnValue to an array of row values
  1766. * appropriate for a call to {@link #addRow(Object...)}, where the generated
  1767. * RowId will be an extra value at the end of the array.
  1768. * @see ColumnImpl#RETURN_ROW_ID
  1769. * @usage _intermediate_method_
  1770. */
  1771. public Object[] asRowWithRowId(Map<String,?> rowMap) {
  1772. return asRow(rowMap, null, true);
  1773. }
  1774. @Override
  1775. public Object[] asUpdateRow(Map<String,?> rowMap) {
  1776. return asRow(rowMap, Column.KEEP_VALUE, false);
  1777. }
  1778. /**
  1779. * @return the generated RowId added to a row of values created via {@link
  1780. * #asRowWithRowId}
  1781. * @usage _intermediate_method_
  1782. */
  1783. public RowId getRowId(Object[] row) {
  1784. return (RowId)row[_columns.size()];
  1785. }
  1786. /**
  1787. * Converts a map of columnName -&gt; columnValue to an array of row values.
  1788. */
  1789. private Object[] asRow(Map<String,?> rowMap, Object defaultValue,
  1790. boolean returnRowId)
  1791. {
  1792. int len = _columns.size();
  1793. if(returnRowId) {
  1794. ++len;
  1795. }
  1796. Object[] row = new Object[len];
  1797. if(defaultValue != null) {
  1798. Arrays.fill(row, defaultValue);
  1799. }
  1800. if(returnRowId) {
  1801. row[len - 1] = ColumnImpl.RETURN_ROW_ID;
  1802. }
  1803. if(rowMap == null) {
  1804. return row;
  1805. }
  1806. for(ColumnImpl col : _columns) {
  1807. if(rowMap.containsKey(col.getName())) {
  1808. col.setRowValue(row, col.getRowValue(rowMap));
  1809. }
  1810. }
  1811. return row;
  1812. }
  1813. @Override
  1814. public Object[] addRow(Object... row) throws IOException {
  1815. return addRows(Collections.singletonList(row), false).get(0);
  1816. }
  1817. @Override
  1818. public <M extends Map<String,Object>> M addRowFromMap(M row)
  1819. throws IOException
  1820. {
  1821. Object[] rowValues = asRow(row);
  1822. addRow(rowValues);
  1823. returnRowValues(row, rowValues, _columns);
  1824. return row;
  1825. }
  1826. @Override
  1827. public List<? extends Object[]> addRows(List<? extends Object[]> rows)
  1828. throws IOException
  1829. {
  1830. return addRows(rows, true);
  1831. }
  1832. @Override
  1833. public <M extends Map<String,Object>> List<M> addRowsFromMaps(List<M> rows)
  1834. throws IOException
  1835. {
  1836. List<Object[]> rowValuesList = new ArrayList<Object[]>(rows.size());
  1837. for(Map<String,Object> row : rows) {
  1838. rowValuesList.add(asRow(row));
  1839. }
  1840. addRows(rowValuesList);
  1841. for(int i = 0; i < rowValuesList.size(); ++i) {
  1842. Map<String,Object> row = rows.get(i);
  1843. Object[] rowValues = rowValuesList.get(i);
  1844. returnRowValues(row, rowValues, _columns);
  1845. }
  1846. return rows;
  1847. }
  1848. private static void returnRowValues(Map<String,Object> row, Object[] rowValues,
  1849. List<ColumnImpl> cols)
  1850. {
  1851. for(ColumnImpl col : cols) {
  1852. col.setRowValue(row, col.getRowValue(rowValues));
  1853. }
  1854. }
  1855. /**
  1856. * Add multiple rows to this table, only writing to disk after all
  1857. * rows have been written, and every time a data page is filled.
  1858. * @param rows List of Object[] row values
  1859. */
  1860. private List<? extends Object[]> addRows(List<? extends Object[]> rows,
  1861. final boolean isBatchWrite)
  1862. throws IOException
  1863. {
  1864. if(rows.isEmpty()) {
  1865. return rows;
  1866. }
  1867. getPageChannel().startWrite();
  1868. try {
  1869. ByteBuffer dataPage = null;
  1870. int pageNumber = PageChannel.INVALID_PAGE_NUMBER;
  1871. int updateCount = 0;
  1872. int autoNumAssignCount = 0;
  1873. WriteRowState writeRowState =
  1874. (!_autoNumColumns.isEmpty() ? new WriteRowState() : null);
  1875. try {
  1876. List<Object[]> dupeRows = null;
  1877. final int numCols = _columns.size();
  1878. for (int i = 0; i < rows.size(); i++) {
  1879. // we need to make sure the row is the right length and is an
  1880. // Object[] (fill with null if too short). note, if the row is
  1881. // copied the caller will not be able to access any generated
  1882. // auto-number value, but if they need that info they should use a
  1883. // row array of the right size/type!
  1884. Object[] row = rows.get(i);
  1885. if((row.length < numCols) || (row.getClass() != Object[].class)) {
  1886. row = dupeRow(row, numCols);
  1887. // copy the input rows to a modifiable list so we can update the
  1888. // elements
  1889. if(dupeRows == null) {
  1890. dupeRows = new ArrayList<Object[]>(rows);
  1891. rows = dupeRows;
  1892. }
  1893. // we copied the row, so put the copy back into the rows list
  1894. dupeRows.set(i, row);
  1895. }
  1896. // handle various value massaging activities
  1897. for(ColumnImpl column : _columns) {
  1898. if(!column.isAutoNumber()) {
  1899. Object val = column.getRowValue(row);
  1900. if(val == null) {
  1901. val = column.generateDefaultValue();
  1902. }
  1903. // pass input value through column validator
  1904. column.setRowValue(row, column.validate(val));
  1905. }
  1906. }
  1907. // fill in autonumbers
  1908. handleAutoNumbersForAdd(row, writeRowState);
  1909. ++autoNumAssignCount;
  1910. // need to assign calculated values after all the other fields are
  1911. // filled in but before final validation
  1912. _calcColEval.calculate(row);
  1913. // run row validation if enabled
  1914. if(_rowValidator != null) {
  1915. _rowValidator.validate(row);
  1916. }
  1917. // write the row of data to a temporary buffer
  1918. ByteBuffer rowData = createRow(
  1919. row, _writeRowBufferH.getPageBuffer(getPageChannel()));
  1920. int rowSize = rowData.remaining();
  1921. if (rowSize > getFormat().MAX_ROW_SIZE) {
  1922. throw new InvalidValueException(withErrorContext(
  1923. "Row size " + rowSize + " is too large"));
  1924. }
  1925. // get page with space
  1926. dataPage = findFreeRowSpace(rowSize, dataPage, pageNumber);
  1927. pageNumber = _addRowBufferH.getPageNumber();
  1928. // determine where this row will end up on the page
  1929. int rowNum = getRowsOnDataPage(dataPage, getFormat());
  1930. RowIdImpl rowId = new RowIdImpl(pageNumber, rowNum);
  1931. // before we actually write the row data, we verify all the database
  1932. // constraints.
  1933. if(!_indexDatas.isEmpty()) {
  1934. IndexData.PendingChange idxChange = null;
  1935. try {
  1936. // handle foreign keys before adding to table
  1937. _fkEnforcer.addRow(row);
  1938. // prepare index updates
  1939. for(IndexData indexData : _indexDatas) {
  1940. idxChange = indexData.prepareAddRow(row, rowId, idxChange);
  1941. }
  1942. // complete index updates
  1943. IndexData.commitAll(idxChange);
  1944. } catch(ConstraintViolationException ce) {
  1945. IndexData.rollbackAll(idxChange);
  1946. throw ce;
  1947. }
  1948. }
  1949. // we have satisfied all the constraints, write the row
  1950. addDataPageRow(dataPage, rowSize, getFormat(), 0);
  1951. dataPage.put(rowData);
  1952. // return rowTd if desired
  1953. if((row.length > numCols) &&
  1954. (row[numCols] == ColumnImpl.RETURN_ROW_ID)) {
  1955. row[numCols] = rowId;
  1956. }
  1957. ++updateCount;
  1958. }
  1959. writeDataPage(dataPage, pageNumber);
  1960. // Update tdef page
  1961. updateTableDefinition(rows.size());
  1962. } catch(Exception rowWriteFailure) {
  1963. boolean isWriteFailure = isWriteFailure(rowWriteFailure);
  1964. if(!isWriteFailure && (autoNumAssignCount > updateCount)) {
  1965. // we assigned some autonumbers which won't get written. attempt to
  1966. // recover them so we don't get ugly "holes"
  1967. restoreAutoNumbersFromAdd(rows.get(autoNumAssignCount - 1));
  1968. }
  1969. if(!isBatchWrite) {
  1970. // just re-throw the original exception
  1971. if(rowWriteFailure instanceof IOException) {
  1972. throw (IOException)rowWriteFailure;
  1973. }
  1974. throw (RuntimeException)rowWriteFailure;
  1975. }
  1976. // attempt to resolve a partial batch write
  1977. if(isWriteFailure) {
  1978. // we don't really know the status of any of the rows, so clear the
  1979. // update count
  1980. updateCount = 0;
  1981. } else if(updateCount > 0) {
  1982. // attempt to flush the rows already written to disk
  1983. try {
  1984. writeDataPage(dataPage, pageNumber);
  1985. // Update tdef page
  1986. updateTableDefinition(updateCount);
  1987. } catch(Exception flushFailure) {
  1988. // the flush failure is "worse" as it implies possible database
  1989. // corruption (failed write vs. a row failure which was not a
  1990. // write failure). we don't know the status of any rows at this
  1991. // point (and the original failure is probably irrelevant)
  1992. LOG.warn(withErrorContext(
  1993. "Secondary row failure which preceded the write failure"),
  1994. rowWriteFailure);
  1995. updateCount = 0;
  1996. rowWriteFailure = flushFailure;
  1997. }
  1998. }
  1999. throw new BatchUpdateException(
  2000. updateCount, withErrorContext("Failed adding rows"),
  2001. rowWriteFailure);
  2002. }
  2003. } finally {
  2004. getPageChannel().finishWrite();
  2005. }
  2006. return rows;
  2007. }
  2008. private static boolean isWriteFailure(Throwable t) {
  2009. while(t != null) {
  2010. if((t instanceof IOException) && !(t instanceof JackcessException)) {
  2011. return true;
  2012. }
  2013. t = t.getCause();
  2014. }
  2015. // some other sort of exception which is not a write failure
  2016. return false;
  2017. }
  2018. @Override
  2019. public Row updateRow(Row row) throws IOException {
  2020. return updateRowFromMap(
  2021. getDefaultCursor().getRowState(), (RowIdImpl)row.getId(), row);
  2022. }
  2023. /**
  2024. * Update the row with the given id. Provided RowId must have previously
  2025. * been returned from this Table.
  2026. * @return the given row, updated with the current row values
  2027. * @throws IllegalStateException if the given row is not valid, or deleted.
  2028. * @usage _intermediate_method_
  2029. */
  2030. public Object[] updateRow(RowId rowId, Object... row) throws IOException {
  2031. return updateRow(
  2032. getDefaultCursor().getRowState(), (RowIdImpl)rowId, row);
  2033. }
  2034. /**
  2035. * Update the given column's value for the given row id. Provided RowId
  2036. * must have previously been returned from this Table.
  2037. * @throws IllegalStateException if the given row is not valid, or deleted.
  2038. * @usage _intermediate_method_
  2039. */
  2040. public void updateValue(Column column, RowId rowId, Object value)
  2041. throws IOException
  2042. {
  2043. Object[] row = new Object[_columns.size()];
  2044. Arrays.fill(row, Column.KEEP_VALUE);
  2045. column.setRowValue(row, value);
  2046. updateRow(rowId, row);
  2047. }
  2048. public <M extends Map<String,Object>> M updateRowFromMap(
  2049. RowState rowState, RowIdImpl rowId, M row)
  2050. throws IOException
  2051. {
  2052. Object[] rowValues = updateRow(rowState, rowId, asUpdateRow(row));
  2053. returnRowValues(row, rowValues, _columns);
  2054. return row;
  2055. }
  2056. /**
  2057. * Update the row for the given rowId.
  2058. * @usage _advanced_method_
  2059. */
  2060. public Object[] updateRow(RowState rowState, RowIdImpl rowId, Object... row)
  2061. throws IOException
  2062. {
  2063. requireValidRowId(rowId);
  2064. getPageChannel().startWrite();
  2065. try {
  2066. // ensure that the relevant row state is up-to-date
  2067. ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
  2068. int oldRowSize = rowBuffer.remaining();
  2069. requireNonDeletedRow(rowState, rowId);
  2070. // we need to make sure the row is the right length & type (fill with
  2071. // null if too short).
  2072. if((row.length < _columns.size()) || (row.getClass() != Object[].class)) {
  2073. row = dupeRow(row, _columns.size());
  2074. }
  2075. // hang on to the raw values of var length columns we are "keeping". this
  2076. // will allow us to re-use pre-written var length data, which can save
  2077. // space for things like long value columns.
  2078. Map<ColumnImpl,byte[]> keepRawVarValues =
  2079. (!_varColumns.isEmpty() ? new HashMap<ColumnImpl,byte[]>() : null);
  2080. // handle various value massaging activities
  2081. for(ColumnImpl column : _columns) {
  2082. if(column.isAutoNumber()) {
  2083. // handle these separately (below)
  2084. continue;
  2085. }
  2086. Object rowValue = column.getRowValue(row);
  2087. if(rowValue == Column.KEEP_VALUE) {
  2088. // fill in any "keep value" fields (restore old value)
  2089. rowValue = getRowColumn(getFormat(), rowBuffer, column, rowState,
  2090. keepRawVarValues);
  2091. } else {
  2092. // set oldValue to something that could not possibly be a real value
  2093. Object oldValue = Column.KEEP_VALUE;
  2094. if(_indexColumns.contains(column)) {
  2095. // read (old) row value to help update indexes
  2096. oldValue = getRowColumn(getFormat(), rowBuffer, column, rowState,
  2097. null);
  2098. } else {
  2099. oldValue = rowState.getRowCacheValue(column.getColumnIndex());
  2100. }
  2101. // if the old value was passed back in, we don't need to validate
  2102. if(oldValue != rowValue) {
  2103. // pass input value through column validator
  2104. rowValue = column.validate(rowValue);
  2105. }
  2106. }
  2107. column.setRowValue(row, rowValue);
  2108. }
  2109. // fill in autonumbers
  2110. handleAutoNumbersForUpdate(row, rowBuffer, rowState);
  2111. // need to assign calculated values after all the other fields are
  2112. // filled in but before final validation
  2113. _calcColEval.calculate(row);
  2114. // run row validation if enabled
  2115. if(_rowValidator != null) {
  2116. _rowValidator.validate(row);
  2117. }
  2118. // generate new row bytes
  2119. ByteBuffer newRowData = createRow(
  2120. row, _writeRowBufferH.getPageBuffer(getPageChannel()), oldRowSize,
  2121. keepRawVarValues);
  2122. if (newRowData.limit() > getFormat().MAX_ROW_SIZE) {
  2123. throw new InvalidValueException(withErrorContext(
  2124. "Row size " + newRowData.limit() + " is too large"));
  2125. }
  2126. if(!_indexDatas.isEmpty()) {
  2127. IndexData.PendingChange idxChange = null;
  2128. try {
  2129. Object[] oldRowValues = rowState.getRowCacheValues();
  2130. // check foreign keys before actually updating
  2131. _fkEnforcer.updateRow(oldRowValues, row);
  2132. // prepare index updates
  2133. for(IndexData indexData : _indexDatas) {
  2134. idxChange = indexData.prepareUpdateRow(oldRowValues, rowId, row,
  2135. idxChange);
  2136. }
  2137. // complete index updates
  2138. IndexData.commitAll(idxChange);
  2139. } catch(ConstraintViolationException ce) {
  2140. IndexData.rollbackAll(idxChange);
  2141. throw ce;
  2142. }
  2143. }
  2144. // see if we can squeeze the new row data into the existing row
  2145. rowBuffer.reset();
  2146. int rowSize = newRowData.remaining();
  2147. ByteBuffer dataPage = null;
  2148. int pageNumber = PageChannel.INVALID_PAGE_NUMBER;
  2149. if(oldRowSize >= rowSize) {
  2150. // awesome, slap it in!
  2151. rowBuffer.put(newRowData);
  2152. // grab the page we just updated
  2153. dataPage = rowState.getFinalPage();
  2154. pageNumber = rowState.getFinalRowId().getPageNumber();
  2155. } else {
  2156. // bummer, need to find a new page for the data
  2157. dataPage = findFreeRowSpace(rowSize, null,
  2158. PageChannel.INVALID_PAGE_NUMBER);
  2159. pageNumber = _addRowBufferH.getPageNumber();
  2160. RowIdImpl headerRowId = rowState.getHeaderRowId();
  2161. ByteBuffer headerPage = rowState.getHeaderPage();
  2162. if(pageNumber == headerRowId.getPageNumber()) {
  2163. // new row is on the same page as header row, share page
  2164. dataPage = headerPage;
  2165. }
  2166. // write out the new row data (set the deleted flag on the new data row
  2167. // so that it is ignored during normal table traversal)
  2168. int rowNum = addDataPageRow(dataPage, rowSize, getFormat(),
  2169. DELETED_ROW_MASK);
  2170. dataPage.put(newRowData);
  2171. // write the overflow info into the header row and clear out the
  2172. // remaining header data
  2173. rowBuffer = PageChannel.narrowBuffer(
  2174. headerPage,
  2175. findRowStart(headerPage, headerRowId.getRowNumber(), getFormat()),
  2176. findRowEnd(headerPage, headerRowId.getRowNumber(), getFormat()));
  2177. rowBuffer.put((byte)rowNum);
  2178. ByteUtil.put3ByteInt(rowBuffer, pageNumber);
  2179. ByteUtil.clearRemaining(rowBuffer);
  2180. // set the overflow flag on the header row
  2181. int headerRowIndex = getRowStartOffset(headerRowId.getRowNumber(),
  2182. getFormat());
  2183. headerPage.putShort(headerRowIndex,
  2184. (short)(headerPage.getShort(headerRowIndex)
  2185. | OVERFLOW_ROW_MASK));
  2186. if(pageNumber != headerRowId.getPageNumber()) {
  2187. writeDataPage(headerPage, headerRowId.getPageNumber());
  2188. }
  2189. }
  2190. writeDataPage(dataPage, pageNumber);
  2191. updateTableDefinition(0);
  2192. } finally {
  2193. getPageChannel().finishWrite();
  2194. }
  2195. return row;
  2196. }
  2197. private ByteBuffer findFreeRowSpace(int rowSize, ByteBuffer dataPage,
  2198. int pageNumber)
  2199. throws IOException
  2200. {
  2201. // assume incoming page is modified
  2202. boolean modifiedPage = true;
  2203. if(dataPage == null) {
  2204. // find owned page w/ free space
  2205. dataPage = findFreeRowSpace(_ownedPages, _freeSpacePages,
  2206. _addRowBufferH);
  2207. if(dataPage == null) {
  2208. // No data pages exist (with free space). Create a new one.
  2209. return newDataPage();
  2210. }
  2211. // found a page, see if it will work
  2212. pageNumber = _addRowBufferH.getPageNumber();
  2213. // since we just loaded this page, it is not yet modified
  2214. modifiedPage = false;
  2215. }
  2216. if(!rowFitsOnDataPage(rowSize, dataPage, getFormat())) {
  2217. // Last data page is full. Write old one and create a new one.
  2218. if(modifiedPage) {
  2219. writeDataPage(dataPage, pageNumber);
  2220. }
  2221. _freeSpacePages.removePageNumber(pageNumber);
  2222. dataPage = newDataPage();
  2223. }
  2224. return dataPage;
  2225. }
  2226. static ByteBuffer findFreeRowSpace(
  2227. UsageMap ownedPages, UsageMap freeSpacePages,
  2228. TempPageHolder rowBufferH)
  2229. throws IOException
  2230. {
  2231. // find last data page (Not bothering to check other pages for free
  2232. // space.)
  2233. UsageMap.PageCursor revPageCursor = ownedPages.cursor();
  2234. revPageCursor.afterLast();
  2235. while(true) {
  2236. int tmpPageNumber = revPageCursor.getPreviousPage();
  2237. if(tmpPageNumber < 0) {
  2238. break;
  2239. }
  2240. // only use if actually listed in free space pages
  2241. if(!freeSpacePages.containsPageNumber(tmpPageNumber)) {
  2242. continue;
  2243. }
  2244. ByteBuffer dataPage = rowBufferH.setPage(ownedPages.getPageChannel(),
  2245. tmpPageNumber);
  2246. if(dataPage.get() == PageTypes.DATA) {
  2247. // found last data page with free space
  2248. return dataPage;
  2249. }
  2250. }
  2251. return null;
  2252. }
  2253. /**
  2254. * Updates the table definition after rows are modified.
  2255. */
  2256. private void updateTableDefinition(int rowCountInc) throws IOException
  2257. {
  2258. // load table definition
  2259. ByteBuffer tdefPage = _tableDefBufferH.setPage(getPageChannel(),
  2260. _tableDefPageNumber);
  2261. // make sure rowcount and autonumber are up-to-date
  2262. _rowCount += rowCountInc;
  2263. tdefPage.putInt(getFormat().OFFSET_NUM_ROWS, _rowCount);
  2264. tdefPage.putInt(getFormat().OFFSET_NEXT_AUTO_NUMBER, _lastLongAutoNumber);
  2265. int ctypeOff = getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER;
  2266. if(ctypeOff >= 0) {
  2267. tdefPage.putInt(ctypeOff, _lastComplexTypeAutoNumber);
  2268. }
  2269. // write any index changes
  2270. for (IndexData indexData : _indexDatas) {
  2271. // write the unique entry count for the index to the table definition
  2272. // page
  2273. tdefPage.putInt(indexData.getUniqueEntryCountOffset(),
  2274. indexData.getUniqueEntryCount());
  2275. // write the entry page for the index
  2276. indexData.update();
  2277. }
  2278. // write modified table definition
  2279. getPageChannel().writePage(tdefPage, _tableDefPageNumber);
  2280. }
  2281. /**
  2282. * Create a new data page
  2283. * @return Page number of the new page
  2284. */
  2285. private ByteBuffer newDataPage() throws IOException {
  2286. ByteBuffer dataPage = _addRowBufferH.setNewPage(getPageChannel());
  2287. dataPage.put(PageTypes.DATA); //Page type
  2288. dataPage.put((byte) 1); //Unknown
  2289. dataPage.putShort((short)getFormat().DATA_PAGE_INITIAL_FREE_SPACE); //Free space in this page
  2290. dataPage.putInt(_tableDefPageNumber); //Page pointer to table definition
  2291. dataPage.putInt(0); //Unknown
  2292. dataPage.putShort((short)0); //Number of rows on this page
  2293. int pageNumber = _addRowBufferH.getPageNumber();
  2294. getPageChannel().writePage(dataPage, pageNumber);
  2295. _ownedPages.addPageNumber(pageNumber);
  2296. _freeSpacePages.addPageNumber(pageNumber);
  2297. return dataPage;
  2298. }
  2299. // exposed for unit tests
  2300. protected ByteBuffer createRow(Object[] rowArray, ByteBuffer buffer)
  2301. throws IOException
  2302. {
  2303. return createRow(rowArray, buffer, 0,
  2304. Collections.<ColumnImpl,byte[]>emptyMap());
  2305. }
  2306. /**
  2307. * Serialize a row of Objects into a byte buffer.
  2308. *
  2309. * @param rowArray row data, expected to be correct length for this table
  2310. * @param buffer buffer to which to write the row data
  2311. * @param minRowSize min size for result row
  2312. * @param rawVarValues optional, pre-written values for var length columns
  2313. * (enables re-use of previously written values).
  2314. * @return the given buffer, filled with the row data
  2315. */
  2316. private ByteBuffer createRow(Object[] rowArray, ByteBuffer buffer,
  2317. int minRowSize,
  2318. Map<ColumnImpl,byte[]> rawVarValues)
  2319. throws IOException
  2320. {
  2321. buffer.putShort(_maxColumnCount);
  2322. NullMask nullMask = new NullMask(_maxColumnCount);
  2323. //Fixed length column data comes first
  2324. int fixedDataStart = buffer.position();
  2325. int fixedDataEnd = fixedDataStart;
  2326. for (ColumnImpl col : _columns) {
  2327. if(col.isVariableLength()) {
  2328. continue;
  2329. }
  2330. Object rowValue = col.getRowValue(rowArray);
  2331. if (col.storeInNullMask()) {
  2332. if(col.writeToNullMask(rowValue)) {
  2333. nullMask.markNotNull(col);
  2334. }
  2335. rowValue = null;
  2336. }
  2337. if(rowValue != null) {
  2338. // we have a value to write
  2339. nullMask.markNotNull(col);
  2340. // remainingRowLength is ignored when writing fixed length data
  2341. buffer.position(fixedDataStart + col.getFixedDataOffset());
  2342. buffer.put(col.write(rowValue, 0));
  2343. }
  2344. // always insert space for the entire fixed data column length
  2345. // (including null values), access expects the row to always be at least
  2346. // big enough to hold all fixed values
  2347. buffer.position(fixedDataStart + col.getFixedDataOffset() +
  2348. col.getLength());
  2349. // keep track of the end of fixed data
  2350. if(buffer.position() > fixedDataEnd) {
  2351. fixedDataEnd = buffer.position();
  2352. }
  2353. }
  2354. // reposition at end of fixed data
  2355. buffer.position(fixedDataEnd);
  2356. // only need this info if this table contains any var length data
  2357. if(_maxVarColumnCount > 0) {
  2358. int maxRowSize = getFormat().MAX_ROW_SIZE;
  2359. // figure out how much space remains for var length data. first,
  2360. // account for already written space
  2361. maxRowSize -= buffer.position();
  2362. // now, account for trailer space
  2363. int trailerSize = (nullMask.byteSize() + 4 + (_maxVarColumnCount * 2));
  2364. maxRowSize -= trailerSize;
  2365. // for each non-null long value column we need to reserve a small
  2366. // amount of space so that we don't end up running out of row space
  2367. // later by being too greedy
  2368. for (ColumnImpl varCol : _varColumns) {
  2369. if((varCol.getType().isLongValue()) &&
  2370. (varCol.getRowValue(rowArray) != null)) {
  2371. maxRowSize -= getFormat().SIZE_LONG_VALUE_DEF;
  2372. }
  2373. }
  2374. //Now write out variable length column data
  2375. short[] varColumnOffsets = new short[_maxVarColumnCount];
  2376. int varColumnOffsetsIndex = 0;
  2377. for (ColumnImpl varCol : _varColumns) {
  2378. short offset = (short) buffer.position();
  2379. Object rowValue = varCol.getRowValue(rowArray);
  2380. if (rowValue != null) {
  2381. // we have a value
  2382. nullMask.markNotNull(varCol);
  2383. byte[] rawValue = null;
  2384. ByteBuffer varDataBuf = null;
  2385. if(((rawValue = rawVarValues.get(varCol)) != null) &&
  2386. (rawValue.length <= maxRowSize)) {
  2387. // save time and potentially db space, re-use raw value
  2388. varDataBuf = ByteBuffer.wrap(rawValue);
  2389. } else {
  2390. // write column value
  2391. varDataBuf = varCol.write(rowValue, maxRowSize);
  2392. }
  2393. maxRowSize -= varDataBuf.remaining();
  2394. if(varCol.getType().isLongValue()) {
  2395. // we already accounted for some amount of the long value data
  2396. // above. add that space back so we don't double count
  2397. maxRowSize += getFormat().SIZE_LONG_VALUE_DEF;
  2398. }
  2399. try {
  2400. buffer.put(varDataBuf);
  2401. } catch(BufferOverflowException e) {
  2402. // if the data is too big for the buffer, then we have gone over
  2403. // the max row size
  2404. throw new InvalidValueException(withErrorContext(
  2405. "Row size " + buffer.limit() + " is too large"));
  2406. }
  2407. }
  2408. // we do a loop here so that we fill in offsets for deleted columns
  2409. while(varColumnOffsetsIndex <= varCol.getVarLenTableIndex()) {
  2410. varColumnOffsets[varColumnOffsetsIndex++] = offset;
  2411. }
  2412. }
  2413. // fill in offsets for any remaining deleted columns
  2414. while(varColumnOffsetsIndex < varColumnOffsets.length) {
  2415. varColumnOffsets[varColumnOffsetsIndex++] = (short) buffer.position();
  2416. }
  2417. // record where we stopped writing
  2418. int eod = buffer.position();
  2419. // insert padding if necessary
  2420. padRowBuffer(buffer, minRowSize, trailerSize);
  2421. buffer.putShort((short) eod); //EOD marker
  2422. //Now write out variable length offsets
  2423. //Offsets are stored in reverse order
  2424. for (int i = _maxVarColumnCount - 1; i >= 0; i--) {
  2425. buffer.putShort(varColumnOffsets[i]);
  2426. }
  2427. buffer.putShort(_maxVarColumnCount); //Number of var length columns
  2428. } else {
  2429. // insert padding for row w/ no var cols
  2430. padRowBuffer(buffer, minRowSize, nullMask.byteSize());
  2431. }
  2432. nullMask.write(buffer); //Null mask
  2433. buffer.flip();
  2434. return buffer;
  2435. }
  2436. /**
  2437. * Fill in all autonumber column values for add.
  2438. */
  2439. private void handleAutoNumbersForAdd(Object[] row, WriteRowState writeRowState)
  2440. throws IOException
  2441. {
  2442. if(_autoNumColumns.isEmpty()) {
  2443. return;
  2444. }
  2445. boolean enableInsert = isAllowAutoNumberInsert();
  2446. writeRowState.resetAutoNumber();
  2447. for(ColumnImpl col : _autoNumColumns) {
  2448. // ignore input row value, use original row value (unless explicitly
  2449. // enabled)
  2450. Object inRowValue = getInputAutoNumberRowValue(enableInsert, col, row);
  2451. ColumnImpl.AutoNumberGenerator autoNumGen = col.getAutoNumberGenerator();
  2452. Object rowValue = ((inRowValue == null) ?
  2453. autoNumGen.getNext(writeRowState) :
  2454. autoNumGen.handleInsert(writeRowState, inRowValue));
  2455. col.setRowValue(row, rowValue);
  2456. }
  2457. }
  2458. /**
  2459. * Fill in all autonumber column values for update.
  2460. */
  2461. private void handleAutoNumbersForUpdate(Object[] row, ByteBuffer rowBuffer,
  2462. RowState rowState)
  2463. throws IOException
  2464. {
  2465. if(_autoNumColumns.isEmpty()) {
  2466. return;
  2467. }
  2468. boolean enableInsert = isAllowAutoNumberInsert();
  2469. rowState.resetAutoNumber();
  2470. for(ColumnImpl col : _autoNumColumns) {
  2471. // ignore input row value, use original row value (unless explicitly
  2472. // enabled)
  2473. Object inRowValue = getInputAutoNumberRowValue(enableInsert, col, row);
  2474. Object rowValue =
  2475. ((inRowValue == null) ?
  2476. getRowColumn(getFormat(), rowBuffer, col, rowState, null) :
  2477. col.getAutoNumberGenerator().handleInsert(rowState, inRowValue));
  2478. col.setRowValue(row, rowValue);
  2479. }
  2480. }
  2481. /**
  2482. * Optionally get the input autonumber row value for the given column from
  2483. * the given row if one was provided.
  2484. */
  2485. private static Object getInputAutoNumberRowValue(
  2486. boolean enableInsert, ColumnImpl col, Object[] row)
  2487. {
  2488. if(!enableInsert) {
  2489. return null;
  2490. }
  2491. Object inRowValue = col.getRowValue(row);
  2492. if((inRowValue == Column.KEEP_VALUE) || (inRowValue == Column.AUTO_NUMBER)) {
  2493. // these "special" values both behave like nothing was given
  2494. inRowValue = null;
  2495. }
  2496. return inRowValue;
  2497. }
  2498. /**
  2499. * Restores all autonumber column values from a failed add row.
  2500. */
  2501. private void restoreAutoNumbersFromAdd(Object[] row)
  2502. throws IOException
  2503. {
  2504. if(_autoNumColumns.isEmpty()) {
  2505. return;
  2506. }
  2507. for(ColumnImpl col : _autoNumColumns) {
  2508. // restore the last value from the row
  2509. col.getAutoNumberGenerator().restoreLast(col.getRowValue(row));
  2510. }
  2511. }
  2512. private static void padRowBuffer(ByteBuffer buffer, int minRowSize,
  2513. int trailerSize)
  2514. {
  2515. int pos = buffer.position();
  2516. if((pos + trailerSize) < minRowSize) {
  2517. // pad the row to get to the min byte size
  2518. int padSize = minRowSize - (pos + trailerSize);
  2519. ByteUtil.clearRange(buffer, pos, pos + padSize);
  2520. ByteUtil.forward(buffer, padSize);
  2521. }
  2522. }
  2523. @Override
  2524. public int getRowCount() {
  2525. return _rowCount;
  2526. }
  2527. int getNextLongAutoNumber() {
  2528. // note, the saved value is the last one handed out, so pre-increment
  2529. return ++_lastLongAutoNumber;
  2530. }
  2531. int getLastLongAutoNumber() {
  2532. // gets the last used auto number (does not modify)
  2533. return _lastLongAutoNumber;
  2534. }
  2535. void adjustLongAutoNumber(int inLongAutoNumber) {
  2536. if(inLongAutoNumber > _lastLongAutoNumber) {
  2537. _lastLongAutoNumber = inLongAutoNumber;
  2538. }
  2539. }
  2540. void restoreLastLongAutoNumber(int lastLongAutoNumber) {
  2541. // restores the last used auto number
  2542. _lastLongAutoNumber = lastLongAutoNumber - 1;
  2543. }
  2544. int getNextComplexTypeAutoNumber() {
  2545. // note, the saved value is the last one handed out, so pre-increment
  2546. return ++_lastComplexTypeAutoNumber;
  2547. }
  2548. int getLastComplexTypeAutoNumber() {
  2549. // gets the last used auto number (does not modify)
  2550. return _lastComplexTypeAutoNumber;
  2551. }
  2552. void adjustComplexTypeAutoNumber(int inComplexTypeAutoNumber) {
  2553. if(inComplexTypeAutoNumber > _lastComplexTypeAutoNumber) {
  2554. _lastComplexTypeAutoNumber = inComplexTypeAutoNumber;
  2555. }
  2556. }
  2557. void restoreLastComplexTypeAutoNumber(int lastComplexTypeAutoNumber) {
  2558. // restores the last used auto number
  2559. _lastComplexTypeAutoNumber = lastComplexTypeAutoNumber - 1;
  2560. }
  2561. @Override
  2562. public String toString() {
  2563. return CustomToStringStyle.builder(this)
  2564. .append("type", (_tableType + (!isSystem() ? " (USER)" : " (SYSTEM)")))
  2565. .append("name", _name)
  2566. .append("rowCount", _rowCount)
  2567. .append("columnCount", _columns.size())
  2568. .append("indexCount(data)", _indexCount)
  2569. .append("logicalIndexCount", _logicalIndexCount)
  2570. .append("validator", CustomToStringStyle.ignoreNull(_rowValidator))
  2571. .append("columns", _columns)
  2572. .append("indexes", _indexes)
  2573. .append("ownedPages", _ownedPages)
  2574. .toString();
  2575. }
  2576. /**
  2577. * @return A simple String representation of the entire table in
  2578. * tab-delimited format
  2579. * @usage _general_method_
  2580. */
  2581. public String display() throws IOException {
  2582. return display(Long.MAX_VALUE);
  2583. }
  2584. /**
  2585. * @param limit Maximum number of rows to display
  2586. * @return A simple String representation of the entire table in
  2587. * tab-delimited format
  2588. * @usage _general_method_
  2589. */
  2590. public String display(long limit) throws IOException {
  2591. reset();
  2592. StringWriter rtn = new StringWriter();
  2593. new ExportUtil.Builder(getDefaultCursor()).setDelimiter("\t").setHeader(true)
  2594. .exportWriter(new BufferedWriter(rtn));
  2595. return rtn.toString();
  2596. }
  2597. /**
  2598. * Updates free space and row info for a new row of the given size in the
  2599. * given data page. Positions the page for writing the row data.
  2600. * @return the row number of the new row
  2601. * @usage _advanced_method_
  2602. */
  2603. public static int addDataPageRow(ByteBuffer dataPage,
  2604. int rowSize,
  2605. JetFormat format,
  2606. int rowFlags)
  2607. {
  2608. int rowSpaceUsage = getRowSpaceUsage(rowSize, format);
  2609. // Decrease free space record.
  2610. short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
  2611. dataPage.putShort(format.OFFSET_FREE_SPACE, (short) (freeSpaceInPage -
  2612. rowSpaceUsage));
  2613. // Increment row count record.
  2614. short rowCount = dataPage.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
  2615. dataPage.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE,
  2616. (short) (rowCount + 1));
  2617. // determine row position
  2618. short rowLocation = findRowEnd(dataPage, rowCount, format);
  2619. rowLocation -= rowSize;
  2620. // write row position
  2621. dataPage.putShort(getRowStartOffset(rowCount, format),
  2622. (short)(rowLocation | rowFlags));
  2623. // set position for row data
  2624. dataPage.position(rowLocation);
  2625. return rowCount;
  2626. }
  2627. /**
  2628. * Returns the row count for the current page. If the page is invalid
  2629. * ({@code null}) or the page is not a DATA page, 0 is returned.
  2630. */
  2631. static int getRowsOnDataPage(ByteBuffer rowBuffer, JetFormat format)
  2632. throws IOException
  2633. {
  2634. int rowsOnPage = 0;
  2635. if((rowBuffer != null) && (rowBuffer.get(0) == PageTypes.DATA)) {
  2636. rowsOnPage = rowBuffer.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
  2637. }
  2638. return rowsOnPage;
  2639. }
  2640. /**
  2641. * @throws IllegalStateException if the given rowId is invalid
  2642. */
  2643. private void requireValidRowId(RowIdImpl rowId) {
  2644. if(!rowId.isValid()) {
  2645. throw new IllegalArgumentException(withErrorContext(
  2646. "Given rowId is invalid: " + rowId));
  2647. }
  2648. }
  2649. /**
  2650. * @throws IllegalStateException if the given row is invalid or deleted
  2651. */
  2652. private void requireNonDeletedRow(RowState rowState, RowIdImpl rowId)
  2653. {
  2654. if(!rowState.isValid()) {
  2655. throw new IllegalArgumentException(withErrorContext(
  2656. "Given rowId is invalid for this table: " + rowId));
  2657. }
  2658. if(rowState.isDeleted()) {
  2659. throw new IllegalStateException(withErrorContext(
  2660. "Row is deleted: " + rowId));
  2661. }
  2662. }
  2663. /**
  2664. * @usage _advanced_method_
  2665. */
  2666. public static boolean isDeletedRow(short rowStart) {
  2667. return ((rowStart & DELETED_ROW_MASK) != 0);
  2668. }
  2669. /**
  2670. * @usage _advanced_method_
  2671. */
  2672. public static boolean isOverflowRow(short rowStart) {
  2673. return ((rowStart & OVERFLOW_ROW_MASK) != 0);
  2674. }
  2675. /**
  2676. * @usage _advanced_method_
  2677. */
  2678. public static short cleanRowStart(short rowStart) {
  2679. return (short)(rowStart & OFFSET_MASK);
  2680. }
  2681. /**
  2682. * @usage _advanced_method_
  2683. */
  2684. public static short findRowStart(ByteBuffer buffer, int rowNum,
  2685. JetFormat format)
  2686. {
  2687. return cleanRowStart(
  2688. buffer.getShort(getRowStartOffset(rowNum, format)));
  2689. }
  2690. /**
  2691. * @usage _advanced_method_
  2692. */
  2693. public static int getRowStartOffset(int rowNum, JetFormat format)
  2694. {
  2695. return format.OFFSET_ROW_START + (format.SIZE_ROW_LOCATION * rowNum);
  2696. }
  2697. /**
  2698. * @usage _advanced_method_
  2699. */
  2700. public static short findRowEnd(ByteBuffer buffer, int rowNum,
  2701. JetFormat format)
  2702. {
  2703. return (short)((rowNum == 0) ?
  2704. format.PAGE_SIZE :
  2705. cleanRowStart(
  2706. buffer.getShort(getRowEndOffset(rowNum, format))));
  2707. }
  2708. /**
  2709. * @usage _advanced_method_
  2710. */
  2711. public static int getRowEndOffset(int rowNum, JetFormat format)
  2712. {
  2713. return format.OFFSET_ROW_START + (format.SIZE_ROW_LOCATION * (rowNum - 1));
  2714. }
  2715. /**
  2716. * @usage _advanced_method_
  2717. */
  2718. public static int getRowSpaceUsage(int rowSize, JetFormat format)
  2719. {
  2720. return rowSize + format.SIZE_ROW_LOCATION;
  2721. }
  2722. private void initAutoNumberColumns() {
  2723. for(ColumnImpl c : _columns) {
  2724. if(c.isAutoNumber()) {
  2725. _autoNumColumns.add(c);
  2726. }
  2727. }
  2728. }
  2729. private void initCalculatedColumns() {
  2730. for(ColumnImpl c : _columns) {
  2731. if(c.isCalculated()) {
  2732. _calcColEval.add(c);
  2733. }
  2734. }
  2735. }
  2736. boolean isThisTable(Identifier identifier) {
  2737. String collectionName = identifier.getCollectionName();
  2738. return ((collectionName == null) ||
  2739. collectionName.equalsIgnoreCase(getName()));
  2740. }
  2741. /**
  2742. * Returns {@code true} if a row of the given size will fit on the given
  2743. * data page, {@code false} otherwise.
  2744. * @usage _advanced_method_
  2745. */
  2746. public static boolean rowFitsOnDataPage(
  2747. int rowLength, ByteBuffer dataPage, JetFormat format)
  2748. throws IOException
  2749. {
  2750. int rowSpaceUsage = getRowSpaceUsage(rowLength, format);
  2751. short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
  2752. int rowsOnPage = getRowsOnDataPage(dataPage, format);
  2753. return ((rowSpaceUsage <= freeSpaceInPage) &&
  2754. (rowsOnPage < format.MAX_NUM_ROWS_ON_DATA_PAGE));
  2755. }
  2756. /**
  2757. * Duplicates and returns a row of data, optionally with a longer length
  2758. * filled with {@code null}.
  2759. */
  2760. static Object[] dupeRow(Object[] row, int newRowLength) {
  2761. Object[] copy = new Object[newRowLength];
  2762. System.arraycopy(row, 0, copy, 0, Math.min(row.length, newRowLength));
  2763. return copy;
  2764. }
  2765. String withErrorContext(String msg) {
  2766. return withErrorContext(msg, getDatabase(), getName());
  2767. }
  2768. private static String withErrorContext(String msg, DatabaseImpl db,
  2769. String tableName) {
  2770. return msg + " (Db=" + db.getName() + ";Table=" + tableName + ")";
  2771. }
  2772. /** various statuses for the row data */
  2773. private enum RowStatus {
  2774. INIT, INVALID_PAGE, INVALID_ROW, VALID, DELETED, NORMAL, OVERFLOW;
  2775. }
  2776. /** the phases the RowState moves through as the data is parsed */
  2777. private enum RowStateStatus {
  2778. INIT, AT_HEADER, AT_FINAL;
  2779. }
  2780. /**
  2781. * Maintains state for writing a new row of data.
  2782. */
  2783. protected static class WriteRowState
  2784. {
  2785. private int _complexAutoNumber = ColumnImpl.INVALID_AUTO_NUMBER;
  2786. public int getComplexAutoNumber() {
  2787. return _complexAutoNumber;
  2788. }
  2789. public void setComplexAutoNumber(int complexAutoNumber) {
  2790. _complexAutoNumber = complexAutoNumber;
  2791. }
  2792. public void resetAutoNumber() {
  2793. _complexAutoNumber = ColumnImpl.INVALID_AUTO_NUMBER;
  2794. }
  2795. }
  2796. /**
  2797. * Maintains the state of reading/updating a row of data.
  2798. * @usage _advanced_class_
  2799. */
  2800. public final class RowState extends WriteRowState
  2801. implements ErrorHandler.Location
  2802. {
  2803. /** Buffer used for reading the header row data pages */
  2804. private final TempPageHolder _headerRowBufferH;
  2805. /** the header rowId */
  2806. private RowIdImpl _headerRowId = RowIdImpl.FIRST_ROW_ID;
  2807. /** the number of rows on the header page */
  2808. private int _rowsOnHeaderPage;
  2809. /** the rowState status */
  2810. private RowStateStatus _status = RowStateStatus.INIT;
  2811. /** the row status */
  2812. private RowStatus _rowStatus = RowStatus.INIT;
  2813. /** buffer used for reading overflow pages */
  2814. private final TempPageHolder _overflowRowBufferH =
  2815. TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
  2816. /** the row buffer which contains the final data (after following any
  2817. overflow pointers) */
  2818. private ByteBuffer _finalRowBuffer;
  2819. /** the rowId which contains the final data (after following any overflow
  2820. pointers) */
  2821. private RowIdImpl _finalRowId = null;
  2822. /** true if the row values array has data */
  2823. private boolean _haveRowValues;
  2824. /** values read from the last row */
  2825. private Object[] _rowValues;
  2826. /** null mask for the last row */
  2827. private NullMask _nullMask;
  2828. /** last modification count seen on the table we track this so that the
  2829. rowState can detect updates to the table and re-read any buffered
  2830. data */
  2831. private int _lastModCount;
  2832. /** optional error handler to use when row errors are encountered */
  2833. private ErrorHandler _errorHandler;
  2834. /** cached variable column offsets for jump-table based rows */
  2835. private short[] _varColOffsets;
  2836. private RowState(TempBufferHolder.Type headerType) {
  2837. _headerRowBufferH = TempPageHolder.newHolder(headerType);
  2838. _rowValues = new Object[TableImpl.this.getColumnCount()];
  2839. _lastModCount = TableImpl.this._modCount;
  2840. }
  2841. @Override
  2842. public TableImpl getTable() {
  2843. return TableImpl.this;
  2844. }
  2845. public ErrorHandler getErrorHandler() {
  2846. return((_errorHandler != null) ? _errorHandler :
  2847. getTable().getErrorHandler());
  2848. }
  2849. public void setErrorHandler(ErrorHandler newErrorHandler) {
  2850. _errorHandler = newErrorHandler;
  2851. }
  2852. public void reset() {
  2853. resetAutoNumber();
  2854. _finalRowId = null;
  2855. _finalRowBuffer = null;
  2856. _rowsOnHeaderPage = 0;
  2857. _status = RowStateStatus.INIT;
  2858. _rowStatus = RowStatus.INIT;
  2859. _varColOffsets = null;
  2860. _nullMask = null;
  2861. if(_haveRowValues) {
  2862. Arrays.fill(_rowValues, null);
  2863. _haveRowValues = false;
  2864. }
  2865. }
  2866. public boolean isUpToDate() {
  2867. return(TableImpl.this._modCount == _lastModCount);
  2868. }
  2869. private void checkForModification() {
  2870. if(!isUpToDate()) {
  2871. reset();
  2872. _headerRowBufferH.invalidate();
  2873. _overflowRowBufferH.invalidate();
  2874. int colCount = TableImpl.this.getColumnCount();
  2875. if(colCount != _rowValues.length) {
  2876. // columns added or removed from table
  2877. _rowValues = new Object[colCount];
  2878. }
  2879. _lastModCount = TableImpl.this._modCount;
  2880. }
  2881. }
  2882. private ByteBuffer getFinalPage()
  2883. throws IOException
  2884. {
  2885. if(_finalRowBuffer == null) {
  2886. // (re)load current page
  2887. _finalRowBuffer = getHeaderPage();
  2888. }
  2889. return _finalRowBuffer;
  2890. }
  2891. public RowIdImpl getFinalRowId() {
  2892. if(_finalRowId == null) {
  2893. _finalRowId = getHeaderRowId();
  2894. }
  2895. return _finalRowId;
  2896. }
  2897. private void setRowStatus(RowStatus rowStatus) {
  2898. _rowStatus = rowStatus;
  2899. }
  2900. public boolean isValid() {
  2901. return(_rowStatus.ordinal() >= RowStatus.VALID.ordinal());
  2902. }
  2903. public boolean isDeleted() {
  2904. return(_rowStatus == RowStatus.DELETED);
  2905. }
  2906. public boolean isOverflow() {
  2907. return(_rowStatus == RowStatus.OVERFLOW);
  2908. }
  2909. public boolean isHeaderPageNumberValid() {
  2910. return(_rowStatus.ordinal() > RowStatus.INVALID_PAGE.ordinal());
  2911. }
  2912. public boolean isHeaderRowNumberValid() {
  2913. return(_rowStatus.ordinal() > RowStatus.INVALID_ROW.ordinal());
  2914. }
  2915. private void setStatus(RowStateStatus status) {
  2916. _status = status;
  2917. }
  2918. public boolean isAtHeaderRow() {
  2919. return(_status.ordinal() >= RowStateStatus.AT_HEADER.ordinal());
  2920. }
  2921. public boolean isAtFinalRow() {
  2922. return(_status.ordinal() >= RowStateStatus.AT_FINAL.ordinal());
  2923. }
  2924. private Object setRowCacheValue(int idx, Object value) {
  2925. _haveRowValues = true;
  2926. _rowValues[idx] = value;
  2927. return value;
  2928. }
  2929. private Object getRowCacheValue(int idx) {
  2930. Object value = _rowValues[idx];
  2931. // only return immutable values. mutable values could have been
  2932. // modified externally and therefore could return an incorrect value
  2933. return(ColumnImpl.isImmutableValue(value) ? value : null);
  2934. }
  2935. public Object[] getRowCacheValues() {
  2936. return dupeRow(_rowValues, _rowValues.length);
  2937. }
  2938. public NullMask getNullMask(ByteBuffer rowBuffer) throws IOException {
  2939. if(_nullMask == null) {
  2940. _nullMask = getRowNullMask(rowBuffer);
  2941. }
  2942. return _nullMask;
  2943. }
  2944. private short[] getVarColOffsets() {
  2945. return _varColOffsets;
  2946. }
  2947. private void setVarColOffsets(short[] varColOffsets) {
  2948. _varColOffsets = varColOffsets;
  2949. }
  2950. public RowIdImpl getHeaderRowId() {
  2951. return _headerRowId;
  2952. }
  2953. public int getRowsOnHeaderPage() {
  2954. return _rowsOnHeaderPage;
  2955. }
  2956. private ByteBuffer getHeaderPage()
  2957. throws IOException
  2958. {
  2959. checkForModification();
  2960. return _headerRowBufferH.getPage(getPageChannel());
  2961. }
  2962. private ByteBuffer setHeaderRow(RowIdImpl rowId)
  2963. throws IOException
  2964. {
  2965. checkForModification();
  2966. // don't do any work if we are already positioned correctly
  2967. if(isAtHeaderRow() && (getHeaderRowId().equals(rowId))) {
  2968. return(isValid() ? getHeaderPage() : null);
  2969. }
  2970. // rejigger everything
  2971. reset();
  2972. _headerRowId = rowId;
  2973. _finalRowId = rowId;
  2974. int pageNumber = rowId.getPageNumber();
  2975. int rowNumber = rowId.getRowNumber();
  2976. if((pageNumber < 0) || !_ownedPages.containsPageNumber(pageNumber)) {
  2977. setRowStatus(RowStatus.INVALID_PAGE);
  2978. return null;
  2979. }
  2980. _finalRowBuffer = _headerRowBufferH.setPage(getPageChannel(),
  2981. pageNumber);
  2982. _rowsOnHeaderPage = getRowsOnDataPage(_finalRowBuffer, getFormat());
  2983. if((rowNumber < 0) || (rowNumber >= _rowsOnHeaderPage)) {
  2984. setRowStatus(RowStatus.INVALID_ROW);
  2985. return null;
  2986. }
  2987. setRowStatus(RowStatus.VALID);
  2988. return _finalRowBuffer;
  2989. }
  2990. private ByteBuffer setOverflowRow(RowIdImpl rowId)
  2991. throws IOException
  2992. {
  2993. // this should never see modifications because it only happens within
  2994. // the positionAtRowData method
  2995. if(!isUpToDate()) {
  2996. throw new IllegalStateException(getTable().withErrorContext(
  2997. "Table modified while searching?"));
  2998. }
  2999. if(_rowStatus != RowStatus.OVERFLOW) {
  3000. throw new IllegalStateException(getTable().withErrorContext(
  3001. "Row is not an overflow row?"));
  3002. }
  3003. _finalRowId = rowId;
  3004. _finalRowBuffer = _overflowRowBufferH.setPage(getPageChannel(),
  3005. rowId.getPageNumber());
  3006. return _finalRowBuffer;
  3007. }
  3008. private Object handleRowError(ColumnImpl column, byte[] columnData,
  3009. Exception error)
  3010. throws IOException
  3011. {
  3012. return getErrorHandler().handleRowError(column, columnData,
  3013. this, error);
  3014. }
  3015. @Override
  3016. public String toString() {
  3017. return CustomToStringStyle.valueBuilder(this)
  3018. .append("headerRowId", _headerRowId)
  3019. .append("finalRowId", _finalRowId)
  3020. .toString();
  3021. }
  3022. }
  3023. /**
  3024. * Utility for managing calculated columns. Calculated columns need to be
  3025. * evaluated in dependency order.
  3026. */
  3027. private class CalcColEvaluator
  3028. {
  3029. /** List of calculated columns in this table, ordered by calculation
  3030. dependency */
  3031. private final List<ColumnImpl> _calcColumns = new ArrayList<ColumnImpl>(1);
  3032. private boolean _sorted;
  3033. public void add(ColumnImpl col) {
  3034. if(!getDatabase().isEvaluateExpressions()) {
  3035. return;
  3036. }
  3037. _calcColumns.add(col);
  3038. // whenever we add new columns, we need to re-sort
  3039. _sorted = false;
  3040. }
  3041. public void reSort() {
  3042. // mark columns for re-sort on next use
  3043. _sorted = false;
  3044. }
  3045. public void calculate(Object[] row) throws IOException {
  3046. if(!_sorted) {
  3047. sortColumnsByDeps();
  3048. _sorted = true;
  3049. }
  3050. for(ColumnImpl col : _calcColumns) {
  3051. Object rowValue = col.getCalculationContext().eval(row);
  3052. col.setRowValue(row, rowValue);
  3053. }
  3054. }
  3055. private void sortColumnsByDeps() {
  3056. // a topological sort sorts nodes where A -> B such that A ends up in
  3057. // the list before B (assuming that we are working with a DAG). In our
  3058. // case, we return "descendent" info as Field1 -> Field2 (where Field1
  3059. // uses Field2 in its calculation). This means that in order to
  3060. // correctly calculate Field1, we need to calculate Field2 first, and
  3061. // hence essentially need the reverse topo sort (a list where Field2
  3062. // comes before Field1).
  3063. (new TopoSorter<ColumnImpl>(_calcColumns, TopoSorter.REVERSE) {
  3064. @Override
  3065. protected void getDescendents(ColumnImpl from,
  3066. List<ColumnImpl> descendents) {
  3067. Set<Identifier> identifiers = new LinkedHashSet<Identifier>();
  3068. from.getCalculationContext().collectIdentifiers(identifiers);
  3069. for(Identifier identifier : identifiers) {
  3070. if(isThisTable(identifier)) {
  3071. String colName = identifier.getObjectName();
  3072. for(ColumnImpl calcCol : _calcColumns) {
  3073. // we only care if the identifier is another calc field
  3074. if(calcCol.getName().equalsIgnoreCase(colName)) {
  3075. descendents.add(calcCol);
  3076. }
  3077. }
  3078. }
  3079. }
  3080. }
  3081. }).sort();
  3082. }
  3083. }
  3084. }