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.

BigIndexTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright (c) 2008 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;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Random;
  17. import junit.framework.TestCase;
  18. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  19. import com.healthmarketscience.jackcess.impl.TableImpl;
  20. import com.healthmarketscience.jackcess.impl.IndexImpl;
  21. import static com.healthmarketscience.jackcess.TestUtil.*;
  22. /**
  23. * @author james
  24. */
  25. public class BigIndexTest extends TestCase {
  26. public BigIndexTest(String name) {
  27. super(name);
  28. }
  29. public void testComplexIndex() throws Exception
  30. {
  31. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX, true)) {
  32. // this file has an index with "compressed" entries and node pages
  33. Database db = openMem(testDB);
  34. TableImpl t = (TableImpl)db.getTable("Table1");
  35. IndexImpl index = t.getIndex("CD_AGENTE");
  36. assertFalse(index.isInitialized());
  37. assertEquals(512, countRows(t));
  38. assertEquals(512, index.getIndexData().getEntryCount());
  39. db.close();
  40. }
  41. }
  42. public void testBigIndex() throws Exception
  43. {
  44. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BIG_INDEX)) {
  45. // this file has an index with "compressed" entries and node pages
  46. Database db = openMem(testDB);
  47. TableImpl t = (TableImpl)db.getTable("Table1");
  48. IndexImpl index = t.getIndex("col1");
  49. assertFalse(index.isInitialized());
  50. assertEquals(0, countRows(t));
  51. assertEquals(0, index.getIndexData().getEntryCount());
  52. db.close();
  53. TestUtil.setTestAutoSync(false);
  54. try {
  55. String extraText = " some random text to fill out the index and make it fill up pages with lots of extra bytes so i will keep typing until i think that i probably have enough text in the index entry so that i do not need to add as many entries in order";
  56. // copy to temp file and attempt to edit
  57. db = openMem(testDB);
  58. t = (TableImpl)db.getTable("Table1");
  59. index = t.getIndex("col1");
  60. // add 2,000 (pseudo) random entries to the table
  61. Random rand = new Random(13L);
  62. for(int i = 0; i < 2000; ++i) {
  63. if((i == 850) || (i == 1850)) {
  64. int end = i + 50;
  65. List<Object[]> rows = new ArrayList<Object[]>(50);
  66. for(; i < end; ++i) {
  67. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  68. String nextVal = "" + nextInt + extraText;
  69. if(((i + 1) % 333) == 0) {
  70. nextVal = null;
  71. }
  72. rows.add(new Object[]{nextVal,
  73. "this is some row data " + nextInt});
  74. }
  75. t.addRows(rows);
  76. --i;
  77. } else {
  78. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  79. String nextVal = "" + nextInt + extraText;
  80. if(((i + 1) % 333) == 0) {
  81. nextVal = null;
  82. }
  83. t.addRow(nextVal, "this is some row data " + nextInt);
  84. }
  85. }
  86. index.getIndexData().validate();
  87. db.flush();
  88. t = null;
  89. System.gc();
  90. t = (TableImpl)db.getTable("Table1");
  91. index = t.getIndex("col1");
  92. // make sure all entries are there and correctly ordered
  93. String firstValue = " ";
  94. String prevValue = firstValue;
  95. int rowCount = 0;
  96. List<String> firstTwo = new ArrayList<String>();
  97. for(Row row : CursorBuilder.createCursor(index)) {
  98. String origVal = row.getString("col1");
  99. String val = origVal;
  100. if(val == null) {
  101. val = firstValue;
  102. }
  103. assertTrue("" + prevValue + " <= " + val + " " + rowCount,
  104. prevValue.compareTo(val) <= 0);
  105. if(firstTwo.size() < 2) {
  106. firstTwo.add(origVal);
  107. }
  108. prevValue = val;
  109. ++rowCount;
  110. }
  111. assertEquals(2000, rowCount);
  112. index.getIndexData().validate();
  113. // delete an entry in the middle
  114. Cursor cursor = CursorBuilder.createCursor(index);
  115. for(int i = 0; i < (rowCount / 2); ++i) {
  116. assertTrue(cursor.moveToNextRow());
  117. }
  118. cursor.deleteCurrentRow();
  119. --rowCount;
  120. // remove all but the first two entries (from the end)
  121. cursor.afterLast();
  122. for(int i = 0; i < (rowCount - 2); ++i) {
  123. assertTrue(cursor.moveToPreviousRow());
  124. cursor.deleteCurrentRow();
  125. }
  126. index.getIndexData().validate();
  127. List<String> found = new ArrayList<String>();
  128. for(Row row : CursorBuilder.createCursor(index)) {
  129. found.add(row.getString("col1"));
  130. }
  131. assertEquals(firstTwo, found);
  132. // remove remaining entries
  133. cursor = CursorBuilder.createCursor(t);
  134. for(int i = 0; i < 2; ++i) {
  135. assertTrue(cursor.moveToNextRow());
  136. cursor.deleteCurrentRow();
  137. }
  138. assertFalse(cursor.moveToNextRow());
  139. assertFalse(cursor.moveToPreviousRow());
  140. index.getIndexData().validate();
  141. // add 50 (pseudo) random entries to the table
  142. rand = new Random(42L);
  143. for(int i = 0; i < 50; ++i) {
  144. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  145. String nextVal = "some prefix " + nextInt + extraText;
  146. if(((i + 1) % 3333) == 0) {
  147. nextVal = null;
  148. }
  149. t.addRow(nextVal, "this is some row data " + nextInt);
  150. }
  151. index.getIndexData().validate();
  152. cursor = CursorBuilder.createCursor(index);
  153. while(cursor.moveToNextRow()) {
  154. cursor.deleteCurrentRow();
  155. }
  156. index.getIndexData().validate();
  157. db.close();
  158. } finally {
  159. TestUtil.clearTestAutoSync();
  160. }
  161. }
  162. }
  163. }