@Override public Value value(final QueryContext qc) throws QueryException { final FItem getKey = checkArity(exprs[1], 1, qc); final long k = Math.min(toLong(exprs[2], qc), Integer.MAX_VALUE); if (k < 1) return Empty.SEQ; final Iter iter = exprs[0].iter(qc); final MinHeap<Item, Item> heap = new MinHeap<>( new Comparator<Item>() { @Override public int compare(final Item it1, final Item it2) { try { return OpV.LT.eval(it1, it2, sc.collation, sc, info) ? -1 : 1; } catch (final QueryException qe) { throw new QueryRTException(qe); } } }); try { for (Item it; (it = iter.next()) != null; ) { heap.insert(checkNoEmpty(getKey.invokeItem(qc, info, it)), it); if (heap.size() > k) heap.removeMin(); } } catch (final QueryRTException ex) { throw ex.getCause(); } final ValueBuilder vb = new ValueBuilder(); while (!heap.isEmpty()) vb.addFront(heap.removeMin()); return vb.value(); }
/** * Binds the specified parameter to a variable. * * @param rxp parameter * @param args argument array * @param values values to be bound; the parameter's default value is assigned if the argument is * {@code null} or empty * @throws QueryException query exception */ private void bind(final RestXqParam rxp, final Expr[] args, final String... values) throws QueryException { final Value val; if (values == null || values.length == 0) { val = rxp.value; } else { final ValueBuilder vb = new ValueBuilder(); for (final String s : values) vb.add(new Atm(s)); val = vb.value(); } bind(rxp.name, args, val); }
@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { final Iter ir = iter(qc); final Item it = ir.next(); if (it == null || ir.size() == 1) return it; final Item n = ir.next(); if (n != null) { final ValueBuilder vb = new ValueBuilder().add(it).add(n); if (ir.next() != null) vb.add(Str.get(DOTS)); throw SEQFOUND_X.get(info, vb.value()); } return it; }
/** * Returns a parameter. * * @param value value * @param name name * @param declared variable declaration flags * @return parameter * @throws QueryException HTTP exception */ private RestXqParam param(final Value value, final QNm name, final boolean[] declared) throws QueryException { // [CG] RESTXQ: allow identical field names? final long vs = value.size(); if (vs < 2) error(ANN_PARAMS, "%", name.string(), 2); // name of parameter final String key = toString(value.itemAt(0), name); // variable template final QNm qnm = checkVariable(toString(value.itemAt(1), name), declared); // default value final ValueBuilder vb = new ValueBuilder(); for (int v = 2; v < vs; v++) vb.add(value.itemAt(v)); return new RestXqParam(qnm, key, vb.value()); }
@Override public Value value(final QueryContext qc) throws QueryException { final FItem f = checkArity(exprs[1], 1, qc); final Iter iter = exprs[0].iter(qc); Item it = iter.next(); if (it == null) return Empty.SEQ; final Value v1 = f.invokeValue(qc, info, it); it = iter.next(); if (it == null) return v1; final ValueBuilder vb = new ValueBuilder().add(v1); do { vb.add(f.invokeValue(qc, info, it)); } while ((it = iter.next()) != null); return vb.value(); }
@Override public final Item ebv(final QueryContext qc, final InputInfo ii) throws QueryException { final Item it; if (seqType().zeroOrOne()) { it = item(qc, info); } else { final Iter ir = iter(qc); it = ir.next(); if (it != null && !(it instanceof ANode)) { final Item n = ir.next(); if (n != null) { final ValueBuilder vb = new ValueBuilder().add(it).add(n); if (ir.next() != null) vb.add(Str.get(DOTS)); throw EBV_X.get(info, vb.value()); } } } return it == null ? Bln.FALSE : it; }
@Override public Value value(final QueryContext qc) { final ValueBuilder vb = new ValueBuilder(); for (final Object k : System.getenv().keySet()) vb.add(Str.get(k.toString())); return vb.value(); }