Project

General

Profile

1
package eu.dnetlib.enabling.resultset.client;
2

    
3
import java.util.List;
4
import java.util.NoSuchElementException;
5

    
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8

    
9
import eu.dnetlib.enabling.resultset.client.utils.ResultSetRuntimeException;
10
import eu.dnetlib.enabling.resultset.client.utils.ResultSetTimeoutException;
11
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
12
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
13

    
14
/**
15
 * 
16
 * @author claudio
17
 * 
18
 */
19
public class ResultSetPageProvider {
20

    
21
	/**
22
	 * logger
23
	 */
24
	private static final Log log = LogFactory.getLog(ResultSetPageProvider.class);
25

    
26
	/**
27
	 * the resultset service
28
	 */
29
	private final ResultSetService resultSet;
30

    
31
	/**
32
	 * the resultset id
33
	 */
34
	private final String rsId;
35

    
36
	/**
37
	 * positional value of the getResult requests
38
	 */
39
	private int fromPosition;
40

    
41
	/**
42
	 * positional value of the getResult requests
43
	 */
44
	private int toPosition;
45

    
46
	/**
47
	 * actual page size
48
	 */
49
	private int pageSize;
50

    
51
	/**
52
	 * default page size
53
	 */
54
	private static int DEFAULT_PAGE_SIZE = 10;
55

    
56
	/**
57
	 * default max waiting time
58
	 */
59
	private static long DEFAULT_MAX_WAIT_TIME = 30000;
60

    
61
	/**
62
	 * actual max waiting time
63
	 */
64
	private long maxWaitTime;
65

    
66
	/**
67
	 * current wait time
68
	 */
69
	private long waitTime;
70

    
71
	/**
72
	 * request counter used to calculate the waitTime
73
	 */
74
	private int delayCount;
75

    
76
	/**
77
	 * resultset status
78
	 */
79
	private String RSStatus;
80

    
81
	/**
82
	 * number of elements in the resultset
83
	 */
84
	private int numberOfElements;
85

    
86
	private final static String RS_CLOSED = "closed";
87

    
88
	private final static String RS_OPEN = "open";
89

    
90
	/**
91
	 * 
92
	 * @param resultSet
93
	 * @param rsId
94
	 * @throws ResultSetException
95
	 */
96
	public ResultSetPageProvider(final ResultSetService resultSet, final String rsId) throws ResultSetRuntimeException {
97

    
98
		this.resultSet = resultSet;
99
		this.rsId = rsId;
100
		this.pageSize = DEFAULT_PAGE_SIZE;
101
		this.maxWaitTime = DEFAULT_MAX_WAIT_TIME;
102
		fromPosition = toPosition = 0;
103
		delayCount = 0;
104
		waitTime = 0;
105
		updateResultSetStatus();
106
	}
107

    
108
	public ResultSetPageProvider(final ResultSetService resultSet, final String rsId, final int pageSize) throws ResultSetRuntimeException {
109

    
110
		this(resultSet, rsId);
111
		this.pageSize = pageSize;
112
	}
113

    
114
	/**
115
	 * 
116
	 * @return
117
	 * @throws ResultSetTimeoutException
118
	 * @throws ResultSetException
119
	 */
120
	public List<String> nextPage() throws ResultSetTimeoutException, ResultSetRuntimeException {
121
		do {
122
			updateResultSetStatus();
123
			int availableElements = numberOfElements - toPosition;
124
			log.debug("availableElements: " + availableElements);
125
			if (availableElements > 0) {
126
				fromPosition = toPosition + 1;
127
				if (availableElements < pageSize) {
128
					toPosition = (fromPosition + availableElements) - 1;
129
				} else {
130
					toPosition = (fromPosition + pageSize) - 1;
131
					delayCount = 0;
132
				}
133
				log.debug(" - getting result from " + fromPosition + " to " + toPosition + ", numberOfElements: " + numberOfElements + ", availableElements: "
134
						+ availableElements);
135
				try {
136
					return resultSet.getResult(rsId, fromPosition, toPosition, "waiting");
137
				} catch (ResultSetException e) {
138
					log.info(e);
139
					throw new NoSuchElementException(e.getMessage());
140
				}
141
			}
142
			if (RSStatus.equals(RS_CLOSED) && (availableElements == 0)) return null;
143
			else {
144
				stopAndWait(++delayCount);
145
			}
146
		} while (true);
147
	}
148

    
149
	/**
150
	 * 
151
	 * @param delayCount
152
	 * @throws ResultSetTimeoutException
153
	 */
154
	private void stopAndWait(final int delayCount) throws ResultSetTimeoutException {
155
		try {
156

    
157
			waitTime = (long) ((Math.pow(1.2, 10 + delayCount) * 10L)) + 200;
158
			// waitTime = (long) Math.exp(delayCount);
159
			if (waitTime > maxWaitTime) {
160
				log.warn("Timeout getting elements from resultset: " + waitTime + ". next poll would wait more than " + maxWaitTime + " ms");
161
				throw new ResultSetTimeoutException("Timeout getting elements from resultset: next poll would wait more than " + maxWaitTime + " ms");
162
			}
163
			log.debug("resultset client is going to sleep for: " + waitTime);
164
			// System.out.println("resultset client is going to sleep for: " + waitTime);
165

    
166
			Thread.sleep(waitTime);
167
		} catch (InterruptedException e) {
168
			log.error("resultSetClient got InterruptedException", e);
169
		}
170
	}
171

    
172
	/**
173
	 * updates the
174
	 * 
175
	 * @throws ResultSetException
176
	 */
177
	private void updateResultSetStatus() throws ResultSetRuntimeException {
178

    
179
		try {
180
			RSStatus = resultSet.getRSStatus(rsId);
181
			numberOfElements = resultSet.getNumberOfElements(rsId);
182
			// System.out.println("updateResultSetStatus: size is " + numberOfElements + " and status is " + RSStatus);
183
		} catch (ResultSetException e) {
184
			log.warn(e);
185
			throw new ResultSetRuntimeException(e);
186
		}
187
	}
188

    
189
	public int getPageSize() {
190
		return pageSize;
191
	}
192

    
193
	public void setPageSize(final int pageSize) {
194
		if (pageSize <= 0) throw new IllegalArgumentException("parameter 'pageSize' must be grater than zero");
195
		this.pageSize = pageSize;
196
	}
197

    
198
	@Deprecated
199
	public boolean hasElements() throws ResultSetRuntimeException {
200
		updateResultSetStatus();
201
		if (RSStatus.equals(RS_OPEN)) return true;
202
		if (RSStatus.equals(RS_CLOSED) && (numberOfElements == 0)) return false;
203
		return true;
204
	}
205

    
206
	public void setMaxWaitTime(final long maxWaitTime) {
207
		if (maxWaitTime <= 0) throw new IllegalArgumentException("parameter 'maxWaitTime' must be grater than zero");
208
		this.maxWaitTime = maxWaitTime;
209
	}
210

    
211
}
(5-5/5)