@SuppressWarnings("unchecked")
 private ObjectSet executeSODAQuery(final A a, Evaluation e) {
   Query q = db().query();
   q.constrain(e);
   ObjectSet set = q.execute();
   return set;
 }
Example #2
0
 /**
  * Runs a query and retrieves multiple results as string.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryMore() throws IOException {
   final Query query = session.query("1 to 3");
   int c = 0;
   while (query.more()) assertEqual(++c, query.next());
   query.close();
 }
Example #3
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBindDynamic() throws IOException {
   final Query query = session.query("declare variable $a as xs:integer external; $a");
   query.bind("a", "1");
   assertEqual("1", query.execute());
   query.close();
 }
Example #4
0
 void renderMappers(Configuration cfg) throws IOException {
   for (EntityModel model : getAllModels()) {
     Map<String, Object> params = new HashMap<String, Object>();
     params.put("model", model);
     params.put("version", currentVersion());
     for (Query query : model.getSelectQueries()) {
       params.put("query", query);
       writeFile(
           defaultPackageName + ".mapper.cursor",
           model.getName() + query.getName() + "Mapper",
           getTemplate(cfg, "java", "cursorobjectmapper"),
           params);
     }
     params.put("prefix", "insert");
     params.put("suffix", "");
     writeFile(
         defaultPackageName + ".mapper.content.insert",
         model.getName() + "Mapper",
         getTemplate(cfg, "java", "contentvaluesmapper"),
         params);
     for (Query query : model.getUpdateQueries()) {
       params.put("prefix", "update");
       params.put("suffix", query.getName());
       writeFile(
           defaultPackageName + ".mapper.content.update",
           model.getName() + query.getName() + "Mapper",
           getTemplate(cfg, "java", "contentvaluesmapper"),
           params);
     }
   }
 }
Example #5
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBindURI() throws IOException {
   final Query query = session.query("declare variable $a external; $a");
   query.bind("$a", "X", "xs:anyURI");
   assertEqual("X", query.next());
   query.close();
 }
Example #6
0
 /**
  * Runs a query with a bound context value.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryContextInt() throws IOException {
   final Query query = session.query(". * 2");
   query.context("6", "xs:integer");
   assertEqual("12", query.next());
   query.close();
 }
Example #7
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBindEmptySequence() throws IOException {
   final Query query = session.query("declare variable $a external; $a");
   query.bind("a", "()", "empty-sequence()");
   assertNull(query.next());
   query.close();
 }
Example #8
0
 /**
  * Runs a query with a bound context value.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryContextVar() throws IOException {
   final Query query = session.query("declare variable $a := .; $a");
   query.context("<a/>", "element()");
   assertEqual("<a/>", query.next());
   query.close();
 }
Example #9
0
 /**
  * Runs a query with a bound context value.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryContext() throws IOException {
   final Query query = session.query(".");
   query.context("5");
   assertEqual("5", query.next());
   query.close();
 }
Example #10
0
 /**
  * Runs a query, omitting more().
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryNoMore() throws IOException {
   final Query query = session.query("1 to 2");
   assertEqual("1", query.next());
   assertEqual("2", query.next());
   assertNull(query.next());
   query.close();
 }
Example #11
0
 /**
  * Runs a query with additional serialization parameters.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void querySerial1() throws IOException {
   session.execute("set serializer wrap-prefix=db,wrap-uri=ns");
   final Query query = session.query(WRAPPER + "()");
   assertTrue("Result expected.", query.more());
   assertEqual("<db:results xmlns:db=\"ns\"/>", query.next());
   assertFalse("No result expected.", query.more());
 }
Example #12
0
 /**
  * Runs 5 queries in parallel.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryParallel2() throws IOException {
   final int size = 8;
   final Query[] cqs = new Query[size];
   for (int q = 0; q < size; q++) cqs[q] = session.query(Integer.toString(q));
   for (int q = 0; q < size; q++) assertEqual(q, cqs[q].next());
   for (final Query query : cqs) query.close();
 }
Example #13
0
 /**
  * Runs a query and checks the serialization parameters.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryOptions() throws IOException {
   final Query query = session.query("declare option output:encoding 'US-ASCII';()");
   query.execute();
   final SerializerOptions sp = new SerializerOptions();
   sp.parse(query.options());
   assertEquals("US-ASCII", sp.get(SerializerOptions.ENCODING));
   query.close();
 }
Example #14
0
 /**
  * Runs a query with additional serialization parameters.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void querySerial2() throws IOException {
   final Query query = session.query(WRAPPER + "1 to 2");
   assertTrue("Result expected.", query.more());
   assertEqual(
       "<db:results xmlns:db=\"ns\">  <db:result>1</db:result>"
           + "  <db:result>2</db:result></db:results>",
       query.next());
 }
Example #15
0
 /**
  * Queries empty content.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryEmptyBinary() throws IOException {
   session.execute("create db " + NAME);
   session.store("X", new ArrayInput(""));
   assertEqual("", session.execute("xquery " + RAW + _DB_RETRIEVE.args(NAME, "X")));
   assertEqual("", session.query(RAW + _DB_RETRIEVE.args(NAME, "X")).execute());
   final Query q = session.query(RAW + _DB_RETRIEVE.args(NAME, "X"));
   assertTrue(q.more());
   assertEqual("", q.next());
   assertNull(q.next());
 }
Example #16
0
  /** Preload all lanes and vehicles once. */
  @PostConstruct
  public void init() {
    System.out.println("Initializing MovementsDAOImpl");
    Query laneQuery = em.createQuery("select x from Lane x");
    lanes = laneQuery.getResultList();

    Query vehicleQuery = em.createQuery("select x from Vehicle x");
    vehicles = vehicleQuery.getResultList();

    generateLicensePlates(LICENSES);
  }
