View Javadoc

1   package org.apache.helix.messaging;
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 java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.helix.Criteria;
28  import org.apache.helix.HelixManager;
29  import org.apache.helix.josql.ClusterJosqlQueryProcessor;
30  import org.apache.helix.josql.ZNRecordRow;
31  import org.apache.log4j.Logger;
32  
33  
34  public class CriteriaEvaluator
35  {
36    private static Logger logger = Logger.getLogger(CriteriaEvaluator.class);
37    
38    public List<Map<String, String>> evaluateCriteria(Criteria recipientCriteria, HelixManager manager)
39    {
40      List<Map<String, String>> selected = new ArrayList<Map<String, String>>();
41      
42      String queryFields = 
43          (!recipientCriteria.getInstanceName().equals("")  ? " " + ZNRecordRow.MAP_SUBKEY  : " ''") +","+
44          (!recipientCriteria.getResource().equals("") ? " " + ZNRecordRow.ZNRECORD_ID : " ''") +","+
45          (!recipientCriteria.getPartition().equals("")   ? " " + ZNRecordRow.MAP_KEY   : " ''") +","+
46          (!recipientCriteria.getPartitionState().equals("") ? " " + ZNRecordRow.MAP_VALUE : " '' ");
47      
48      String matchCondition = 
49          ZNRecordRow.MAP_SUBKEY   + " LIKE '" + (!recipientCriteria.getInstanceName().equals("") ? (recipientCriteria.getInstanceName() +"'") :   "%' ") + " AND "+
50          ZNRecordRow.ZNRECORD_ID+ " LIKE '" + (!recipientCriteria.getResource().equals("") ? (recipientCriteria.getResource() +"'") : "%' ") + " AND "+
51          ZNRecordRow.MAP_KEY   + " LIKE '" + (!recipientCriteria.getPartition().equals("")   ? (recipientCriteria.getPartition()  +"'") :  "%' ") + " AND "+
52          ZNRecordRow.MAP_VALUE  + " LIKE '" + (!recipientCriteria.getPartitionState().equals("") ? (recipientCriteria.getPartitionState()+"'") :  "%' ") + " AND "+
53          ZNRecordRow.MAP_SUBKEY   + " IN ((SELECT [*]id FROM :LIVEINSTANCES))";
54          
55      
56      String queryTarget = recipientCriteria.getDataSource().toString() + ClusterJosqlQueryProcessor.FLATTABLE;
57      
58      String josql = "SELECT DISTINCT " + queryFields
59                   + " FROM " + queryTarget + " WHERE "
60                   + matchCondition;
61      ClusterJosqlQueryProcessor p = new ClusterJosqlQueryProcessor(manager);
62      List<Object> result = new ArrayList<Object>();
63      try
64      {
65        logger.info("JOSQL query: " + josql);
66        result = p.runJoSqlQuery(josql, null, null);
67      } 
68      catch (Exception e)
69      {
70        logger.error("", e);
71        return selected;
72      } 
73      
74      for(Object o : result)
75      {
76        Map<String, String> resultRow = new HashMap<String, String>();
77        List<Object> row = (List<Object>)o;
78        resultRow.put("instanceName", (String)(row.get(0)));
79        resultRow.put("resourceName", (String)(row.get(1)));
80        resultRow.put("partitionName", (String)(row.get(2)));
81        resultRow.put("partitionState", (String)(row.get(3)));
82        selected.add(resultRow);
83      }
84      logger.info("JOSQL query return " + selected.size() + " rows");
85      return selected;
86    }
87  }