001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.indexmanager;
018:
019: import junit.framework.TestCase;
020: import org.apache.lucene.index.TermFreqVector;
021: import org.apache.lucene.index.TermPositionVector;
022: import org.apache.lucene.index.TermVectorOffsetInfo;
023: import org.compass.core.Compass;
024: import org.compass.core.CompassSession;
025: import org.compass.core.CompassTransaction;
026: import org.compass.core.Resource;
027: import org.compass.core.cache.first.NullFirstLevelCache;
028: import org.compass.core.config.CompassConfiguration;
029: import org.compass.core.config.CompassEnvironment;
030: import org.compass.core.config.CompassSettings;
031: import org.compass.core.engine.SearchEngineException;
032: import org.compass.core.engine.SearchEngineIndexManager;
033: import org.compass.core.lucene.LuceneEnvironment;
034: import org.compass.core.lucene.util.LuceneHelper;
035: import org.compass.core.test.AbstractTestCase;
036:
037: /**
038: * @author kimchy
039: */
040: public class ReplaceIndexTests extends TestCase {
041:
042: private Compass compass;
043:
044: protected String[] getMappings() {
045: return new String[] { "indexmanager/indexmanager.cpm.xml" };
046: }
047:
048: public void testReplaceFSCompundWithFSCompund() throws Exception {
049: CompassSettings actualSettings = new CompassSettings()
050: .setSetting(CompassEnvironment.CONNECTION,
051: "target/test-index")
052: .setBooleanSetting(
053: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
054: true);
055: CompassSettings fromSettings = new CompassSettings()
056: .setSetting(CompassEnvironment.CONNECTION,
057: "target/test-tempindex")
058: .setBooleanSetting(
059: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
060: true);
061: setUpOrigCompass(actualSettings);
062: try {
063: innerTestReplaceIndex(fromSettings);
064: } finally {
065: tearDownOrigCompass();
066: }
067: }
068:
069: public void testReplaceFSCompundWithFSUnCompound() throws Exception {
070: CompassSettings actualSettings = new CompassSettings()
071: .setSetting(CompassEnvironment.CONNECTION,
072: "target/test-index")
073: .setBooleanSetting(
074: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
075: true);
076: CompassSettings fromSettings = new CompassSettings()
077: .setSetting(CompassEnvironment.CONNECTION,
078: "target/test-index-temp")
079: .setBooleanSetting(
080: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
081: false);
082: setUpOrigCompass(actualSettings);
083: try {
084: innerTestReplaceIndex(fromSettings);
085: } finally {
086: tearDownOrigCompass();
087: }
088: }
089:
090: public void testReplaceFSUnCompundWithFSUnCompound()
091: throws Exception {
092: CompassSettings actualSettings = new CompassSettings()
093: .setSetting(CompassEnvironment.CONNECTION,
094: "target/test-index")
095: .setBooleanSetting(
096: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
097: false);
098: CompassSettings fromSettings = new CompassSettings()
099: .setSetting(CompassEnvironment.CONNECTION,
100: "target/test-index-temp")
101: .setBooleanSetting(
102: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
103: false);
104: setUpOrigCompass(actualSettings);
105: try {
106: innerTestReplaceIndex(fromSettings);
107: } finally {
108: tearDownOrigCompass();
109: }
110: }
111:
112: public void testReplaceFSUnCompundWithFSCompound() throws Exception {
113: CompassSettings actualSettings = new CompassSettings()
114: .setSetting(CompassEnvironment.CONNECTION,
115: "target/test-index")
116: .setBooleanSetting(
117: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
118: false);
119: CompassSettings fromSettings = new CompassSettings()
120: .setSetting(CompassEnvironment.CONNECTION,
121: "target/test-index-temp")
122: .setBooleanSetting(
123: LuceneEnvironment.SearchEngineIndex.USE_COMPOUND_FILE,
124: true);
125: setUpOrigCompass(actualSettings);
126: try {
127: innerTestReplaceIndex(fromSettings);
128: } finally {
129: tearDownOrigCompass();
130: }
131: }
132:
133: public void testReplaceFSWithRAM() throws Exception {
134: CompassSettings actualSettings = new CompassSettings()
135: .setSetting(CompassEnvironment.CONNECTION,
136: "target/test-index");
137: CompassSettings fromSettings = new CompassSettings()
138: .setSetting(CompassEnvironment.CONNECTION,
139: "ram://target/test-index-temp");
140: setUpOrigCompass(actualSettings);
141: try {
142: innerTestReplaceIndex(fromSettings);
143: } finally {
144: tearDownOrigCompass();
145: }
146: }
147:
148: public void testReplaceRAMWithRAM() throws Exception {
149: CompassSettings actualSettings = new CompassSettings()
150: .setSetting(CompassEnvironment.CONNECTION,
151: "ram://target/test-index");
152: CompassSettings fromSettings = new CompassSettings()
153: .setSetting(CompassEnvironment.CONNECTION,
154: "ram://target/test-index-temp");
155: setUpOrigCompass(actualSettings);
156: try {
157: innerTestReplaceIndex(fromSettings);
158: } finally {
159: tearDownOrigCompass();
160: }
161: }
162:
163: public void testReplaceJdbcWithFS() throws Exception {
164: CompassSettings actualSettings = new CompassSettings()
165: .setSetting(CompassEnvironment.CONNECTION,
166: "jdbc://jdbc:hsqldb:mem:test")
167: .setSetting(LuceneEnvironment.JdbcStore.DIALECT,
168: "org.apache.lucene.store.jdbc.dialect.HSQLDialect")
169: .setSetting(
170: LuceneEnvironment.JdbcStore.Connection.DRIVER_CLASS,
171: "org.hsqldb.jdbcDriver")
172: .setSetting(
173: LuceneEnvironment.JdbcStore.Connection.USERNAME,
174: "sa")
175: .setSetting(
176: LuceneEnvironment.JdbcStore.Connection.PASSWORD,
177: "");
178: CompassSettings fromSettings = new CompassSettings()
179: .setSetting(CompassEnvironment.CONNECTION,
180: "target/test-index-temp");
181: setUpOrigCompass(actualSettings);
182: try {
183: innerTestReplaceIndex(fromSettings);
184: } finally {
185: tearDownOrigCompass();
186: }
187: }
188:
189: public void testReplaceJdbcWithJdbc() throws Exception {
190: CompassSettings actualSettings = new CompassSettings()
191: .setSetting(CompassEnvironment.CONNECTION,
192: "jdbc://jdbc:hsqldb:mem:test1")
193: .setSetting(LuceneEnvironment.JdbcStore.DIALECT,
194: "org.apache.lucene.store.jdbc.dialect.HSQLDialect")
195: .setSetting(
196: LuceneEnvironment.JdbcStore.Connection.DRIVER_CLASS,
197: "org.hsqldb.jdbcDriver")
198: .setSetting(
199: LuceneEnvironment.JdbcStore.Connection.USERNAME,
200: "sa")
201: .setSetting(
202: LuceneEnvironment.JdbcStore.Connection.PASSWORD,
203: "");
204: CompassSettings fromSettings = new CompassSettings()
205: .setSetting(CompassEnvironment.CONNECTION,
206: "jdbc://jdbc:hsqldb:mem:test2")
207: .setSetting(LuceneEnvironment.JdbcStore.DIALECT,
208: "org.apache.lucene.store.jdbc.dialect.HSQLDialect")
209: .setSetting(
210: LuceneEnvironment.JdbcStore.Connection.DRIVER_CLASS,
211: "org.hsqldb.jdbcDriver")
212: .setSetting(
213: LuceneEnvironment.JdbcStore.Connection.USERNAME,
214: "sa")
215: .setSetting(
216: LuceneEnvironment.JdbcStore.Connection.PASSWORD,
217: "");
218: setUpOrigCompass(actualSettings);
219: try {
220: innerTestReplaceIndex(fromSettings);
221: } finally {
222: tearDownOrigCompass();
223: }
224: }
225:
226: public void testReplaceFSWithJdbc() throws Exception {
227: CompassSettings actualSettings = new CompassSettings()
228: .setSetting(CompassEnvironment.CONNECTION,
229: "target/test-index");
230: CompassSettings fromSettings = new CompassSettings()
231: .setSetting(CompassEnvironment.CONNECTION,
232: "jdbc://jdbc:hsqldb:mem:test")
233: .setSetting(LuceneEnvironment.JdbcStore.DIALECT,
234: "org.apache.lucene.store.jdbc.dialect.HSQLDialect")
235: .setSetting(
236: LuceneEnvironment.JdbcStore.Connection.DRIVER_CLASS,
237: "org.hsqldb.jdbcDriver")
238: .setSetting(
239: LuceneEnvironment.JdbcStore.Connection.USERNAME,
240: "sa")
241: .setSetting(
242: LuceneEnvironment.JdbcStore.Connection.PASSWORD,
243: "");
244: setUpOrigCompass(actualSettings);
245: try {
246: innerTestReplaceIndex(fromSettings);
247: } finally {
248: tearDownOrigCompass();
249: }
250: }
251:
252: private void setUpOrigCompass(CompassSettings actualSettings) {
253: CompassConfiguration conf = new CompassConfiguration()
254: .configure("/org/compass/core/test/compass.cfg.xml");
255: String[] mappings = getMappings();
256: for (int i = 0; i < mappings.length; i++) {
257: conf.addResource("org/compass/core/test/" + mappings[i],
258: AbstractTestCase.class.getClassLoader());
259: }
260: conf.getSettings().setSetting(
261: CompassEnvironment.Cache.FirstLevel.TYPE,
262: NullFirstLevelCache.class.getName());
263: conf.getSettings().addSettings(actualSettings);
264: compass = conf.buildCompass();
265: try {
266: compass.getSearchEngineIndexManager().deleteIndex();
267: } catch (Exception e) {
268: e.printStackTrace();
269: }
270: compass.getSearchEngineIndexManager().verifyIndex();
271: }
272:
273: private void tearDownOrigCompass() {
274: compass.close();
275: try {
276: compass.getSearchEngineIndexManager().deleteIndex();
277: } catch (Exception e) {
278: e.printStackTrace(System.err);
279: }
280: }
281:
282: private void innerTestReplaceIndex(CompassSettings fromSettings)
283: throws Exception {
284: // first create some data in the current index
285: // this will aquire a read cache on the index
286: CompassSession session = compass.openSession();
287: CompassTransaction tr = session.beginTransaction();
288: A a = new A();
289: a.setId(new Long(1));
290: a.setValue("first test string");
291: session.save("a1", a);
292: session.save("a2", a);
293: tr.commit();
294: session.close();
295:
296: // check that we have the date
297: session = compass.openSession();
298: tr = session.beginTransaction();
299: a = (A) session.get("a1", new Long(1));
300: assertNotNull(a);
301: a = (A) session.get("a1", new Long(2));
302: assertNull(a);
303: a = (A) session.get("a2", new Long(1));
304: assertNotNull(a);
305: a = (A) session.get("a2", new Long(2));
306: assertNull(a);
307: tr.commit();
308: session.close();
309:
310: // create some other data in a different index
311: final Compass tempCompass = compass.clone(fromSettings);
312: tempCompass.getSearchEngineIndexManager().deleteIndex();
313: tempCompass.getSearchEngineIndexManager().verifyIndex();
314:
315: SearchEngineIndexManager indexManager = compass
316: .getSearchEngineIndexManager();
317: indexManager.replaceIndex(tempCompass
318: .getSearchEngineIndexManager(),
319: new SearchEngineIndexManager.ReplaceIndexCallback() {
320: public void buildIndexIfNeeded()
321: throws SearchEngineException {
322: CompassSession session = tempCompass
323: .openSession();
324: CompassTransaction tr = session
325: .beginTransaction();
326: A a = new A();
327: a.setId(new Long(2));
328: a.setValue("first test string");
329: session.save("a1", a);
330: session.save("a2", a);
331: tr.commit();
332: session.close();
333: }
334: });
335:
336: tempCompass.close();
337: tempCompass.getSearchEngineIndexManager().deleteIndex();
338:
339: // see if the index was replaced
340: session = compass.openSession();
341: tr = session.beginTransaction();
342: a = (A) session.get("a1", new Long(1));
343: assertNull(a);
344: a = (A) session.get("a1", new Long(2));
345: assertNotNull(a);
346: innerTestTermVectorYesWithPostionsAndOffsets(session, "a1");
347:
348: a = (A) session.get("a2", new Long(1));
349: assertNull(a);
350: a = (A) session.get("a2", new Long(2));
351: assertNotNull(a);
352: innerTestTermVectorYesWithPostionsAndOffsets(session, "a2");
353:
354: tr.commit();
355: session.close();
356: }
357:
358: private void innerTestTermVectorYesWithPostionsAndOffsets(
359: CompassSession session, String subIndex) {
360: Long id = new Long(2);
361: A a = (A) session.load(subIndex, id);
362: assertEquals("first test string", a.getValue());
363: Resource r = session.loadResource(subIndex, id);
364: assertNotNull(r.getValue("mvalue1"));
365: assertEquals(true, r.getProperty("mvalue1").isStored());
366: assertEquals(true, r.getProperty("mvalue1").isIndexed());
367: assertEquals(true, r.getProperty("mvalue1")
368: .isTermVectorStored());
369:
370: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
371: session, r, "mvalue1");
372: assertEquals("mvalue1", termInfoVector.getField());
373: String[] terms = termInfoVector.getTerms();
374: assertEquals(3, terms.length);
375: assertEquals("first", terms[0]);
376: assertEquals("string", terms[1]);
377: assertEquals("test", terms[2]);
378:
379: int[] freqs = termInfoVector.getTermFrequencies();
380: assertEquals(3, freqs.length);
381:
382: int[] positions = ((TermPositionVector) termInfoVector)
383: .getTermPositions(0);
384: assertNotNull(positions);
385: assertEquals(1, positions.length);
386:
387: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
388: .getOffsets(0);
389: assertNotNull(offsets);
390: assertEquals(1, offsets.length);
391:
392: TermFreqVector[] termInfoVectors = LuceneHelper
393: .getTermFreqVectors(session, r);
394: assertEquals(1, termInfoVectors.length);
395: }
396:
397: }
|