Example #17
0
 /**
  * Runs two queries in parallel.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryParallel() throws IOException {
   final Query query1 = session.query("1 to 2");
   final Query query2 = session.query("reverse(3 to 4)");
   assertEqual("1", query1.next());
   assertEqual("4", query2.next());
   assertEqual("2", query1.next());
   assertEqual("3", query2.next());
   assertNull(query1.next());
   assertNull(query2.next());
   query1.close();
   query2.close();
 }
Example #18
0
  /** For debugging. */
  public static void main(String[] args) throws Exception {
    final String usage = "NutchBean query";

    if (args.length == 0) {
      System.err.println(usage);
      System.exit(-1);
    }

    final Configuration conf = NutchConfiguration.create();
    final NutchBean bean = new NutchBean(conf);
    try {
      final Query query = Query.parse(args[0], conf);
      final Hits hits = bean.search(query, 10);
      System.out.println("Total hits: " + hits.getTotal());
      final int length = (int) Math.min(hits.getTotal(), 10);
      final Hit[] show = hits.getHits(0, length);
      final HitDetails[] details = bean.getDetails(show);
      final Summary[] summaries = bean.getSummary(details, query);

      for (int i = 0; i < hits.getLength(); i++) {
        System.out.println(" " + i + " " + details[i] + "\n" + summaries[i]);
      }
    } catch (Throwable t) {
      LOG.error("Exception occured while executing search: " + t, t);
      System.exit(1);
    }
    System.exit(0);
  }
  private void logResultInfo(Query query, String queryResult) {
    StringBuffer sb = new StringBuffer();

    sb.append(
        "\n\n\tQuery "
            + query.getNr()
            + " of run "
            + (query.getQueryMix().getQueryMixRuns() + 1)
            + ":\n");
    sb.append("\n\tQuery string:\n\n");
    sb.append(query.getQueryString());
    sb.append("\n\n\tResult:\n\n");
    sb.append(queryResult);
    sb.append(
        "\n\n__________________________________________________________________________________\n");
    logger.log(Level.ALL, sb.toString());
  }
