private static void handleBlockOperation(StringBuilder builder, BlockOperation blockOperation) throws RemoteException { Map<String, Set<SearchCondition>> blockFieldConditions = blockOperation.getConditions(); String blockOperator = ""; if(blockOperation instanceof AndBlock) blockOperator = ","; if(blockOperation instanceof OrBlock) blockOperator = "|"; boolean first = true; for(Map.Entry<String, Set<SearchCondition>> blockConditions : blockFieldConditions.entrySet()) { handleBlockFieldOperation(builder, blockConditions.getKey(), blockConditions.getValue(), blockOperator, first); first = false; } for(BlockOperation childBlock : blockOperation.getChildBlocks()) { if(!first) builder.append(blockOperator); builder.append("("); handleBlockOperation(builder, childBlock); builder.append(")"); first = false; } }
private static void addQueryFieldToBlock(BlockOperation block, String field, List<QueryField> fields) { List<RangeCondition> rangeConditions = new ArrayList<>(); for(QueryField queryField : fields) { SearchCondition searchCondition = createCondition(queryField); if(searchCondition instanceof RangeCondition) { rangeConditions.add((RangeCondition)searchCondition); } else { block.addCondition(field, searchCondition); } } if(rangeConditions.size() > 1) { block.addCondition(field, mergeRangeConditions(rangeConditions)); } else if(!rangeConditions.isEmpty()){ block.addCondition(field, rangeConditions.get(0)); } }
private IndexSearchResultIteratorCollection doBlockHierarchy(BlockOperation blockOperation, SearchLimit limit) throws JasDBStorageException { StatRecord record = StatisticsMonitor.createRecord("bag:search:blockoperation"); IndexSearchResultIteratorCollection results = doBlockOperation(blockOperation, limit, blockOperation.getFields(), null); record.stop(); record = StatisticsMonitor.createRecord("bag:search:childblockMerge"); BlockMerger merger = blockOperation.getMerger(); for(BlockOperation childBlock : blockOperation.getChildBlocks()) { IndexSearchResultIteratorCollection childResults = doBlockHierarchy(childBlock, limit); if(results != null) { results = merger.mergeIterators(results, childResults); } else { results = childResults; } } record.stop(); return results; }
private Set<SearchCondition> getSearchConditions(BlockOperation blockOperation, Index index) { KeyInfo keyInfo = index.getKeyInfo(); return blockOperation.getConditions(keyInfo.getKeyNameMapper(), keyInfo.getKeyFields()); } }
public IndexSearchResultIteratorCollection doTableScan(BlockOperation operation, Set<String> fields, IndexSearchResultIteratorCollection currentResults) throws JasDBStorageException { BlockMerger merger = operation.getMerger(); List<Key> foundKeys = new ArrayList<>(); Set<String> payloadFields = new HashSet<>(fields); payloadFields.add(SimpleEntity.DOCUMENT_ID); payloadFields.removeIf(field -> !operation.hasConditions(field)); if(currentResults != null) { LOG.debug("Doing table scan for fields: {} with limited set: {}", fields, currentResults.size()); for(Key key : currentResults) { RecordResult result = recordWriter.readRecord(KeyUtil.getDocumentKey(currentResults.getKeyNameMapper(), key)); doTableScanConditions(operation, merger, foundKeys, result, fields); } } else { LOG.debug("Doing a full table scan for fields: {}", fields); RecordIterator recordIterator = recordWriter.readAllRecords(); for(RecordResult result : recordIterator) { doTableScanConditions(operation, merger, foundKeys, result, payloadFields); } } return new IndexSearchResultIteratorImpl(foundKeys, DEFAULT_DOC_ID_MAPPER.clone()); }
public static BlockOperation transformBuilder(QueryBuilder builder) { BlockOperation block = null; switch(builder.getBlockType()) { case AND: block = new AndBlock(); break; case OR: block = new OrBlock(); break; } for(Map.Entry<String, List<QueryField>> entry : builder.getQueryFields().entrySet()) { addQueryFieldToBlock(block, entry.getKey(), entry.getValue()); } for(QueryBuilder childBuilder : builder.getChildBuilders()) { block.addChildBlock(transformBuilder(childBuilder)); } return block; }
private void doTableScanConditions(BlockOperation operation, BlockMerger merger, List<Key> foundKeys, RecordResult result, Set<String> fields) throws JasDBStorageException { Entity entity = SimpleEntity.fromStream(result.getStream()); boolean first = true; boolean match = false; for(String field : fields) { Property property = entity.getProperty(field); if(property != null) { Set<Key> keys = PropertyKeyMapper.mapToKeys(property); Set<SearchCondition> conditions = operation.getConditions(field); for(SearchCondition condition : conditions) { boolean newmatch = checkCondition(condition, keys); match = first && newmatch || merger.includeResult(match, newmatch); first = false; if (!merger.continueEvaluation(match)) { return; } } } else if(!merger.continueEvaluation(false)) { //in this case when no property we should stop evaluating return; } } if(match) { foundKeys.add(entityToKey(entity)); } }