use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunName method eval.
@Override
public Sequence eval(Sequence contextSequence, final Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
// If we have one argument, we take it into account
final Sequence seq;
if (getSignature().getArgumentCount() > 0) {
seq = getArgument(0).eval(contextSequence, contextItem);
} else {
// Otherwise, we take the context sequence and we iterate over it
seq = contextSequence;
}
if (seq == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
final Sequence result;
if (seq.isEmpty()) {
result = StringValue.EMPTY_STRING;
} else {
final Item item = seq.itemAt(0);
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
if (n instanceof INode) {
result = new StringValue(((INode) n).getQName().getStringValue());
} else {
result = StringValue.EMPTY_STRING;
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunAvg method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
Sequence result;
final Sequence inner = getArgument(0).eval(contextSequence, contextItem);
if (inner.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final SequenceIterator iter = inner.iterate();
Item item = iter.nextItem();
AtomicValue value = item.atomize();
// Any values of type xdt:untypedAtomic are cast to xs:double
if (value.getType() == Type.UNTYPED_ATOMIC) {
value = value.convertTo(Type.DOUBLE);
}
if (!(value instanceof ComputableValue)) {
throw new XPathException(this, ErrorCodes.FORG0006, Type.getTypeName(value.getType()) + "(" + value + ") " + "can not be an operand in a sum", value);
}
// Set the first value
ComputableValue sum = (ComputableValue) value;
while (iter.hasNext()) {
item = iter.nextItem();
value = item.atomize();
// Any value of type xdt:untypedAtomic are cast to xs:double
if (value.getType() == Type.UNTYPED_ATOMIC) {
value = value.convertTo(Type.DOUBLE);
}
if (!(value instanceof ComputableValue)) {
throw new XPathException(this, ErrorCodes.FORG0006, "" + Type.getTypeName(value.getType()) + "(" + value + ") can not be an operand in a sum", value);
}
if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
if (((NumericValue) value).isInfinite()) {
gotInfinity = true;
}
if (((NumericValue) value).isNaN()) {
sum = DoubleValue.NaN;
break;
}
}
try {
sum = (ComputableValue) sum.promote(value);
// Aggregate next values
sum = sum.plus((ComputableValue) value);
} catch (final XPathException e) {
throw new XPathException(this, ErrorCodes.FORG0006, e.getMessage());
}
}
result = sum.div(new IntegerValue(inner.getItemCount()));
}
if (!gotInfinity) {
if (Type.subTypeOfUnion(result.getItemType(), Type.NUMBER) && ((NumericValue) result).isInfinite()) {
// Throw an overflow exception here since we get an infinity
// whereas is hasn't been provided by the sequence
// TODO ? -pb
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunDistinctValues method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
final Collator collator = getCollator(contextSequence, contextItem, 2);
final TreeSet<AtomicValue> set = new TreeSet<>(new ValueComparator(collator));
final ValueSequence result = new ValueSequence();
Item item;
AtomicValue value;
boolean hasAlreadyNaN = false;
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
item = i.nextItem();
value = item.atomize();
if (!set.contains(value)) {
if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
if (((NumericValue) value).isNaN()) {
// contains multiple NaN values a single NaN is returned.
if (!hasAlreadyNaN) {
set.add(value);
result.add(value);
hasAlreadyNaN = true;
}
} else {
set.add(value);
result.add(value);
}
} else {
set.add(value);
result.add(value);
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class BasicNodeSetTest method executeQuery.
private static Sequence executeQuery(final DBBroker broker, final String query, final int expected, final String expectedResult) throws XPathException, SAXException, PermissionDeniedException {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final Sequence seq = xquery.execute(broker, query, null);
assertEquals(expected, seq.getItemCount());
if (expectedResult != null) {
final Item item = seq.itemAt(0);
final String value = serialize(broker, item);
assertEquals(expectedResult, value);
}
return seq;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class RecoveryTest method verify.
private void verify(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException, BTreeException, LockException {
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final Serializer serializer = broker.borrowSerializer();
try {
try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/hamlet.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/hamlet.xml' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
assertNotNull(data);
}
try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/test_string.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test_string.xml' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
assertNotNull(data);
}
final String lastSampleName = SAMPLES.getShakespeareXmlSampleNames()[SAMPLES.getShakespeareXmlSampleNames().length - 1];
try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(lastSampleName), LockMode.READ_LOCK)) {
assertNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/'" + lastSampleName + " should not exist anymore", lockedDoc);
}
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final Sequence seq = xquery.execute(broker, "//SPEECH[contains(LINE, 'king')]", null);
assertNotNull(seq);
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
final String value = serializer.serialize((NodeValue) next);
}
} finally {
broker.returnSerializer(serializer);
}
try (final LockedDocument lockedBinDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_BINARY_URI), LockMode.READ_LOCK)) {
assertNotNull("Binary document is null", lockedBinDoc);
final BinaryDocument binDoc = (BinaryDocument) lockedBinDoc.getDocument();
try (final InputStream is = broker.getBinaryResource(binDoc)) {
final byte[] bdata = new byte[(int) binDoc.getContentLength()];
is.read(bdata);
final String data = new String(bdata);
assertNotNull(data);
}
}
final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
assertNotNull(domDb);
try (final Writer writer = new StringWriter()) {
domDb.dump(writer);
}
final TransactionManager transact = pool.getTransactionManager();
try (final Txn transaction = transact.beginTransaction()) {
try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
assertNotNull(root);
transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(root.getURI()));
broker.removeCollection(transaction, root);
}
transact.commit(transaction);
}
}
}
Aggregations