Example #20
0
 /**
  * Runs a query with an external variable declaration.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBind() throws IOException {
   final Query query = session.query("declare variable $a external; $a");
   query.bind("$a", "4");
   assertEqual("4", query.execute());
   query.bind("$a", "5");
   assertEqual("5", query.next());
   query.bind("$a", "6");
   assertEqual("6", query.next());
   query.close();
 }
Example #21
0
 /**
  * Binds maps to external variables via JSON.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBindJson() throws IOException {
   final String var = "declare variable $x external;",
       map = "{\"foo\":[1,2,3],\"bar\":{\"a\":null,\"\":false}}";
   final String[][] tests = {
     {"for $k in map:keys($x) order by $k descending return $k", "foo bar"},
     {"every $k in map:keys($x('foo')) satisfies $k eq $x('foo')($k)", "true"},
     {"empty($x('bar')('a')) and not($x('bar')(''))", "true"},
   };
   for (final String[] test : tests) {
     final Query q = session.query(var + test[0]);
     try {
       q.bind("$x", map, "json");
       assertEqual(test[1], q.execute());
     } finally {
       q.close();
     }
   }
 }
Example #22
0
  /**
   * Runs a query and checks the updating flag.
   *
   * @throws IOException I/O exception
   */
  @Test
  public void queryUpdating() throws IOException {
    // test non-updating query
    Query query = session.query("12345678");
    assertFalse(query.updating());
    assertEqual("12345678", query.execute());
    assertFalse(query.updating());
    query.close();

    // test updating query
    query = session.query("insert node <a/> into <b/>");
    assertTrue(query.updating());
    assertEqual("", query.execute());
    assertTrue(query.updating());
    query.close();
  }
 /*
  * Execute Query with Query Object
  */
 public void executeQuery(Query query, byte queryType) {
   if (query.isParametrized)
     executeQuery(
         query.getParametrizedQueryString(),
         query.getEncodedParamString(),
         queryType,
         query.getNr(),
         query.getQueryMix());
   else executeQuery(query.getQueryString(), "", queryType, query.getNr(), query.getQueryMix());
 }
  /*
   * (non-Javadoc)
   * @see benchmark.testdriver.ServerConnection#executeValidation(benchmark.testdriver.Query, byte, java.lang.String[])
   * Gather information about the result a query returns.
   */
  public QueryResult executeValidation(Query query, byte queryType) {
    String queryString = query.getQueryString();
    String parametrizedQueryString = query.getParametrizedQueryString();
    String encodedParamString = query.getEncodedParamString();
    int queryNr = query.getNr();
    String[] rowNames = query.getRowNames();
    boolean sorted = queryString.toLowerCase().contains("order by");
    QueryResult queryResult = null;
    NetQuery qe;
    if (query.isParametrized)
      qe =
          new NetQuery(
              serverURL, parametrizedQueryString, encodedParamString, queryType, defaultGraph, 0);
    else qe = new NetQuery(serverURL, queryString, "", queryType, defaultGraph, 0);
    InputStream is = qe.exec();
    Document doc = getXMLDocument(is);
    XMLOutputter outputter = new XMLOutputter();
    logResultInfo(query, outputter.outputString(doc));

    if (queryType == Query.SELECT_TYPE)
      queryResult = gatherResultInfoForSelectQuery(queryString, queryNr, sorted, doc, rowNames);

    if (queryResult != null) queryResult.setRun(query.getQueryMix().getRun());
    return queryResult;
  }
