Search in sources :

Example 21 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class AbstractExistHttpServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    // prepare the database
    try {
        setPool(getOrCreateBrokerPool(config));
    } catch (final EXistException e) {
        throw new ServletException("No database instance available");
    } catch (final DatabaseConfigurationException e) {
        throw new ServletException("Unable to configure database instance: " + e.getMessage(), e);
    }
    // general eXist Servlet config
    doGeneralExistServletConfig(config);
}
Also used : ServletException(javax.servlet.ServletException) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) EXistException(org.exist.EXistException)

Example 22 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class ValueIndexFactory method deserialize.

public static final Indexable deserialize(final byte[] data, final int start, final int len) throws EXistException {
    final int type = data[start];
    /* xs:string */
    if (Type.subTypeOf(type, Type.STRING)) {
        final String s = new String(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE), len - (ValueIndexFactory.LENGTH_VALUE_TYPE), UTF_8);
        return new StringValue(s);
    } else /* xs:dateTime */
    if (Type.subTypeOf(type, Type.DATE_TIME)) {
        try {
            final XMLGregorianCalendar xmlutccal = DatatypeFactory.newInstance().newXMLGregorianCalendar(ByteConversion.byteToIntH(data, start + 1), data[start + 5], data[start + 6], data[start + 7], data[start + 8], data[start + 9], ByteConversion.byteToShortH(data, start + 10), 0);
            return new DateTimeValue(xmlutccal);
        } catch (final DatatypeConfigurationException dtce) {
            throw new EXistException("Could not deserialize xs:dateTime data type" + "for range index key: " + Type.getTypeName(type) + " - " + dtce.getMessage());
        }
    } else /* xs:date */
    if (Type.subTypeOf(type, Type.DATE)) {
        try {
            final XMLGregorianCalendar xmlutccal = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(ByteConversion.byteToIntH(data, start + 1), data[start + 5], data[start + 6], 0);
            return new DateValue(xmlutccal);
        } catch (final DatatypeConfigurationException | XPathException dtce) {
            throw new EXistException("Could not deserialize xs:date data type" + " for range index key: " + Type.getTypeName(type) + " - " + dtce.getMessage());
        }
    } else /* xs:integer */
    if (Type.subTypeOf(type, Type.INTEGER)) {
        return new IntegerValue(ByteConversion.byteToLong(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE)) ^ 0x8000000000000000L);
    } else /* xs:double */
    if (type == Type.DOUBLE) {
        final long bits = ByteConversion.byteToLong(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE)) ^ 0x8000000000000000L;
        final double d = Double.longBitsToDouble(bits);
        return new DoubleValue(d);
    } else /* xs:float */
    if (type == Type.FLOAT) {
        final int bits = ByteConversion.byteToInt(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE)) ^ 0x80000000;
        final float f = Float.intBitsToFloat(bits);
        return new FloatValue(f);
    } else /* xs:decimal */
    if (type == Type.DECIMAL) {
        // actually loaded from string data due to the uncertain length
        final String s = new String(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE), len - (ValueIndexFactory.LENGTH_VALUE_TYPE), UTF_8);
        return new DecimalValue(new BigDecimal(s));
    } else /* xs:boolean */
    if (type == Type.BOOLEAN) {
        return new BooleanValue(data[start + (ValueIndexFactory.LENGTH_VALUE_TYPE)] == 1);
    } else /* unknown! */
    {
        throw new EXistException("Unknown data type for deserialization: " + Type.getTypeName(type));
    }
}
Also used : XPathException(org.exist.xquery.XPathException) EXistException(org.exist.EXistException) BigDecimal(java.math.BigDecimal) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException)

Example 23 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class ValueIndexFactory method serialize.

public static final byte[] serialize(final Indexable value, final int offset, final boolean caseSensitive) throws EXistException {
    /* xs:string */
    if (Type.subTypeOf(value.getType(), Type.STRING)) {
        final String val = caseSensitive ? ((StringValue) value).getStringValue() : ((StringValue) value).getStringValue().toLowerCase();
        final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + UTF8.encoded(val)];
        // TODO: cast to byte is not safe
        data[offset] = (byte) value.getType();
        UTF8.encode(val, data, offset + ValueIndexFactory.LENGTH_VALUE_TYPE);
        return data;
    } else /* xs:dateTime */
    if (Type.subTypeOf(value.getType(), Type.DATE_TIME)) {
        final XMLGregorianCalendar utccal = ((AbstractDateTimeValue) value).calendar.normalize();
        // allocate an appropriately sized
        final byte[] data = new byte[offset + 12];
        // put the type in the byte array
        data[offset] = (byte) Type.DATE_TIME;
        ByteConversion.intToByteH(utccal.getYear(), data, offset + 1);
        data[offset + 5] = (byte) utccal.getMonth();
        data[offset + 6] = (byte) utccal.getDay();
        data[offset + 7] = (byte) utccal.getHour();
        data[offset + 8] = (byte) utccal.getMinute();
        data[offset + 9] = (byte) utccal.getSecond();
        final int ms = utccal.getMillisecond();
        ByteConversion.shortToByteH((short) (ms == DatatypeConstants.FIELD_UNDEFINED ? 0 : ms), data, offset + 10);
        // return the byte array
        return (data);
    } else /* xs:date */
    if (Type.subTypeOf(value.getType(), Type.DATE)) {
        final XMLGregorianCalendar utccal = ((AbstractDateTimeValue) value).calendar.normalize();
        // allocate an appropriately sized
        final byte[] data = new byte[offset + 7];
        data[offset] = (byte) Type.DATE;
        ByteConversion.intToByteH(utccal.getYear(), data, offset + 1);
        data[offset + 5] = (byte) utccal.getMonth();
        data[offset + 6] = (byte) utccal.getDay();
        return data;
    } else /* xs:integer */
    if (Type.subTypeOf(value.getType(), Type.INTEGER)) {
        final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + 8];
        data[offset] = (byte) Type.INTEGER;
        final long l = ((IntegerValue) value).getValue() - Long.MIN_VALUE;
        ByteConversion.longToByte(l, data, offset + ValueIndexFactory.LENGTH_VALUE_TYPE);
        return data;
    } else /* xs:double */
    if (value.getType() == Type.DOUBLE) {
        final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + 8];
        data[offset] = (byte) Type.DOUBLE;
        final long bits = Double.doubleToLongBits(((DoubleValue) value).getValue()) ^ 0x8000000000000000L;
        ByteConversion.longToByte(bits, data, offset + ValueIndexFactory.LENGTH_VALUE_TYPE);
        return data;
    } else /* xs:float */
    if (value.getType() == Type.FLOAT) {
        final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + 4];
        data[offset] = (byte) Type.FLOAT;
        final int bits = Float.floatToIntBits(((FloatValue) value).getValue()) ^ 0x80000000;
        ByteConversion.intToByteH(bits, data, offset + ValueIndexFactory.LENGTH_VALUE_TYPE);
        return data;
    } else /* xs:boolean */
    if (value.getType() == Type.BOOLEAN) {
        final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + 1];
        data[offset] = Type.BOOLEAN;
        data[offset + ValueIndexFactory.LENGTH_VALUE_TYPE] = (byte) (((BooleanValue) value).getValue() ? 1 : 0);
        return data;
    } else if (value.getType() == Type.DECIMAL) {
        // actually stored as string data due to variable length
        final BigDecimal dec = ((DecimalValue) value).getValue();
        final String val = dec.toString();
        final byte[] data = new byte[offset + ValueIndexFactory.LENGTH_VALUE_TYPE + UTF8.encoded(val)];
        // TODO: cast to byte is not safe
        data[offset] = (byte) value.getType();
        UTF8.encode(val, data, offset + ValueIndexFactory.LENGTH_VALUE_TYPE);
        return data;
    } else /* unknown! */
    {
        throw new EXistException("Unknown data type for serialization: " + Type.getTypeName(value.getType()));
    }
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) EXistException(org.exist.EXistException) BigDecimal(java.math.BigDecimal)

Example 24 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class Repair method startDB.

private void startDB() {
    try {
        Configuration config = new Configuration();
        BrokerPool.configure(1, 5, config);
        pool = BrokerPool.getInstance();
    } catch (DatabaseConfigurationException | EXistException e) {
        e.printStackTrace();
    }
}
Also used : Configuration(org.exist.util.Configuration) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) EXistException(org.exist.EXistException)

Example 25 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class GeneralComparison method preSelect.

