1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
.checkers
;
5 import com
.google
.appengine
.api
.search
.SearchQueryException
;
6 import com
.google
.appengine
.api
.search
.SearchServicePb
.SearchParams
;
7 import com
.google
.appengine
.api
.search
.query
.QueryTreeBuilder
;
8 import com
.google
.apphosting
.api
.AppEngineInternal
;
10 import org
.antlr
.runtime
.RecognitionException
;
12 import java
.io
.UnsupportedEncodingException
;
15 * Checks values of {@link com.google.appengine.api.search.Query}.
19 public final class QueryChecker
{
22 * Checks that query is not null and is parsable.
23 * @param query the query to check
24 * @return the checked query
25 * @throws SearchQueryException if the query is not parsable
26 * @throws IllegalArgumentException if the query is too long
28 public static String
checkQuery(String query
) {
29 checkQueryFast(query
);
30 return checkQueryParses(query
);
34 * Checks if the given query parses. This method assumes that the
35 * other checks, such as length, non-null, etc., were already
38 * @param query the query to check
39 * @return the checked query
40 * @throws SearchQueryException if the query is not parsable
42 private static String
checkQueryParses(String query
) {
44 new QueryTreeBuilder().parse(query
);
45 } catch (RecognitionException e
) {
46 throw new SearchQueryException("Unable to parse query: " + query
);
52 * Checks the search specification is valid, specifically, has a valid
53 * index specification, a non-null query, a non-null number of documents
54 * to return specification, a valid cursor if present, valid sort
55 * specification list, a valid collection of field names for sorting,
56 * and a valid scorer specification.
58 * @param params the SearchParams to check
59 * @return this checked SearchParams
60 * @throws IllegalArgumentException if some part of the specification is
62 * @throws SearchQueryException if the query is unparsable
64 public static SearchParams
checkValid(SearchParams params
) {
65 checkValidFast(params
);
66 checkQueryParses(params
.getQuery());
71 * Performs a fast check of the search parameters. This method does not
72 * check if the query stored in the parameters parses.
74 * @param params the search parameters to check
75 * @return this checked SearchParams
76 * @throws IllegalArgumentException if parameters are not valid
78 public static SearchParams
checkValidFast(SearchParams params
) {
79 IndexChecker
.checkName(params
.getIndexSpec().getName());
80 checkQueryFast(params
.getQuery());
81 QueryOptionsChecker
.checkValid(params
);
86 * Checks if the query is not null, and not overly long. This method
87 * does not check if the query is parsable.
89 * @param query the query to check
90 * @return the checked query
92 private static String
checkQueryFast(String query
) {
93 Preconditions
.checkNotNull(query
, "query cannot be null");
96 length
= query
.getBytes("UTF-8").length
;
97 } catch (UnsupportedEncodingException e
){
98 throw new IllegalArgumentException("Unsupported encoding UTF-8. Shouldn't happen, ever!");
100 Preconditions
.checkArgument(
101 length
<= SearchApiLimits
.MAXIMUM_QUERY_LENGTH
,
102 String
.format("query string must not be longer than %d bytes, was %d",
103 SearchApiLimits
.MAXIMUM_QUERY_LENGTH
, length
));