2513160c20733f4cc4719b1603d92e3436d0c841
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard (fchouinard@gmail.com) - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.eventlog
;
15 import java
.util
.Vector
;
17 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
21 * <b><u>TmfEventRequest</u></b>
23 * TmfEventRequests are used to obtain blocks of contiguous events from an
24 * event stream, either all the events within a given time window or n events
25 * starting a a specific timestamp. Open ranges can be used, especially for
26 * continuous streaming.
28 * The request is processed asynchronously by an ITmfRequestProcessor and,
29 * as blocks of events become available, the callback function newEvents()
30 * is invoked, synchronously, for each block. When returning from newEvents(),
31 * the event instances go out of scope and become eligible for gc. It is
32 * is thus the responsibility of the requester to either copy or keep a
33 * reference to the events it wishes to track specifically.
35 * This event block approach is necessary to avoid busting the heap for very
36 * large trace files. The block size is configurable.
38 * The ITmfRequestProcessor indicates that the request is completed by
39 * calling done(). The request can be canceled at any time with cancel().
42 *<pre><code><i>TmfTimeWindow range = new TmfTimewindow(...);
43 *TmfEventRequest request = new TmfEventRequest(range, 0, NB_EVENTS, BLOCK_SIZE) {
45 * public void newEvents(Vector<TmfEvent> events) {
46 * for (TmfEvent e : events) {
51 *fProcessor.process(request, true);
54 * TODO: Consider extending DataRequestMonitor from DSF concurrency plugin.
55 * The main issue is the slicing of the result in blocks and continuous
58 public class TmfEventRequest
{
60 // ========================================================================
62 // ========================================================================
64 // The default maximum number of events per chunk
65 public static final int DEFAULT_BLOCK_SIZE
= 1000;
67 // ========================================================================
69 // ========================================================================
71 private final TmfTimeRange fRange
; // The requested events timestamp range
72 private final long fOffset
; // The synchronization offset to apply
73 private final int fNbRequestedEvents
; // The number of events to read (-1 == the whole range)
74 private final int fBlockSize
; // The maximum number of events per chunk
76 private Object lock
= new Object();
77 private boolean fRequestCompleted
= false;
78 private boolean fRequestCanceled
= false;
80 // ========================================================================
82 // ========================================================================
89 public TmfEventRequest(TmfTimeRange range
, long offset
, int nbEvents
) {
90 this(range
, offset
, nbEvents
, DEFAULT_BLOCK_SIZE
);
97 * @param maxBlockSize Size of the largest blocks expected
99 public TmfEventRequest(TmfTimeRange range
, long offset
, int nbEvents
, int maxBlockSize
) {
102 fNbRequestedEvents
= nbEvents
;
103 fBlockSize
= maxBlockSize
;
106 // ========================================================================
108 // ========================================================================
111 * @return the requested time range
113 public TmfTimeRange
getRange() {
120 public long getOffset() {
125 * @return the number of requested events (-1 = all)
127 public int getNbRequestedEvents() {
128 return fNbRequestedEvents
;
132 * @return the block size
134 public int getBlockize() {
139 * @return indicates if the request is completed
141 public boolean isCompleted() {
142 return fRequestCompleted
;
146 * @return indicates if the request is canceled
148 public boolean isCancelled() {
149 return fRequestCanceled
;
152 // ========================================================================
154 // ========================================================================
159 * - Events are received in the order they appear in the stream.
160 * - Called by the request processor, in its execution thread, every time a
161 * block of events becomes available.
162 * - Request processor performs a synchronous call to newEvents()
163 * i.e. its execution threads holds until newEvents() returns.
164 * - Original events are disposed of on return i.e. keep a reference (or a
165 * copy) if some persistence is needed between invocations.
166 * - When there are no more events,
168 * @param events - an array of events
170 public void newEvents(Vector
<TmfEvent
> events
) {
174 * To suspend the client thread until the request completes (or is
177 * @throws InterruptedException
179 public void waitForCompletion() {
180 synchronized (lock
) {
181 while (!fRequestCompleted
)
184 } catch (InterruptedException e
) {
191 * Complete the request. Called by the request processor upon completion.
195 fRequestCompleted
= true;
201 * Cancel the request.
203 public void cancel() {
205 fRequestCanceled
= true;
206 fRequestCompleted
= true;
This page took 0.034655 seconds and 4 git commands to generate.