/** * Quickly build a dom tree. Use per item, not for trees with more than 2000 nodes (or about * that). The XMLNodes are not in the Hibernate session and don't lazy load their parent or * anything else. They should not need to, though. The attached XpathHolders are in the session * and behave normally. * * @param parent * @return */ public XMLNode getDOMTree(XMLNode parent) { StatelessSession ss = DB.getStatelessSession(); List<XMLNode> l; int maxNodes = 10000; if (parent.getSize() < maxNodes) maxNodes = (int) parent.getSize(); Stack<XMLNode> stack = new Stack<XMLNode>(); HashMap<Long, XpathHolder> xpathCache = new HashMap<Long, XpathHolder>(); l = ss.createSQLQuery( "select * from xml_node_" + parent.getXmlObject().getDbID() + " where xml_node_id >= :parentId order by xml_node_id") .addEntity(XMLNode.class) .setLong("parentId", parent.getNodeId()) .setMaxResults(maxNodes) .list(); // now every node has the wrong parent and XpathHolder and no Children.. for (XMLNode x : l) { // find the right place in stack x.setChildren(new ArrayList<XMLNode>()); while (!stack.isEmpty()) { if (stack.peek().getNodeId() != x.getParentNode().getNodeId()) stack.pop(); else break; } if (!stack.isEmpty()) { x.setParentNode(stack.peek()); stack.peek().getChildren().add(x); } 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(parent.getXmlObject()); } if (l.size() > 0) return l.get(0); else return null; }
/** * 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(); } }