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.

IndexTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.SortedSet;
  30. import java.util.TreeSet;
  31. import junit.framework.TestCase;
  32. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  33. import static com.healthmarketscience.jackcess.JetFormatTest.*;
  34. /**
  35. * @author James Ahlborn
  36. */
  37. public class IndexTest extends TestCase {
  38. public IndexTest(String name) {
  39. super(name);
  40. }
  41. public void testByteOrder() throws Exception {
  42. byte b1 = (byte)0x00;
  43. byte b2 = (byte)0x01;
  44. byte b3 = (byte)0x7F;
  45. byte b4 = (byte)0x80;
  46. byte b5 = (byte)0xFF;
  47. assertTrue(ByteUtil.asUnsignedByte(b1) < ByteUtil.asUnsignedByte(b2));
  48. assertTrue(ByteUtil.asUnsignedByte(b2) < ByteUtil.asUnsignedByte(b3));
  49. assertTrue(ByteUtil.asUnsignedByte(b3) < ByteUtil.asUnsignedByte(b4));
  50. assertTrue(ByteUtil.asUnsignedByte(b4) < ByteUtil.asUnsignedByte(b5));
  51. }
  52. public void testByteCodeComparator() {
  53. byte[] b0 = null;
  54. byte[] b1 = new byte[]{(byte)0x00};
  55. byte[] b2 = new byte[]{(byte)0x00, (byte)0x00};
  56. byte[] b3 = new byte[]{(byte)0x00, (byte)0x01};
  57. byte[] b4 = new byte[]{(byte)0x01};
  58. byte[] b5 = new byte[]{(byte)0x80};
  59. byte[] b6 = new byte[]{(byte)0xFF};
  60. byte[] b7 = new byte[]{(byte)0xFF, (byte)0x00};
  61. byte[] b8 = new byte[]{(byte)0xFF, (byte)0x01};
  62. List<byte[]> expectedList = Arrays.<byte[]>asList(b0, b1, b2, b3, b4,
  63. b5, b6, b7, b8);
  64. SortedSet<byte[]> sortedSet = new TreeSet<byte[]>(
  65. Index.BYTE_CODE_COMPARATOR);
  66. sortedSet.addAll(expectedList);
  67. assertEquals(expectedList, new ArrayList<byte[]>(sortedSet));
  68. }
  69. public void testPrimaryKey() throws Exception {
  70. for (final TestDB testDB : SUPPORTED_DBS_TEST) {
  71. Table table = open(testDB).getTable("Table1");
  72. Map<String, Boolean> foundPKs = new HashMap<String, Boolean>();
  73. for(Index index : table.getIndexes()) {
  74. foundPKs.put(index.getColumns().iterator().next().getName(),
  75. index.isPrimaryKey());
  76. }
  77. Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>();
  78. expectedPKs.put("A", Boolean.TRUE);
  79. expectedPKs.put("B", Boolean.FALSE);
  80. assertEquals(expectedPKs, foundPKs);
  81. }
  82. }
  83. public void testIndexSlots() throws Exception
  84. {
  85. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
  86. Database mdb = open(testDB);
  87. Table table = mdb.getTable("Table1");
  88. for(Index idx : table.getIndexes()) {
  89. idx.initialize();
  90. }
  91. assertEquals(4, table.getIndexes().size());
  92. assertEquals(4, table.getIndexSlotCount());
  93. checkIndexColumns(table,
  94. "id", "id",
  95. "PrimaryKey", "id",
  96. "Table2Table1", "otherfk1",
  97. "Table3Table1", "otherfk2");
  98. table = mdb.getTable("Table2");
  99. for(Index idx : table.getIndexes()) {
  100. idx.initialize();
  101. }
  102. assertEquals(2, table.getIndexes().size());
  103. assertEquals(3, table.getIndexSlotCount());
  104. checkIndexColumns(table,
  105. "id", "id",
  106. "PrimaryKey", "id");
  107. table = mdb.getTable("Table3");
  108. for(Index idx : table.getIndexes()) {
  109. idx.initialize();
  110. }
  111. assertEquals(2, table.getIndexes().size());
  112. assertEquals(3, table.getIndexSlotCount());
  113. checkIndexColumns(table,
  114. "id", "id",
  115. "PrimaryKey", "id");
  116. }
  117. }
  118. public void testComplexIndex() throws Exception
  119. {
  120. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX)) {
  121. // this file has an index with "compressed" entries and node pages
  122. Database db = open(testDB);
  123. Table t = db.getTable("Table1");
  124. Index index = t.getIndexes().get(0);
  125. assertFalse(index.isInitialized());
  126. assertEquals(512, countRows(t));
  127. assertEquals(512, index.getEntryCount());
  128. db.close();
  129. // copy to temp file and attempt to edit
  130. db = openCopy(testDB);
  131. t = db.getTable("Table1");
  132. index = t.getIndexes().get(0);
  133. System.out.println("IndexTest: Index type: " + index.getClass());
  134. try {
  135. t.addRow(99, "abc", "def");
  136. if(index instanceof SimpleIndex) {
  137. // SimpleIndex doesn't support writing these indexes
  138. fail("Should have thrown UnsupportedOperationException");
  139. }
  140. } catch(UnsupportedOperationException e) {
  141. // success
  142. if(index instanceof BigIndex) {
  143. throw e;
  144. }
  145. }
  146. }
  147. }
  148. public void testEntryDeletion() throws Exception {
  149. for (final TestDB testDB : SUPPORTED_DBS_TEST) {
  150. Table table = openCopy(testDB).getTable("Table1");
  151. for(int i = 0; i < 10; ++i) {
  152. table.addRow("foo" + i, "bar" + i, (byte)42 + i, (short)53 + i, 13 * i,
  153. (6.7d / i), null, null, true);
  154. }
  155. table.reset();
  156. assertRowCount(12, table);
  157. for(Index index : table.getIndexes()) {
  158. assertEquals(12, index.getEntryCount());
  159. }
  160. table.reset();
  161. table.getNextRow();
  162. table.getNextRow();
  163. table.deleteCurrentRow();
  164. table.getNextRow();
  165. table.deleteCurrentRow();
  166. table.getNextRow();
  167. table.getNextRow();
  168. table.deleteCurrentRow();
  169. table.getNextRow();
  170. table.getNextRow();
  171. table.getNextRow();
  172. table.deleteCurrentRow();
  173. table.reset();
  174. assertRowCount(8, table);
  175. for(Index index : table.getIndexes()) {
  176. assertEquals(8, index.getEntryCount());
  177. }
  178. }
  179. }
  180. public void testIgnoreNulls() throws Exception
  181. {
  182. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) {
  183. Database db = openCopy(testDB);
  184. doTestIgnoreNulls(db, "TableIgnoreNulls1");
  185. doTestIgnoreNulls(db, "TableIgnoreNulls2");
  186. db.close();
  187. }
  188. }
  189. private void doTestIgnoreNulls(Database db, String tableName)
  190. throws Exception
  191. {
  192. Table orig = db.getTable(tableName);
  193. Index origI = orig.getIndex("DataIndex");
  194. Table temp = db.getTable(tableName + "_temp");
  195. Index tempI = temp.getIndex("DataIndex");
  196. // copy from orig table to temp table
  197. for(Map<String,Object> row : orig) {
  198. temp.addRow(orig.asRow(row));
  199. }
  200. assertEquals(origI.getEntryCount(), tempI.getEntryCount());
  201. Cursor origC = Cursor.createIndexCursor(orig, origI);
  202. Cursor tempC = Cursor.createIndexCursor(temp, tempI);
  203. while(true) {
  204. boolean origHasNext = origC.moveToNextRow();
  205. boolean tempHasNext = tempC.moveToNextRow();
  206. assertTrue(origHasNext == tempHasNext);
  207. if(!origHasNext) {
  208. break;
  209. }
  210. Map<String,Object> origRow = origC.getCurrentRow();
  211. Cursor.Position origCurPos = origC.getSavepoint().getCurrentPosition();
  212. Map<String,Object> tempRow = tempC.getCurrentRow();
  213. Cursor.Position tempCurPos = tempC.getSavepoint().getCurrentPosition();
  214. assertEquals(origRow, tempRow);
  215. assertEquals(IndexCodesTest.entryToString(origCurPos),
  216. IndexCodesTest.entryToString(tempCurPos));
  217. }
  218. }
  219. public void testUnique() throws Exception
  220. {
  221. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX_PROPERTIES)) {
  222. Database db = openCopy(testDB);
  223. Table t = db.getTable("TableUnique1_temp");
  224. Index index = t.getIndex("DataIndex");
  225. doTestUnique(index, 1,
  226. null, true,
  227. "unique data", true,
  228. null, true,
  229. "more", false,
  230. "stuff", false,
  231. "unique data", false);
  232. t = db.getTable("TableUnique2_temp");
  233. index = t.getIndex("DataIndex");
  234. doTestUnique(index, 2,
  235. null, null, true,
  236. "unique data", 42, true,
  237. "unique data", null, true,
  238. null, null, true,
  239. "some", 42, true,
  240. "more unique data", 13, true,
  241. null, -4242, true,
  242. "another row", -3462, false,
  243. null, 49, false,
  244. "more", null, false,
  245. "unique data", 42, false,
  246. "unique data", null, false,
  247. null, -4242, false);
  248. db.close();
  249. }
  250. }
  251. private void doTestUnique(Index index, int numValues,
  252. Object... testData)
  253. throws Exception
  254. {
  255. for(int i = 0; i < testData.length; i += (numValues + 1)) {
  256. Object[] row = new Object[numValues + 1];
  257. row[0] = "testRow" + i;
  258. for(int j = 1; j < (numValues + 1); ++j) {
  259. row[j] = testData[i + j - 1];
  260. }
  261. boolean expectedSuccess = (Boolean)testData[i + numValues];
  262. IOException failure = null;
  263. try {
  264. index.addRow(row, new RowId(400 + i, 0));
  265. } catch(IOException e) {
  266. failure = e;
  267. }
  268. if(expectedSuccess) {
  269. assertNull(failure);
  270. } else {
  271. assertTrue(failure != null);
  272. assertTrue(failure.getMessage().contains("uniqueness"));
  273. }
  274. }
  275. }
  276. public void testUniqueEntryCount() throws Exception {
  277. for (final TestDB testDB : SUPPORTED_DBS_TEST) {
  278. Database db = openCopy(testDB);
  279. Table table = db.getTable("Table1");
  280. Index indA = table.getIndex("PrimaryKey");
  281. Index indB = table.getIndex("B");
  282. assertEquals(2, indA.getUniqueEntryCount());
  283. assertEquals(2, indB.getUniqueEntryCount());
  284. List<String> bElems = Arrays.asList("bar", null, "baz", "argle", null,
  285. "bazzle", "37", "bar", "bar", "BAZ");
  286. for(int i = 0; i < 10; ++i) {
  287. table.addRow("foo" + i, bElems.get(i), (byte)42 + i, (short)53 + i,
  288. 13 * i, (6.7d / i), null, null, true);
  289. }
  290. assertEquals(12, indA.getEntryCount());
  291. assertEquals(12, indB.getEntryCount());
  292. assertEquals(12, indA.getUniqueEntryCount());
  293. assertEquals(8, indB.getUniqueEntryCount());
  294. table = null;
  295. indA = null;
  296. indB = null;
  297. table = db.getTable("Table1");
  298. indA = table.getIndex("PrimaryKey");
  299. indB = table.getIndex("B");
  300. assertEquals(12, indA.getEntryCount());
  301. assertEquals(12, indB.getEntryCount());
  302. assertEquals(12, indA.getUniqueEntryCount());
  303. assertEquals(8, indB.getUniqueEntryCount());
  304. Cursor c = Cursor.createCursor(table);
  305. assertTrue(c.moveToNextRow());
  306. final Map<String,Object> row = c.getCurrentRow();
  307. // Row order is arbitrary, so v2007 row order difference is valid
  308. if (Database.FileFormat.V2007.equals(testDB.getExpectedFileFormat())) {
  309. DatabaseTest.checkTestDBTable1RowA(testDB, table, row);
  310. } else {
  311. DatabaseTest.checkTestDBTable1RowABCDEFG(testDB, table, row);
  312. }
  313. c.deleteCurrentRow();
  314. assertEquals(11, indA.getEntryCount());
  315. assertEquals(11, indB.getEntryCount());
  316. assertEquals(12, indA.getUniqueEntryCount());
  317. assertEquals(8, indB.getUniqueEntryCount());
  318. db.close();
  319. }
  320. }
  321. public void testReplId() throws Exception
  322. {
  323. for (final TestDB testDB : SUPPORTED_DBS_TEST) {
  324. Database db = openCopy(testDB);
  325. Table table = db.getTable("Table4");
  326. for(int i = 0; i< 20; ++i) {
  327. table.addRow("row" + i, Column.AUTO_NUMBER);
  328. }
  329. assertEquals(20, table.getRowCount());
  330. db.close();
  331. }
  332. }
  333. private void checkIndexColumns(Table table, String... idxInfo)
  334. throws Exception
  335. {
  336. Map<String, String> expectedIndexes = new HashMap<String, String>();
  337. for(int i = 0; i < idxInfo.length; i+=2) {
  338. expectedIndexes.put(idxInfo[i], idxInfo[i+1]);
  339. }
  340. for(Index idx : table.getIndexes()) {
  341. String colName = expectedIndexes.get(idx.getName());
  342. assertEquals(1, idx.getColumns().size());
  343. assertEquals(colName, idx.getColumns().get(0).getName());
  344. if("PrimaryKey".equals(idx.getName())) {
  345. assertTrue(idx.isPrimaryKey());
  346. } else {
  347. assertFalse(idx.isPrimaryKey());
  348. }
  349. }
  350. }
  351. }