public NodeSet preSelect(Sequence contextSequence, boolean useContext) throws XPathException {
    // the expression can be called multiple times, so we need to clear the previous preselectResult
    preselectResult = null;
    final long start = System.currentTimeMillis();
    final int indexType = Optimize.getQNameIndexType(context, contextSequence, contextQName);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Using QName index on type {}", Type.getTypeName(indexType));
    }
    final Sequence rightSeq = getRight().eval(contextSequence);
    // into preselectResult
    if (rightSeq.getItemCount() > 1) {
        preselectResult = new NewArrayNodeSet();
    }
    // Iterate through each item in the right-hand sequence
    for (final SequenceIterator itRightSeq = Atomize.atomize(rightSeq).iterate(); itRightSeq.hasNext(); ) {
        // Get the index key
        Item key = itRightSeq.nextItem();
        // if key has truncation, convert it to string
        if (truncation != StringTruncationOperator.NONE) {
            if (!Type.subTypeOf(key.getType(), Type.STRING)) {
                LOG.info("Truncated key. Converted from {} to xs:string", Type.getTypeName(key.getType()));
                // truncation is only possible on strings
                key = key.convertTo(Type.STRING);
            }
        } else // TODO : use Type.isSubType() ??? -pb
        if (key.getType() != indexType) {
            // try to convert the key to the index type
            try {
                key = key.convertTo(indexType);
            } catch (final XPathException xpe) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Cannot convert key: {} to required index type: {}", Type.getTypeName(key.getType()), Type.getTypeName(indexType));
                }
                throw (new XPathException(this, "Cannot convert key to required index type"));
            }
        }
        // If key implements org.exist.storage.Indexable, we can use the index
        if (key instanceof Indexable) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Using QName range index for key: {}", key.getStringValue());
            }
            NodeSet temp;
            final NodeSet contextSet = useContext ? contextSequence.toNodeSet() : null;
            final Collator collator = ((collationArg != null) ? getCollator(contextSequence) : null);
            if (truncation == StringTruncationOperator.NONE) {
                temp = context.getBroker().getValueIndex().find(context.getWatchDog(), relation, contextSequence.getDocumentSet(), contextSet, NodeSet.DESCENDANT, contextQName, (Indexable) key);
                hasUsedIndex = true;
            } else {
                try {
                    final String matchString = key.getStringValue();
                    final int matchType = getMatchType(truncation);
                    temp = context.getBroker().getValueIndex().match(context.getWatchDog(), contextSequence.getDocumentSet(), contextSet, NodeSet.DESCENDANT, matchString, contextQName, matchType, collator, truncation);
                    hasUsedIndex = true;
                } catch (final EXistException e) {
                    throw (new XPathException(this, "Error during index lookup: " + e.getMessage(), e));
                }
            }
            // else replace it.
            if (preselectResult == null) {
                preselectResult = temp;
            } else {
                preselectResult.addAll(temp);
            }
        }
    }
    if (context.getProfiler().traceFunctions()) {
        context.getProfiler().traceIndexUsage(context, PerformanceStats.RANGE_IDX_TYPE, this, PerformanceStats.OPTIMIZED_INDEX, System.currentTimeMillis() - start);
    }
    return ((preselectResult == null) ? NodeSet.EMPTY_SET : preselectResult);
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) VirtualNodeSet(org.exist.dom.persistent.VirtualNodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) Item(org.exist.xquery.value.Item) ContextItem(org.exist.dom.persistent.ContextItem) SequenceIterator(org.exist.xquery.value.SequenceIterator) Indexable(org.exist.storage.Indexable) Sequence(org.exist.xquery.value.Sequence) EXistException(org.exist.EXistException) Collator(com.ibm.icu.text.Collator)

Aggregations

EXistException (org.exist.EXistException)218 PermissionDeniedException (org.exist.security.PermissionDeniedException)80 IOException (java.io.IOException)63 DBBroker (org.exist.storage.DBBroker)58 Txn (org.exist.storage.txn.Txn)46 XmldbURI (org.exist.xmldb.XmldbURI)42 Collection (org.exist.collections.Collection)41 SAXException (org.xml.sax.SAXException)32 LockException (org.exist.util.LockException)31 DocumentImpl (org.exist.dom.persistent.DocumentImpl)28 Subject (org.exist.security.Subject)23 XPathException (org.exist.xquery.XPathException)22 LockedDocument (org.exist.dom.persistent.LockedDocument)21 TriggerException (org.exist.collections.triggers.TriggerException)20 Path (java.nio.file.Path)19 URISyntaxException (java.net.URISyntaxException)18 BrokerPool (org.exist.storage.BrokerPool)18 TransactionManager (org.exist.storage.txn.TransactionManager)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17