comparison src/org/dancres/blitz/entry/Readahead.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.entry;
2
3 import org.dancres.blitz.ActiveObject;
4 import org.dancres.blitz.ActiveObjectRegistry;
5 import org.dancres.blitz.Logging;
6 import org.dancres.blitz.mangler.MangledEntry;
7
8 import java.util.HashSet;
9 import java.util.Set;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12 import java.io.IOException;
13
14 import EDU.oswego.cs.dl.util.concurrent.QueuedExecutor;
15
16 /**
17 */
18 public class Readahead implements ActiveObject {
19 private static Logger theLogger =
20 Logging.newLogger("org.dancres.blitz.entry.Readahead");
21
22 private static Readahead theReadahead = new Readahead();
23
24 private QueuedExecutor theExecutor;
25
26 private Set theActiveTemplates = new HashSet();
27
28 static Readahead get() {
29 return theReadahead;
30 }
31
32 private Readahead() {
33 ActiveObjectRegistry.add(this);
34 }
35
36 /**
37 * @return <code>true</code> if the readahead will go active
38 */
39 boolean add(MangledEntry aTemplate, int aReadahead) {
40 if ((! aTemplate.isSnapshot()) &&
41 (! aTemplate.isWildcard())) {
42 if (theLogger.isLoggable(Level.FINEST))
43 theLogger.log(Level.FINEST,
44 "Readahead not snapshot/wildcard: " + aTemplate.getType());
45 return false;
46 }
47
48 if (aReadahead == 0) {
49 if (theLogger.isLoggable(Level.FINEST))
50 theLogger.log(Level.FINEST,
51 "Readahead not set: " + aTemplate.getType());
52 return false;
53 }
54
55 synchronized(theActiveTemplates) {
56 if (! theActiveTemplates.contains(aTemplate)) {
57 if (theLogger.isLoggable(Level.FINEST))
58 theLogger.log(Level.FINEST,
59 "Readahead going active: " + aReadahead);
60 theActiveTemplates.add(aTemplate);
61
62 try {
63 theExecutor.execute(new ReadTask(aTemplate, aReadahead));
64 } catch (InterruptedException anIE) {
65 // Doesn't matter it's just a readahead
66 }
67
68 return true;
69 } else
70 return false;
71 }
72 }
73
74 void remove(MangledEntry aTemplate) {
75 synchronized(theActiveTemplates) {
76 theActiveTemplates.remove(aTemplate);
77 }
78 }
79
80 public void begin() {
81 theExecutor = new QueuedExecutor();
82 }
83
84 public void halt() {
85 theExecutor.shutdownNow();
86 }
87
88 static class ReadTask implements Runnable {
89 private MangledEntry theTemplate;
90 private int theReadahead;
91
92 ReadTask(MangledEntry aTemplate, int aReadahead) {
93 theTemplate = aTemplate;
94 theReadahead = aReadahead;
95 }
96
97 public void run() {
98 try {
99 String myType = theTemplate.getType();
100
101 EntryReposRecovery myRepos =
102 EntryRepositoryFactory.get().getAdmin(myType);
103
104 // System.err.println("Attempting readahead of 10");
105
106 myRepos.find(theTemplate, new CountingVisitor(theReadahead));
107 } catch (IOException anIOE) {
108 theLogger.log(Level.SEVERE,
109 "Readahead failed with", anIOE);
110 } finally {
111 Readahead.get().remove(theTemplate);
112 }
113 }
114 }
115
116 static class CountingVisitor implements SearchVisitor {
117 private int theRequiredOffers;
118 private int theTotalOffers;
119
120 CountingVisitor(int aRequired) {
121 theRequiredOffers = aRequired;
122 }
123
124 public boolean isDeleter() {
125 return false;
126 }
127
128 public int offer(SearchOffer anOffer) {
129 ++theTotalOffers;
130
131 if (theTotalOffers == theRequiredOffers)
132 return STOP;
133 else
134 return TRY_AGAIN;
135 }
136 }
137 }