Author: shawkins
Date: 2011-11-13 08:03:20 -0500 (Sun, 13 Nov 2011)
New Revision: 3640
Modified:
trunk/common-core/src/main/java/org/teiid/core/types/SQLXMLImpl.java
trunk/common-core/src/main/java/org/teiid/core/types/XMLType.java
trunk/engine/src/main/java/org/teiid/common/buffer/FileStoreInputStreamFactory.java
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java
trunk/engine/src/main/java/org/teiid/query/mapping/xml/MappingOutputter.java
Log:
TEIID-1824 performance enhancement for sql/xml
Modified: trunk/common-core/src/main/java/org/teiid/core/types/SQLXMLImpl.java
===================================================================
--- trunk/common-core/src/main/java/org/teiid/core/types/SQLXMLImpl.java 2011-11-12
12:46:01 UTC (rev 3639)
+++ trunk/common-core/src/main/java/org/teiid/core/types/SQLXMLImpl.java 2011-11-13
13:03:20 UTC (rev 3640)
@@ -117,7 +117,7 @@
if (sourceClass == null || sourceClass == StreamSource.class) {
return (T)new StreamSource(getBinaryStream(), this.getStreamFactory().getSystemId());
} else if (sourceClass == StAXSource.class) {
- XMLInputFactory factory = XMLInputFactory.newInstance();
+ XMLInputFactory factory = XMLType.getXmlInputFactory();
try {
return (T) new StAXSource(factory.createXMLStreamReader(getBinaryStream()));
} catch (XMLStreamException e) {
Modified: trunk/common-core/src/main/java/org/teiid/core/types/XMLType.java
===================================================================
--- trunk/common-core/src/main/java/org/teiid/core/types/XMLType.java 2011-11-12 12:46:01
UTC (rev 3639)
+++ trunk/common-core/src/main/java/org/teiid/core/types/XMLType.java 2011-11-13 13:03:20
UTC (rev 3640)
@@ -54,9 +54,31 @@
private static final long serialVersionUID = -7922647237095135723L;
+ private static ThreadLocal<XMLInputFactory> threadLocalFactory = new
ThreadLocal<XMLInputFactory>() {
+ protected XMLInputFactory initialValue() {
+ return XMLInputFactory.newInstance();
+ }
+ };
+ private static XMLInputFactory factory = XMLInputFactory.newInstance();
+ private static Boolean factoriesTreadSafe;
+
private transient Type type = Type.UNKNOWN;
private String encoding;
-
+
+ public static boolean isThreadSafeXmlFactories() {
+ if (factoriesTreadSafe == null) {
+ factoriesTreadSafe = factory.getClass().getName().contains(".wstx.");
//$NON-NLS-1$
+ }
+ return factoriesTreadSafe;
+ }
+
+ public static XMLInputFactory getXmlInputFactory() {
+ if (isThreadSafeXmlFactories()) {
+ return factory;
+ }
+ return threadLocalFactory.get();
+ }
+
public XMLType(){
}
@@ -175,7 +197,6 @@
}
public static String getEncoding(InputStream is) {
- XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader;
try {
reader = factory.createXMLStreamReader(is);
Modified:
trunk/engine/src/main/java/org/teiid/common/buffer/FileStoreInputStreamFactory.java
===================================================================
---
trunk/engine/src/main/java/org/teiid/common/buffer/FileStoreInputStreamFactory.java 2011-11-12
12:46:01 UTC (rev 3639)
+++
trunk/engine/src/main/java/org/teiid/common/buffer/FileStoreInputStreamFactory.java 2011-11-13
13:03:20 UTC (rev 3640)
@@ -26,12 +26,14 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
+import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import org.teiid.common.buffer.FileStore.FileStoreOutputStream;
import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.InputStreamFactory;
+import org.teiid.core.util.InputStreamReader;
public final class FileStoreInputStreamFactory extends InputStreamFactory {
private final FileStore lobBuffer;
@@ -51,6 +53,11 @@
}
return lobBuffer.createInputStream(0);
}
+
+ @Override
+ public Reader getCharacterStream() throws IOException {
+ return new InputStreamReader(getInputStream(),
Charset.forName(encoding).newDecoder());
+ }
@Override
public long getLength() {
Modified:
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java
===================================================================
---
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java 2011-11-12
12:46:01 UTC (rev 3639)
+++
trunk/engine/src/main/java/org/teiid/query/function/source/XMLSystemFunctions.java 2011-11-13
13:03:20 UTC (rev 3640)
@@ -234,9 +234,39 @@
return true;
}
}
+
+ private static ThreadLocal<TransformerFactory> threadLocalTransformerFactory = new
ThreadLocal<TransformerFactory>() {
+ protected TransformerFactory initialValue() {
+ return TransformerFactory.newInstance();
+ }
+ };
+ static ThreadLocal<XMLOutputFactory> threadLocalOutputFactory = new
ThreadLocal<XMLOutputFactory>() {
+ protected XMLOutputFactory initialValue() {
+ return newXmlOutputFactory();
+ }
+ };
+ static ThreadLocal<XMLEventFactory> threadLocalEventtFactory = new
ThreadLocal<XMLEventFactory>() {
+ protected XMLEventFactory initialValue() {
+ return XMLEventFactory.newInstance();
+ }
+ };
+ private static final String P_OUTPUT_VALIDATE_STRUCTURE =
"com.ctc.wstx.outputValidateStructure"; //$NON-NLS-1$
+ static XMLOutputFactory newXmlOutputFactory() throws FactoryConfigurationError {
+ XMLOutputFactory factory = XMLOutputFactory.newInstance();
+ if (factory.isPropertySupported(P_OUTPUT_VALIDATE_STRUCTURE)) {
+ factory.setProperty(P_OUTPUT_VALIDATE_STRUCTURE, false);
+ }
+ return factory;
+ }
+ static XMLOutputFactory xmlOutputFactory = newXmlOutputFactory();
+
+ public static XMLOutputFactory getOutputFactory() throws FactoryConfigurationError {
+ if (XMLType.isThreadSafeXmlFactories()) {
+ return xmlOutputFactory;
+ }
+ return threadLocalOutputFactory.get();
+ }
- private static final String P_OUTPUT_VALIDATE_STRUCTURE =
"com.ctc.wstx.outputValidateStructure"; //$NON-NLS-1$
-
public static ClobType xslTransform(CommandContext context, Object xml, Object
styleSheet) throws Exception {
Source styleSource = null;
Source xmlSource = null;
@@ -244,7 +274,7 @@
styleSource = convertToSource(styleSheet);
xmlSource = convertToSource(xml);
final Source xmlParam = xmlSource;
- TransformerFactory factory = TransformerFactory.newInstance();
+ TransformerFactory factory = threadLocalTransformerFactory.get();
final Transformer transformer = factory.newTransformer(styleSource);
//this creates a non-validated sqlxml - it may not be valid xml/root-less xml
@@ -284,7 +314,7 @@
try {
XMLOutputFactory factory = getOutputFactory();
XMLEventWriter eventWriter = factory.createXMLEventWriter(writer);
- XMLEventFactory eventFactory = XMLEventFactory.newInstance();
+ XMLEventFactory eventFactory = threadLocalEventtFactory.get();
for (Evaluator.NameValuePair nameValuePair : values) {
if (nameValuePair.value == null) {
continue;
@@ -320,7 +350,7 @@
try {
XMLOutputFactory factory = getOutputFactory();
XMLEventWriter eventWriter = factory.createXMLEventWriter(writer);
- XMLEventFactory eventFactory = XMLEventFactory.newInstance();
+ XMLEventFactory eventFactory = threadLocalEventtFactory.get();
addElement(name, writer, eventWriter, eventFactory, namespaces, attributes,
contents);
eventWriter.close();
} catch (XMLStreamException e) {
@@ -465,14 +495,6 @@
}
- private static XMLOutputFactory getOutputFactory() throws FactoryConfigurationError {
- XMLOutputFactory factory = XMLOutputFactory.newInstance();
- if (factory.isPropertySupported(P_OUTPUT_VALIDATE_STRUCTURE)) {
- factory.setProperty(P_OUTPUT_VALIDATE_STRUCTURE, false);
- }
- return factory;
- }
-
public static XMLType xmlPi(String name) {
return xmlPi(name, ""); //$NON-NLS-1$
}
@@ -555,7 +577,7 @@
}
case UNKNOWN: //assume a document
case DOCUMENT: //filter the doc declaration
- XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+ XMLInputFactory inputFactory = XMLType.getXmlInputFactory();
if (!(r instanceof BufferedReader)) {
r = new BufferedReader(r);
}
@@ -569,7 +591,7 @@
eventWriter.add(eventReader);
break;
case TEXT:
- XMLEventFactory eventFactory = XMLEventFactory.newInstance();
+ XMLEventFactory eventFactory = threadLocalEventtFactory.get();
char[] buf = new char[1 << 13];
int read = -1;
while ((read = r.read(buf)) != -1) {
Modified: trunk/engine/src/main/java/org/teiid/query/mapping/xml/MappingOutputter.java
===================================================================
---
trunk/engine/src/main/java/org/teiid/query/mapping/xml/MappingOutputter.java 2011-11-12
12:46:01 UTC (rev 3639)
+++
trunk/engine/src/main/java/org/teiid/query/mapping/xml/MappingOutputter.java 2011-11-13
13:03:20 UTC (rev 3640)
@@ -35,6 +35,7 @@
import javax.xml.stream.XMLStreamWriter;
import org.teiid.query.QueryPlugin;
+import org.teiid.query.function.source.XMLSystemFunctions;
/**
@@ -59,7 +60,7 @@
*/
public void write(MappingDocument doc, PrintWriter stream) throws IOException {
try {
- XMLOutputFactory xof = XMLOutputFactory.newInstance();
+ XMLOutputFactory xof = XMLSystemFunctions.getOutputFactory();
writer = xof.createXMLStreamWriter(stream);
writer.writeStartDocument("UTF-8", "1.0"); //$NON-NLS-1$
//$NON-NLS-2$
writer.writeStartElement(ELEM_ROOT);
Show replies by date