Search in sources :

Example 6 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class GMLHSQLIndexWorker method getGeometricPropertyForNodes.

@Override
protected ValueSequence getGeometricPropertyForNodes(XQueryContext context, NodeSet contextSet, Connection conn, String propertyName) throws SQLException, XPathException {
    // TODO : generate it in AbstractGMLJDBCIndexWorker
    String docConstraint = "";
    boolean refine_query_on_doc = false;
    if (contextSet != null) {
        if (contextSet.getDocumentSet().getDocumentCount() <= index.getMaxDocsInContextToRefineQuery()) {
            DocumentImpl doc;
            Iterator<DocumentImpl> it = contextSet.getDocumentSet().getDocumentIterator();
            doc = it.next();
            docConstraint = "(DOCUMENT_URI = '" + doc.getURI().toString() + "')";
            while (it.hasNext()) {
                doc = it.next();
                docConstraint = docConstraint + " OR (DOCUMENT_URI = '" + doc.getURI().toString() + "')";
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Refine query on documents is enabled.");
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Refine query on documents is disabled.");
            }
        }
    }
    PreparedStatement ps = conn.prepareStatement("SELECT " + propertyName + ", DOCUMENT_URI, NODE_ID_UNITS, NODE_ID" + " FROM " + GMLHSQLIndex.TABLE_NAME + (refine_query_on_doc ? " WHERE " + docConstraint : ""));
    ResultSet rs = null;
    try {
        rs = ps.executeQuery();
        ValueSequence result;
        if (contextSet == null)
            result = new ValueSequence();
        else
            result = new ValueSequence(contextSet.getLength());
        while (rs.next()) {
            DocumentImpl doc = null;
            try {
                doc = (DocumentImpl) broker.getXMLResource(XmldbURI.create(rs.getString("DOCUMENT_URI")));
            } catch (PermissionDeniedException e) {
                LOG.debug(e);
                // Untested, but that is roughly what should be returned.
                if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else
                    throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
                // Ignore since the broker has no right on the document
                continue;
            }
            if (contextSet.getDocumentSet().contains(doc.getDocId())) {
                NodeId nodeId = new DLN(rs.getInt("NODE_ID_UNITS"), rs.getBytes("NODE_ID"), 0);
                NodeProxy p = new NodeProxy(doc, nodeId);
                // VirtualNodeSet when on the DESCENDANT_OR_SELF axis
                if (contextSet.get(p) != null) {
                    if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
                        result.add(new BooleanValue(rs.getBoolean(1)));
                    } else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
                        result.add(new DoubleValue(rs.getDouble(1)));
                    } else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
                        result.add(new StringValue(rs.getString(1)));
                    } else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
                        result.add(BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(rs.getBytes(1))));
                    } else
                        throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
                }
            }
        }
        return result;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
    }
}
Also used : DLN(org.exist.numbering.DLN) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) DoubleValue(org.exist.xquery.value.DoubleValue) BooleanValue(org.exist.xquery.value.BooleanValue) ValueSequence(org.exist.xquery.value.ValueSequence) NodeId(org.exist.numbering.NodeId) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) StringValue(org.exist.xquery.value.StringValue)

