📄 browse.java
字号:
log.debug("Index has changed"); } cache.remove(key); return null; } } catch (SQLException sqle) { if (log.isDebugEnabled()) { log.debug("Caught SQLException: " + sqle, sqle); } return null; } // Cached object if (log.isDebugEnabled()) { log.debug("Using cached browse"); } cachedInfo.setCached(true); return cachedInfo; } /** * Return true if an index has changed * * @param key * @return * @throws SQLException */ public static boolean indexHasChanged(BrowseScope key) throws SQLException { Context context = null; try { context = new Context(); TableRow results = countAndMax(context, key); long count = -1; int max = -1; if (results != null) { // use getIntColumn for Oracle count data if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) { count = results.getIntColumn("count"); } else //getLongColumn works for postgres { count = results.getLongColumn("count"); } max = results.getIntColumn("max"); } context.complete(); // Same? if ((count == getCount(key)) && (max == getMaximum(key))) { return false; } // Update 'em setMaximum(key, max); setCount(key, count); // The index has in fact changed return true; } catch (SQLException sqle) { if (context != null) { context.abort(); } throw sqle; } } /** * Compute and save the values for the number of values in the index, and * the maximum such value. * * @param key */ public static void updateIndexData(BrowseScope key) { Context context = null; try { context = new Context(); TableRow results = countAndMax(context, key); long count = -1; int max = -1; if (results != null) { //use getIntColumn for Oracle count data if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) { count = results.getIntColumn("count"); } else //getLongColumn works for postgres { count = results.getLongColumn("count"); } max = results.getIntColumn("max"); } context.complete(); setMaximum(key, max); setCount(key, count); } catch (Exception e) { if (context != null) { context.abort(); } e.printStackTrace(); } } /** * Retrieve the values for count and max * * @param context * @param scope * @return * @throws SQLException */ public static TableRow countAndMax(Context context, BrowseScope scope) throws SQLException { // The basic idea here is that we'll check an indexes // maximum id and its count: if the maximum id has changed, // then there are new values, and if the count has changed, // then some items may have been removed. We assume that // values never change. String sql = new StringBuffer().append( "select count({0}) as count, max({0}) as max from ").append( BrowseTables.getTable(scope)).append( Browse.getScopeClause(scope, "where")).toString(); // Format it to use the correct columns String countColumn = BrowseTables.getIndexColumn(scope); Object[] args = new Object[] { countColumn, countColumn }; String SQL = MessageFormat.format(sql, args); // Run the query if (log.isDebugEnabled()) { log.debug("Running SQL to check whether index has changed: \"" + SQL + "\""); } return DatabaseManager.querySingle(context, null, SQL); } /** * Add info to cache, using key. * * @param key * @param info */ public static void add(BrowseScope key, BrowseInfo info) { // Don't bother caching browses with no results, they are // fairly cheap to calculate if (info.getResultCount() == 0) { return; } // Add the info to the cache // Since the object is passed in to us (and thus the caller // may change it), we make a copy. cache.put(key.clone(), info); // Make sure the date cache is current cleanDateCache(); // Save a new entry into the date cache dateCache.put(new java.util.Date(), key); } /** * Remove entries from the date cache */ private static void cleanDateCache() { synchronized (dateCache) { // Plenty of room! if (dateCache.size() < CACHE_MAXIMUM) { return; } // Remove the oldest object dateCache.remove(dateCache.firstKey()); } } /** * Return the maximum value * * @param scope * @return */ private static int getMaximum(BrowseScope scope) { String table = BrowseTables.getTable(scope); Integer value = (Integer) tableMax.get(table); return (value == null) ? (-1) : value.intValue(); } private static long getCount(BrowseScope scope) { String table = BrowseTables.getTable(scope); Long value = (Long) tableSize.get(table); return (value == null) ? (-1) : value.longValue(); } private static void setMaximum(BrowseScope scope, int max) { String table = BrowseTables.getTable(scope); tableMax.put(table, new Integer(max)); } private static void setCount(BrowseScope scope, long count) { String table = BrowseTables.getTable(scope); tableSize.put(table, new Long(count)); }}// Encapsulates browse table info:// * Each scope and browsetype has a corresponding table or view// * Each browse table or view has a value column// * Some of the browse tables are true tables, others are views.// The index maintenance code needs to know the true tables.// The true tables have index columns, which can be used for cachingclass BrowseTables{ private static final String[] BROWSE_TABLES = new String[] { "Communities2Item", "ItemsByAuthor", "ItemsByDate", "ItemsByDateAccessioned", "ItemsByTitle", "ItemsBySubject" }; /** * Return the browse tables. This only returns true tables, views are * ignored. * * @return */ public static String[] tables() { return BROWSE_TABLES; } /** * Return the browse table or view for scope. * * @param scope * @return */ public static String getTable(BrowseScope scope) { int browseType = scope.getBrowseType(); boolean isCommunity = scope.isCommunityScope(); boolean isCollection = scope.isCollectionScope(); if ((browseType == Browse.AUTHORS_BROWSE) || (browseType == Browse.ITEMS_BY_AUTHOR_BROWSE)) { if (isCommunity) { return "CommunityItemsByAuthor"; } if (isCollection) { return "CollectionItemsByAuthor"; } return "ItemsByAuthor"; } if (browseType == Browse.ITEMS_BY_TITLE_BROWSE) { if (isCommunity) { return "CommunityItemsByTitle"; } if (isCollection) { return "CollectionItemsByTitle"; } return "ItemsByTitle"; } if (browseType == Browse.ITEMS_BY_DATE_BROWSE) { if (isCommunity) { return "CommunityItemsByDate"; } if (isCollection) { return "CollectionItemsByDate"; } return "ItemsByDate"; } if ((browseType == Browse.SUBJECTS_BROWSE) || (browseType == Browse.ITEMS_BY_SUBJECT_BROWSE)) { if (isCommunity) { return "CommunityItemsBySubject"; } if (isCollection) { return "CollectionItemsBySubject"; } return "ItemsBySubject"; } throw new IllegalArgumentException( "No table for browse and scope combination"); } /** * Return the name of the column that holds the index. * * @param scope * @return */ public static String getIndexColumn(BrowseScope scope) { int browseType = scope.getBrowseType(); if (browseType == Browse.AUTHORS_BROWSE) { return "items_by_author_id"; } if (browseType == Browse.ITEMS_BY_AUTHOR_BROWSE) { return "items_by_author_id"; } if (browseType == Browse.ITEMS_BY_DATE_BROWSE) { return "items_by_date_id"; } if (browseType == Browse.ITEMS_BY_TITLE_BROWSE) { return "items_by_title_id"; } if (browseType == Browse.SUBJECTS_BROWSE) { return "items_by_subject_id"; } if (browseType == Browse.ITEMS_BY_SUBJECT_BROWSE) { return "items_by_subject_id"; } throw new IllegalArgumentException("Unknown browse type: " + browseType); } /** * Return the name of the column that holds the Browse value (the title, * author, date, etc). * * @param scope * @return */ public static String getValueColumn(BrowseScope scope) { int browseType = scope.getBrowseType(); if (browseType == Browse.AUTHORS_BROWSE) { return "sort_author"; } if (browseType == Browse.ITEMS_BY_AUTHOR_BROWSE) { return "sort_author"; } if (browseType == Browse.ITEMS_BY_DATE_BROWSE) { return "date_issued"; } // Note that we use the normalized form of the title if (browseType == Browse.ITEMS_BY_TITLE_BROWSE) { return "sort_title"; } if (browseType == Browse.SUBJECTS_BROWSE) { return "sort_subject"; } if (browseType == Browse.ITEMS_BY_SUBJECT_BROWSE) { return "sort_subject"; } throw new IllegalArgumentException("Unknown browse type: " + browseType); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -