Search in sources :

Example 11 with DLN

use of org.exist.numbering.DLN in project exist by eXist-db.

the class ExceptTest method memtree_except_persistent.

/**
 * Tests the XQuery `except` operator against an
 * in-memory node on the left and a persistent node on the right
 */
@Test
public void memtree_except_persistent() throws XPathException, NoSuchMethodException {
    final XQueryContext mockContext = createMock(XQueryContext.class);
    final PathExpr mockLeft = createMock(PathExpr.class);
    final PathExpr mockRight = createMock(PathExpr.class);
    final Sequence mockContextSequence = createMock(Sequence.class);
    final Item mockContextItem = createMock(Item.class);
    final Profiler mockProfiler = createMock(Profiler.class);
    final DocumentImpl mockPersistentDoc = createMock(DocumentImpl.class);
    final NodeProxy mockPersistentNode = createMockBuilder(NodeProxy.class).withConstructor(DocumentImpl.class, NodeId.class).withArgs(mockPersistentDoc, new DLN(1)).addMockedMethods(NodeProxy.class.getMethod("isEmpty", new Class[] {}), NodeProxy.class.getMethod("getItemType", new Class[] {}), NodeProxy.class.getMethod("equals", new Class[] { Object.class })).createMock();
    expect(mockContext.nextExpressionId()).andReturn(1);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    // memtree node
    expect(mockLeft.eval(mockContextSequence, mockContextItem)).andReturn((org.exist.dom.memtree.ElementImpl) createInMemoryDocument().getDocumentElement());
    // persistent node
    expect(mockRight.eval(mockContextSequence, mockContextItem)).andReturn(mockPersistentNode);
    expect(mockPersistentNode.isEmpty()).andReturn(false);
    expect(mockPersistentNode.getItemType()).andReturn(Type.NODE);
    expect(mockPersistentDoc.getDocId()).andReturn(1).times(2);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    replay(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
    // test
    final Except except = new Except(mockContext, mockLeft, mockRight);
    final Sequence result = except.eval(mockContextSequence, mockContextItem);
    assertEquals(1, ((ValueSequence) result).size());
    verify(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
}
Also used : DLN(org.exist.numbering.DLN) NodeId(org.exist.numbering.NodeId) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) Test(org.junit.Test)

Example 12 with DLN

use of org.exist.numbering.DLN in project exist by eXist-db.

the class IntersectTest method persistent_intersect_memtree.

/**
 * Tests the XQuery `intersect` operator against a
 * persistent node on the left and an in-memory node on the right
 */
@Test
public void persistent_intersect_memtree() throws XPathException, NoSuchMethodException {
    final XQueryContext mockContext = createMock(XQueryContext.class);
    final PathExpr mockLeft = createMock(PathExpr.class);
    final PathExpr mockRight = createMock(PathExpr.class);
    final Sequence mockContextSequence = createMock(Sequence.class);
    final Item mockContextItem = createMock(Item.class);
    final Profiler mockProfiler = createMock(Profiler.class);
    final DocumentImpl mockPersistentDoc = createMock(DocumentImpl.class);
    final NodeProxy mockPersistentNode = createMockBuilder(NodeProxy.class).withConstructor(DocumentImpl.class, NodeId.class).withArgs(mockPersistentDoc, new DLN(1)).addMockedMethods(NodeProxy.class.getMethod("isEmpty", new Class[] {}), NodeProxy.class.getMethod("getItemType", new Class[] {}), NodeProxy.class.getMethod("equals", new Class[] { Object.class })).createMock();
    expect(mockContext.nextExpressionId()).andReturn(1);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    // persistent node
    expect(mockLeft.eval(mockContextSequence, mockContextItem)).andReturn(mockPersistentNode);
    // memtree node
    expect(mockRight.eval(mockContextSequence, mockContextItem)).andReturn((org.exist.dom.memtree.ElementImpl) createInMemoryDocument().getDocumentElement());
    expect(mockPersistentNode.isEmpty()).andReturn(false);
    expect(mockPersistentNode.getItemType()).andReturn(Type.NODE);
    expect(mockPersistentDoc.getDocId()).andReturn(1).times(2);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    replay(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
    // test
    final Intersect intersect = new Intersect(mockContext, mockLeft, mockRight);
    final Sequence result = intersect.eval(mockContextSequence, mockContextItem);
    assertEquals(0, ((ValueSequence) result).size());
    verify(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
}
Also used : Item(org.exist.xquery.value.Item) DLN(org.exist.numbering.DLN) NodeId(org.exist.numbering.NodeId) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) Test(org.junit.Test)

Example 13 with DLN

use of org.exist.numbering.DLN in project exist by eXist-db.

the class GMLHSQLIndexWorker method checkIndex.

@Override
protected boolean checkIndex(DBBroker broker, Connection conn) throws SQLException {
    PreparedStatement ps = conn.prepareStatement("SELECT * FROM " + GMLHSQLIndex.TABLE_NAME + ";");
    ResultSet rs = null;
    try {
        rs = ps.executeQuery();
        while (rs.next()) {
            Geometry original_geometry = wkbReader.read(rs.getBytes("WKB"));
            if (!original_geometry.equals(wktReader.read(rs.getString("WKT")))) {
                LOG.info("Inconsistent WKT : {}", rs.getString("WKT"));
                return false;
            }
            Geometry EPSG4326_geometry = wkbReader.read(rs.getBytes("EPSG4326_WKB"));
            if (!EPSG4326_geometry.equals(wktReader.read(rs.getString("EPSG4326_WKT")))) {
                LOG.info("Inconsistent WKT : {}", rs.getString("EPSG4326_WKT"));
                return false;
            }
            if (!original_geometry.getGeometryType().equals(rs.getString("GEOMETRY_TYPE"))) {
                LOG.info("Inconsistent geometry type: {}", rs.getDouble("GEOMETRY_TYPE"));
                return false;
            }
            if (original_geometry.getEnvelopeInternal().getMinX() != rs.getDouble("MINX")) {
                LOG.info("Inconsistent MinX: {}", rs.getDouble("MINX"));
                return false;
            }
            if (original_geometry.getEnvelopeInternal().getMaxX() != rs.getDouble("MAXX")) {
                LOG.info("Inconsistent MaxX: {}", rs.getDouble("MAXX"));
                return false;
            }
            if (original_geometry.getEnvelopeInternal().getMinY() != rs.getDouble("MINY")) {
                LOG.info("Inconsistent MinY: {}", rs.getDouble("MINY"));
                return false;
            }
            if (original_geometry.getEnvelopeInternal().getMaxY() != rs.getDouble("MAXY")) {
                LOG.info("Inconsistent MaxY: {}", rs.getDouble("MAXY"));
                return false;
            }
            if (original_geometry.getCentroid().getCoordinate().x != rs.getDouble("CENTROID_X")) {
                LOG.info("Inconsistent X for centroid : {}", rs.getDouble("CENTROID_X"));
                return false;
            }
            if (original_geometry.getCentroid().getCoordinate().y != rs.getDouble("CENTROID_Y")) {
                LOG.info("Inconsistent Y for centroid : {}", rs.getDouble("CENTROID_Y"));
                return false;
            }
            if (original_geometry.getArea() != rs.getDouble("AREA")) {
                LOG.info("Inconsistent area: {}", rs.getDouble("AREA"));
                return false;
            }
            String srsName = rs.getString("SRS_NAME");
            try {
                if (!transformGeometry(original_geometry, srsName, "EPSG:4326").equals(EPSG4326_geometry)) {
                    LOG.info("Transformed original geometry inconsistent with stored tranformed one");
                    return false;
                }
            } catch (SpatialIndexException e) {
                // Transforms the exception into an SQLException.
                SQLException ee = new SQLException(e.getMessage());
                ee.initCause(e);
                throw ee;
            }
            if (EPSG4326_geometry.getEnvelopeInternal().getMinX() != rs.getDouble("EPSG4326_MINX")) {
                LOG.info("Inconsistent MinX: {}", rs.getDouble("EPSG4326_MINX"));
                return false;
            }
            if (EPSG4326_geometry.getEnvelopeInternal().getMaxX() != rs.getDouble("EPSG4326_MAXX")) {
                LOG.info("Inconsistent MaxX: {}", rs.getDouble("EPSG4326_MAXX"));
                return false;
            }
            if (EPSG4326_geometry.getEnvelopeInternal().getMinY() != rs.getDouble("EPSG4326_MINY")) {
                LOG.info("Inconsistent MinY: {}", rs.getDouble("EPSG4326_MINY"));
                return false;
            }
            if (EPSG4326_geometry.getEnvelopeInternal().getMaxY() != rs.getDouble("EPSG4326_MAXY")) {
                LOG.info("Inconsistent MaxY: {}", rs.getDouble("EPSG4326_MAXY"));
                return false;
            }
            if (EPSG4326_geometry.getCentroid().getCoordinate().x != rs.getDouble("EPSG4326_CENTROID_X")) {
                LOG.info("Inconsistent X for centroid : {}", rs.getDouble("EPSG4326_CENTROID_X"));
                return false;
            }
            if (EPSG4326_geometry.getCentroid().getCoordinate().y != rs.getDouble("EPSG4326_CENTROID_Y")) {
                LOG.info("Inconsistent Y for centroid : {}", rs.getDouble("EPSG4326_CENTROID_Y"));
                return false;
            }
            if (EPSG4326_geometry.getArea() != rs.getDouble("EPSG4326_AREA")) {
                LOG.info("Inconsistent area: {}", rs.getDouble("EPSG4326_AREA"));
                return false;
            }
            if (original_geometry.isEmpty() == rs.getBoolean("IS_CLOSED")) {
                LOG.info("Inconsistent area: {}", rs.getBoolean("IS_CLOSED"));
                return false;
            }
            if (original_geometry.isSimple() != rs.getBoolean("IS_SIMPLE")) {
                LOG.info("Inconsistent area: {}", rs.getBoolean("IS_SIMPLE"));
                return false;
            }
            if (original_geometry.isValid() != rs.getBoolean("IS_VALID")) {
                LOG.info("Inconsistent area: {}", rs.getBoolean("IS_VALID"));
                return false;
            }
            DocumentImpl doc = null;
            try {
                doc = (DocumentImpl) broker.getXMLResource(XmldbURI.create(rs.getString("DOCUMENT_URI")));
            } catch (PermissionDeniedException e) {
                // The broker has no right on the document
                LOG.error(e);
                return false;
            }
            NodeId nodeId = new DLN(rs.getInt("NODE_ID_UNITS"), rs.getBytes("NODE_ID"), 0);
            IStoredNode node = broker.objectWith(new NodeProxy(doc, nodeId));
            if (node == null) {
                LOG.info("Node {}doesn't exist", nodeId);
                return false;
            }
            if (!AbstractGMLJDBCIndexWorker.GML_NS.equals(node.getNamespaceURI())) {
                LOG.info("GML indexed node ({}) is in the '{}' namespace. '" + AbstractGMLJDBCIndexWorker.GML_NS + "' was expected !", node.getNodeId(), node.getNamespaceURI());
                return false;
            }
            if (!original_geometry.getGeometryType().equals(node.getLocalName())) {
                if ("Box".equals(node.getLocalName()) && "Polygon".equals(original_geometry.getGeometryType())) {
                    LOG.debug("GML indexed node ({}) is a gml:Box indexed as a polygon", node.getNodeId());
                } else {
                    LOG.info("GML indexed node ({}) has '{}' as its local name. '{}' was expected !", node.getNodeId(), node.getLocalName(), original_geometry.getGeometryType());
                    return false;
                }
            }
            LOG.info(node);
        }
        return true;
    } catch (ParseException e) {
        // Transforms the exception into an SQLException.
        // Very unlikely to happen though...
        SQLException ee = new SQLException(e.getMessage());
        ee.initCause(e);
        throw ee;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
    }
}
Also used : DLN(org.exist.numbering.DLN) Geometry(com.vividsolutions.jts.geom.Geometry) NodeId(org.exist.numbering.NodeId) PermissionDeniedException(org.exist.security.PermissionDeniedException) ParseException(com.vividsolutions.jts.io.ParseException)

Example 14 with DLN

use of org.exist.numbering.DLN 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 15 with DLN

use of org.exist.numbering.DLN in project exist by eXist-db.

the class ElementImplTest method isSameNode_sameElement.

@Test
public void isSameNode_sameElement() {
    final DocumentImpl doc = EasyMock.createMock(DocumentImpl.class);
    expect(doc.getDocId()).andReturn(21).times(2);
    replay(doc);
    final ElementImpl elem = new ElementImpl();
    elem.setOwnerDocument(doc);
    elem.setNodeId(new DLN("1.2"));
    assertTrue(elem.isSameNode(elem));
    verify(doc);
}
Also used : DLN(org.exist.numbering.DLN) Test(org.junit.Test)

Aggregations

DLN (org.exist.numbering.DLN)16 Test (org.junit.Test)12 NodeId (org.exist.numbering.NodeId)8 DocumentImpl (org.exist.dom.persistent.DocumentImpl)4 NodeProxy (org.exist.dom.persistent.NodeProxy)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 Geometry (com.vividsolutions.jts.geom.Geometry)3 ParseException (com.vividsolutions.jts.io.ParseException)3 ValueSequence (org.exist.xquery.value.ValueSequence)3 Item (org.exist.xquery.value.Item)2 Sequence (org.exist.xquery.value.Sequence)2 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)1 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)1 BooleanValue (org.exist.xquery.value.BooleanValue)1 DoubleValue (org.exist.xquery.value.DoubleValue)1 StringValue (org.exist.xquery.value.StringValue)1