Example 7 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class FunGeometricProperties method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence result = null;
    Sequence nodes = args[0];
    if (nodes.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        try {
            Geometry geometry = null;
            String sourceCRS = null;
            AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
            if (indexWorker == null) {
                logger.error("Unable to find a spatial index worker");
                throw new XPathException(this, "Unable to find a spatial index worker");
            }
            String propertyName = null;
            if (isCalledAs("getWKT")) {
                propertyName = "WKT";
            } else if (isCalledAs("getWKB")) {
                propertyName = "WKB";
            } else if (isCalledAs("getMinX")) {
                propertyName = "MINX";
            } else if (isCalledAs("getMaxX")) {
                propertyName = "MAXX";
            } else if (isCalledAs("getMinY")) {
                propertyName = "MINY";
            } else if (isCalledAs("getMaxY")) {
                propertyName = "MAXY";
            } else if (isCalledAs("getCentroidX")) {
                propertyName = "CENTROID_X";
            } else if (isCalledAs("getCentroidY")) {
                propertyName = "CENTROID_Y";
            } else if (isCalledAs("getArea")) {
                propertyName = "AREA";
            } else if (isCalledAs("getEPSG4326WKT")) {
                propertyName = "EPSG4326_WKT";
            } else if (isCalledAs("getEPSG4326WKB")) {
                propertyName = "EPSG4326_WKB";
            } else if (isCalledAs("getEPSG4326MinX")) {
                propertyName = "EPSG4326_MINX";
            } else if (isCalledAs("getEPSG4326MaxX")) {
                propertyName = "EPSG4326_MAXX";
            } else if (isCalledAs("getEPSG4326MinY")) {
                propertyName = "EPSG4326_MINY";
            } else if (isCalledAs("getEPSG4326MaxY")) {
                propertyName = "EPSG4326_MAXY";
            } else if (isCalledAs("getEPSG4326CentroidX")) {
                propertyName = "EPSG4326_CENTROID_X";
            } else if (isCalledAs("getEPSG4326CentroidY")) {
                propertyName = "EPSG4326_CENTROID_Y";
            } else if (isCalledAs("getEPSG4326Area")) {
                propertyName = "EPSG4326_AREA";
            } else if (isCalledAs("getSRS")) {
                propertyName = "SRS_NAME";
            } else if (isCalledAs("getGeometryType")) {
                propertyName = "GEOMETRY_TYPE";
            } else if (isCalledAs("isClosed")) {
                propertyName = "IS_CLOSED";
            } else if (isCalledAs("isSimple")) {
                propertyName = "IS_SIMPLE";
            } else if (isCalledAs("isValid")) {
                propertyName = "IS_VALID";
            } else {
                logger.error("Unknown spatial property: {}", getName().getLocalPart());
                throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
            }
            NodeValue geometryNode = (NodeValue) nodes.itemAt(0);
            if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                // The node should be indexed : get its property
                result = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, propertyName);
                hasUsedIndex = true;
            } else {
                // builds the geometry
                sourceCRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                // Transform the geometry to EPSG:4326 if relevant
                if (propertyName.contains("EPSG4326")) {
                    geometry = indexWorker.transformGeometry(geometry, sourceCRS, "EPSG:4326");
                    if (isCalledAs("getEPSG4326WKT")) {
                        result = new StringValue(wktWriter.write(geometry));
                    } else if (isCalledAs("getEPSG4326WKB")) {
                        byte[] data = wkbWriter.write(geometry);
                        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
                    } else if (isCalledAs("getEPSG4326MinX")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
                    } else if (isCalledAs("getEPSG4326MaxX")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
                    } else if (isCalledAs("getEPSG4326MinY")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
                    } else if (isCalledAs("getEPSG4326MaxY")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
                    } else if (isCalledAs("getEPSG4326CentroidX")) {
                        result = new DoubleValue(geometry.getCentroid().getX());
                    } else if (isCalledAs("getEPSG4326CentroidY")) {
                        result = new DoubleValue(geometry.getCentroid().getY());
                    } else if (isCalledAs("getEPSG4326Area")) {
                        result = new DoubleValue(geometry.getArea());
                    }
                } else if (isCalledAs("getWKT")) {
                    result = new StringValue(wktWriter.write(geometry));
                } else if (isCalledAs("getWKB")) {
                    byte[] data = wkbWriter.write(geometry);
                    return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
                } else if (isCalledAs("getMinX")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
                } else if (isCalledAs("getMaxX")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
                } else if (isCalledAs("getMinY")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
                } else if (isCalledAs("getMaxY")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
                } else if (isCalledAs("getCentroidX")) {
                    result = new DoubleValue(geometry.getCentroid().getX());
                } else if (isCalledAs("getCentroidY")) {
                    result = new DoubleValue(geometry.getCentroid().getY());
                } else if (isCalledAs("getArea")) {
                    result = new DoubleValue(geometry.getArea());
                } else if (isCalledAs("getSRS")) {
                    result = new StringValue(((Element) geometryNode).getAttribute("srsName"));
                } else if (isCalledAs("getGeometryType")) {
                    result = new StringValue(geometry.getGeometryType());
                } else if (isCalledAs("isClosed")) {
                    result = new BooleanValue(!geometry.isEmpty());
                } else if (isCalledAs("isSimple")) {
                    result = new BooleanValue(geometry.isSimple());
                } else if (isCalledAs("isValid")) {
                    result = new BooleanValue(geometry.isValid());
                } else {
                    logger.error("Unknown spatial property: {}", getName().getLocalPart());
                    throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
                }
            }
        } catch (SpatialIndexException e) {
            logger.error(e.getMessage());
            throw new XPathException(e);
        }
    }
    return result;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Element(org.w3c.dom.Element) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Sequence(org.exist.xquery.value.Sequence) SpatialIndexException(org.exist.indexing.spatial.SpatialIndexException) NodeProxy(org.exist.dom.persistent.NodeProxy) Geometry(com.vividsolutions.jts.geom.Geometry) DoubleValue(org.exist.xquery.value.DoubleValue) AbstractGMLJDBCIndexWorker(org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker) BooleanValue(org.exist.xquery.value.BooleanValue) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) StringValue(org.exist.xquery.value.StringValue)

