01: package org.terracotta.modules.lucene_2_0_0;
02:
03: import com.tc.object.config.ConfigVisitor;
04: import com.tc.object.config.DSOClientConfigHelper;
05: import com.tc.simulator.app.ApplicationConfig;
06: import com.tc.simulator.listener.ListenerProvider;
07: import com.tc.test.TempDirectoryHelper;
08: import com.tc.util.Assert;
09: import com.tc.util.TIMUtil;
10: import com.tctest.runner.AbstractTransparentApp;
11:
12: import java.io.File;
13: import java.io.IOException;
14: import java.util.concurrent.BrokenBarrierException;
15: import java.util.concurrent.CyclicBarrier;
16:
17: public final class SimpleLuceneDistributedIndexApp extends
18: AbstractTransparentApp {
19:
20: private static final String SEARCH_FIELD = "contents";
21: private final CyclicBarrier barrier;
22:
23: public SimpleLuceneDistributedIndexApp(String appId,
24: ApplicationConfig cfg, ListenerProvider listenerProvider) {
25: super (appId, cfg, listenerProvider);
26: barrier = new CyclicBarrier(getParticipantCount());
27: }
28:
29: public void run() {
30: try {
31: final boolean writerNode = barrier.await() == 0;
32: LuceneSampleDataIndex index = null;
33:
34: if (writerNode) {
35: index = new LuceneSampleDataIndex(
36: getTempDirectory(true));
37: }
38:
39: barrier.await();
40:
41: if (!writerNode) {
42: index = new LuceneSampleDataIndex(
43: getTempDirectory(false));
44: }
45: barrier.await();
46:
47: int count = index.query("buddha").length();
48: Assert.assertEquals(count, 0);
49: barrier.await();
50: if (writerNode)
51: index.put(SEARCH_FIELD, "buddha");
52: barrier.await();
53: count = index.query("buddha").length();
54: Assert.assertEquals(count, 1);
55: count = index.query("lamb").length();
56: Assert.assertEquals(count, 14);
57: barrier.await();
58: if (writerNode)
59: index.put(SEARCH_FIELD, "Mary had a little lamb.");
60: barrier.await();
61: count = index.query("lamb").length();
62: Assert.assertEquals(count, 15);
63:
64: } catch (BrokenBarrierException e) {
65: barrier.reset();
66: notifyError(e);
67: } catch (InterruptedException e) {
68: barrier.reset();
69: notifyError(e);
70: } catch (Exception e) {
71: barrier.reset();
72: notifyError(e);
73: }
74: }
75:
76: public static void visitL1DSOConfig(ConfigVisitor visitor,
77: DSOClientConfigHelper config) {
78: config.addModule(TIMUtil.LUCENE_2_0_0, TIMUtil
79: .getVersion(TIMUtil.LUCENE_2_0_0));
80:
81: config.addIncludePattern(LuceneSampleDataIndex.class.getName());
82: config.addIncludePattern(SimpleLuceneDistributedIndexApp.class
83: .getName());
84:
85: config.addRoot("directory", LuceneSampleDataIndex.class
86: .getName()
87: + ".directory");
88: config.addRoot("barrier", SimpleLuceneDistributedIndexApp.class
89: .getName()
90: + ".barrier");
91: }
92:
93: private File getTempDirectory(boolean clean) throws IOException {
94: return new TempDirectoryHelper(getClass(), clean)
95: .getDirectory();
96: }
97: }
|