All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redhat.lightblue.assoc.ep.JoinSearch Maven / Gradle / Ivy

There is a newer version: 2.18.0
Show newest version
/*
 Copyright 2013 Red Hat, Inc. and/or its affiliates.

 This file is part of lightblue.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see .
 */
package com.redhat.lightblue.assoc.ep;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redhat.lightblue.query.QueryExpression;
import com.redhat.lightblue.query.NaryLogicalOperator;

import com.redhat.lightblue.mediator.OperationContext;
import com.redhat.lightblue.crud.CRUDFindRequest;
import com.redhat.lightblue.crud.DocumentStream;
import com.redhat.lightblue.crud.DocCtx;

/**
 * Performs searches based on the n-tuple of result documents obtained from the
 * source steps
 *
 * Input: JoinTuple Output: ResultDocument
 */
public class JoinSearch extends AbstractSearchStep {

    private static final Logger LOGGER = LoggerFactory.getLogger(JoinSearch.class);

    private final Source source;

    public JoinSearch(ExecutionBlock block, Source source) {
        super(block);
        this.source = source;
    }

    @Override
    protected DocumentStream getSearchResults(final ExecutionContext ctx) {
        return new BatchQueryIterator(256,ctx);
    }

    /**
     * Streaming batch query executor/iterator
     *
     * When the results are retrieved from the stream, executes a
     * batch of queries, computes results, and streams them to the
     * caller
     */
    private class BatchQueryIterator implements DocumentStream {
        private final int batchSize;
        private final ExecutionContext ctx;
        private final Iterator sourceStream;
        private final ArrayList> listeners=new ArrayList<>();

        private DocumentStream currentIterator;
        private boolean done=false; // Are we still iterating, or are we done?
        
        public BatchQueryIterator(int batchSize,ExecutionContext ctx) {
            this.batchSize=batchSize;
            this.ctx=ctx;
            sourceStream=source.getStep().getResults(ctx).stream().iterator();
        }

        @Override
        public boolean hasNext() {
            if(!done) {
                if(currentIterator==null||!currentIterator.hasNext())
                    retrieveNextBatch();
                if(done)
                    return false;
                else
                    return currentIterator.hasNext();
            } else {
                return false;
            }
        }
        
        @Override
        public ResultDocument next() {
            if(!done) {
                if(currentIterator==null||!currentIterator.hasNext())
                    retrieveNextBatch();
                if(currentIterator!=null) {
                    ResultDocument doc=new ResultDocument(block,currentIterator.next().getOutputDocument());
                    for(Consumer l:listeners)
                        l.accept(doc);
                    return doc;
                }
            }
            throw new NoSuchElementException();
        }

        @Override
        public void close() {
            if(currentIterator!=null)
                currentIterator.close();
        }

        @Override
        public void addListener(Consumer listener) {
            listeners.add(listener);
        }
        
        private void retrieveNextBatch() {
            do {
                int n=0;
                ArrayList qBatch=new ArrayList<>(batchSize);
                if(currentIterator!=null) {
                    currentIterator.close();
                    currentIterator=null;
                }
                while(sourceStream.hasNext()&&n




© 2015 - 2024 Weber Informatics LLC | Privacy Policy