Package dovetaildb.dbservice

Source Code of dovetaildb.dbservice.QueryNodeIter

package dovetaildb.dbservice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import dovetaildb.bytes.Bytes;
import dovetaildb.iter.EmptyIter;
import dovetaildb.iter.Iter;
import dovetaildb.querynode.QueryNode;
import dovetaildb.querynode.QueryNodeTemplate;
import dovetaildb.querynode.RangeQueryNode;
import dovetaildb.querynode.QueryNode.NextStatus;
import dovetaildb.util.Pair;

public class QueryNodeIter extends AbstractIter {

  private QueryNode node;
  private Map<String, QueryNode> vars;
  QueryNode[] orderedVars;

  public static Iter make(QueryNodeTemplate templ) {
    QueryNodeIter iter = new QueryNodeIter();
    if (templ.queryNode == null) return EmptyIter.EMPTY_ITER;
    iter.node = templ.queryNode;
    iter.vars = templ.varMappings;
    Map.Entry[] entries =
      iter.vars.entrySet().toArray(new Map.Entry[]{});
    Arrays.sort(entries);
    iter.orderedVars = new QueryNode[entries.length];
    for(int i=0; i<entries.length; i++) {
      iter.orderedVars[i] = ((Map.Entry<String,QueryNode>)entries[i]).getValue();
    }
    return iter;
  }

  public void adjustMin(String name, Object newMin, boolean isExclusive) {
    Bytes bytes = DbServiceUtil.sencode(newMin);
    ((RangeQueryNode)vars.get(name)).adjustSuffixMin(bytes, isExclusive);
  }

  public void adjustMax(String name, Object newMax, boolean isExclusive) {
    Bytes bytes = DbServiceUtil.sencode(newMax);
    ((RangeQueryNode)vars.get(name)).adjustSuffixMax(bytes, isExclusive);
  }

  private void bind(long docId, DbResult dbResult, QueryNode node) {
    if (node.doc() < docId) node.skipTo(docId);
    if (node.doc() != docId) return;
    NextStatus next;
    do {
      dbResult.addTerm(node.term());
      next = node.nextTerm();
    } while(next == NextStatus.NEXT_TERM);
  }
 
  public int pullAvailable(Object[] buffer, boolean block) {
    if (node == null) return -1;
    int i = 0;
    for(; i<buffer.length; i++) {
      Object item = buffer[i];
//      DbResultset rSet;
      long docId = node.doc();
     
      DbResult dbResult;
      if (item != null && item instanceof DbResult) {
        dbResult = (DbResult)item;
      } else {
        dbResult = new DbResult();
      }
      bind(docId, dbResult, orderedVars[0]);
      buffer[i] = dbResult;
//      if (item instanceof DbResultset) {
//        rSet = (DbResultset) item;
//        rSet.docId = docId;
//      } else {
//        ArrayList<Pair<String, DbResult>> input = new ArrayList<Pair<String, DbResult>>();
//        for(Map.Entry<String, QueryNode> entry : vars.entrySet()) {
//          DbResult dbResult = new DbResult();
//          input.add(new Pair<String,DbResult>(entry.getKey(), dbResult));
//        }
//        rSet = new DbResultset(docId, input);
//      }
//      DbResult[] results = rSet.results;
//      for(int j=0; j<results.length; j++) {
//        results[j].clearTerms();
//        bind(docId, results[j], orderedVars[j]);
//      }
//      buffer[i] = rSet;

      if (! node.next()) {
        node = null;
        i += 1;
        break;
      }
    }
    return i;
  }

}
TOP

Related Classes of dovetaildb.dbservice.QueryNodeIter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.