Package dovetaildb.querynode

Examples of dovetaildb.querynode.QueryNode


import dovetaildb.querynode.QueryNodeTest;

public class QueryNodeDbResultTest extends TestCase {
 
  public void testBasic() {
    QueryNode node = QueryNodeTest.testingQueryNode();
    QueryNodeDbResult result = new QueryNodeDbResult(node);
    result.initialize(node.doc());
    assertTrue(result.isObject());
    assertEquals("002",result.derefByKey("id").simplify());
    assertEquals("32",result.derefByKey("age").simplify());
    assertEquals("phil",result.derefByKey("name").simplify());
    assertEquals(null,result.derefByKey("xxx").simplify());
View Full Code Here


    ensureIsAdvanced();
    if (outerNode == null) {
      outerNode = null;
      return;
    }
    QueryNode node = index.getRange(new Range(range), revNum);
    if (node == null) {
      outerNode = null;
      return;
    }
    long curDoc = outerNode.doc();
    node.seek(curDoc, ArrayBytes.EMPTY_BYTES);
    if (node.doc() == Long.MAX_VALUE) {
      outerNode = null;
      return;
    }
    rangeSpecializations.add(new RangeSpecialization(range, node));
    ArrayList<QueryNode> nodes = new ArrayList<QueryNode>();
View Full Code Here

        addSpecializationRange(range);
        optimized = true;
      } else {
        Object[] heap = specializationNode.heap.heap;
        for(int i=1; i<heap.length; i++) {
          QueryNode curNode = (QueryNode) heap[i];
          if (curNode == target.node) {
            QueryNode newNode = specializationNode.heap.specializeChild(i, range);
            optimized |= target.node != newNode;
            if (optimized) {
              target.node = newNode;
              rebuildSpecializationRange();
            }
View Full Code Here

    assertEquals(null,index.getTerm(t1, revNum));
  }
 
  public void checkTwoInserts(long revNum) {
    long doc0, doc1, doc2;
    QueryNode s;
    QueryNode allTerms = index.getRange(new Range(ArrayBytes.EMPTY_BYTES, null, null, true, true), revNum);
    s = index.getTerm(t1, revNum);
    assertTrue(s.next());
    doc0 = s.doc();
    assertFalse(s.next());
   
    allTerms.seek(doc0, ArrayBytes.EMPTY_BYTES);
    assertEquals(0, allTerms.term().compareTo(t1));
    assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
    assertEquals(0, allTerms.term().compareTo(t2));
    assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
    assertEquals(0, allTerms.term().compareTo(t4));
    assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
    assertEquals(0, allTerms.term().compareTo(t5));
    assertEquals(NextStatus.AT_END, allTerms.nextTerm());
   
    s = index.getTerm(t1, revNum);
    doc1 = s.doc();
    assertTrue(s.next());
    doc2 = s.doc();
    assertFalse(s.next());
    assertTrue(doc1 < doc2);
   
    allTerms = index.getRange(new Range(ArrayBytes.EMPTY_BYTES, null, null, true, true), revNum);
    allTerms.seek(doc1, ArrayBytes.EMPTY_BYTES);
    assertEquals(0, allTerms.term().compareTo(t1));
    assertEquals(NextStatus.NEXT_TERM, allTerms.nextTerm());
    assertEquals(0, allTerms.term().compareTo(t3));
    assertEquals(NextStatus.NEXT_DOC, allTerms.nextTerm());
   
    s = index.getRange(new Range(t0, t3, t4, false, false),  revNum);
    assertNull(s);
    s = index.getRange(new Range(t0, t3, t4, false, true), revNum);
    assertNotNull(s);
    assertFalse(s.next());
    s = index.getRange(new Range(t0, t3, t4, true, false),  revNum);
    assertNotNull(s);
    assertFalse(s.next());
    s = index.getRange(new Range(t0, t3, t4, true, true),  revNum);
    assertNotNull(s);
    assertTrue(s.next());
    assertFalse(s.next());
   
    allTerms = index.getRange(new Range(ArrayBytes.EMPTY_BYTES, null, null, true, true), revNum);
    Range r = new Range(t1, null, null, true, true);
    s = allTerms.specialize(r);
    assertNotNull(s);

    r = new Range(t3, t3, t3, true, true);
    s = allTerms.specialize(r);
    // could be null (no results expected)
    if (s != null) { // if not null, make sure no results match
      do {
        assertFalse(r.matches(s.term()));
      } while(s.nextTerm() != NextStatus.AT_END);
View Full Code Here

    edits = new ArrayList<EditRec>(removeQueue);
    EditRec.sortByTerm(edits);
   
    long revNum = index.commitNewRev(EditRec.cloneList(edits));
   
    QueryNode node = index.getRange(Range.OPEN_RANGE, revNum);
    long expectedCt = removeQueue.size();
    assertNotNull(node);
    for(ct=1; ct<expectedCt; ct++) {
      assertTrue(node.nextTerm() != NextStatus.AT_END);
    }
    assertEquals(NextStatus.AT_END, node.nextTerm());

    for(EditRec edit : removeQueue) { edit.isDeletion = true; }
    int modCap=1;
    while( (!addQueue.isEmpty()) && (!removeQueue.isEmpty()) ) {
      tryQueries(removeQueue, revNum, r);

      int numRemovals = r.nextInt(Math.min(modCap, removeQueue.size()+1));
      int numAdds     = r.nextInt(Math.min(modCap, addQueue.size()+1));
     
      edits = new ArrayList<EditRec>();
      edits.addAll(removeQueue.subList(0, numRemovals));
      List<EditRec> curAdds = addQueue.subList(0, numAdds);
//      System.out.println("removals "+edits);
//      System.out.println("adds "+curAdds);
      edits.addAll(curAdds);
      EditRec.sortByTerm(edits);
//      long checkpoint = r.nextLong();
//      System.out.println("checkpoint "+checkpoint);
//      if (checkpoint == 4723830616276967586L) {
//        System.out.println("checkpoint "+checkpoint);
//      }
      revNum = index.commitNewRev(EditRec.cloneList(edits));
     
      removeQueue = removeQueue.subList(numRemovals, removeQueue.size());
      removeQueue = new ArrayList<EditRec>(removeQueue);
      for(EditRec edit : curAdds) { edit.isDeletion = true; }
      removeQueue.addAll(curAdds);
      addQueue    =    addQueue.subList(numAdds,     addQueue.size());
      expectedCt += numAdds - numRemovals;
     
      ArrayList<EditRec> tmpQueue = new ArrayList<EditRec>(removeQueue);
      EditRec.sortById(tmpQueue);
      int tmpIdx = 0;
      QueryNode n = index.getRange(Range.OPEN_RANGE, revNum);
      float jumpChance = 0.2f;
      while(n != null) {
        if (r.nextFloat() < jumpChance) {
          jumpChance *= 0.9;
          tmpIdx = r.nextInt(tmpQueue.size());
          long docId = tmpQueue.get(tmpIdx).docId;
//          System.out.println("Jump to "+docId);
          while (tmpIdx > 0 && tmpQueue.get(tmpIdx-1).docId == docId) {
            tmpIdx--;
          }
          n.seek(docId, ArrayBytes.EMPTY_BYTES);
        }
        assertEquals(n.doc(), tmpQueue.get(tmpIdx).docId);
        assertEquals(n.term(), tmpQueue.get(tmpIdx).term);
        if (n.nextTerm() == QueryNode.NextStatus.AT_END) break;
        tmpIdx++;
      }

      modCap += 2;
    }
View Full Code Here

    boolean inclMax = r.nextBoolean();
    return new Range(pre, min, max, inclMin, inclMax);
  }
  private void tryQuery(List<EditRec> byId, long revNum, Random r) {
    Filter<Bytes> matches = null;
    QueryNode node = null;
//    long y = r.nextLong();
//    System.out.println(" NEW ONE "+y);
//    if (y == 6290374244227081246l) {
//      System.out.println("Y "+y);
//    }
    switch (r.nextInt(2)) {
    case 0:
      final ArrayList<Bytes> termsForQuery = new ArrayList<Bytes>();
      final ArrayList<Bytes> terms = new ArrayList<Bytes>();
      for(int i=r.nextInt(10); i>=0; i--) {
        Bytes bytes = randTerm(r);
        terms.add(bytes);
        termsForQuery.add(bytes.copy());
      }
      node = index.getTerms(termsForQuery, revNum);
      matches = new Filter<Bytes>(){
        public MatchReq filter(Bytes item) {
          return terms.contains(item) ? MatchReq.mustmatch : MatchReq.nomatch;
        }
      };
      break;
    case 1:
      Range range = genRange(r);
      range.flattenTerms();
      boolean relaxed = false;
      Range spec = null;
      if (r.nextBoolean()) {
        node = index.getRange(new Range(range), revNum);
      } else {
        spec = new Range(range);
        Bytes prefix = range.getPrefix();
        Bytes min = range.getMinSuffix();
        Bytes max = range.getMaxSuffix();
        if (min == null && max == null) {
          range.setPrefix(new SlicedBytes(prefix, 0, r.nextInt(prefix.getLength()+1)));
        } else if (min == null) {
          range.setMaxSuffix(new SlicedBytes(max, 0, r.nextInt(max.getLength()+1)));
        } else {
          range.setMinSuffix(new SlicedBytes(min, 0, r.nextInt(min.getLength()+1)));
        }
        spec.flattenTerms();
        range.flattenTerms();
        node = index.getRange(new Range(range), revNum);
//        while (node != null) {
//          if (r.nextBoolean()) break;
//          if (node.nextTerm() == NextStatus.AT_END) break;
//        }
        if (node != null) {
          QueryNode specializedNode = node.specialize(new Range(spec));
          range.flattenTerms();
          relaxed = (specializedNode != node);
          if (! relaxed) {
            specializedNode = index.getRange(new Range(range), revNum);
          }
View Full Code Here

  private ArrayList<EditRec> encodeBuffer(String bagName, long revNum, ApiBuffer value) {
    ArrayList<EditRec> edits = new ArrayList<EditRec>();

    BagEntry bagRec = bagIndexes.get(bagName);
    BagIndex index = bagRec.getBagIndex();
    QueryNode idQuery = null;
    Map<String,Object> entries = value.getEntries();
    QueryNode data = null;
    if (revNum > 0) {
      data = index.getRange(Range.OPEN_RANGE, revNum);
      int capacity = value.getDeletions().size()+value.getEntries().size();
      if (capacity > 0) {
        ArrayList<Bytes> idTerms = new ArrayList<Bytes>(capacity);
        for(String key : value.getDeletions()) {
          idTerms.add(new CompoundBytes(ID_BYTES, new ArrayBytes(Util.decodeString(key))));
        }
        for(String key : entries.keySet()) {
          idTerms.add(new CompoundBytes(ID_BYTES, new ArrayBytes(Util.decodeString(key))));
        }
        idQuery = index.getTerms(idTerms, revNum);
      }
    }
    long lastDocId = Long.MIN_VALUE;
    if (idQuery != null) {
      long docId = idQuery.doc();
      assert docId > lastDocId;
      lastDocId = docId;
      do {
        Bytes idTerm = idQuery.term();
        assert idTerm.compareToParts(ID_BYTES, 0, 0, NUM_ID_BYTES, NUM_ID_BYTES, 9) == 0;
        byte[] idBytes = idTerm.subBytes(NUM_ID_BYTES, idTerm.getLength()-NUM_ID_BYTES).getBytes();
        String idString = Util.encodeBytes(idBytes);
        Object val = entries.get(idString);
        if (val != null) {
          entries.remove(idString);
          DbServiceUtil.sencodeMulti(ArrayBytes.EMPTY_BYTES, ArrayBytes.EMPTY_BYTES, val, edits, docId, false);
        }
       
        /* QNWORK
        data.seek(docId, ArrayBytes.EMPTY_BYTES);
        do {
          edits.add(new EditRec(docId, data.term().copy(), true));
        } while(data.nextTerm() == NextStatus.NEXT_TERM);
        */
        if (! data.positionSet(docId, ArrayBytes.EMPTY_BYTES)) {
          assert false;
        }
        do {
          edits.add(new EditRec(docId, data.term().copy(), true));
        } while (data.positionNext());
        docId = idQuery.nextValidDocId(docId + 1);
      } while(docId != Long.MAX_VALUE);
    }
    long insId = -1;
    for(Object val : entries.values()) {
View Full Code Here

      if (revNum == -1) return EmptyIter.EMPTY_ITER;
      QueryNodeTemplate templ = DbServiceUtil.applyPatternToBagIndex(query, index, revNum);
      //System.out.println("Query plan: "+templ.queryNode);
      if (templ.queryNode == null) return EmptyIter.EMPTY_ITER;
      if (templ.varMappings.size() == 1) {
        QueryNode display = templ.varMappings.values().iterator().next();
        return new QueryNodeIter(templ.queryNode, display, index, revNum);
      } else {
        throw new RuntimeException("Multiple result returns not yet supported");
      }
    } catch (TxnNotFoundException e) {
View Full Code Here

    }
    return templ;
  }
  public static QueryNodeTemplate applyPatternToBagIndex(Bytes prefix, Object pattern, BagIndex index, long revNum) {
    Map<String, QueryNode> vars = new HashMap<String, QueryNode>();
    QueryNode queryNode;
    if (pattern instanceof Map) {
      Map map = (Map)pattern;
      prefix = new CompoundBytes(prefix, HEADER_BYTE_MAPOPEN);
      ArrayList<QueryNode> nodes = new ArrayList<QueryNode>();
      for(Object entryObj : map.entrySet()) {
View Full Code Here

    }
  }
 
  public static QueryNodeTemplate applyQueryToBagIndex(Bytes prefix, List query, BagIndex index, long revNum) {
    Map<String, QueryNode> vars = new HashMap<String, QueryNode>();
    QueryNode queryNode;
    ArrayList<QueryNode> clauses;
    if (query == null) {
      queryNode = index.getRange(Range.OPEN_RANGE, revNum);
    } else {
      int opHash = query.get(0).hashCode();
      int numArgs = query.size();
      switch(opHash) {
      case DbServiceUtil.OP_HASH_AS:
        queryNode = index.getRange(new Range(prefix, null, null, true, true), revNum);
        if (query.size() > 2) {
          throw new RuntimeException("Not yet supported");
//          QueryNodeTemplate subNode=applyQueryToBagIndex(prefix, (List)query.get(2), index, revNum);
//          vars.put((String)query.get(1), ExternalTermQueryNode.make(subNode.queryNode, queryNode));
//          queryNode = subNode.queryNode;
//          vars = subNode.varMappings;
        } else {
          vars.put((String)query.get(1), queryNode);
        }
        break;
      case DbServiceUtil.OP_HASH_OR:
      case DbServiceUtil.OP_HASH_AND:
        Object subQueryObject = query.get(1);
        if (!(subQueryObject instanceof List)) {
          throw new ApiException("QueryFormatError", "\""+query.get(0)+"\" operator must have a list in the first position, instead found: "+subQueryObject);
        }
        List<Object> subQueries = (List<Object>)query.get(1);
        int numQueries = subQueries.size();
        clauses = new ArrayList<QueryNode>(numQueries);
        for(int i=0; i<numQueries; i++) {
          QueryNodeTemplate node=applyPatternToBagIndex(prefix, subQueries.get(i), index, revNum);
          if (node.queryNode != null) clauses.add(node.queryNode);
          vars.putAll(node.varMappings);
        }
        if (opHash == DbServiceUtil.OP_HASH_OR) {
          queryNode = OrderedOrQueryNode.make(clauses);
        } else {
          queryNode = AndQueryNode.make(clauses);
        }
        break;
      case DbServiceUtil.OP_HASH_NOT:
        QueryNode matchesSoFar = index.getRange(new Range(prefix, null, null, true, true), revNum);
        clauses = new ArrayList<QueryNode>(numArgs-1);
        for(int i=1; i<numArgs; i++) {
          QueryNodeTemplate node=applyPatternToBagIndex(prefix, query.get(i), index, revNum);
          if (node.queryNode != null) clauses.add(node.queryNode);
        }
        QueryNode negativeMatches = OrderedOrQueryNode.make(clauses);
        queryNode = AndNotQueryNode.make(matchesSoFar, negativeMatches);
        break;
      default:
        queryNode = index.getRange(parseRange(prefix, opHash, query), revNum);
      }
View Full Code Here

TOP

Related Classes of dovetaildb.querynode.QueryNode

Copyright © 2018 www.massapicom. 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.