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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  18. import com.healthmarketscience.jackcess.impl.TableImpl;
  19. import com.healthmarketscience.jackcess.impl.IndexImpl;
  20. import static com.healthmarketscience.jackcess.TestUtil.*;
  21. import static org.junit.jupiter.api.Assertions.*;
  22. import org.junit.jupiter.api.Test;
  23. /**
  24. * @author james
  25. */
  26. public class BigIndexTest
  27. {
  28. @Test
  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. @Test
  43. public void testBigIndex() throws Exception
  44. {
  45. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BIG_INDEX)) {
  46. // this file has an index with "compressed" entries and node pages
  47. Database db = openMem(testDB);
  48. TableImpl t = (TableImpl)db.getTable("Table1");
  49. IndexImpl index = t.getIndex("col1");
  50. assertFalse(index.isInitialized());
  51. assertEquals(0, countRows(t));
  52. assertEquals(0, index.getIndexData().getEntryCount());
  53. db.close();
  54. TestUtil.setTestAutoSync(false);
  55. try {
  56. 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";
  57. // copy to temp file and attempt to edit
  58. db = openMem(testDB);
  59. t = (TableImpl)db.getTable("Table1");
  60. index = t.getIndex("col1");
  61. // add 2,000 (pseudo) random entries to the table
  62. Random rand = new Random(13L);
  63. for(int i = 0; i < 2000; ++i) {
  64. if((i == 850) || (i == 1850)) {
  65. int end = i + 50;
  66. List<Object[]> rows = new ArrayList<Object[]>(50);
  67. for(; i < end; ++i) {
  68. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  69. String nextVal = "" + nextInt + extraText;
  70. if(((i + 1) % 333) == 0) {
  71. nextVal = null;
  72. }
  73. rows.add(new Object[]{nextVal,
  74. "this is some row data " + nextInt});
  75. }
  76. t.addRows(rows);
  77. --i;
  78. } else {
  79. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  80. String nextVal = "" + nextInt + extraText;
  81. if(((i + 1) % 333) == 0) {
  82. nextVal = null;
  83. }
  84. t.addRow(nextVal, "this is some row data " + nextInt);
  85. }
  86. }
  87. index.getIndexData().validate(false);
  88. db.flush();
  89. t = null;
  90. System.gc();
  91. t = (TableImpl)db.getTable("Table1");
  92. index = t.getIndex("col1");
  93. // make sure all entries are there and correctly ordered
  94. String firstValue = " ";
  95. String prevValue = firstValue;
  96. int rowCount = 0;
  97. List<String> firstTwo = new ArrayList<String>();
  98. for(Row row : CursorBuilder.createCursor(index)) {
  99. String origVal = row.getString("col1");
  100. String val = origVal;
  101. if(val == null) {
  102. val = firstValue;
  103. }
  104. assertTrue(prevValue.compareTo(val) <= 0, "" + prevValue + " <= " + val + " " + rowCount);
  105. if(firstTwo.size() < 2) {
  106. firstTwo.add(origVal);
  107. }
  108. prevValue = val;
  109. ++rowCount;
  110. }
  111. assertEquals(2000, rowCount);
  112. index.getIndexData().validate(false);
  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(false);
  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(false);
  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(false);
  152. cursor = CursorBuilder.createCursor(index);
  153. while(cursor.moveToNextRow()) {
  154. cursor.deleteCurrentRow();
  155. }
  156. index.getIndexData().validate(false);
  157. db.close();
  158. } finally {
  159. TestUtil.clearTestAutoSync();
  160. }
  161. }
  162. }
  163. }