/** * @param catalogTracker * @param serverName * @return List of user regions installed on this server (does not include catalog regions). * @throws IOException */ public static NavigableMap<HRegionInfo, Result> getServerUserRegions( CatalogTracker catalogTracker, final ServerName serverName) throws IOException { final NavigableMap<HRegionInfo, Result> hris = new TreeMap<HRegionInfo, Result>(); // Fill the above hris map with entries from .META. that have the passed // servername. CollectingVisitor<Result> v = new CollectingVisitor<Result>() { @Override void add(Result r) { if (r == null || r.isEmpty()) return; ServerName sn = getServerNameFromCatalogResult(r); if (sn != null && sn.equals(serverName)) this.results.add(r); } }; fullScan(catalogTracker, v); List<Result> results = v.getResults(); if (results != null && !results.isEmpty()) { // Convert results to Map keyed by HRI for (Result r : results) { Pair<HRegionInfo, ServerName> p = parseCatalogResult(r); if (p != null && p.getFirst() != null) hris.put(p.getFirst(), r); } } return hris; }
/** * Checks if the specified table exists. Looks at the META table hosted on the specified server. * * @param catalogTracker * @param tableName table to check * @return true if the table exists in meta, false if not * @throws IOException */ public static boolean tableExists(CatalogTracker catalogTracker, String tableName) throws IOException { if (tableName.equals(HTableDescriptor.ROOT_TABLEDESC.getNameAsString()) || tableName.equals(HTableDescriptor.META_TABLEDESC.getNameAsString())) { // Catalog tables always exist. return true; } final byte[] tableNameBytes = Bytes.toBytes(tableName); // Make a version of ResultCollectingVisitor that only collects the first CollectingVisitor<HRegionInfo> visitor = new CollectingVisitor<HRegionInfo>() { private HRegionInfo current = null; @Override public boolean visit(Result r) throws IOException { this.current = parseHRegionInfoFromCatalogResult(r, HConstants.REGIONINFO_QUALIFIER); if (this.current == null) { LOG.warn("No serialized HRegionInfo in " + r); return true; } if (!isInsideTable(this.current, tableNameBytes)) return false; // Else call super and add this Result to the collection. super.visit(r); // Stop collecting regions from table after we get one. return false; } @Override void add(Result r) { // Add the current HRI. this.results.add(this.current); } }; fullScan(catalogTracker, visitor, getTableStartRowForMeta(tableNameBytes)); // If visitor has results >= 1 then table exists. return visitor.getResults().size() >= 1; }