/** * Explicitly closes the query object to release the resources */ @Override public void close() { if (query != null) { query.closeAll(); query = null; } } }
public void closeAllQueries() { for (Query q : queries) { try { q.closeAll(); } catch (Throwable t) { LOG.error("Failed to close a query", t); } } }
/** * This is a cleanup method which is used to rollback a active transaction * if the success flag is false and close the associated Query object. This method is used * internally and visible for testing purposes only * @param success Rollback the current active transaction if false * @param query Query object which needs to be closed */ @VisibleForTesting void rollbackAndCleanup(boolean success, Query query) { try { if (!success) { rollbackTransaction(); } } finally { if (query != null) { query.closeAll(); } } }
@Override public List<Object[]> run(List<String> input) throws MetaException { String queryText = queryText0 + makeParams(input.size()) + ")"; Object[] params = new Object[input.size() + 3]; params[0] = catName; params[1] = dbName; params[2] = tableName; for (int i = 0; i < input.size(); ++i) { params[i + 3] = input.get(i); } long start = doTrace ? System.nanoTime() : 0; Query query = pm.newQuery("javax.jdo.query.SQL", queryText); Object qResult = executeWithArray(query, params, queryText); MetastoreDirectSqlUtils.timingTrace(doTrace, queryText0 + "...)", start, (doTrace ? System.nanoTime() : 0)); if (qResult == null) { query.closeAll(); return null; } addQueryAfterUse(query); return MetastoreDirectSqlUtils.ensureList(qResult); } };
private boolean isViewTable(String catName, String dbName, String tblName) throws MetaException { Query query = null; try { String queryText = "select \"TBL_TYPE\" from " + TBLS + "" + " inner join " + DBS + " on " + TBLS + ".\"DB_ID\" = " + DBS + ".\"DB_ID\" " + " where " + TBLS + ".\"TBL_NAME\" = ? and " + DBS + ".\"NAME\" = ? and " + DBS + ".\"CTLG_NAME\" = ?"; Object[] params = new Object[] { tblName, dbName, catName }; query = pm.newQuery("javax.jdo.query.SQL", queryText); query.setUnique(true); Object result = executeWithArray(query, params, queryText); return (result != null) && result.toString().equals(TableType.VIRTUAL_VIEW.toString()); } finally { if (query != null) { query.closeAll(); } } }
query.closeAll();
private void dropPartitionsNoTxn(String catName, String dbName, String tblName, List<String> partNames) { ObjectPair<Query, Map<String, String>> queryWithParams = getPartQueryWithParams(catName, dbName, tblName, partNames); Query query = queryWithParams.getFirst(); query.setClass(MPartition.class); long deleted = query.deletePersistentAll(queryWithParams.getSecond()); LOG.debug("Deleted {} partition from store", deleted); query.closeAll(); }
private MDelegationToken getTokenFrom(String tokenId) { Query query = pm.newQuery(MDelegationToken.class, "tokenIdentifier == tokenId"); query.declareParameters("java.lang.String tokenId"); query.setUnique(true); MDelegationToken delegationToken = (MDelegationToken) query.execute(tokenId); if (query != null) { query.closeAll(); } return delegationToken; }
private void getStatsTableListResult( String queryText, List<org.apache.hadoop.hive.common.TableName> result) throws MetaException { LOG.debug("Running {}", queryText); Query<?> query = pm.newQuery("javax.jdo.query.SQL", queryText); try { List<Object[]> sqlResult = MetastoreDirectSqlUtils .ensureList(executeWithArray(query, STATS_TABLE_TYPES, queryText)); for (Object[] line : sqlResult) { result.add(new org.apache.hadoop.hive.common.TableName( MetastoreDirectSqlUtils.extractSqlString(line[2]), MetastoreDirectSqlUtils .extractSqlString(line[1]), MetastoreDirectSqlUtils.extractSqlString(line[0]))); } } finally { query.closeAll(); } } }
private MSerDeInfo getMSerDeInfo(String serDeName) throws MetaException { Query query = null; try { query = pm.newQuery(MSerDeInfo.class, "name == serDeName"); query.declareParameters("java.lang.String serDeName"); query.setUnique(true); MSerDeInfo mSerDeInfo = (MSerDeInfo)query.execute(serDeName); pm.retrieve(mSerDeInfo); return mSerDeInfo; } finally { if (query != null) { query.closeAll(); } } }
private Integer getNumPartitionsViaOrmFilter(Table table, ExpressionTree tree, boolean isValidatedFilter) throws MetaException { Map<String, Object> params = new HashMap<>(); String jdoFilter = makeQueryFilterString(table.getCatName(), table.getDbName(), table, tree, params, isValidatedFilter); if (jdoFilter == null) { assert !isValidatedFilter; return null; } Query query = pm.newQuery( "select count(partitionName) from org.apache.hadoop.hive.metastore.model.MPartition" ); query.setFilter(jdoFilter); String parameterDeclaration = makeParameterDeclarationStringObj(params); query.declareParameters(parameterDeclaration); Long result = (Long) query.executeWithMap(params); query.closeAll(); return result.intValue(); } /**
private List<String> getPartitionNamesNoTxn(String catName, String dbName, String tableName, short max) { List<String> pns = new ArrayList<>(); if (max == 0) { return pns; } catName = normalizeIdentifier(catName); dbName = normalizeIdentifier(dbName); tableName = normalizeIdentifier(tableName); Query query = pm.newQuery("select partitionName from org.apache.hadoop.hive.metastore.model.MPartition " + "where table.database.name == t1 && table.tableName == t2 && table.database.catalogName == t3 " + "order by partitionName asc"); query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3"); query.setResult("partitionName"); if (max > 0) { query.setRange(0, max); } Collection<String> names = (Collection<String>) query.execute(dbName, tableName, catName); pns.addAll(names); if (query != null) { query.closeAll(); } return pns; }
/** * Gets partition names from the table via ORM (JDOQL) name filter. * @param dbName Database name. * @param tblName Table name. * @param partNames Partition names to get the objects for. * @return Resulting partitions. */ private List<Partition> getPartitionsViaOrmFilter(String catName, String dbName, String tblName, List<String> partNames) throws MetaException { if (partNames.isEmpty()) { return new ArrayList<>(); } ObjectPair<Query, Map<String, String>> queryWithParams = getPartQueryWithParams(catName, dbName, tblName, partNames); Query query = queryWithParams.getFirst(); query.setResultClass(MPartition.class); query.setClass(MPartition.class); query.setOrdering("partitionName ascending"); @SuppressWarnings("unchecked") List<MPartition> mparts = (List<MPartition>)query.executeWithMap(queryWithParams.getSecond()); List<Partition> partitions = convertToParts(catName, dbName, tblName, mparts); if (query != null) { query.closeAll(); } return partitions; }
private void lockForUpdate() throws MetaException { String selectQuery = "select \"NEXT_EVENT_ID\" from \"NOTIFICATION_SEQUENCE\""; String selectForUpdateQuery = sqlGenerator.addForUpdateClause(selectQuery); new RetryingExecutor(conf, () -> { prepareQuotes(); Query query = pm.newQuery("javax.jdo.query.SQL", selectForUpdateQuery); query.setUnique(true); // only need to execute it to get db Lock query.execute(); query.closeAll(); }).run(); }
/** * Detaches column descriptors from storage descriptors; returns the set of unique CDs * thus detached. This is done before dropping partitions because CDs are reused between * SDs; so, we remove the links to delete SDs and then check the returned CDs to see if * they are referenced by other SDs. */ private HashSet<MColumnDescriptor> detachCdsFromSdsNoTxn( String catName, String dbName, String tblName, List<String> partNames) { ObjectPair<Query, Map<String, String>> queryWithParams = getPartQueryWithParams(catName, dbName, tblName, partNames); Query query = queryWithParams.getFirst(); query.setClass(MPartition.class); query.setResult("sd"); @SuppressWarnings("unchecked") List<MStorageDescriptor> sds = (List<MStorageDescriptor>)query.executeWithMap( queryWithParams.getSecond()); HashSet<MColumnDescriptor> candidateCds = new HashSet<>(); for (MStorageDescriptor sd : sds) { if (sd != null && sd.getCD() != null) { candidateCds.add(sd.getCD()); sd.setCD(null); } } if (query != null) { query.closeAll(); } return candidateCds; }
query.closeAll(); return results;
private boolean runTestQuery() { Transaction tx = pm.currentTransaction(); boolean doCommit = false; if (!tx.isActive()) { tx.begin(); doCommit = true; } Query query = null; // Run a self-test query. If it doesn't work, we will self-disable. What a PITA... String selfTestQuery = "select \"DB_ID\" from " + DBS + ""; try { prepareTxn(); query = pm.newQuery("javax.jdo.query.SQL", selfTestQuery); query.execute(); return true; } catch (Throwable t) { doCommit = false; LOG.warn("Self-test query [" + selfTestQuery + "] failed; direct SQL is disabled", t); tx.rollback(); return false; } finally { if (doCommit) { tx.commit(); } if (query != null) { query.closeAll(); } } }
private MISchema getMISchema(String catName, String dbName, String name) { Query query = null; try { name = normalizeIdentifier(name); dbName = normalizeIdentifier(dbName); catName = normalizeIdentifier(catName); query = pm.newQuery(MISchema.class, "name == schemaName && db.name == dbname && db.catalogName == cat"); query.declareParameters( "java.lang.String schemaName, java.lang.String dbname, java.lang.String cat"); query.setUnique(true); MISchema mSchema = (MISchema)query.execute(name, dbName, catName); pm.retrieve(mSchema); return mSchema; } finally { if (query != null) { query.closeAll(); } } }
private MSchemaVersion getMSchemaVersion(String catName, String dbName, String schemaName, int version) { Query query = null; try { dbName = normalizeIdentifier(dbName); schemaName = normalizeIdentifier(schemaName); query = pm.newQuery(MSchemaVersion.class, "iSchema.name == schemaName && iSchema.db.name == dbName &&" + "iSchema.db.catalogName == cat && version == schemaVersion"); query.declareParameters( "java.lang.String schemaName, java.lang.String dbName," + "java.lang.String cat, java.lang.Integer schemaVersion"); query.setUnique(true); MSchemaVersion mSchemaVersion = (MSchemaVersion)query.executeWithArray(schemaName, dbName, catName, version); pm.retrieve(mSchemaVersion); if (mSchemaVersion != null) { pm.retrieveAll(mSchemaVersion.getCols()); if (mSchemaVersion.getSerDe() != null) { pm.retrieve(mSchemaVersion.getSerDe()); } } return mSchemaVersion; } finally { if (query != null) { query.closeAll(); } } }