public CandidateResults search( final SearchEdge searchEdge, final SearchTypes searchTypes, final String query, final int limit, final int offset, final Map<String, Class> fieldsWithType, final boolean analyzeOnly, final boolean returnQuery ) { Preconditions.checkNotNull( query, "query cannot be null" ); final ParsedQuery parsedQuery = ParsedQueryBuilder.build(query); return search(searchEdge, searchTypes, parsedQuery, limit, offset, fieldsWithType, analyzeOnly, returnQuery); }
public PipelineContext( final ApplicationScope applicationScope, final RequestCursor requestCursor, final int limit, final int id, boolean keepStaleEntries, String query ) { this.applicationScope = applicationScope; this.id = id; this.keepStaleEntries = keepStaleEntries; this.query = query; this.parsedQuery = ParsedQueryBuilder.build(query); if (parsedQuery != null && parsedQuery.isDirectQuery()) { // for direct query, use no limit or cursor this.limit = MAX_LIMIT + 1; this.requestCursor = new RequestCursor(Optional.absent()); } else { this.limit = limit; this.requestCursor = requestCursor; } }
@Test public void orderByGrammar() throws QueryParseException { String s = "select * where name = 'bob' order by name asc"; ParsedQuery query = ParsedQueryBuilder.build( s ); assertEquals( 1, query.getSortPredicates().size() ); }
@Test public void testNoViolations() throws Throwable { when ( fig.getQueryBreakerErrorSortPredicateCount() ).thenReturn( 5 ); when ( fig.getQueryBreakerErrorOperandCount() ).thenReturn( 5 ); when ( fig.getQueryBreakerErrorIndexSizeBytes() ).thenReturn( 5L ); when ( fig.getQueryBreakerErrorCollectionSizeBytes() ).thenReturn( 5L ); ParsedQuery parsedQuery; List<Map<String,Object>> violations; parsedQuery = ParsedQueryBuilder.build("select * order by created asc"); violations = QueryAnalyzer.analyze(parsedQuery, 1, 1, fig ); assertEquals(0, violations.size()); parsedQuery = ParsedQueryBuilder.build("select name='value' order by created asc"); violations = QueryAnalyzer.analyze(parsedQuery, 1, 1, fig ); assertEquals(0, violations.size()); parsedQuery = ParsedQueryBuilder.build("where name='value'"); violations = QueryAnalyzer.analyze(parsedQuery, 1, 1, fig ); assertEquals(0, violations.size()); }
@Test public void badOrderByGrammar() throws QueryParseException { // from isn't allowed String s = "select * where name = 'bob' order by"; String error = null; try { ParsedQueryBuilder.build ( s ); } catch ( QueryParseException qpe ) { error = qpe.getMessage(); } assertTrue( error.startsWith( "The query cannot be parsed" ) ); }
@Test public void collectionSizeViolation() throws Throwable { // the sort violation is only tripped when the collection size warning is tripped when ( fig.getQueryBreakerErrorCollectionSizeBytes() ).thenReturn( 0L ); ParsedQuery parsedQuery = ParsedQueryBuilder.build("select created order by created asc"); List<Map<String,Object>> violations; violations = QueryAnalyzer.analyze(parsedQuery, 1, 1, fig ); boolean violationExists = violationExists(violations, QueryAnalyzer.v_large_collection); if(!violationExists){ fail("Collection Size Violation should be present"); } }
@Test public void badOperand() throws QueryParseException { // from isn't allowed String s = "select * where name != 'bob'"; String error = null; try { ParsedQueryBuilder.build( s ); fail( "should throw an exception" ); } catch ( RuntimeException qpe ) { error = qpe.getMessage(); } assertEquals( "NoViableAltException('!'@[1:1: Tokens : ( T__32 | T__33 | T__34 | T__35 | T__36 | T__37 | T__38 | T__39 | T__40 |" +" T__41 | LT | LTE | EQ | GT | GTE | BOOLEAN | AND | OR | NOT | ASC | DESC | CONTAINS | WITHIN | OF | DIRECT |" +" UUID | ID | LONG | FLOAT | STRING | WS );])", error ); } }
@Test public void operandCountViolation() throws Throwable { ParsedQuery parsedQuery = ParsedQueryBuilder.build("where name='value'"); List<Map<String,Object>> violations; violations = QueryAnalyzer.analyze(parsedQuery, 0, 0, fig ); boolean violationExists = violationExists(violations, QueryAnalyzer.v_operand_count); if(!violationExists){ fail("Operand Count Violation should be present"); } }
@Test public void predicateCountViolation() throws Throwable { ParsedQuery parsedQuery = ParsedQueryBuilder.build("where name='value'"); List<Map<String,Object>> violations; violations = QueryAnalyzer.analyze(parsedQuery, 0, 0, fig ); boolean violationExists = violationExists(violations, QueryAnalyzer.v_operand_count); if(!violationExists){ fail("Operand Count Violation should be present"); } }
@Test public void IndexSizeViolation() throws Throwable { ParsedQuery parsedQuery = ParsedQueryBuilder.build("select created order by created asc"); List<Map<String,Object>> violations; violations = QueryAnalyzer.analyze(parsedQuery, 0, 1, fig ); boolean violationExists = violationExists(violations, QueryAnalyzer.v_large_index); if(!violationExists){ fail("Index Size Violation should be present"); } }
@Test public void fullSortViolation() throws Throwable { ParsedQuery parsedQuery = ParsedQueryBuilder.build("select * order by created asc"); List<Map<String,Object>> violations; violations = QueryAnalyzer.analyze(parsedQuery, 1, 1, fig ); boolean violationExists = violationExists(violations, QueryAnalyzer.v_full_collection_sort); if(!violationExists){ fail("Full Collection Sort Violation should be present"); } }