KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > modules > lucene_2_0_0 > SimpleLuceneDistributedIndexApp


1 package org.terracotta.modules.lucene_2_0_0;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.concurrent.BrokenBarrierException JavaDoc;
6 import java.util.concurrent.CyclicBarrier JavaDoc;
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 JavaDoc SEARCH_FIELD = "contents";
19   private final CyclicBarrier JavaDoc barrier;
20
21   public SimpleLuceneDistributedIndexApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
22     super(appId, cfg, listenerProvider);
23     barrier = new CyclicBarrier JavaDoc(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 JavaDoc e) {
52       barrier.reset();
53       notifyError(e);
54     } catch (InterruptedException JavaDoc e) {
55       barrier.reset();
56       notifyError(e);
57     } catch (Exception JavaDoc e) {
58       barrier.reset();
59       notifyError(e);
60     }
61   }
62
63   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
64     config.addIncludePattern(CyclicBarrier JavaDoc.class.getName());
65     config.addIncludePattern("org.apache.lucene.store..*");
66     config.addIncludePattern(LuceneSampleDataIndex.class.getName());
67     String JavaDoc className = SimpleLuceneDistributedIndexApp.class.getName();
68     config.addIncludePattern(className);
69     config.addWriteAutolock("* " + CyclicBarrier JavaDoc.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 JavaDoc getTempDirectory(boolean clean) throws IOException JavaDoc {
76     return new TempDirectoryHelper(getClass(), clean).getDirectory();
77   }
78 }
79
Popular Tags