Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TableUpdater.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. Copyright (c) 2016 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.io.IOException;
  15. import java.nio.ByteBuffer;
  16. import java.util.ArrayList;
  17. import java.util.EnumSet;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Set;
  21. import com.healthmarketscience.jackcess.ColumnBuilder;
  22. import com.healthmarketscience.jackcess.DataType;
  23. import com.healthmarketscience.jackcess.IndexBuilder;
  24. /**
  25. * Helper class used to maintain state during table mutation.
  26. *
  27. * @author James Ahlborn
  28. * @usage _advanced_class_
  29. */
  30. public class TableUpdater extends TableMutator
  31. {
  32. private final TableImpl _table;
  33. private ColumnBuilder _column;
  34. private IndexBuilder _index;
  35. private int _origTdefLen;
  36. private int _addedTdefLen;
  37. private List<Integer> _nextPages = new ArrayList<Integer>(1);
  38. private ColumnState _colState;
  39. private IndexDataState _idxDataState;
  40. private IndexImpl.ForeignKeyReference _fkReference;
  41. public TableUpdater(TableImpl table) {
  42. super(table.getDatabase());
  43. _table = table;
  44. }
  45. public ColumnBuilder getColumn() {
  46. return _column;
  47. }
  48. public IndexBuilder getIndex() {
  49. return _index;
  50. }
  51. @Override
  52. String getTableName() {
  53. return _table.getName();
  54. }
  55. @Override
  56. public int getTdefPageNumber() {
  57. return _table.getTableDefPageNumber();
  58. }
  59. @Override
  60. short getColumnNumber(String colName) {
  61. for(ColumnImpl col : _table.getColumns()) {
  62. if(col.getName().equalsIgnoreCase(colName)) {
  63. return col.getColumnNumber();
  64. }
  65. }
  66. return IndexData.COLUMN_UNUSED;
  67. }
  68. @Override
  69. public ColumnState getColumnState(ColumnBuilder col) {
  70. return ((col == _column) ? _colState : null);
  71. }
  72. @Override
  73. public IndexDataState getIndexDataState(IndexBuilder idx) {
  74. return ((idx == _index) ? _idxDataState : null);
  75. }
  76. void setForeignKey(IndexImpl.ForeignKeyReference fkReference) {
  77. _fkReference = fkReference;
  78. }
  79. @Override
  80. public IndexImpl.ForeignKeyReference getForeignKey(IndexBuilder idx) {
  81. return ((idx == _index) ? _fkReference : null);
  82. }
  83. int getAddedTdefLen() {
  84. return _addedTdefLen;
  85. }
  86. void addTdefLen(int add) {
  87. _addedTdefLen += add;
  88. }
  89. void setOrigTdefLen(int len) {
  90. _origTdefLen = len;
  91. }
  92. List<Integer> getNextPages() {
  93. return _nextPages;
  94. }
  95. void resetTdefInfo() {
  96. _addedTdefLen = 0;
  97. _origTdefLen = 0;
  98. _nextPages.clear();
  99. }
  100. public ColumnImpl addColumn(ColumnBuilder column) throws IOException {
  101. _column = column;
  102. validateAddColumn();
  103. // assign column number and do some assorted column bookkeeping
  104. short columnNumber = (short)_table.getMaxColumnCount();
  105. _column.setColumnNumber(columnNumber);
  106. if(_column.getType().isLongValue()) {
  107. _colState = new ColumnState();
  108. }
  109. getPageChannel().startExclusiveWrite();
  110. try {
  111. return _table.mutateAddColumn(this);
  112. } finally {
  113. getPageChannel().finishWrite();
  114. }
  115. }
  116. public IndexImpl addIndex(IndexBuilder index) throws IOException {
  117. _index = index;
  118. validateAddIndex();
  119. // assign index number and do some assorted index bookkeeping
  120. int indexNumber = _table.getLogicalIndexCount();
  121. _index.setIndexNumber(indexNumber);
  122. // initialize backing index state
  123. initIndexDataState();
  124. getPageChannel().startExclusiveWrite();
  125. try {
  126. if(_idxDataState.getIndexDataNumber() == _table.getIndexCount()) {
  127. // we need a new backing index data
  128. _table.mutateAddIndexData(this);
  129. // we need to modify the table def again when adding the Index, so reset
  130. resetTdefInfo();
  131. }
  132. return _table.mutateAddIndex(this);
  133. } finally {
  134. getPageChannel().finishWrite();
  135. }
  136. }
  137. boolean validateUpdatedTdef(ByteBuffer tableBuffer) {
  138. // sanity check the updates
  139. return((_origTdefLen + _addedTdefLen) == tableBuffer.limit());
  140. }
  141. private void validateAddColumn() {
  142. if(_column == null) {
  143. throw new IllegalArgumentException(withErrorContext(
  144. "Cannot add column with no column"));
  145. }
  146. if((_table.getColumnCount() + 1) > getFormat().MAX_COLUMNS_PER_TABLE) {
  147. throw new IllegalArgumentException(withErrorContext(
  148. "Cannot add column to table with " +
  149. getFormat().MAX_COLUMNS_PER_TABLE + " columns"));
  150. }
  151. Set<String> colNames = getColumnNames();
  152. // next, validate the column definition
  153. validateColumn(colNames, _column);
  154. if(_column.isAutoNumber()) {
  155. // for most autonumber types, we can only have one of each type
  156. Set<DataType> autoTypes = EnumSet.noneOf(DataType.class);
  157. for(ColumnImpl column : _table.getAutoNumberColumns()) {
  158. autoTypes.add(column.getType());
  159. }
  160. validateAutoNumberColumn(autoTypes, _column);
  161. }
  162. }
  163. private void validateAddIndex() {
  164. if(_index == null) {
  165. throw new IllegalArgumentException(withErrorContext(
  166. "Cannot add index with no index"));
  167. }
  168. if((_table.getLogicalIndexCount() + 1) > getFormat().MAX_INDEXES_PER_TABLE) {
  169. throw new IllegalArgumentException(withErrorContext(
  170. "Cannot add index to table with " +
  171. getFormat().MAX_INDEXES_PER_TABLE + " indexes"));
  172. }
  173. boolean foundPk[] = new boolean[1];
  174. Set<String> idxNames = getIndexNames(_table, foundPk);
  175. // next, validate the index definition
  176. validateIndex(getColumnNames(), idxNames, foundPk, _index);
  177. }
  178. private Set<String> getColumnNames() {
  179. Set<String> colNames = new HashSet<String>();
  180. for(ColumnImpl column : _table.getColumns()) {
  181. colNames.add(column.getName().toUpperCase());
  182. }
  183. return colNames;
  184. }
  185. static Set<String> getIndexNames(TableImpl table, boolean[] foundPk) {
  186. Set<String> idxNames = new HashSet<String>();
  187. for(IndexImpl index : table.getIndexes()) {
  188. idxNames.add(index.getName().toUpperCase());
  189. if(index.isPrimaryKey() && (foundPk != null)) {
  190. foundPk[0] = true;
  191. }
  192. }
  193. return idxNames;
  194. }
  195. private void initIndexDataState() {
  196. _idxDataState = new IndexDataState();
  197. _idxDataState.addIndex(_index);
  198. // search for an existing index which matches the given index (in terms of
  199. // the backing data)
  200. IndexData idxData = findIndexData(_index, _table, (byte)0, (byte)0);
  201. int idxDataNumber = ((idxData != null) ?
  202. idxData.getIndexDataNumber() :
  203. _table.getIndexCount());
  204. _idxDataState.setIndexDataNumber(idxDataNumber);
  205. }
  206. static IndexData findIndexData(IndexBuilder idx, TableImpl table,
  207. byte ignoreIdxFlags, byte ignoreColFlags)
  208. {
  209. for(IndexData idxData : table.getIndexDatas()) {
  210. if(sameIndexData(idx, idxData, ignoreIdxFlags, ignoreColFlags)) {
  211. return idxData;
  212. }
  213. }
  214. return null;
  215. }
  216. private static boolean sameIndexData(IndexBuilder idx1, IndexData idx2,
  217. byte ignoreIdxFlags, byte ignoreColFlags) {
  218. // index data can be combined if flags match and columns (and col flags)
  219. // match
  220. if((idx1.getFlags() | ignoreIdxFlags) !=
  221. (idx2.getIndexFlags() | ignoreIdxFlags)) {
  222. return false;
  223. }
  224. if(idx1.getColumns().size() != idx2.getColumns().size()) {
  225. return false;
  226. }
  227. for(int i = 0; i < idx1.getColumns().size(); ++i) {
  228. IndexBuilder.Column col1 = idx1.getColumns().get(i);
  229. IndexData.ColumnDescriptor col2 = idx2.getColumns().get(i);
  230. if(!sameIndexData(col1, col2, ignoreColFlags)) {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. private static boolean sameIndexData(
  237. IndexBuilder.Column col1, IndexData.ColumnDescriptor col2,
  238. int ignoreColFlags) {
  239. return (col1.getName().equals(col2.getName()) &&
  240. ((col1.getFlags() | ignoreColFlags) ==
  241. (col2.getFlags() | ignoreColFlags)));
  242. }
  243. @Override
  244. protected String withErrorContext(String msg) {
  245. String objStr = "";
  246. if(_column != null) {
  247. objStr = ";Column=" + _column.getName();
  248. } else if(_index != null) {
  249. objStr = ";Index=" + _index.getName();
  250. }
  251. return msg + "(Table=" + _table.getName() + objStr + ")";
  252. }
  253. }