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();
result = new ExtArrayNodeSet(); //new ExtArrayNodeSet(docs.getLength(), 250)
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);
//Node is in the context : check if it is accurate
//contextSet.contains(p) would have made more sense but there is a problem with
//VirtualNodeSet when on the DESCENDANT_OR_SELF axis