/** * This version merges all elements of the same name, regardless where they appear. Counting * really only works for attributes and text() nodes. * * @param xo * @param elementName * @return */ public float getAvgLength(XmlObject xo, String elementName) { Double val; log.debug("start query elem='" + elementName + "' " + xo.getDbID()); if (elementName.startsWith("@")) { val = (Double) getSession() .createQuery( "select avg( length (content) ) from XMLNode " + "where xmlObject = :xo and xpathHolder.name = :name ") .setEntity("xo", xo) .setString("name", elementName) .uniqueResult(); } else { val = (Double) getSession() .createQuery( "select avg(length( content )) from XMLNode " + "where xmlObject = :xo and xpathHolder.parent.name = :name " + "and xpathHolder.name = 'text()'") .setEntity("xo", xo) .setString("name", elementName) .uniqueResult(); } log.debug("end query"); return val.floatValue(); }
/** * The normal getById performs very badly, provide the xmlObject to find the node much quicker. * * @param id * @param obj * @return */ public XMLNode getByIdObject(XmlObject obj, Long id) { return (XMLNode) getSession() .createSQLQuery( "select * from xml_node_" + obj.getDbID() + " where xml_node_id = :nodeId") .addEntity(XMLNode.class) .setLong("nodeId", id) .uniqueResult(); }
public XMLNode getStatelessByIdObject(XmlObject obj, Long id) { XMLNode x = (XMLNode) DB.getStatelessSession() .createSQLQuery( "select * from xml_node_" + obj.getDbID() + " where xml_node_id = :nodeId") .addEntity(XMLNode.class) .setLong("nodeId", id) .uniqueResult(); if (x != null) x.setChildren(new ArrayList<XMLNode>()); x.setXpathHolder(DB.getXpathHolderDAO().findById(x.getXpathHolder().getDbID(), false)); return x; }
/** * Cursor over all nodes. (You still have to get XpathHolders the normal way) Call the index * function on given object. Proceeds in node order. * * @param xo * @param ni */ public void indexNodes(XmlObject xo, NodeIndexer ni) { StatelessSession ss = DB.getStatelessSession(); Stack<XMLNode> stack = new Stack<XMLNode>(); HashMap<Long, XpathHolder> xpathCache = new HashMap<Long, XpathHolder>(); ScrollableResults sr = null; try { sr = ss.createSQLQuery("select * from xml_node_" + xo.getDbID() + " order by xml_node_id") .addEntity(XMLNode.class) .scroll(ScrollMode.FORWARD_ONLY); while (sr.next()) { XMLNode x = (XMLNode) sr.get()[0]; while (!stack.isEmpty()) { if ((x.getParentNode() == null) || (stack.peek().getNodeId() != x.getParentNode().getNodeId())) stack.pop(); else break; } stack.push(x); // now the xpathholder XpathHolder path = xpathCache.get(x.getXpathHolder().getDbID()); if (path == null) { path = DB.getXpathHolderDAO().findById(x.getXpathHolder().getDbID(), false); xpathCache.put(path.getDbID(), path); } x.setXpathHolder(path); x.setXmlObject(xo); // node ready to index ni.index(x); } ni.index(null); } catch (Exception e) { log.error("Error while scrolling XMLNodes for indexing.", e); } finally { if (sr != null) sr.close(); } }
public Map<Long, Object[]> getStatsForXpaths(XmlObject xo) { List<Object[]> queryResult = getSession() .createSQLQuery( "select xpath_summary_id, avg( length( content )), count( distinct content )" + " from xml_node_" + xo.getDbID() + " group by xpath_summary_id") .list(); Map<Long, Object[]> result = new HashMap<Long, Object[]>(); for (Object[] oa : queryResult) { if (oa[0] == null) continue; Long xp = ((Integer) oa[0]).longValue(); BigDecimal f = (BigDecimal) oa[1]; BigInteger count = (BigInteger) oa[2]; Object[] val = new Object[2]; val[0] = (f == null ? -1f : f.floatValue()); val[1] = (count == null ? -1l : count.longValue()); result.put(xp, val); } return result; }