1 | /* |
2 | * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. |
3 | * |
4 | * WSO2 Inc. licenses this file to you under the Apache License, |
5 | * Version 2.0 (the "License"); you may not use this file except |
6 | * in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, |
12 | * software distributed under the License is distributed on an |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | * KIND, either express or implied. See the License for the |
15 | * specific language governing permissions and limitations |
16 | * under the License. |
17 | */ |
18 | package org.wso2.siddhi.core.stream.handler.window; |
19 | |
20 | import org.wso2.siddhi.core.event.ComplexEvent; |
21 | import org.wso2.siddhi.core.event.Event; |
22 | import org.wso2.siddhi.core.event.ListEvent; |
23 | import org.wso2.siddhi.core.event.StreamEvent; |
24 | import org.wso2.siddhi.core.event.remove.RemoveEvent; |
25 | import org.wso2.siddhi.core.event.remove.RemoveListEvent; |
26 | import org.wso2.siddhi.core.util.SchedulerQueue; |
27 | |
28 | public class LengthWindowHandler extends WindowHandler { |
29 | |
30 | int lengthToKeep; |
31 | private volatile int currentLength = 0; |
32 | |
33 | @Override |
34 | public void setParameters(Object[] parameters) { |
35 | lengthToKeep = (Integer) parameters[0]; |
36 | } |
37 | |
38 | @Override |
39 | public void process(ComplexEvent complexEvent) { |
40 | try { |
41 | if (complexEvent instanceof Event) { |
42 | SchedulerQueue<StreamEvent> queue = getWindow(); |
43 | queue.put((StreamEvent) complexEvent); |
44 | getNextPreStreamFlowProcessor().process(complexEvent); |
45 | if (currentLength == lengthToKeep) { |
46 | getNextPreStreamFlowProcessor().process(new RemoveEvent((Event) queue.take(),System.currentTimeMillis())); |
47 | }else { |
48 | currentLength++; |
49 | } |
50 | } else if (complexEvent instanceof ListEvent) { |
51 | SchedulerQueue<StreamEvent> queue = getWindow(); |
52 | Event[] newEvents = ((ListEvent) complexEvent).getEvents(); |
53 | int oldEventLength = newEvents.length - (lengthToKeep - currentLength); |
54 | if (oldEventLength > 0) { |
55 | Event[] oldEvents = new Event[newEvents.length - (lengthToKeep - currentLength)]; |
56 | int oldEventIndex = 0; |
57 | for (Event event : newEvents) { |
58 | queue.put(event); |
59 | getNextPreStreamFlowProcessor().process(event); |
60 | if (currentLength == lengthToKeep) { |
61 | oldEvents[oldEventIndex] = new RemoveEvent((Event) queue.take(),System.currentTimeMillis()); |
62 | oldEventIndex++; |
63 | } else { |
64 | currentLength++; |
65 | } |
66 | } |
67 | getNextPreStreamFlowProcessor().process(new RemoveListEvent(oldEvents,System.currentTimeMillis())); |
68 | } else { |
69 | for (Event event : newEvents) { |
70 | queue.put(event); |
71 | getNextPreStreamFlowProcessor().process(event); |
72 | currentLength++; |
73 | } |
74 | } |
75 | } |
76 | } catch (InterruptedException e) { |
77 | e.printStackTrace(); |
78 | } |
79 | } |
80 | |
81 | } |