Author: vgritsenko
Date: Sun Nov 26 14:44:04 2006
New Revision: 479437
URL: http://svn.apache.org/viewvc?view=rev&rev=479437
Log:
<action dev="VG" type="update" fixes-bug="41003" due-to="Natalia Shilenkova">
Change InlineMetaReader.read() method signature to remove
unnecessary data copying.
</action>
Modified:
xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java
xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaReader.java
xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaService.java
xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/NullReader.java
xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/ResourceTypeReader.java
xml/xindice/trunk/java/tests/src/org/apache/xindice/core/meta/inline/ResourceTypeReaderTest.java
xml/xindice/trunk/status.xml
Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java?view=diff&rev=479437&r1=479436&r2=479437
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/data/Value.java Sun Nov 26 14:44:04 2006
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * CVS $Id$
+ * $Id$
*/
package org.apache.xindice.core.data;
@@ -31,13 +31,13 @@
* The content window of Value objects are immutable, but the
* underlying byte array is not.
*
- * @version CVS $Revision$, $Date$
+ * @version $Revision$, $Date$
*/
public class Value implements Comparable {
- protected byte[] data = null;
- protected int pos = 0;
- protected int len = -1;
+ protected byte[] data;
+ protected int pos;
+ protected int len;
public Value(Value value) {
@@ -48,10 +48,14 @@
public Value(byte[] data) {
this.data = data;
+ this.pos = 0;
this.len = data.length;
}
public Value(byte[] data, int pos, int len) {
+ if (pos >= data.length || pos < 0 || pos + len > data.length) {
+ throw new ArrayIndexOutOfBoundsException("Value cannot be created");
+ }
this.data = data;
this.pos = pos;
this.len = len;
@@ -60,6 +64,7 @@
public Value(String data) {
try {
this.data = data.getBytes("utf-8");
+ this.pos = 0;
this.len = this.data.length;
} catch (UnsupportedEncodingException e) {
throw new XindiceRuntimeException("Java doesn't support UTF-8 encoding", e);
@@ -82,6 +87,34 @@
}
/**
+ * Returns the byte at the specified index.
+ *
+ * @param index byte index
+ * @return the byte at the specified index.
+ * @throws ArrayIndexOutOfBoundsException if index is negative number or
+ * is not less that the length of Value data
+ */
+ public final byte byteAt(int index) {
+ if (index < 0 || index >= len) {
+ throw new ArrayIndexOutOfBoundsException(index);
+ }
+ return data[pos + index];
+ }
+
+ /**
+ * Get a new Value that is part of this Value object.
+ *
+ * @param start beginning index
+ * @param len length of the new Value
+ * @return Value object
+ * @throws ArrayIndexOutOfBoundsException if start index is either negative
+ * or isn't less then length of original Value
+ */
+ public final Value getSubvalue(int start, int len) {
+ return new Value(data, start, len);
+ }
+
+ /**
* getLength retrieves the length of the data being stored by the Value.
*
* @return The Value length
@@ -129,7 +162,7 @@
}
public boolean equals(Value value) {
- return len == value.len ? compareTo(value) == 0 : false;
+ return len == value.len && compareTo(value) == 0;
}
public boolean equals(Object obj) {
Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaReader.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaReader.java?view=diff&rev=479437&r1=479436&r2=479437
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaReader.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaReader.java Sun Nov 26 14:44:04 2006
@@ -13,11 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * CVS $Id$
+ * $Id$
*/
package org.apache.xindice.core.meta.inline;
+import org.apache.xindice.core.data.Value;
+
/**
* The <code>Value</code> associated with a <code>Record</code>
* can be prefixed by a header containing 'inline' metadata.
@@ -25,7 +27,7 @@
* this extension is to support binary resources in the same
* filer where the XML documents live.
*
- * @version CVS $Revision$, $Date$
+ * @version $Revision$, $Date$
*/
public interface InlineMetaReader {
@@ -40,13 +42,10 @@
* When a header is read, it generates a <code>Map</code>
* containing the attributes carried by the header.
*
- * @param data array in which the header is embedded
- * @param offset from the beginning of the data array to the
- * beginning of the header.
- * @param length of the header data
+ * @param data Value object in which the header is embedded
* @return Map containing the attributes read from the header
* @throws InlineMetaException if the header data is corrupted or of
* the wrong length
*/
- InlineMetaMap read(byte[] data, int offset, int length) throws InlineMetaException;
+ InlineMetaMap read(Value data) throws InlineMetaException;
}
Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaService.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaService.java?view=diff&rev=479437&r1=479436&r2=479437
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaService.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/InlineMetaService.java Sun Nov 26 14:44:04 2006
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * CVS $Id$
+ * $Id$
*/
package org.apache.xindice.core.meta.inline;
@@ -22,18 +22,17 @@
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.FaultCodes;
import org.apache.xindice.core.data.Value;
-import org.apache.xindice.core.data.Record;
/**
* If the documents in a collection have headers, the Collection
* object holds an instance of this class. All services the collection
* needs for working with inline metadata are provided by this class.
*
- * @version CVS $Revision$, $Date$
+ * @version $Revision$, $Date$
*/
public class InlineMetaService {
- private static Log log = LogFactory.getLog(InlineMetaService.class);
+ private static final Log log = LogFactory.getLog(InlineMetaService.class);
/**
* The known readers. All readers for header versions up to
@@ -119,22 +118,24 @@
* the header is corrupted.
*/
public DatabaseEntry readDatabaseEntry(Value rawValue) throws InlineMetaException {
- byte[] rawData = rawValue.getData();
if (log.isDebugEnabled()) {
- log.debug("readDatabaseEntry: rawData: length=" + rawData.length + " byte 0: " + rawData[0] + " byte 1: " + rawData[1]);
+ log.debug("readDatabaseEntry: rawData: length=" + rawValue.getLength() +
+ " byte 0: " + rawValue.byteAt(0) + " byte 1: " + rawValue.byteAt(1));
}
/*
* Read the header.
*/
- int version = rawData[1];
+ int headerLen = rawValue.byteAt(0);
+ int version = rawValue.byteAt(1);
if (!haveReaderForVersion(version)) {
throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR,
"No inline metadata reader available for version " + version);
}
+
final InlineMetaReader reader = readerByVersion[version];
- InlineMetaMap map = reader.read(rawData, 2, rawData[0] - 2);
+ InlineMetaMap map = reader.read(rawValue.getSubvalue(2, headerLen - 2));
if (log.isDebugEnabled()) {
log.debug("readDatabaseEntry: map: type=" + map.get("type"));
}
@@ -143,7 +144,7 @@
* Exract the data into a Value object.
*/
- Value value = new Value(rawData, rawData[0], rawData.length - rawData[0]);
+ Value value = rawValue.getSubvalue(headerLen, rawValue.getLength() - headerLen);
// FIXME: May be Record should be used instead? new Record(null, value, map);
return new DatabaseEntry(map, value);
}
Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/NullReader.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/NullReader.java?view=diff&rev=479437&r1=479436&r2=479437
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/NullReader.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/NullReader.java Sun Nov 26 14:44:04 2006
@@ -13,19 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * CVS $Id$
+ * $Id$
*/
package org.apache.xindice.core.meta.inline;
import org.apache.xindice.core.FaultCodes;
+import org.apache.xindice.core.data.Value;
/**
* Read metadata of length zero. Handy for comparing performance
* of the metadata-free database with a database using the metadata
* machinery but containing no metadata.
*
- * @version CVS $Revision$, $Date$
+ * @version $Revision$, $Date$
*/
public class NullReader implements InlineMetaReader {
@@ -37,12 +38,12 @@
}
/**
- * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(byte[], int, int)
+ * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(Value)
*/
- public InlineMetaMap read(byte[] data, int offset, int length) throws InlineMetaException {
-
- if (length != 0) {
- throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED, "Expecting header length of 0");
+ public InlineMetaMap read(Value data) throws InlineMetaException {
+ if (data.getLength() != 0) {
+ throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED,
+ "Expecting header length of 0");
}
return new NullMap();
@@ -67,7 +68,8 @@
* @see org.apache.xindice.core.meta.inline.InlineMetaMap#get(String)
*/
public Object get(String key) throws InlineMetaException {
- throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR, "NullMap does not accept key '" + key + "'");
+ throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR,
+ "NullMap does not accept key '" + key + "'");
}
/**
@@ -81,8 +83,8 @@
* @see org.apache.xindice.core.meta.inline.InlineMetaMap#put(String,Object)
*/
public void put(String key, Object value) throws InlineMetaException {
- throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR, "NullMap does not accept key '" + key + "'");
+ throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR,
+ "NullMap does not accept key '" + key + "'");
}
}
-
}
Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/ResourceTypeReader.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/ResourceTypeReader.java?view=diff&rev=479437&r1=479436&r2=479437
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/ResourceTypeReader.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/core/meta/inline/ResourceTypeReader.java Sun Nov 26 14:44:04 2006
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * CVS $Id$
+ * $Id$
*/
package org.apache.xindice.core.meta.inline;
@@ -21,18 +21,20 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.FaultCodes;
+import org.apache.xindice.core.data.Value;
/**
*
- * @version CVS $Revision$, $Date$
+ * @version $Revision$, $Date$
*/
public class ResourceTypeReader implements InlineMetaReader {
- private static Log log = LogFactory.getLog(ResourceTypeReader.class);
+ private static final Log log = LogFactory.getLog(ResourceTypeReader.class);
- public static final Integer XML = new Integer(1);
+ public static final Integer XML = new Integer(1);
public static final Integer BINARY = new Integer(2);
+
/**
* @see org.apache.xindice.core.meta.inline.InlineMetaReader#getVersion()
*/
@@ -41,29 +43,24 @@
}
/**
- * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(byte[], int, int)
+ * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(Value)
*/
- public InlineMetaMap read(byte[] data, int offset, int length) throws InlineMetaException {
+ public InlineMetaMap read(Value data) throws InlineMetaException {
if (log.isDebugEnabled()) {
- log.debug("ResourceTypeReader.read: data length=" + data.length + " offset=" + offset + " length=" + length);
+ log.debug("ResourceTypeReader.read: data length=" + data.getLength());
}
- if (length != 1) {
- throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED, "Expecting header length of 1");
+ if (data.getLength() != 1) {
+ throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED,
+ "Expecting header length of 1");
}
Integer type;
- try {
- type = new Integer(data[offset]);
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new InlineMetaException(
- FaultCodes.COL_DOCUMENT_MALFORMED,
- "Error reading from data (data length " + data.length + ", offset=" + offset + ", header length " + length + ")",
- e);
- }
+ type = new Integer(data.byteAt(0));
if (!XML.equals(type) && !BINARY.equals(type)) {
- throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED, "Unexpected type value: " + type);
+ throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED,
+ "Unexpected type value: " + type);
}
ResourceTypeMap resourceTypeMap = new ResourceTypeMap();
@@ -95,7 +92,8 @@
if ("type".equals(key)) {
return type;
} else {
- throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR, "ResourceTypeMap does not accept key '" + key + "'");
+ throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR,
+ "ResourceTypeMap does not accept key '" + key + "'");
}
}
@@ -114,10 +112,12 @@
if (value instanceof Integer) {
type = (Integer) value;
} else {
- throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR, "ResourceTypeMap key 'type' requires an Integer value");
+ throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR,
+ "ResourceTypeMap key 'type' requires an Integer value");
}
} else {
- throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR, "ResourceTypeMap does not accept key '" + key + "'");
+ throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR,
+ "ResourceTypeMap does not accept key '" + key + "'");
}
}
}
Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/core/meta/inline/ResourceTypeReaderTest.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/core/meta/inline/ResourceTypeReaderTest.java?view=diff&rev=479437&r1=479436&r2=479437
==============================================================================
--- xml/xindice/trunk/java/tests/src/org/apache/xindice/core/meta/inline/ResourceTypeReaderTest.java (original)
+++ xml/xindice/trunk/java/tests/src/org/apache/xindice/core/meta/inline/ResourceTypeReaderTest.java Sun Nov 26 14:44:04 2006
@@ -13,19 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
- * CVS $Id$
+ * $Id$
*/
package org.apache.xindice.core.meta.inline;
-import org.apache.xindice.core.meta.inline.InlineMetaException;
-import org.apache.xindice.core.meta.inline.InlineMetaMap;
-import org.apache.xindice.core.meta.inline.ResourceTypeReader;
+import org.apache.xindice.core.data.Value;
import junit.framework.TestCase;
/**
- * @version CVS $Revision$, $Date$
+ *
+ * @version $Revision$, $Date$
* @author Gary Shea <[email protected]>
*/
public class ResourceTypeReaderTest extends TestCase {
@@ -43,14 +42,14 @@
InlineMetaMap map;
- map = reader.read(binaryData, 2, 1);
+ map = reader.read(new Value(binaryData, 2, 1));
assertEquals(ResourceTypeReader.BINARY, map.get("type"));
- map = reader.read(xmlData, 2, 1);
+ map = reader.read(new Value(xmlData, 2, 1));
assertEquals(ResourceTypeReader.XML, map.get("type"));
try {
- reader.read(evilData, 2, 1);
+ reader.read(new Value(evilData, 2, 1));
fail("failed to throw InlineMetaException on bad type value (3)");
} catch (InlineMetaException e) {
// expected exception
Modified: xml/xindice/trunk/status.xml
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/status.xml?view=diff&rev=479437&r1=479436&r2=479437
==============================================================================
--- xml/xindice/trunk/status.xml (original)
+++ xml/xindice/trunk/status.xml Sun Nov 26 14:44:04 2006
@@ -75,6 +75,10 @@
<changes>
<release version="1.1b5-dev" date="Oct 27 2006">
+ <action dev="VG" type="update" fixes-bug="41003" due-to="Natalia Shilenkova">
+ Change InlineMetaReader.read() method signature to remove
+ unnecessary data copying.
+ </action>
<action dev="VG" type="update">
Update Ant to 1.6.5 version.
</action>
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.