1 package org.terracotta.modules.lucene_2_0_0; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.util.concurrent.BrokenBarrierException ; 6 import java.util.concurrent.CyclicBarrier ; 7 8 import com.tc.object.config.ConfigVisitor; 9 import com.tc.object.config.DSOClientConfigHelper; 10 import com.tc.simulator.app.ApplicationConfig; 11 import com.tc.simulator.listener.ListenerProvider; 12 import com.tc.test.TempDirectoryHelper; 13 import com.tc.util.Assert; 14 import com.tctest.runner.AbstractTransparentApp; 15 16 public final class SimpleLuceneDistributedIndexApp extends AbstractTransparentApp { 17 18 private static final String SEARCH_FIELD = "contents"; 19 private final CyclicBarrier barrier; 20 21 public SimpleLuceneDistributedIndexApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 22 super(appId, cfg, listenerProvider); 23 barrier = new CyclicBarrier (getParticipantCount()); 24 } 25 26 public void run() { 27 try { 28 boolean writerNode = false; 29 if (barrier.await() == 0) writerNode = true; 30 LuceneSampleDataIndex index = null; 31 if (writerNode) index = new LuceneSampleDataIndex(getTempDirectory(true)); 32 barrier.await(); 33 if (!writerNode) index = new LuceneSampleDataIndex(getTempDirectory(false)); 34 barrier.await(); 35 36 int count = index.query("buddha").length(); 37 Assert.assertEquals(count, 0); 38 barrier.await(); 39 if (writerNode) index.put(SEARCH_FIELD, "buddha"); 40 barrier.await(); 41 count = index.query("buddha").length(); 42 Assert.assertEquals(count, 1); 43 count = index.query("lamb").length(); 44 Assert.assertEquals(count, 14); 45 barrier.await(); 46 if (writerNode) index.put(SEARCH_FIELD, "Mary had a little lamb."); 47 barrier.await(); 48 count = index.query("lamb").length(); 49 Assert.assertEquals(count, 15); 50 51 } catch (BrokenBarrierException e) { 52 barrier.reset(); 53 notifyError(e); 54 } catch (InterruptedException e) { 55 barrier.reset(); 56 notifyError(e); 57 } catch (Exception e) { 58 barrier.reset(); 59 notifyError(e); 60 } 61 } 62 63 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 64 config.addIncludePattern(CyclicBarrier .class.getName()); 65 config.addIncludePattern("org.apache.lucene.store..*"); 66 config.addIncludePattern(LuceneSampleDataIndex.class.getName()); 67 String className = SimpleLuceneDistributedIndexApp.class.getName(); 68 config.addIncludePattern(className); 69 config.addWriteAutolock("* " + CyclicBarrier .class.getName() + "*.*(..)"); 70 config.addWriteAutolock("* " + LuceneSampleDataIndex.class.getName() + "*.*(..)"); 71 config.addRoot("directory", LuceneSampleDataIndex.class.getName() + ".directory"); 72 config.addRoot("barrier", className + ".barrier"); 73 } 74 75 private File getTempDirectory(boolean clean) throws IOException { 76 return new TempDirectoryHelper(getClass(), clean).getDirectory(); 77 } 78 } 79 | Popular Tags |