Example 8 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class RandomFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final Sequence result;
    final Random rnd = new Random();
    if (getArgumentCount() == 0) {
        if (isCalledAs("random")) {
            result = new DoubleValue(rnd.nextDouble());
        } else {
            final BigInteger rndInt = new BigInteger(64, rnd);
            result = new IntegerValue(rndInt, Type.UNSIGNED_LONG);
        }
    } else {
        final IntegerValue upper = (IntegerValue) args[0].convertTo(Type.INTEGER);
        result = new IntegerValue(rnd.nextInt(upper.getInt()));
    }
    return result;
}
Also used : Random(java.util.Random) DoubleValue(org.exist.xquery.value.DoubleValue) IntegerValue(org.exist.xquery.value.IntegerValue) BigInteger(java.math.BigInteger) Sequence(org.exist.xquery.value.Sequence)

Example 9 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class BTreeTest method numbers.

@Test
public void numbers() throws TerminatedException, DBException, EXistException, IOException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final BTree btree = new BTree(pool, BTREE_TEST_FILE_ID, BTREE_TEST_FILE_VERSION, false, pool.getCacheManager(), file)) {
        btree.create((short) -1);
        for (int i = 1; i <= COUNT; i++) {
            Value value = new SimpleValue(new DoubleValue(i));
            btree.addValue(value, i);
        }
        btree.flush();
        for (int i = 1; i <= COUNT; i++) {
            long p = btree.findValue(new SimpleValue(new DoubleValue(i)));
            assertEquals(p, i);
        }
        // Testing IndexQuery.GT
        IndexQuery query;
        for (int i = 0; i < COUNT; i += 10) {
            query = new IndexQuery(IndexQuery.GT, new SimpleValue(new DoubleValue(i)));
            btree.query(query, new SimpleCallback());
            assertEquals(COUNT - i, count);
        }
        // Testing IndexQuery.GEQ
        query = new IndexQuery(IndexQuery.GEQ, new SimpleValue(new DoubleValue(COUNT / 2)));
        btree.query(query, new SimpleCallback());
        assertEquals(COUNT / 2 + 1, count);
        // Testing IndexQuery.NEQ
        for (int i = 1; i <= COUNT / 8; i++) {
            query = new IndexQuery(IndexQuery.NEQ, new SimpleValue(new DoubleValue(i)));
            btree.query(query, new SimpleCallback());
            assertEquals(COUNT - 1, count);
        }
    }
}
Also used : DoubleValue(org.exist.xquery.value.DoubleValue) DoubleValue(org.exist.xquery.value.DoubleValue) AtomicValue(org.exist.xquery.value.AtomicValue) BrokerPool(org.exist.storage.BrokerPool)

