1 package com.tctest; 2 3 import java.util.Iterator ; 4 import java.util.concurrent.BrokenBarrierException ; 5 import java.util.concurrent.CyclicBarrier ; 6 7 import org.apache.lucene.analysis.standard.StandardAnalyzer; 8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.document.Field; 10 import org.apache.lucene.index.IndexWriter; 11 import org.apache.lucene.queryParser.QueryParser; 12 import org.apache.lucene.store.RAMDirectory; 13 import org.apache.lucene.search.Query; 14 import org.apache.lucene.search.IndexSearcher; 15 import org.apache.lucene.search.BooleanQuery; 16 import org.apache.lucene.search.Hit; 17 import org.apache.lucene.search.Hits; 18 19 import com.tc.util.Assert; 20 import com.tc.object.config.ConfigLockLevel; 21 import com.tc.object.config.ConfigVisitor; 22 import com.tc.object.config.DSOClientConfigHelper; 23 import com.tc.object.config.TransparencyClassSpec; 24 import com.tc.simulator.app.ApplicationConfig; 25 import com.tc.simulator.listener.ListenerProvider; 26 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 27 28 32 public class RAMDirectoryTestApp extends AbstractErrorCatchingTransparentApp { 33 34 static final int EXPECTED_THREAD_COUNT = 2; 35 36 private final CyclicBarrier barrier; 37 38 private final RAMDirectory clusteredDirectory; 39 40 private final StandardAnalyzer analyzer; 41 42 47 public static void visitL1DSOConfig(final ConfigVisitor visitor, 48 final DSOClientConfigHelper config) { 49 config.addNewModule("clustered-lucene-2.0.0", "1.0.0"); 50 config.addAutolock("* *..*.*(..)", ConfigLockLevel.WRITE); 51 52 final String testClass = RAMDirectoryTestApp.class.getName(); 53 final TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 54 spec.addRoot("barrier", "barrier"); 55 spec.addRoot("clusteredDirectory", "clusteredDirectory"); 56 } 57 58 public RAMDirectoryTestApp(final String appId, final ApplicationConfig cfg, 59 final ListenerProvider listenerProvider) { 60 super(appId, cfg, listenerProvider); 61 clusteredDirectory = new RAMDirectory(); 62 analyzer = new StandardAnalyzer(); 63 barrier = new CyclicBarrier (getParticipantCount()); 64 } 65 66 70 protected void runTest() throws Throwable { 71 if (barrier.await() == 0) { 72 addDataToMap("DATA1"); 73 letOtherNodeProceed(); 74 waitForPermissionToProceed(); 75 verifyEntries("DATA2"); 76 } else { 77 waitForPermissionToProceed(); 78 verifyEntries("DATA1"); 79 addDataToMap("DATA2"); 80 letOtherNodeProceed(); 81 } 82 barrier.await(); 83 } 84 85 private void letOtherNodeProceed() throws InterruptedException , 87 BrokenBarrierException { 88 barrier.await(); 89 } 90 91 private void waitForPermissionToProceed() throws InterruptedException , 93 BrokenBarrierException { 94 barrier.await(); 95 } 96 97 102 private void addDataToMap(final String value) 103 throws Throwable { 104 final Document doc = new Document(); 105 doc.add(new Field("key", value, Field.Store.YES, Field.Index.TOKENIZED)); 106 doc.add(new Field("value", value, Field.Store.YES, 107 Field.Index.TOKENIZED)); 108 109 synchronized (clusteredDirectory) { 110 final IndexWriter writer = new IndexWriter(this.clusteredDirectory, 111 this.analyzer, this.clusteredDirectory.list().length == 0); 112 writer.addDocument(doc); 113 writer.optimize(); 114 writer.close(); 115 } 116 } 117 118 123 private void verifyEntries(final String value) throws Exception { 124 final StringBuffer data = new StringBuffer (); 125 synchronized (clusteredDirectory) { 126 final QueryParser parser = new QueryParser("key", this.analyzer); 127 final Query query = parser.parse(value); 128 BooleanQuery.setMaxClauseCount(100000); 129 final IndexSearcher is = new IndexSearcher(this.clusteredDirectory); 130 final Hits hits = is.search(query); 131 132 for (Iterator i = hits.iterator(); i.hasNext();) { 133 final Hit hit = (Hit) i.next(); 134 final Document document = hit.getDocument(); 135 data.append(document.get("value")); 136 } 137 } 138 Assert.assertEquals(value, data.toString()); 139 } 140 } 141 | Popular Tags |