Example #25
0
  /**
   * Runs a query with an external variable declaration.
   *
   * @throws IOException I/O exception
   */
  @Test
  public void queryBindInt() throws IOException {
    Query query = session.query("declare variable $a as xs:integer external; $a");
    query.bind("a", "5", "xs:integer");
    assertEqual("5", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", Int.get(1), "xs:integer");
    assertEqual("1", query.next());
    query.close();
  }
Example #26
0
 /**
  * Queries empty content.
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryEmptyString() throws IOException {
   final Query q = session.query("'',1");
   assertTrue(q.more());
   assertEqual("", q.next());
   assertTrue(q.more());
   assertEqual("1", q.next());
   assertNull(q.next());
 }
Example #27
0
 /**
  * Queries binary content (works only if output stream is specified).
  *
  * @throws IOException I/O exception
  */
 @Test
 public void queryBinary() throws IOException {
   if (out == null) return;
   session.execute("create db " + NAME);
   final byte[] tmp = {0, 1, 2, 127, 0, -1, -2, -128};
   session.store("X", new ArrayInput(tmp));
   final String retr = _DB_RETRIEVE.args(NAME, "X");
   // check command
   session.execute("xquery " + RAW + retr + ',' + retr);
   assertTrue(eq(out.toArray(), concat(tmp, tmp)));
   out.reset();
   // check query execution
   session.query(RAW + retr + ',' + retr).execute();
   assertTrue(eq(out.toArray(), concat(tmp, tmp)));
   out.reset();
   // check iterator
   final Query q = session.query(RAW + retr + ',' + retr);
   q.next();
   assertTrue(eq(out.toArray(), tmp));
   out.reset();
   q.next();
   assertTrue(eq(out.toArray(), tmp));
   assertNull(q.next());
 }
Example #28
0
  /**
   * Search for pages matching a query, eliminating excessive hits with matching values for a named
   * field. Hits after the first <code>maxHitsPerDup</code> are removed from results. The remaining
   * hits have {@link Hit#moreFromDupExcluded()} set.
   *
   * <p>If maxHitsPerDup is zero then all hits are returned.
   *
   * @param query query
   * @param numHits number of requested hits
   * @param maxHitsPerDup the maximum hits returned with matching values, or zero
   * @param dedupField field name to check for duplicates
   * @param sortField Field to sort on (or null if no sorting).
   * @param reverse True if we are to reverse sort by <code>sortField</code>.
   * @return Hits the matching hits
   * @throws IOException
   */
  public Hits search(
      Query query,
      int numHits,
      int maxHitsPerDup,
      String dedupField,
      String sortField,
      boolean reverse)
      throws IOException {
    if (maxHitsPerDup <= 0) // disable dup checking
    return search(query, numHits, dedupField, sortField, reverse);

    final float rawHitsFactor = this.conf.getFloat("searcher.hostgrouping.rawhits.factor", 2.0f);
    int numHitsRaw = (int) (numHits * rawHitsFactor);
    if (LOG.isInfoEnabled()) {
      LOG.info("searching for " + numHitsRaw + " raw hits");
    }
    Hits hits = searchBean.search(query, numHitsRaw, dedupField, sortField, reverse);
    final long total = hits.getTotal();
    final Map<String, DupHits> dupToHits = new HashMap<String, DupHits>();
    final List<Hit> resultList = new ArrayList<Hit>();
    final Set<Hit> seen = new HashSet<Hit>();
    final List<String> excludedValues = new ArrayList<String>();
    boolean totalIsExact = true;
    for (int rawHitNum = 0; rawHitNum < hits.getTotal(); rawHitNum++) {
      // get the next raw hit
      if (rawHitNum >= hits.getLength()) {
        // optimize query by prohibiting more matches on some excluded values
        final Query optQuery = (Query) query.clone();
        for (int i = 0; i < excludedValues.size(); i++) {
          if (i == MAX_PROHIBITED_TERMS) break;
          optQuery.addProhibitedTerm(excludedValues.get(i), dedupField);
        }
        numHitsRaw = (int) (numHitsRaw * rawHitsFactor);
        if (LOG.isInfoEnabled()) {
          LOG.info("re-searching for " + numHitsRaw + " raw hits, query: " + optQuery);
        }
        hits = searchBean.search(optQuery, numHitsRaw, dedupField, sortField, reverse);
        if (LOG.isInfoEnabled()) {
          LOG.info("found " + hits.getTotal() + " raw hits");
        }
        rawHitNum = -1;
        continue;
      }

      final Hit hit = hits.getHit(rawHitNum);
      if (seen.contains(hit)) continue;
      seen.add(hit);

      // get dup hits for its value
      final String value = hit.getDedupValue();
      DupHits dupHits = dupToHits.get(value);
      if (dupHits == null) dupToHits.put(value, dupHits = new DupHits());

      // does this hit exceed maxHitsPerDup?
      if (dupHits.size() == maxHitsPerDup) { // yes -- ignore the hit
        if (!dupHits.maxSizeExceeded) {

          // mark prior hits with moreFromDupExcluded
          for (int i = 0; i < dupHits.size(); i++) {
            dupHits.get(i).setMoreFromDupExcluded(true);
          }
          dupHits.maxSizeExceeded = true;

          excludedValues.add(value); // exclude dup
        }
        totalIsExact = false;
      } else { // no -- collect the hit
        resultList.add(hit);
        dupHits.add(hit);

        // are we done?
        // we need to find one more than asked for, so that we can tell if
        // there are more hits to be shown
        if (resultList.size() > numHits) break;
      }
    }

    final Hits results = new Hits(total, resultList.toArray(new Hit[resultList.size()]));
    results.setTotalIsExact(totalIsExact);
    return results;
  }
Example #29
0
  @Override
  public Weight createWeight(IndexSearcher searcher) throws IOException {
    final Weight weight = query.createWeight(searcher);
    return new Weight() {
      @Override
      public String toString() {
        return "no-zero:" + weight.toString();
      }

      @Override
      public Query getQuery() {
        return weight.getQuery();
      }

      @Override
      public float getValueForNormalization() throws IOException {
        return weight.getValueForNormalization();
      }

      @Override
      public void normalize(float queryNorm, float topLevelBoost) {
        weight.normalize(queryNorm, topLevelBoost);
      }

      @Override
      public Explanation explain(AtomicReaderContext context, int doc) throws IOException {
        return weight.explain(context, doc);
      }

      @Override
      public Scorer scorer(AtomicReaderContext context, Bits acceptDocs) throws IOException {
        final Scorer scorer = weight.scorer(context, acceptDocs);
        if (scorer == null) return scorer;
        return new Scorer(weight) {
          float current_score = -1f;

          @Override
          public int docID() {
            return scorer.docID();
          }

          @Override
          public int freq() throws IOException {
            return scorer.freq();
          }

          @Override
          public int nextDoc() throws IOException {
            while (true) {
              int n = scorer.nextDoc();
              if (n == DocIdSetIterator.NO_MORE_DOCS) return n;
              current_score = scorer.score();
              if (current_score != 0) return n;
            }
          }

          @Override
          public int advance(int target) throws IOException {
            int n = scorer.advance(target);
            if (n == DocIdSetIterator.NO_MORE_DOCS) return n;
            current_score = scorer.score();
            if (current_score != 0) return n;
            // if the score is 0, we just return the next non-zero next doc
            return nextDoc();
          }

          @Override
          public float score() throws IOException {
            return current_score;
          }

          @Override
          public long cost() {
            return scorer.cost();
          }

          @Override
          public String toString() {
            return "no-zero:" + scorer.toString();
          }
        };
      }
    };
  }
Example #30
0
 @Override
 public String toString(String field) {
   return "no-zero:" + query.toString(field);
 }