View Javadoc

1   package org.apache.helix.healthcheck;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.log4j.Logger;
23  
24  public class WindowAggregationType implements AggregationType
25  {
26  
27    private static final Logger logger = Logger
28        .getLogger(WindowAggregationType.class);
29  
30    public final String WINDOW_DELIM = "#";
31  
32    public final static String TYPE_NAME = "window";
33  
34    int _windowSize = 1;
35  
36    public WindowAggregationType(int ws)
37    {
38      super();
39      _windowSize = ws;
40    }
41  
42    @Override
43    public String getName()
44    {
45      StringBuilder sb = new StringBuilder();
46      sb.append(TYPE_NAME);
47      sb.append(DELIM);
48      sb.append(_windowSize);
49      return sb.toString();
50    }
51  
52    @Override
53    public String merge(String incomingVal, String existingVal, long prevTimestamp)
54    {
55      String[] windowVals;
56      if (existingVal == null)
57      {
58        return incomingVal;
59      }
60      else
61      {
62        windowVals = existingVal.split(WINDOW_DELIM);
63        int currLength = windowVals.length;
64        // window not full
65        if (currLength < _windowSize)
66        {
67          return existingVal + WINDOW_DELIM + incomingVal;
68        }
69        // evict oldest
70        else
71        {
72          int firstDelim = existingVal.indexOf(WINDOW_DELIM);
73          return existingVal.substring(firstDelim + 1) + WINDOW_DELIM
74              + incomingVal;
75        }
76      }
77    }
78  }