From: Wolfgang M. M. <wol...@us...> - 2004-09-12 09:26:02
|
Update of /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21081/src/org/exist/xquery/value Modified Files: OrderedValueSequence.java NodeValue.java Sequence.java AbstractSequence.java BooleanValue.java AtomicValue.java Item.java Added Files: PreorderedValueSequence.java Log Message: * Added support for XQuery pragmas to set serialization and watchdog settings. * org.exist.storage.serializers.Serializer now passes all output to an instance of the Receiver interface instead of a SAX ContentHandler. Receiver resembles SAX, but has methods that are closer to eXist's internal storage. For example, it directly accepts a QName in startElement to avoid unnecessary string allocations. * Fixed various performance leaks in cross-document joins. Index: Item.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value/Item.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Item.java 28 May 2004 10:54:10 -0000 1.2 --- Item.java 12 Sep 2004 09:25:15 -0000 1.3 *************** *** 21,25 **** package org.exist.xquery.value; ! import org.exist.memtree.Receiver; import org.exist.storage.DBBroker; import org.exist.xquery.XPathException; --- 21,25 ---- package org.exist.xquery.value; ! import org.exist.memtree.DocumentBuilderReceiver; import org.exist.storage.DBBroker; import org.exist.xquery.XPathException; *************** *** 74,78 **** public void toSAX(DBBroker broker, ContentHandler handler) throws SAXException; ! public void copyTo(DBBroker broker, Receiver receiver) throws SAXException; public int conversionPreference(Class javaClass); --- 74,78 ---- public void toSAX(DBBroker broker, ContentHandler handler) throws SAXException; ! public void copyTo(DBBroker broker, DocumentBuilderReceiver receiver) throws SAXException; public int conversionPreference(Class javaClass); Index: OrderedValueSequence.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value/OrderedValueSequence.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** OrderedValueSequence.java 28 May 2004 10:54:10 -0000 1.2 --- OrderedValueSequence.java 12 Sep 2004 09:25:15 -0000 1.3 *************** *** 29,32 **** --- 29,39 ---- /** + * A sequence that sorts its entries in the order specified by the order specs of + * an "order by" clause. Used by {@link org.exist.xquery.ForExpr}. + * + * Contrary to class {@link org.exist.xquery.value.PreorderedValueSequence}, + * all order expressions are evaluated once for each item in the sequence + * <b>while</b> items are added. + * * @author wolf */ *************** *** 37,40 **** --- 44,49 ---- private int count = 0; + private long execTime = 0; + public OrderedValueSequence(OrderSpec orderSpecs[], int size) { this.orderSpecs = orderSpecs; *************** *** 99,102 **** --- 108,112 ---- public void sort() { FastQSort.sort(items, 0, count - 1); + System.out.println("order by took " + execTime); } *************** *** 124,127 **** --- 134,138 ---- public Entry(Item item) throws XPathException { + long start = System.currentTimeMillis(); this.item = item; values = new AtomicValue[orderSpecs.length]; *************** *** 135,138 **** --- 146,150 ---- orderSpecs[i].getSortExpression().pprint() + " ; found: " + seq.getLength()); } + execTime = execTime + (System.currentTimeMillis() - start); } Index: BooleanValue.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value/BooleanValue.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** BooleanValue.java 28 May 2004 10:54:10 -0000 1.3 --- BooleanValue.java 12 Sep 2004 09:25:15 -0000 1.4 *************** *** 31,34 **** --- 31,45 ---- private boolean value; + /** + * Returns one of the static fields TRUE or FALSE depending on + * the value of the parameter. + * + * @param bool + * @return + */ + public final static BooleanValue valueOf(boolean bool) { + return bool ? TRUE : FALSE; + } + public BooleanValue(boolean bool) { value = bool; Index: AbstractSequence.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value/AbstractSequence.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** AbstractSequence.java 14 Aug 2004 09:45:46 -0000 1.7 --- AbstractSequence.java 12 Sep 2004 09:25:15 -0000 1.8 *************** *** 184,186 **** --- 184,201 ---- } } + + /* (non-Javadoc) + * @see org.exist.xquery.value.Sequence#isCached() + */ + public boolean isCached() { + // always return false by default + return false; + } + + /* (non-Javadoc) + * @see org.exist.xquery.value.Sequence#setIsCached(boolean) + */ + public void setIsCached(boolean cached) { + // ignore by default + } } Index: Sequence.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value/Sequence.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Sequence.java 2 Jun 2004 11:34:34 -0000 1.4 --- Sequence.java 12 Sep 2004 09:25:15 -0000 1.5 *************** *** 164,167 **** --- 164,183 ---- public Object toJavaObject(Class target) throws XPathException; + /** + * Returns true if the sequence is the result of a previous operation + * and has been cached. + * + * @return + */ + public boolean isCached(); + + /** + * Indicates that the sequence is the result of a previous operation + * and has not been recomputed. + * + * @param cached + */ + public void setIsCached(boolean cached); + public void setSelfAsContext(); } --- NEW FILE: PreorderedValueSequence.java --- /* * eXist Open Source Native XML Database * Copyright (C) 2001-04 Wolfgang M. Meier (wol...@ex...) * and others (see http://exist-db.org) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: PreorderedValueSequence.java,v 1.1 2004/09/12 09:25:15 wolfgang_m Exp $ */ package org.exist.xquery.value; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import org.exist.dom.ContextItem; import org.exist.dom.NodeProxy; import org.exist.dom.NodeSet; import org.exist.xquery.Expression; import org.exist.xquery.OrderSpec; import org.exist.xquery.XPathException; /** * A sequence that sorts its items in the order specified by the order specs * of an "order by" clause. Used by {@link org.exist.xquery.ForExpr}. * * For better performance, the whole input sequence is sorted in one single step. * However, this only works if every order expression returns a result of type * node. * * @author wolf */ public class PreorderedValueSequence extends AbstractSequence { private OrderSpec orderSpecs[]; private OrderedNodeProxy[] nodes; public PreorderedValueSequence(OrderSpec specs[], Sequence input) throws XPathException { this.orderSpecs = specs; nodes = new OrderedNodeProxy[input.getLength()]; int j = 0; for(SequenceIterator i = input.unorderedIterator(); i.hasNext(); j++) { NodeProxy p = (NodeProxy)i.nextItem(); nodes[j] = new OrderedNodeProxy(p); p.addContextNode(nodes[j]); } processAll(); } private void processAll() throws XPathException { long start = System.currentTimeMillis(); for(int i = 0; i < orderSpecs.length; i++) { Expression expr = orderSpecs[i].getSortExpression(); expr.setInPredicate(true); NodeSet result = expr.eval(null).toNodeSet(); for(Iterator j = result.iterator(); j.hasNext(); ) { NodeProxy p = (NodeProxy)j.next(); ContextItem context = p.getContext(); while(context != null) { if(context.getNode() instanceof OrderedNodeProxy) { OrderedNodeProxy cp = (OrderedNodeProxy)context.getNode(); cp.values[i] = p.atomize(); System.out.println(cp.values[i]); } context = context.getNextItem(); } } } System.out.println("Sorting took " + (System.currentTimeMillis() - start)); } /* (non-Javadoc) * @see org.exist.xquery.value.AbstractSequence#getItemType() */ public int getItemType() { return Type.NODE; } /* (non-Javadoc) * @see org.exist.xquery.value.AbstractSequence#iterate() */ public SequenceIterator iterate() { sort(); return new PreorderedValueSequenceIterator(); } /* (non-Javadoc) * @see org.exist.xquery.value.AbstractSequence#unorderedIterator() */ public SequenceIterator unorderedIterator() { return new PreorderedValueSequenceIterator(); } /* (non-Javadoc) * @see org.exist.xquery.value.AbstractSequence#getLength() */ public int getLength() { return nodes.length; } /* (non-Javadoc) * @see org.exist.xquery.value.AbstractSequence#add(org.exist.xquery.value.Item) */ public void add(Item item) throws XPathException { } /* (non-Javadoc) * @see org.exist.xquery.value.AbstractSequence#itemAt(int) */ public Item itemAt(int pos) { return nodes[pos]; } /* (non-Javadoc) * @see org.exist.xquery.value.Sequence#toNodeSet() */ public NodeSet toNodeSet() throws XPathException { return null; } private void sort() { Arrays.sort(nodes, new OrderedComparator()); } private class OrderedComparator implements Comparator { /* (non-Javadoc) * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ public int compare(Object o1, Object o2) { final OrderedNodeProxy p1 = (OrderedNodeProxy)o1; final OrderedNodeProxy p2 = (OrderedNodeProxy)o2; int cmp = 0; AtomicValue a, b; for(int i = 0; i < p1.values.length; i++) { try { a = p1.values[i]; b = p2.values[i]; if(a == AtomicValue.EMPTY_VALUE && b != AtomicValue.EMPTY_VALUE) { if((orderSpecs[i].getModifiers() & OrderSpec.EMPTY_LEAST) != 0) cmp = -1; else cmp = 1; } else if(b == AtomicValue.EMPTY_VALUE && a != AtomicValue.EMPTY_VALUE) { if((orderSpecs[i].getModifiers() & OrderSpec.EMPTY_LEAST) != 0) cmp = 1; else cmp = -1; } else cmp = a.compareTo(b); if((orderSpecs[i].getModifiers() & OrderSpec.DESCENDING_ORDER) != 0) cmp = cmp * -1; if(cmp != 0) break; } catch (XPathException e) { } } return cmp; } } private class OrderedNodeProxy extends NodeProxy { AtomicValue[] values; public OrderedNodeProxy(NodeProxy p) { super(p); values = new AtomicValue[orderSpecs.length]; for(int i = 0; i < values.length; i++) values[i] = AtomicValue.EMPTY_VALUE; } } private class PreorderedValueSequenceIterator implements SequenceIterator { int pos = 0; /* (non-Javadoc) * @see org.exist.xquery.value.SequenceIterator#hasNext() */ public boolean hasNext() { return pos < nodes.length; } /* (non-Javadoc) * @see org.exist.xquery.value.SequenceIterator#nextItem() */ public Item nextItem() { if(pos < nodes.length) return nodes[pos++]; return null; } } } Index: NodeValue.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value/NodeValue.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NodeValue.java 29 Jan 2004 15:06:42 -0000 1.1 --- NodeValue.java 12 Sep 2004 09:25:15 -0000 1.2 *************** *** 23,30 **** package org.exist.xquery.value; - import org.exist.storage.serializers.Serializer; import org.exist.xquery.XPathException; import org.w3c.dom.Node; - import org.xml.sax.SAXException; /** --- 23,28 ---- Index: AtomicValue.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xquery/value/AtomicValue.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** AtomicValue.java 2 Jun 2004 11:34:34 -0000 1.4 --- AtomicValue.java 12 Sep 2004 09:25:15 -0000 1.5 *************** *** 23,27 **** import org.exist.dom.DocumentSet; import org.exist.dom.NodeSet; ! import org.exist.memtree.Receiver; import org.exist.storage.DBBroker; import org.exist.xquery.Cardinality; --- 23,27 ---- import org.exist.dom.DocumentSet; import org.exist.dom.NodeSet; ! import org.exist.memtree.DocumentBuilderReceiver; import org.exist.storage.DBBroker; import org.exist.xquery.Cardinality; *************** *** 127,131 **** } ! public void copyTo(DBBroker broker, Receiver receiver) throws SAXException { try { final String s = getStringValue(); --- 127,131 ---- } ! public void copyTo(DBBroker broker, DocumentBuilderReceiver receiver) throws SAXException { try { final String s = getStringValue(); *************** *** 212,215 **** --- 212,230 ---- /* (non-Javadoc) + * @see org.exist.xquery.value.Sequence#isCached() + */ + public boolean isCached() { + // always returns false by default + return false; + } + + /* (non-Javadoc) + * @see org.exist.xquery.value.Sequence#setIsCached(boolean) + */ + public void setIsCached(boolean cached) { + // ignore + } + + /* (non-Javadoc) * @see org.exist.xquery.value.Sequence#setSelfAsContext() */ |