Github user JamesRTaylor commented on a diff in the pull request: https://github.com/apache/phoenix/pull/12#discussion_r17405112 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java --- @@ -667,14 +693,104 @@ private PTable getTable(RegionScanner scanner, long clientTimeStamp, long tableT } else { addColumnToTable(results, colName, famName, colKeyValues, columns, saltBucketNum != null); } + } + byte[] tenIdBytes = QueryConstants.EMPTY_BYTE_ARRAY; + if (tenantId != null) { + tenIdBytes = tenantId.getBytes(); } - + byte[] schNameInBytes = QueryConstants.EMPTY_BYTE_ARRAY; + if (schemaName != null) { + schNameInBytes = Bytes.toBytes(schemaName.getString()); + } + byte[] tableKey = SchemaUtil.getTableKey(tenIdBytes, schNameInBytes , PhoenixDatabaseMetaData.SYSTEM_STATS_NAME_BYTES); + ImmutableBytesPtr key = new ImmutableBytesPtr(tableKey); + Cache<ImmutableBytesPtr, PTable> metaDataCache = + GlobalCache.getInstance(this.env).getMetaDataCache(); + PTable statsPTable = metaDataCache.getIfPresent(key); + PTableStats stats = updateStatsInternal(schNameInBytes, tableNameBytes, columns, statsPTable); return PTableImpl.makePTable(tenantId, schemaName, tableName, tableType, indexState, timeStamp, tableSeqNum, pkName, saltBucketNum, columns, tableType == INDEX ? dataTableName : null, indexes, isImmutableRows, physicalTables, defaultFamilyName, viewStatement, disableWAL, - multiTenant, viewType, viewIndexId, indexType); + multiTenant, viewType, viewIndexId, indexType, stats); + } + + private PTableStats updateStatsInternal(byte[] schemaBytes, byte[] tableNameBytes, List<PColumn> columns, PTable statsPTable) + throws IOException { + List<PName> family = Lists.newArrayListWithExpectedSize(columns.size()); + for (PColumn column : columns) { + PName familyName = column.getFamilyName(); + if (familyName != null) { + family.add(familyName); + } + } + HTable statsHTable = null; + try { + // Can we do a new HTable instance here? Or get it from a pool or cache of these instances? + statsHTable = new HTable(this.env.getConfiguration(), + PhoenixDatabaseMetaData.SYSTEM_STATS_NAME_BYTES); + Scan s = new Scan(); + if (tableNameBytes != null) { + // Check for an efficient way here + byte[] combinedKey = Bytes.add(Bytes.add(schemaBytes, QueryConstants.SEPARATOR_BYTE_ARRAY), + tableNameBytes); + s.setStartRow(combinedKey); + s.setStopRow(createStopRow(combinedKey)); + } + ResultScanner scanner = statsHTable.getScanner(s); + Result result = null; + byte[] fam = null; + List<byte[]> guidePosts = new ArrayList<byte[]>(); + TreeMap<byte[], List<byte[]>> guidePostsPerCf = new TreeMap<byte[], List<byte[]>>(Bytes.BYTES_COMPARATOR); + while ((result = scanner.next()) != null) { + CellScanner cellScanner = result.cellScanner(); + while (cellScanner.advance()) { + Cell current = cellScanner.current(); + // For now collect only guide posts + if (Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(), + current.getQualifierLength(), PhoenixDatabaseMetaData.GUIDE_POSTS_BYTES, 0, + PhoenixDatabaseMetaData.GUIDE_POSTS_BYTES.length)) { + byte[] cfInCell = StatisticsUtils.getCFFromRowKey(tableNameBytes, current.getRowArray(), + current.getRowOffset(), current.getRowLength()); + if (fam == null) { + fam = cfInCell; + } else if (!Bytes.equals(fam, cfInCell)) { + guidePostsPerCf.put(cfInCell, guidePosts); + // clear - Take a clone? + guidePosts = new ArrayList<byte[]>(); + fam = cfInCell; + } + guidePosts.add(new ImmutableBytesPtr(current.getValueArray(), current.getValueOffset(), current + .getValueLength()).copyBytesIfNecessary()); + } + } + } + if(fam != null) { + guidePostsPerCf.put(fam, guidePosts); + } + return new PTableStatsImpl(guidePostsPerCf); + } catch (Exception e) { + if (e instanceof org.apache.hadoop.hbase.TableNotFoundException) { + logger.warn("Stats table not yet online", e); + } else { + throw new IOException(e); + } + } finally { + if (statsHTable != null) { + statsHTable.close(); + } + } + return PTableStatsImpl.NO_STATS; } + private byte[] createStopRow(byte[] tableNameBytes) { --- End diff -- Not needed
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---