@Override public String toString() { if (map != null) { return map.toString(); } else { return "[LAZY node " + identifier + "]"; } }
@Override public boolean containsKey(Object key) { if (JdbcIndexDefinition.this.identifier.equals(key)) return true; if (keys.containsKey(key)) return true; check(); return map.containsKey(key); }
@Override public String get(Object key) { if (JdbcIndexDefinition.this.identifier.equals(key)) return identifier; if (keys.containsKey(key)) return keys.get(key); check(); return map.get(key); }
@Override public org.mmbase.bridge.Node getNode(final Cloud userCloud, final Document doc) { String docId = doc.get("number"); if (docId == null) { throw new IllegalArgumentException("No number found in " + doc); } LazyMap m = nodeCache.get(docId); // if (m == null) { Map<String, String> keys = new HashMap<String, String>(); for (String keyWord : keyWords) { keys.put(keyWord, doc.get(keyWord)); } m = new LazyMap(docId, keys); nodeCache.put(docId, m); } org.mmbase.bridge.Node node = new MapNode<String>( m, new MapNodeManager(userCloud, m) { @Override public boolean hasField(String name) { if (JdbcIndexDefinition.this.key.equals(name)) return true; return super.hasField(name); } @Override public org.mmbase.bridge.Field getField(String name) { if (map == null && JdbcIndexDefinition.this.key.equals(name)) { org.mmbase.core.CoreField fd = org.mmbase.core.util.Fields.createField( name, org.mmbase.core.util.Fields.classToType(Object.class), org.mmbase.bridge.Field.TYPE_UNKNOWN, org.mmbase.bridge.Field.STATE_VIRTUAL, null); return new org.mmbase.bridge.implementation.BasicField(fd, this); } else { return super.getField(name); } } }); if (log.isDebugEnabled()) { log.debug("Returning node for " + node); } return node; }
JdbcIndexDefinition( DataSource ds, Element element, Set allIndexedFields, boolean storeText, boolean mergeText, Analyzer a, boolean isSub) { this.dataSource = ds; indexSql = element.getAttribute("sql"); key = element.getAttribute("key"); String elementId = element.getAttribute("identifier"); identifier = "".equals(elementId) ? key : elementId; findSql = element.getAttribute("find"); NodeList childNodes = element.getChildNodes(); for (int k = 0; k < childNodes.getLength(); k++) { if (childNodes.item(k) instanceof Element) { Element childElement = (Element) childNodes.item(k); if ("field".equals(childElement.getLocalName())) { if (childElement.getAttribute("keyword").equals("true")) { keyWords.add(childElement.getAttribute("name")); } String m = childElement.getAttribute("multiple"); if ("".equals(m)) m = "add"; if (!m.equals("add")) { nonDefaultMultiples.put( childElement.getAttribute("name"), Indexer.Multiple.valueOf(m.toUpperCase())); } String b = childElement.getAttribute("boost"); if (!b.equals("")) { boosts.put(childElement.getAttribute("name"), Float.valueOf(b)); } } else if ("related".equals(childElement.getLocalName())) { subQueries.add( new JdbcIndexDefinition( ds, childElement, allIndexedFields, storeText, mergeText, a, true)); } } } this.analyzer = a; this.isSub = isSub; assert !isSub || "".equals(findSql); }
protected void check() { if (map == null) { Connection connection = null; Statement statement = null; ResultSet results = null; try { connection = dataSource.getConnection(); statement = connection.createStatement(); long start = System.currentTimeMillis(); String s = getFindSql(identifier); if (log.isTraceEnabled()) { log.trace("About to execute " + s + " because ", new Exception()); } results = statement.executeQuery(s); ResultSetMetaData meta = results.getMetaData(); map = new HashMap<String, String>(); if (results.next()) { for (int i = 1; i <= meta.getColumnCount(); i++) { String value = org.mmbase.util.Casting.toString(results.getString(i)); map.put(meta.getColumnName(i).toLowerCase(), value); } } long duration = (System.currentTimeMillis() - start); if (duration > 500) { log.warn("Executed " + s + " in " + duration + " ms"); } else if (duration > 100) { log.debug("Executed " + s + " in " + duration + " ms"); } else { log.trace("Executed " + s + " in " + duration + " ms"); } } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } finally { if (results != null) try { results.close(); } catch (Exception e) { } if (statement != null) try { statement.close(); } catch (Exception e) { } if (connection != null) try { connection.close(); } catch (Exception e) { } } } }
@Override public int size() { check(); return map.size(); }
@Override public Set<Map.Entry<String, String>> entrySet() { check(); return map.entrySet(); }