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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright (c) 2008 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.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Random;
  27. import junit.framework.TestCase;
  28. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  29. import static com.healthmarketscience.jackcess.JetFormatTest.*;
  30. /**
  31. * @author james
  32. */
  33. public class BigIndexTest extends TestCase {
  34. private String _oldBigIndexValue = null;
  35. public BigIndexTest(String name) {
  36. super(name);
  37. }
  38. @Override
  39. protected void setUp() {
  40. _oldBigIndexValue = System.getProperty(Database.USE_BIG_INDEX_PROPERTY);
  41. System.setProperty(Database.USE_BIG_INDEX_PROPERTY,
  42. Boolean.TRUE.toString());
  43. }
  44. @Override
  45. protected void tearDown() {
  46. if (_oldBigIndexValue != null) {
  47. System.setProperty(Database.USE_BIG_INDEX_PROPERTY, _oldBigIndexValue);
  48. } else {
  49. System.clearProperty(Database.USE_BIG_INDEX_PROPERTY);
  50. }
  51. }
  52. public void testComplexIndex() throws Exception
  53. {
  54. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.COMP_INDEX)) {
  55. // this file has an index with "compressed" entries and node pages
  56. Database db = open(testDB);
  57. Table t = db.getTable("Table1");
  58. Index index = t.getIndex("CD_AGENTE");
  59. assertFalse(index.isInitialized());
  60. assertEquals(512, countRows(t));
  61. assertEquals(512, index.getEntryCount());
  62. db.close();
  63. }
  64. }
  65. public void testBigIndex() throws Exception
  66. {
  67. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.BIG_INDEX)) {
  68. // this file has an index with "compressed" entries and node pages
  69. Database db = open(testDB);
  70. Table t = db.getTable("Table1");
  71. Index index = t.getIndex("col1");
  72. assertFalse(index.isInitialized());
  73. assertEquals(0, countRows(t));
  74. assertEquals(0, index.getEntryCount());
  75. db.close();
  76. DatabaseTest._autoSync = false;
  77. try {
  78. 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";
  79. // copy to temp file and attempt to edit
  80. db = openCopy(testDB);
  81. t = db.getTable("Table1");
  82. index = t.getIndex("col1");
  83. System.out.println("BigIndexTest: Index type: " + index.getClass());
  84. // add 2,000 (pseudo) random entries to the table
  85. Random rand = new Random(13L);
  86. for(int i = 0; i < 2000; ++i) {
  87. if((i == 850) || (i == 1850)) {
  88. int end = i + 50;
  89. List<Object[]> rows = new ArrayList<Object[]>(50);
  90. for(; i < end; ++i) {
  91. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  92. String nextVal = "" + nextInt + extraText;
  93. if(((i + 1) % 333) == 0) {
  94. nextVal = null;
  95. }
  96. rows.add(new Object[]{nextVal,
  97. "this is some row data " + nextInt});
  98. }
  99. t.addRows(rows);
  100. --i;
  101. } else {
  102. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  103. String nextVal = "" + nextInt + extraText;
  104. if(((i + 1) % 333) == 0) {
  105. nextVal = null;
  106. }
  107. t.addRow(nextVal, "this is some row data " + nextInt);
  108. }
  109. }
  110. ((BigIndex)index).validate();
  111. db.flush();
  112. t = db.getTable("Table1");
  113. index = t.getIndex("col1");
  114. // make sure all entries are there and correctly ordered
  115. String firstValue = " ";
  116. String prevValue = firstValue;
  117. int rowCount = 0;
  118. List<String> firstTwo = new ArrayList<String>();
  119. for(Map<String,Object> row : Cursor.createIndexCursor(t, index)) {
  120. String origVal = (String)row.get("col1");
  121. String val = origVal;
  122. if(val == null) {
  123. val = firstValue;
  124. }
  125. assertTrue("" + prevValue + " <= " + val + " " + rowCount,
  126. prevValue.compareTo(val) <= 0);
  127. if(firstTwo.size() < 2) {
  128. firstTwo.add(origVal);
  129. }
  130. prevValue = val;
  131. ++rowCount;
  132. }
  133. assertEquals(2000, rowCount);
  134. ((BigIndex)index).validate();
  135. // delete an entry in the middle
  136. Cursor cursor = Cursor.createIndexCursor(t, index);
  137. for(int i = 0; i < (rowCount / 2); ++i) {
  138. assertTrue(cursor.moveToNextRow());
  139. }
  140. cursor.deleteCurrentRow();
  141. --rowCount;
  142. // remove all but the first two entries (from the end)
  143. cursor.afterLast();
  144. for(int i = 0; i < (rowCount - 2); ++i) {
  145. assertTrue(cursor.moveToPreviousRow());
  146. cursor.deleteCurrentRow();
  147. }
  148. ((BigIndex)index).validate();
  149. List<String> found = new ArrayList<String>();
  150. for(Map<String,Object> row : Cursor.createIndexCursor(t, index)) {
  151. found.add((String)row.get("col1"));
  152. }
  153. assertEquals(firstTwo, found);
  154. // remove remaining entries
  155. cursor = Cursor.createCursor(t);
  156. for(int i = 0; i < 2; ++i) {
  157. assertTrue(cursor.moveToNextRow());
  158. cursor.deleteCurrentRow();
  159. }
  160. assertFalse(cursor.moveToNextRow());
  161. assertFalse(cursor.moveToPreviousRow());
  162. ((BigIndex)index).validate();
  163. // add 50 (pseudo) random entries to the table
  164. rand = new Random(42L);
  165. for(int i = 0; i < 50; ++i) {
  166. int nextInt = rand.nextInt(Integer.MAX_VALUE);
  167. String nextVal = "some prefix " + nextInt + extraText;
  168. if(((i + 1) % 3333) == 0) {
  169. nextVal = null;
  170. }
  171. t.addRow(nextVal, "this is some row data " + nextInt);
  172. }
  173. ((BigIndex)index).validate();
  174. cursor = Cursor.createIndexCursor(t, index);
  175. while(cursor.moveToNextRow()) {
  176. cursor.deleteCurrentRow();
  177. }
  178. ((BigIndex)index).validate();
  179. db.close();
  180. } finally {
  181. DatabaseTest._autoSync = Database.DEFAULT_AUTO_SYNC;
  182. }
  183. }
  184. }
  185. }