Example 10 with DoubleValue

use of org.exist.xquery.value.DoubleValue in project exist by eXist-db.

the class BTreeTest method numbersWithPrefix.

@Test
public void numbersWithPrefix() throws DBException, EXistException, IOException, TerminatedException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final BTree btree = new BTree(pool, BTREE_TEST_FILE_ID, BTREE_TEST_FILE_VERSION, false, pool.getCacheManager(), file)) {
        btree.create((short) -1);
        for (int i = 1; i <= COUNT; i++) {
            Value value = new PrefixValue(99, new DoubleValue(i));
            btree.addValue(value, i);
        }
        for (int i = 1; i <= COUNT; i++) {
            Value value = new PrefixValue(100, new DoubleValue(i));
            btree.addValue(value, i);
        }
        btree.flush();
        for (int i = 1; i <= COUNT; i++) {
            long p = btree.findValue(new PrefixValue(99, new DoubleValue(i)));
            assertEquals(p, i);
        }
        Value prefix = new PrefixValue(99);
        // Testing IndexQuery.TRUNC_RIGHT
        IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, new PrefixValue(99));
        btree.query(query, new PrefixIndexCallback());
        assertEquals(COUNT, count);
        // Testing IndexQuery.GT
        for (int i = 0; i < COUNT; i += 10) {
            query = new IndexQuery(IndexQuery.GT, new PrefixValue(99, new DoubleValue(i)));
            btree.query(query, prefix, new PrefixIndexCallback());
            assertEquals(COUNT - i, count);
        }
        // Testing IndexQuery.GEQ
        query = new IndexQuery(IndexQuery.GEQ, new PrefixValue(99, new DoubleValue(COUNT / 2)));
        btree.query(query, prefix, new PrefixIndexCallback());
        assertEquals(COUNT / 2 + 1, count);
        // Testing IndexQuery.LT
        query = new IndexQuery(IndexQuery.LT, new PrefixValue(99, new DoubleValue(COUNT / 2)));
        btree.query(query, prefix, new PrefixIndexCallback());
        assertEquals(COUNT / 2 - 1, count);
        // Testing IndexQuery.LEQ
        query = new IndexQuery(IndexQuery.LEQ, new PrefixValue(99, new DoubleValue(COUNT / 2)));
        btree.query(query, prefix, new PrefixIndexCallback());
        assertEquals(COUNT / 2, count);
        // Testing IndexQuery.NEQ
        for (int i = 1; i <= COUNT / 8; i++) {
            count = 0;
            query = new IndexQuery(IndexQuery.NEQ, new PrefixValue(99, new DoubleValue(i)));
            btree.query(query, prefix, new PrefixIndexCallback());
            assertEquals(COUNT - 1, count);
        }
    }
}
Also used : DoubleValue(org.exist.xquery.value.DoubleValue) DoubleValue(org.exist.xquery.value.DoubleValue) AtomicValue(org.exist.xquery.value.AtomicValue) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

DoubleValue (org.exist.xquery.value.DoubleValue)10 Sequence (org.exist.xquery.value.Sequence)5 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)3 XPathException (org.exist.xquery.XPathException)3 AtomicValue (org.exist.xquery.value.AtomicValue)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 StringValue (org.exist.xquery.value.StringValue)3 ValueSequence (org.exist.xquery.value.ValueSequence)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 NodeProxy (org.exist.dom.persistent.NodeProxy)2 AbstractGMLJDBCIndexWorker (org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker)2 SpatialIndexException (org.exist.indexing.spatial.SpatialIndexException)2 BrokerPool (org.exist.storage.BrokerPool)2 IntegerValue (org.exist.xquery.value.IntegerValue)2 NodeValue (org.exist.xquery.value.NodeValue)2 Element (org.w3c.dom.Element)2 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 BigInteger (java.math.BigInteger)1