use of org.exist.numbering.DLN in project exist by eXist-db.
the class IntersectTest method memtree_intersect_persistent.
/**
* Tests the XQuery `intersect` operator against an
* in-memory node on the left and a persistent node on the right
*/
@Test
public void memtree_intersect_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(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);
}
use of org.exist.numbering.DLN in project exist by eXist-db.
the class ExceptTest method persistent_except_memtree.
/**
* Tests the XQuery `except` operator against a
* persistent node on the left and an in-memory node on the right
*/
@Test
public void persistent_except_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(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);
}
use of org.exist.numbering.DLN in project exist by eXist-db.
the class ElementImplTest method isSameNode_nonText.
@Test
public void isSameNode_nonText() {
final DocumentImpl doc = EasyMock.createMock(DocumentImpl.class);
replay(doc);
final ElementImpl elem = new ElementImpl();
elem.setOwnerDocument(doc);
elem.setNodeId(new DLN("1.2"));
final TextImpl text = new TextImpl("hello");
text.setOwnerDocument(doc);
text.setNodeId(new DLN("1.2.1"));
assertFalse(elem.isSameNode(text));
verify(doc);
}
use of org.exist.numbering.DLN in project exist by eXist-db.
the class ElementImplTest method isSameNode_differentText.
@Test
public void isSameNode_differentText() {
final DocumentImpl doc = EasyMock.createMock(DocumentImpl.class);
replay(doc);
final ElementImpl elem = new ElementImpl();
elem.setOwnerDocument(doc);
elem.setNodeId(new DLN("1.2"));
final ElementImpl elem2 = new ElementImpl();
elem2.setOwnerDocument(doc);
elem2.setNodeId(new DLN("1.7"));
assertFalse(elem.isSameNode(elem2));
verify(doc);
}
use of org.exist.numbering.DLN in project exist by eXist-db.
the class GMLHSQLIndexWorker method search.
@Override
protected NodeSet search(DBBroker broker, NodeSet contextSet, Geometry EPSG4326_geometry, int spatialOp, Connection conn) throws SQLException {
String extraSelection = null;
String bboxConstraint = null;
// TODO : generate it in AbstractGMLJDBCIndexWorker
String docConstraint = "";
boolean refine_query_on_doc = false;
if (contextSet != null) {
if (contextSet.getDocumentSet().getDocumentCount() <= index.getMaxDocsInContextToRefineQuery()) {
refine_query_on_doc = true;
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.");
}
}
}
switch(spatialOp) {
// BBoxes are equal
case SpatialOperator.EQUALS:
bboxConstraint = "(EPSG4326_MINX = ? AND EPSG4326_MAXX = ?)" + " AND (EPSG4326_MINY = ? AND EPSG4326_MAXY = ?)";
break;
// Nothing much we can do with the BBox at this stage
case SpatialOperator.DISJOINT:
// Retrieve the BBox though...
extraSelection = ", EPSG4326_MINX, EPSG4326_MAXX, EPSG4326_MINY, EPSG4326_MAXY";
break;
// BBoxes intersect themselves
case SpatialOperator.INTERSECTS:
case SpatialOperator.TOUCHES:
case SpatialOperator.CROSSES:
case SpatialOperator.OVERLAPS:
bboxConstraint = "(EPSG4326_MAXX >= ? AND EPSG4326_MINX <= ?)" + " AND (EPSG4326_MAXY >= ? AND EPSG4326_MINY <= ?)";
break;
// BBox is fully within
case SpatialOperator.WITHIN:
bboxConstraint = "(EPSG4326_MINX >= ? AND EPSG4326_MAXX <= ?)" + " AND (EPSG4326_MINY >= ? AND EPSG4326_MAXY <= ?)";
break;
// BBox fully contains
case SpatialOperator.CONTAINS:
bboxConstraint = "(EPSG4326_MINX <= ? AND EPSG4326_MAXX >= ?)" + " AND (EPSG4326_MINY <= ? AND EPSG4326_MAXY >= ?)";
break;
default:
throw new IllegalArgumentException("Unsupported spatial operator:" + spatialOp);
}
PreparedStatement ps = conn.prepareStatement("SELECT EPSG4326_WKB, DOCUMENT_URI, NODE_ID_UNITS, NODE_ID" + (extraSelection == null ? "" : extraSelection) + " FROM " + GMLHSQLIndex.TABLE_NAME + (bboxConstraint == null ? (refine_query_on_doc ? " WHERE " + docConstraint : "") : " WHERE " + (refine_query_on_doc ? "(" + docConstraint + ") AND " : "") + bboxConstraint) + ";");
if (bboxConstraint != null) {
ps.setDouble(1, EPSG4326_geometry.getEnvelopeInternal().getMinX());
ps.setDouble(2, EPSG4326_geometry.getEnvelopeInternal().getMaxX());
ps.setDouble(3, EPSG4326_geometry.getEnvelopeInternal().getMinY());
ps.setDouble(4, EPSG4326_geometry.getEnvelopeInternal().getMaxY());
}
ResultSet rs = null;
NodeSet result = null;
try {
int disjointPostFiltered = 0;
rs = ps.executeQuery();
// new ExtArrayNodeSet(docs.getLength(), 250)
result = new ExtArrayNodeSet();
while (rs.next()) {
DocumentImpl doc = null;
try {
doc = (DocumentImpl) broker.getXMLResource(XmldbURI.create(rs.getString("DOCUMENT_URI")));
} catch (PermissionDeniedException e) {
LOG.debug(e);
// Ignore since the broker has no right on the document
continue;
}
// contextSet == null should be used to scan the whole index
if (contextSet == null || refine_query_on_doc || 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 == null || contextSet.get(p) != null) {
boolean geometryMatches = false;
if (spatialOp == SpatialOperator.DISJOINT) {
// No BBox intersection : obviously disjoint
if (rs.getDouble("EPSG4326_MAXX") < EPSG4326_geometry.getEnvelopeInternal().getMinX() || rs.getDouble("EPSG4326_MINX") > EPSG4326_geometry.getEnvelopeInternal().getMaxX() || rs.getDouble("EPSG4326_MAXY") < EPSG4326_geometry.getEnvelopeInternal().getMinY() || rs.getDouble("EPSG4326_MINY") > EPSG4326_geometry.getEnvelopeInternal().getMaxY()) {
geometryMatches = true;
disjointPostFiltered++;
}
}
// Possible match : check the geometry
if (!geometryMatches) {
try {
Geometry geometry = wkbReader.read(rs.getBytes("EPSG4326_WKB"));
switch(spatialOp) {
case SpatialOperator.EQUALS:
geometryMatches = geometry.equals(EPSG4326_geometry);
break;
case SpatialOperator.DISJOINT:
geometryMatches = geometry.disjoint(EPSG4326_geometry);
break;
case SpatialOperator.INTERSECTS:
geometryMatches = geometry.intersects(EPSG4326_geometry);
break;
case SpatialOperator.TOUCHES:
geometryMatches = geometry.touches(EPSG4326_geometry);
break;
case SpatialOperator.CROSSES:
geometryMatches = geometry.crosses(EPSG4326_geometry);
break;
case SpatialOperator.WITHIN:
geometryMatches = geometry.within(EPSG4326_geometry);
break;
case SpatialOperator.CONTAINS:
geometryMatches = geometry.contains(EPSG4326_geometry);
break;
case SpatialOperator.OVERLAPS:
geometryMatches = geometry.overlaps(EPSG4326_geometry);
break;
}
} 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;
}
}
if (geometryMatches)
result.add(p);
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} eligible geometries, {}selected{}", rs.getRow(), result.getItemCount(), spatialOp == SpatialOperator.DISJOINT ? "(" + disjointPostFiltered + " post filtered)" : "");
}
return result;
} finally {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
}
}
Aggregations