PageRenderTime 67ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/release-0.0.0-rc0/hive/external/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java

#
Java | 3285 lines | 2940 code | 234 blank | 111 comment | 505 complexity | c77e7956bd825332aaaf457243b8e157 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.metastore;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import java.util.Map.Entry;
  29. import java.util.Properties;
  30. import java.util.concurrent.locks.Lock;
  31. import java.util.concurrent.locks.ReentrantLock;
  32. import javax.jdo.JDOHelper;
  33. import javax.jdo.JDOObjectNotFoundException;
  34. import javax.jdo.PersistenceManager;
  35. import javax.jdo.PersistenceManagerFactory;
  36. import javax.jdo.Query;
  37. import javax.jdo.Transaction;
  38. import javax.jdo.datastore.DataStoreCache;
  39. import org.antlr.runtime.CharStream;
  40. import org.antlr.runtime.CommonTokenStream;
  41. import org.antlr.runtime.RecognitionException;
  42. import org.apache.commons.logging.Log;
  43. import org.apache.commons.logging.LogFactory;
  44. import org.apache.hadoop.conf.Configurable;
  45. import org.apache.hadoop.conf.Configuration;
  46. import org.apache.hadoop.hive.common.FileUtils;
  47. import org.apache.hadoop.hive.conf.HiveConf;
  48. import org.apache.hadoop.hive.metastore.api.Database;
  49. import org.apache.hadoop.hive.metastore.api.FieldSchema;
  50. import org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege;
  51. import org.apache.hadoop.hive.metastore.api.HiveObjectRef;
  52. import org.apache.hadoop.hive.metastore.api.HiveObjectType;
  53. import org.apache.hadoop.hive.metastore.api.Index;
  54. import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
  55. import org.apache.hadoop.hive.metastore.api.MetaException;
  56. import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
  57. import org.apache.hadoop.hive.metastore.api.Order;
  58. import org.apache.hadoop.hive.metastore.api.Partition;
  59. import org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet;
  60. import org.apache.hadoop.hive.metastore.api.PrincipalType;
  61. import org.apache.hadoop.hive.metastore.api.PrivilegeBag;
  62. import org.apache.hadoop.hive.metastore.api.PrivilegeGrantInfo;
  63. import org.apache.hadoop.hive.metastore.api.Role;
  64. import org.apache.hadoop.hive.metastore.api.SerDeInfo;
  65. import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
  66. import org.apache.hadoop.hive.metastore.api.Table;
  67. import org.apache.hadoop.hive.metastore.api.Type;
  68. import org.apache.hadoop.hive.metastore.model.MDatabase;
  69. import org.apache.hadoop.hive.metastore.model.MFieldSchema;
  70. import org.apache.hadoop.hive.metastore.model.MIndex;
  71. import org.apache.hadoop.hive.metastore.model.MOrder;
  72. import org.apache.hadoop.hive.metastore.model.MPartition;
  73. import org.apache.hadoop.hive.metastore.model.MDBPrivilege;
  74. import org.apache.hadoop.hive.metastore.model.MPartitionColumnPrivilege;
  75. import org.apache.hadoop.hive.metastore.model.MPartitionPrivilege;
  76. import org.apache.hadoop.hive.metastore.model.MRole;
  77. import org.apache.hadoop.hive.metastore.model.MTableColumnPrivilege;
  78. import org.apache.hadoop.hive.metastore.model.MGlobalPrivilege;
  79. import org.apache.hadoop.hive.metastore.model.MRoleMap;
  80. import org.apache.hadoop.hive.metastore.model.MSerDeInfo;
  81. import org.apache.hadoop.hive.metastore.model.MStorageDescriptor;
  82. import org.apache.hadoop.hive.metastore.model.MTable;
  83. import org.apache.hadoop.hive.metastore.model.MTablePrivilege;
  84. import org.apache.hadoop.hive.metastore.model.MType;
  85. import org.apache.hadoop.hive.metastore.parser.FilterLexer;
  86. import org.apache.hadoop.hive.metastore.parser.FilterParser;
  87. import org.apache.hadoop.hive.metastore.parser.ExpressionTree.ANTLRNoCaseStringStream;
  88. import org.apache.hadoop.util.StringUtils;
  89. /**
  90. * This class is the interface between the application logic and the database
  91. * store that contains the objects. Refrain putting any logic in mode.M* objects
  92. * or in this file as former could be auto generated and this class would need
  93. * to be made into a interface that can read both from a database and a
  94. * filestore.
  95. */
  96. public class ObjectStore implements RawStore, Configurable {
  97. private static Properties prop = null;
  98. private static PersistenceManagerFactory pmf = null;
  99. private static Lock pmfPropLock = new ReentrantLock();
  100. private static final Log LOG = LogFactory.getLog(ObjectStore.class.getName());
  101. private static enum TXN_STATUS {
  102. NO_STATE, OPEN, COMMITED, ROLLBACK
  103. }
  104. private static final Map<String, Class> PINCLASSMAP;
  105. static {
  106. Map<String, Class> map = new HashMap();
  107. map.put("table", MTable.class);
  108. map.put("storagedescriptor", MStorageDescriptor.class);
  109. map.put("serdeinfo", MSerDeInfo.class);
  110. map.put("partition", MPartition.class);
  111. map.put("database", MDatabase.class);
  112. map.put("type", MType.class);
  113. map.put("fieldschema", MFieldSchema.class);
  114. map.put("order", MOrder.class);
  115. PINCLASSMAP = Collections.unmodifiableMap(map);
  116. }
  117. private boolean isInitialized = false;
  118. private PersistenceManager pm = null;
  119. private Configuration hiveConf;
  120. int openTrasactionCalls = 0;
  121. private Transaction currentTransaction = null;
  122. private TXN_STATUS transactionStatus = TXN_STATUS.NO_STATE;
  123. public ObjectStore() {
  124. }
  125. public Configuration getConf() {
  126. return hiveConf;
  127. }
  128. /**
  129. * Called whenever this object is instantiated using ReflectionUils, and also
  130. * on connection retries. In cases of connection retries, conf will usually
  131. * contain modified values.
  132. */
  133. @SuppressWarnings("nls")
  134. public void setConf(Configuration conf) {
  135. // Although an instance of ObjectStore is accessed by one thread, there may
  136. // be many threads with ObjectStore instances. So the static variables
  137. // pmf and prop need to be protected with locks.
  138. pmfPropLock.lock();
  139. try {
  140. isInitialized = false;
  141. hiveConf = conf;
  142. Properties propsFromConf = getDataSourceProps(conf);
  143. boolean propsChanged = !propsFromConf.equals(prop);
  144. if (propsChanged) {
  145. pmf = null;
  146. prop = null;
  147. }
  148. assert(!isActiveTransaction());
  149. shutdown();
  150. // Always want to re-create pm as we don't know if it were created by the
  151. // most recent instance of the pmf
  152. pm = null;
  153. openTrasactionCalls = 0;
  154. currentTransaction = null;
  155. transactionStatus = TXN_STATUS.NO_STATE;
  156. initialize(propsFromConf);
  157. if (!isInitialized) {
  158. throw new RuntimeException(
  159. "Unable to create persistence manager. Check dss.log for details");
  160. } else {
  161. LOG.info("Initialized ObjectStore");
  162. }
  163. } finally {
  164. pmfPropLock.unlock();
  165. }
  166. }
  167. private ClassLoader classLoader;
  168. {
  169. classLoader = Thread.currentThread().getContextClassLoader();
  170. if (classLoader == null) {
  171. classLoader = ObjectStore.class.getClassLoader();
  172. }
  173. }
  174. @SuppressWarnings("nls")
  175. private void initialize(Properties dsProps) {
  176. LOG.info("ObjectStore, initialize called");
  177. prop = dsProps;
  178. pm = getPersistenceManager();
  179. isInitialized = pm != null;
  180. return;
  181. }
  182. /**
  183. * Properties specified in hive-default.xml override the properties specified
  184. * in jpox.properties.
  185. */
  186. @SuppressWarnings("nls")
  187. private static Properties getDataSourceProps(Configuration conf) {
  188. Properties prop = new Properties();
  189. Iterator<Map.Entry<String, String>> iter = conf.iterator();
  190. while (iter.hasNext()) {
  191. Map.Entry<String, String> e = iter.next();
  192. if (e.getKey().contains("datanucleus") || e.getKey().contains("jdo")) {
  193. Object prevVal = prop.setProperty(e.getKey(), conf.get(e.getKey()));
  194. if (LOG.isDebugEnabled()
  195. && !e.getKey().equals(HiveConf.ConfVars.METASTOREPWD.varname)) {
  196. LOG.debug("Overriding " + e.getKey() + " value " + prevVal
  197. + " from jpox.properties with " + e.getValue());
  198. }
  199. }
  200. }
  201. if (LOG.isDebugEnabled()) {
  202. for (Entry<Object, Object> e : prop.entrySet()) {
  203. if (!e.getKey().equals(HiveConf.ConfVars.METASTOREPWD.varname)) {
  204. LOG.debug(e.getKey() + " = " + e.getValue());
  205. }
  206. }
  207. }
  208. return prop;
  209. }
  210. private static PersistenceManagerFactory getPMF() {
  211. if (pmf == null) {
  212. pmf = JDOHelper.getPersistenceManagerFactory(prop);
  213. DataStoreCache dsc = pmf.getDataStoreCache();
  214. if (dsc != null) {
  215. HiveConf conf = new HiveConf(ObjectStore.class);
  216. String objTypes = HiveConf.getVar(conf, HiveConf.ConfVars.METASTORE_CACHE_PINOBJTYPES);
  217. LOG.info("Setting MetaStore object pin classes with hive.metastore.cache.pinobjtypes=\"" + objTypes + "\"");
  218. if (objTypes != null && objTypes.length() > 0) {
  219. objTypes = objTypes.toLowerCase();
  220. String[] typeTokens = objTypes.split(",");
  221. for (String type : typeTokens) {
  222. type = type.trim();
  223. if (PINCLASSMAP.containsKey(type)) {
  224. dsc.pinAll(true, PINCLASSMAP.get(type));
  225. }
  226. else {
  227. LOG.warn(type + " is not one of the pinnable object types: " + org.apache.commons.lang.StringUtils.join(PINCLASSMAP.keySet(), " "));
  228. }
  229. }
  230. }
  231. } else {
  232. LOG.warn("PersistenceManagerFactory returned null DataStoreCache object. Unable to initialize object pin types defined by hive.metastore.cache.pinobjtypes");
  233. }
  234. }
  235. return pmf;
  236. }
  237. private PersistenceManager getPersistenceManager() {
  238. return getPMF().getPersistenceManager();
  239. }
  240. public void shutdown() {
  241. if (pm != null) {
  242. pm.close();
  243. }
  244. }
  245. /**
  246. * Opens a new one or the one already created Every call of this function must
  247. * have corresponding commit or rollback function call
  248. *
  249. * @return an active transaction
  250. */
  251. public boolean openTransaction() {
  252. openTrasactionCalls++;
  253. if (openTrasactionCalls == 1) {
  254. currentTransaction = pm.currentTransaction();
  255. currentTransaction.begin();
  256. transactionStatus = TXN_STATUS.OPEN;
  257. } else {
  258. // something is wrong since openTransactionCalls is greater than 1 but
  259. // currentTransaction is not active
  260. assert ((currentTransaction != null) && (currentTransaction.isActive()));
  261. }
  262. return currentTransaction.isActive();
  263. }
  264. /**
  265. * if this is the commit of the first open call then an actual commit is
  266. * called.
  267. *
  268. * @return Always returns true
  269. */
  270. @SuppressWarnings("nls")
  271. public boolean commitTransaction() {
  272. if (TXN_STATUS.ROLLBACK == transactionStatus) {
  273. return false;
  274. }
  275. if (openTrasactionCalls <= 0) {
  276. throw new RuntimeException("commitTransaction was called but openTransactionCalls = "
  277. + openTrasactionCalls + ". This probably indicates that there are unbalanced " +
  278. "calls to openTransaction/commitTransaction");
  279. }
  280. if (!currentTransaction.isActive()) {
  281. throw new RuntimeException(
  282. "Commit is called, but transaction is not active. Either there are"
  283. + " mismatching open and close calls or rollback was called in the same trasaction");
  284. }
  285. openTrasactionCalls--;
  286. if ((openTrasactionCalls == 0) && currentTransaction.isActive()) {
  287. transactionStatus = TXN_STATUS.COMMITED;
  288. currentTransaction.commit();
  289. }
  290. return true;
  291. }
  292. /**
  293. * @return true if there is an active transaction. If the current transaction
  294. * is either committed or rolled back it returns false
  295. */
  296. public boolean isActiveTransaction() {
  297. if (currentTransaction == null) {
  298. return false;
  299. }
  300. return currentTransaction.isActive();
  301. }
  302. /**
  303. * Rolls back the current transaction if it is active
  304. */
  305. public void rollbackTransaction() {
  306. if (openTrasactionCalls < 1) {
  307. return;
  308. }
  309. openTrasactionCalls = 0;
  310. if (currentTransaction.isActive()
  311. && transactionStatus != TXN_STATUS.ROLLBACK) {
  312. transactionStatus = TXN_STATUS.ROLLBACK;
  313. // could already be rolled back
  314. currentTransaction.rollback();
  315. }
  316. }
  317. public void createDatabase(Database db) throws InvalidObjectException, MetaException {
  318. boolean commited = false;
  319. MDatabase mdb = new MDatabase();
  320. mdb.setName(db.getName().toLowerCase());
  321. mdb.setLocationUri(db.getLocationUri());
  322. mdb.setDescription(db.getDescription());
  323. mdb.setParameters(db.getParameters());
  324. try {
  325. openTransaction();
  326. pm.makePersistent(mdb);
  327. commited = commitTransaction();
  328. } finally {
  329. if (!commited) {
  330. rollbackTransaction();
  331. }
  332. }
  333. }
  334. @SuppressWarnings("nls")
  335. private MDatabase getMDatabase(String name) throws NoSuchObjectException {
  336. MDatabase mdb = null;
  337. boolean commited = false;
  338. try {
  339. openTransaction();
  340. name = name.toLowerCase().trim();
  341. Query query = pm.newQuery(MDatabase.class, "name == dbname");
  342. query.declareParameters("java.lang.String dbname");
  343. query.setUnique(true);
  344. mdb = (MDatabase) query.execute(name);
  345. pm.retrieve(mdb);
  346. commited = commitTransaction();
  347. } finally {
  348. if (!commited) {
  349. rollbackTransaction();
  350. }
  351. }
  352. if (mdb == null) {
  353. throw new NoSuchObjectException("There is no database named " + name);
  354. }
  355. return mdb;
  356. }
  357. public Database getDatabase(String name) throws NoSuchObjectException {
  358. MDatabase mdb = null;
  359. boolean commited = false;
  360. try {
  361. openTransaction();
  362. mdb = getMDatabase(name);
  363. commited = commitTransaction();
  364. } finally {
  365. if (!commited) {
  366. rollbackTransaction();
  367. }
  368. }
  369. Database db = new Database();
  370. db.setName(mdb.getName());
  371. db.setDescription(mdb.getDescription());
  372. db.setLocationUri(mdb.getLocationUri());
  373. db.setParameters(mdb.getParameters());
  374. return db;
  375. }
  376. /**
  377. * Alter the database object in metastore. Currently only the parameters
  378. * of the database can be changed.
  379. * @param dbName the database name
  380. * @param db the Hive Database object
  381. * @throws MetaException
  382. * @throws NoSuchObjectException
  383. */
  384. public boolean alterDatabase(String dbName, Database db)
  385. throws MetaException, NoSuchObjectException {
  386. MDatabase mdb = null;
  387. boolean committed = false;
  388. try {
  389. mdb = getMDatabase(dbName);
  390. // currently only allow changing database parameters
  391. mdb.setParameters(db.getParameters());
  392. openTransaction();
  393. pm.makePersistent(mdb);
  394. committed = commitTransaction();
  395. } finally {
  396. if (!committed) {
  397. rollbackTransaction();
  398. return false;
  399. }
  400. }
  401. return true;
  402. }
  403. public boolean dropDatabase(String dbname) throws NoSuchObjectException, MetaException {
  404. boolean success = false;
  405. LOG.info("Dropping database " + dbname + " along with all tables");
  406. dbname = dbname.toLowerCase();
  407. try {
  408. openTransaction();
  409. // first drop tables
  410. for (String tableName : getAllTables(dbname)) {
  411. dropTable(dbname, tableName);
  412. }
  413. // then drop the database
  414. MDatabase db = getMDatabase(dbname);
  415. pm.retrieve(db);
  416. if (db != null) {
  417. List<MDBPrivilege> dbGrants = this.listDatabaseGrants(dbname);
  418. if (dbGrants != null && dbGrants.size() > 0) {
  419. pm.deletePersistentAll(dbGrants);
  420. }
  421. pm.deletePersistent(db);
  422. }
  423. success = commitTransaction();
  424. } finally {
  425. if (!success) {
  426. rollbackTransaction();
  427. }
  428. }
  429. return success;
  430. }
  431. public List<String> getDatabases(String pattern) throws MetaException {
  432. boolean commited = false;
  433. List<String> databases = null;
  434. try {
  435. openTransaction();
  436. // Take the pattern and split it on the | to get all the composing
  437. // patterns
  438. String[] subpatterns = pattern.trim().split("\\|");
  439. String query = "select name from org.apache.hadoop.hive.metastore.model.MDatabase where (";
  440. boolean first = true;
  441. for (String subpattern : subpatterns) {
  442. subpattern = "(?i)" + subpattern.replaceAll("\\*", ".*");
  443. if (!first) {
  444. query = query + " || ";
  445. }
  446. query = query + " name.matches(\"" + subpattern + "\")";
  447. first = false;
  448. }
  449. query = query + ")";
  450. Query q = pm.newQuery(query);
  451. q.setResult("name");
  452. q.setOrdering("name ascending");
  453. Collection names = (Collection) q.execute();
  454. databases = new ArrayList<String>();
  455. for (Iterator i = names.iterator(); i.hasNext();) {
  456. databases.add((String) i.next());
  457. }
  458. commited = commitTransaction();
  459. } finally {
  460. if (!commited) {
  461. rollbackTransaction();
  462. }
  463. }
  464. return databases;
  465. }
  466. public List<String> getAllDatabases() throws MetaException {
  467. return getDatabases(".*");
  468. }
  469. private MType getMType(Type type) {
  470. List<MFieldSchema> fields = new ArrayList<MFieldSchema>();
  471. if (type.getFields() != null) {
  472. for (FieldSchema field : type.getFields()) {
  473. fields.add(new MFieldSchema(field.getName(), field.getType(), field
  474. .getComment()));
  475. }
  476. }
  477. return new MType(type.getName(), type.getType1(), type.getType2(), fields);
  478. }
  479. private Type getType(MType mtype) {
  480. List<FieldSchema> fields = new ArrayList<FieldSchema>();
  481. if (mtype.getFields() != null) {
  482. for (MFieldSchema field : mtype.getFields()) {
  483. fields.add(new FieldSchema(field.getName(), field.getType(), field
  484. .getComment()));
  485. }
  486. }
  487. Type ret = new Type();
  488. ret.setName(mtype.getName());
  489. ret.setType1(mtype.getType1());
  490. ret.setType2(mtype.getType2());
  491. ret.setFields(fields);
  492. return ret;
  493. }
  494. public boolean createType(Type type) {
  495. boolean success = false;
  496. MType mtype = getMType(type);
  497. boolean commited = false;
  498. try {
  499. openTransaction();
  500. pm.makePersistent(mtype);
  501. commited = commitTransaction();
  502. success = true;
  503. } finally {
  504. if (!commited) {
  505. rollbackTransaction();
  506. }
  507. }
  508. return success;
  509. }
  510. public Type getType(String typeName) {
  511. Type type = null;
  512. boolean commited = false;
  513. try {
  514. openTransaction();
  515. Query query = pm.newQuery(MType.class, "name == typeName");
  516. query.declareParameters("java.lang.String typeName");
  517. query.setUnique(true);
  518. MType mtype = (MType) query.execute(typeName.trim());
  519. pm.retrieve(type);
  520. if (mtype != null) {
  521. type = getType(mtype);
  522. }
  523. commited = commitTransaction();
  524. } finally {
  525. if (!commited) {
  526. rollbackTransaction();
  527. }
  528. }
  529. return type;
  530. }
  531. public boolean dropType(String typeName) {
  532. boolean success = false;
  533. boolean commited = false;
  534. try {
  535. openTransaction();
  536. Query query = pm.newQuery(MType.class, "name == typeName");
  537. query.declareParameters("java.lang.String typeName");
  538. query.setUnique(true);
  539. MType type = (MType) query.execute(typeName.trim());
  540. pm.retrieve(type);
  541. pm.deletePersistent(type);
  542. commited = commitTransaction();
  543. success = true;
  544. } catch (JDOObjectNotFoundException e) {
  545. commited = commitTransaction();
  546. LOG.debug("type not found " + typeName, e);
  547. } finally {
  548. if (!commited) {
  549. rollbackTransaction();
  550. }
  551. }
  552. return success;
  553. }
  554. public void createTable(Table tbl) throws InvalidObjectException, MetaException {
  555. boolean commited = false;
  556. try {
  557. openTransaction();
  558. MTable mtbl = convertToMTable(tbl);
  559. pm.makePersistent(mtbl);
  560. PrincipalPrivilegeSet principalPrivs = tbl.getPrivileges();
  561. List<Object> toPersistPrivObjs = new ArrayList<Object>();
  562. if (principalPrivs != null) {
  563. int now = (int)(System.currentTimeMillis()/1000);
  564. Map<String, List<PrivilegeGrantInfo>> userPrivs = principalPrivs.getUserPrivileges();
  565. putPersistentPrivObjects(mtbl, toPersistPrivObjs, now, userPrivs, PrincipalType.USER);
  566. Map<String, List<PrivilegeGrantInfo>> groupPrivs = principalPrivs.getGroupPrivileges();
  567. putPersistentPrivObjects(mtbl, toPersistPrivObjs, now, groupPrivs, PrincipalType.GROUP);
  568. Map<String, List<PrivilegeGrantInfo>> rolePrivs = principalPrivs.getRolePrivileges();
  569. putPersistentPrivObjects(mtbl, toPersistPrivObjs, now, rolePrivs, PrincipalType.ROLE);
  570. }
  571. pm.makePersistentAll(toPersistPrivObjs);
  572. commited = commitTransaction();
  573. } finally {
  574. if (!commited) {
  575. rollbackTransaction();
  576. }
  577. }
  578. }
  579. /**
  580. * Convert PrivilegeGrantInfo from privMap to MTablePrivilege, and add all of
  581. * them to the toPersistPrivObjs. These privilege objects will be persisted as
  582. * part of createTable.
  583. *
  584. * @param mtbl
  585. * @param toPersistPrivObjs
  586. * @param now
  587. * @param privMap
  588. * @param type
  589. */
  590. private void putPersistentPrivObjects(MTable mtbl, List<Object> toPersistPrivObjs,
  591. int now, Map<String, List<PrivilegeGrantInfo>> privMap, PrincipalType type) {
  592. if (privMap != null) {
  593. for (Map.Entry<String, List<PrivilegeGrantInfo>> entry : privMap
  594. .entrySet()) {
  595. String principalName = entry.getKey();
  596. List<PrivilegeGrantInfo> privs = entry.getValue();
  597. for (int i = 0; i < privs.size(); i++) {
  598. PrivilegeGrantInfo priv = privs.get(i);
  599. if (priv == null) {
  600. continue;
  601. }
  602. MTablePrivilege mTblSec = new MTablePrivilege(
  603. principalName, type.toString(), mtbl, priv.getPrivilege(),
  604. now, priv.getGrantor(), priv.getGrantorType().toString(), priv
  605. .isGrantOption());
  606. toPersistPrivObjs.add(mTblSec);
  607. }
  608. }
  609. }
  610. }
  611. public boolean dropTable(String dbName, String tableName) throws MetaException {
  612. boolean success = false;
  613. try {
  614. openTransaction();
  615. MTable tbl = getMTable(dbName, tableName);
  616. pm.retrieve(tbl);
  617. if (tbl != null) {
  618. // first remove all the partitions
  619. List<MTablePrivilege> tabGrants = listAllTableGrants(dbName, tableName);
  620. if (tabGrants != null && tabGrants.size() > 0) {
  621. pm.deletePersistentAll(tabGrants);
  622. }
  623. List<MTableColumnPrivilege> tblColGrants = listTableAllColumnGrants(dbName,
  624. tableName);
  625. if (tblColGrants != null && tblColGrants.size() > 0) {
  626. pm.deletePersistentAll(tblColGrants);
  627. }
  628. List<MPartitionPrivilege> partGrants = this.listTableAllPartitionGrants(dbName, tableName);
  629. if (partGrants != null && partGrants.size() > 0) {
  630. pm.deletePersistentAll(partGrants);
  631. }
  632. List<MPartitionColumnPrivilege> partColGrants = listTableAllPartitionColumnGrants(dbName,
  633. tableName);
  634. if (partColGrants != null && partColGrants.size() > 0) {
  635. pm.deletePersistentAll(partColGrants);
  636. }
  637. pm.deletePersistentAll(listMPartitions(dbName, tableName, -1));
  638. // then remove the table
  639. pm.deletePersistentAll(tbl);
  640. }
  641. success = commitTransaction();
  642. } finally {
  643. if (!success) {
  644. rollbackTransaction();
  645. }
  646. }
  647. return success;
  648. }
  649. public Table getTable(String dbName, String tableName) throws MetaException {
  650. boolean commited = false;
  651. Table tbl = null;
  652. try {
  653. openTransaction();
  654. tbl = convertToTable(getMTable(dbName, tableName));
  655. commited = commitTransaction();
  656. } finally {
  657. if (!commited) {
  658. rollbackTransaction();
  659. }
  660. }
  661. return tbl;
  662. }
  663. public List<String> getTables(String dbName, String pattern)
  664. throws MetaException {
  665. boolean commited = false;
  666. List<String> tbls = null;
  667. try {
  668. openTransaction();
  669. dbName = dbName.toLowerCase().trim();
  670. // Take the pattern and split it on the | to get all the composing
  671. // patterns
  672. String[] subpatterns = pattern.trim().split("\\|");
  673. String query =
  674. "select tableName from org.apache.hadoop.hive.metastore.model.MTable "
  675. + "where database.name == dbName && (";
  676. boolean first = true;
  677. for (String subpattern : subpatterns) {
  678. subpattern = "(?i)" + subpattern.replaceAll("\\*", ".*");
  679. if (!first) {
  680. query = query + " || ";
  681. }
  682. query = query + " tableName.matches(\"" + subpattern + "\")";
  683. first = false;
  684. }
  685. query = query + ")";
  686. Query q = pm.newQuery(query);
  687. q.declareParameters("java.lang.String dbName");
  688. q.setResult("tableName");
  689. q.setOrdering("tableName ascending");
  690. Collection names = (Collection) q.execute(dbName);
  691. tbls = new ArrayList<String>();
  692. for (Iterator i = names.iterator(); i.hasNext();) {
  693. tbls.add((String) i.next());
  694. }
  695. commited = commitTransaction();
  696. } finally {
  697. if (!commited) {
  698. rollbackTransaction();
  699. }
  700. }
  701. return tbls;
  702. }
  703. public List<String> getAllTables(String dbName) throws MetaException {
  704. return getTables(dbName, ".*");
  705. }
  706. private MTable getMTable(String db, String table) {
  707. MTable mtbl = null;
  708. boolean commited = false;
  709. try {
  710. openTransaction();
  711. db = db.toLowerCase().trim();
  712. table = table.toLowerCase().trim();
  713. Query query = pm.newQuery(MTable.class, "tableName == table && database.name == db");
  714. query.declareParameters("java.lang.String table, java.lang.String db");
  715. query.setUnique(true);
  716. mtbl = (MTable) query.execute(table, db);
  717. pm.retrieve(mtbl);
  718. commited = commitTransaction();
  719. } finally {
  720. if (!commited) {
  721. rollbackTransaction();
  722. }
  723. }
  724. return mtbl;
  725. }
  726. private Table convertToTable(MTable mtbl) throws MetaException {
  727. if (mtbl == null) {
  728. return null;
  729. }
  730. String tableType = mtbl.getTableType();
  731. if (tableType == null) {
  732. // for backwards compatibility with old metastore persistence
  733. if (mtbl.getViewOriginalText() != null) {
  734. tableType = TableType.VIRTUAL_VIEW.toString();
  735. } else if ("TRUE".equals(mtbl.getParameters().get("EXTERNAL"))) {
  736. tableType = TableType.EXTERNAL_TABLE.toString();
  737. } else {
  738. tableType = TableType.MANAGED_TABLE.toString();
  739. }
  740. }
  741. return new Table(mtbl.getTableName(), mtbl.getDatabase().getName(), mtbl
  742. .getOwner(), mtbl.getCreateTime(), mtbl.getLastAccessTime(), mtbl
  743. .getRetention(), convertToStorageDescriptor(mtbl.getSd()),
  744. convertToFieldSchemas(mtbl.getPartitionKeys()), mtbl.getParameters(),
  745. mtbl.getViewOriginalText(), mtbl.getViewExpandedText(),
  746. tableType);
  747. }
  748. private MTable convertToMTable(Table tbl) throws InvalidObjectException,
  749. MetaException {
  750. if (tbl == null) {
  751. return null;
  752. }
  753. MDatabase mdb = null;
  754. try {
  755. mdb = getMDatabase(tbl.getDbName());
  756. } catch (NoSuchObjectException e) {
  757. LOG.error(StringUtils.stringifyException(e));
  758. throw new InvalidObjectException("Database " + tbl.getDbName()
  759. + " doesn't exist.");
  760. }
  761. // If the table has property EXTERNAL set, update table type
  762. // accordingly
  763. String tableType = tbl.getTableType();
  764. boolean isExternal = "TRUE".equals(tbl.getParameters().get("EXTERNAL"));
  765. if (TableType.MANAGED_TABLE.toString().equals(tableType)) {
  766. if (isExternal) {
  767. tableType = TableType.EXTERNAL_TABLE.toString();
  768. }
  769. }
  770. if (TableType.EXTERNAL_TABLE.toString().equals(tableType)) {
  771. if (!isExternal) {
  772. tableType = TableType.MANAGED_TABLE.toString();
  773. }
  774. }
  775. return new MTable(tbl.getTableName().toLowerCase(), mdb,
  776. convertToMStorageDescriptor(tbl.getSd()), tbl.getOwner(), tbl
  777. .getCreateTime(), tbl.getLastAccessTime(), tbl.getRetention(),
  778. convertToMFieldSchemas(tbl.getPartitionKeys()), tbl.getParameters(),
  779. tbl.getViewOriginalText(), tbl.getViewExpandedText(),
  780. tableType);
  781. }
  782. private List<MFieldSchema> convertToMFieldSchemas(List<FieldSchema> keys) {
  783. List<MFieldSchema> mkeys = null;
  784. if (keys != null) {
  785. mkeys = new ArrayList<MFieldSchema>(keys.size());
  786. for (FieldSchema part : keys) {
  787. mkeys.add(new MFieldSchema(part.getName().toLowerCase(),
  788. part.getType(), part.getComment()));
  789. }
  790. }
  791. return mkeys;
  792. }
  793. private List<FieldSchema> convertToFieldSchemas(List<MFieldSchema> mkeys) {
  794. List<FieldSchema> keys = null;
  795. if (mkeys != null) {
  796. keys = new ArrayList<FieldSchema>(mkeys.size());
  797. for (MFieldSchema part : mkeys) {
  798. keys.add(new FieldSchema(part.getName(), part.getType(), part
  799. .getComment()));
  800. }
  801. }
  802. return keys;
  803. }
  804. private List<MOrder> convertToMOrders(List<Order> keys) {
  805. List<MOrder> mkeys = null;
  806. if (keys != null) {
  807. mkeys = new ArrayList<MOrder>(keys.size());
  808. for (Order part : keys) {
  809. mkeys.add(new MOrder(part.getCol().toLowerCase(), part.getOrder()));
  810. }
  811. }
  812. return mkeys;
  813. }
  814. private List<Order> convertToOrders(List<MOrder> mkeys) {
  815. List<Order> keys = null;
  816. if (mkeys != null) {
  817. keys = new ArrayList<Order>();
  818. for (MOrder part : mkeys) {
  819. keys.add(new Order(part.getCol(), part.getOrder()));
  820. }
  821. }
  822. return keys;
  823. }
  824. private SerDeInfo converToSerDeInfo(MSerDeInfo ms) throws MetaException {
  825. if (ms == null) {
  826. throw new MetaException("Invalid SerDeInfo object");
  827. }
  828. return new SerDeInfo(ms.getName(), ms.getSerializationLib(), ms
  829. .getParameters());
  830. }
  831. private MSerDeInfo converToMSerDeInfo(SerDeInfo ms) throws MetaException {
  832. if (ms == null) {
  833. throw new MetaException("Invalid SerDeInfo object");
  834. }
  835. return new MSerDeInfo(ms.getName(), ms.getSerializationLib(), ms
  836. .getParameters());
  837. }
  838. // MSD and SD should be same objects. Not sure how to make then same right now
  839. // MSerdeInfo *& SerdeInfo should be same as well
  840. private StorageDescriptor convertToStorageDescriptor(MStorageDescriptor msd)
  841. throws MetaException {
  842. if (msd == null) {
  843. return null;
  844. }
  845. return new StorageDescriptor(convertToFieldSchemas(msd.getCols()), msd
  846. .getLocation(), msd.getInputFormat(), msd.getOutputFormat(), msd
  847. .isCompressed(), msd.getNumBuckets(), converToSerDeInfo(msd
  848. .getSerDeInfo()), msd.getBucketCols(), convertToOrders(msd
  849. .getSortCols()), msd.getParameters());
  850. }
  851. private MStorageDescriptor convertToMStorageDescriptor(StorageDescriptor sd)
  852. throws MetaException {
  853. if (sd == null) {
  854. return null;
  855. }
  856. return new MStorageDescriptor(convertToMFieldSchemas(sd.getCols()), sd
  857. .getLocation(), sd.getInputFormat(), sd.getOutputFormat(), sd
  858. .isCompressed(), sd.getNumBuckets(), converToMSerDeInfo(sd
  859. .getSerdeInfo()), sd.getBucketCols(),
  860. convertToMOrders(sd.getSortCols()), sd.getParameters());
  861. }
  862. public boolean addPartition(Partition part) throws InvalidObjectException,
  863. MetaException {
  864. boolean success = false;
  865. boolean commited = false;
  866. try {
  867. MTable table = this.getMTable(part.getDbName(), part.getTableName());
  868. List<MTablePrivilege> tabGrants = null;
  869. List<MTableColumnPrivilege> tabColumnGrants = null;
  870. if ("TRUE".equalsIgnoreCase(table.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) {
  871. tabGrants = this.listAllTableGrants(part
  872. .getDbName(), part.getTableName());
  873. tabColumnGrants = this.listTableAllColumnGrants(
  874. part.getDbName(), part.getTableName());
  875. }
  876. openTransaction();
  877. MPartition mpart = convertToMPart(part);
  878. pm.makePersistent(mpart);
  879. int now = (int)(System.currentTimeMillis()/1000);
  880. List<Object> toPersist = new ArrayList<Object>();
  881. if (tabGrants != null) {
  882. for (MTablePrivilege tab: tabGrants) {
  883. MPartitionPrivilege partGrant = new MPartitionPrivilege(tab
  884. .getPrincipalName(), tab.getPrincipalType(),
  885. mpart, tab.getPrivilege(), now, tab.getGrantor(), tab
  886. .getGrantorType(), tab.getGrantOption());
  887. toPersist.add(partGrant);
  888. }
  889. }
  890. if (tabColumnGrants != null) {
  891. for (MTableColumnPrivilege col : tabColumnGrants) {
  892. MPartitionColumnPrivilege partColumn = new MPartitionColumnPrivilege(col
  893. .getPrincipalName(), col.getPrincipalType(), mpart, col
  894. .getColumnName(), col.getPrivilege(), now, col.getGrantor(), col
  895. .getGrantorType(), col.getGrantOption());
  896. toPersist.add(partColumn);
  897. }
  898. if (toPersist.size() > 0) {
  899. pm.makePersistentAll(toPersist);
  900. }
  901. }
  902. commited = commitTransaction();
  903. success = true;
  904. } finally {
  905. if (!commited) {
  906. rollbackTransaction();
  907. }
  908. }
  909. return success;
  910. }
  911. public Partition getPartition(String dbName, String tableName,
  912. List<String> part_vals) throws NoSuchObjectException, MetaException {
  913. openTransaction();
  914. Partition part = convertToPart(getMPartition(dbName, tableName, part_vals));
  915. commitTransaction();
  916. if(part == null) {
  917. throw new NoSuchObjectException("partition values="
  918. + part_vals.toString());
  919. }
  920. return part;
  921. }
  922. private MPartition getMPartition(String dbName, String tableName,
  923. List<String> part_vals) throws MetaException {
  924. MPartition mpart = null;
  925. boolean commited = false;
  926. try {
  927. openTransaction();
  928. dbName = dbName.toLowerCase().trim();
  929. tableName = tableName.toLowerCase().trim();
  930. MTable mtbl = getMTable(dbName, tableName);
  931. if (mtbl == null) {
  932. commited = commitTransaction();
  933. return null;
  934. }
  935. // Change the query to use part_vals instead of the name which is
  936. // redundant
  937. String name = Warehouse.makePartName(convertToFieldSchemas(mtbl
  938. .getPartitionKeys()), part_vals);
  939. Query query = pm.newQuery(MPartition.class,
  940. "table.tableName == t1 && table.database.name == t2 && partitionName == t3");
  941. query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
  942. query.setUnique(true);
  943. mpart = (MPartition) query.execute(tableName, dbName, name);
  944. pm.retrieve(mpart);
  945. commited = commitTransaction();
  946. } finally {
  947. if (!commited) {
  948. rollbackTransaction();
  949. }
  950. }
  951. return mpart;
  952. }
  953. private MPartition convertToMPart(Partition part)
  954. throws InvalidObjectException, MetaException {
  955. if (part == null) {
  956. return null;
  957. }
  958. MTable mt = getMTable(part.getDbName(), part.getTableName());
  959. if (mt == null) {
  960. throw new InvalidObjectException(
  961. "Partition doesn't have a valid table or database name");
  962. }
  963. return new MPartition(Warehouse.makePartName(convertToFieldSchemas(mt
  964. .getPartitionKeys()), part.getValues()), mt, part.getValues(), part
  965. .getCreateTime(), part.getLastAccessTime(),
  966. convertToMStorageDescriptor(part.getSd()), part.getParameters());
  967. }
  968. private Partition convertToPart(MPartition mpart) throws MetaException {
  969. if (mpart == null) {
  970. return null;
  971. }
  972. return new Partition(mpart.getValues(), mpart.getTable().getDatabase()
  973. .getName(), mpart.getTable().getTableName(), mpart.getCreateTime(),
  974. mpart.getLastAccessTime(), convertToStorageDescriptor(mpart.getSd()),
  975. mpart.getParameters());
  976. }
  977. public boolean dropPartition(String dbName, String tableName,
  978. List<String> part_vals) throws MetaException {
  979. boolean success = false;
  980. try {
  981. openTransaction();
  982. MPartition part = getMPartition(dbName, tableName, part_vals);
  983. if (part != null) {
  984. List<MFieldSchema> schemas = part.getTable().getPartitionKeys();
  985. List<String> colNames = new ArrayList<String>();
  986. for (MFieldSchema col: schemas) {
  987. colNames.add(col.getName());
  988. }
  989. String partName = FileUtils.makePartName(colNames, part_vals);
  990. List<MPartitionPrivilege> partGrants = listPartitionGrants(
  991. dbName, tableName, partName);
  992. if (partGrants != null && partGrants.size() > 0) {
  993. pm.deletePersistentAll(partGrants);
  994. }
  995. List<MPartitionColumnPrivilege> partColumnGrants = listPartitionAllColumnGrants(
  996. dbName, tableName, partName);
  997. if (partColumnGrants != null && partColumnGrants.size() > 0) {
  998. pm.deletePersistentAll(partColumnGrants);
  999. }
  1000. pm.deletePersistent(part);
  1001. }
  1002. success = commitTransaction();
  1003. } finally {
  1004. if (!success) {
  1005. rollbackTransaction();
  1006. }
  1007. }
  1008. return success;
  1009. }
  1010. public List<Partition> getPartitions(String dbName, String tableName, int max)
  1011. throws MetaException {
  1012. openTransaction();
  1013. List<Partition> parts = convertToParts(listMPartitions(dbName, tableName,
  1014. max));
  1015. commitTransaction();
  1016. return parts;
  1017. }
  1018. @Override
  1019. public List<Partition> getPartitionsWithAuth(String dbName, String tblName,
  1020. short maxParts, String userName, List<String> groupNames)
  1021. throws MetaException, NoSuchObjectException, InvalidObjectException {
  1022. boolean success = false;
  1023. try {
  1024. openTransaction();
  1025. List<MPartition> mparts = listMPartitions(dbName, tblName, maxParts);
  1026. List<Partition> parts = new ArrayList<Partition>(mparts.size());
  1027. if (mparts != null && mparts.size()>0) {
  1028. for (MPartition mpart : mparts) {
  1029. MTable mtbl = mpart.getTable();
  1030. Partition part = convertToPart(mpart);
  1031. parts.add(part);
  1032. if ("TRUE".equalsIgnoreCase(mtbl.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) {
  1033. String partName = Warehouse.makePartName(this.convertToFieldSchemas(mtbl
  1034. .getPartitionKeys()), part.getValues());
  1035. PrincipalPrivilegeSet partAuth = this.getPartitionPrivilegeSet(dbName,
  1036. tblName, partName, userName, groupNames);
  1037. part.setPrivileges(partAuth);
  1038. }
  1039. }
  1040. }
  1041. success = commitTransaction();
  1042. return parts;
  1043. } finally {
  1044. if (!success) {
  1045. rollbackTransaction();
  1046. }
  1047. }
  1048. }
  1049. @Override
  1050. public Partition getPartitionWithAuth(String dbName, String tblName,
  1051. List<String> partVals, String user_name, List<String> group_names)
  1052. throws NoSuchObjectException, MetaException, InvalidObjectException {
  1053. boolean success = false;
  1054. try {
  1055. openTransaction();
  1056. MPartition mpart = getMPartition(dbName, tblName, partVals);
  1057. if (mpart == null) {
  1058. commitTransaction();
  1059. throw new NoSuchObjectException("partition values="
  1060. + partVals.toString());
  1061. }
  1062. Partition part = null;
  1063. MTable mtbl = mpart.getTable();
  1064. part = convertToPart(mpart);
  1065. if ("TRUE".equalsIgnoreCase(mtbl.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) {
  1066. String partName = Warehouse.makePartName(this.convertToFieldSchemas(mtbl
  1067. .getPartitionKeys()), partVals);
  1068. PrincipalPrivilegeSet partAuth = this.getPartitionPrivilegeSet(dbName,
  1069. tblName, partName, user_name, group_names);
  1070. part.setPrivileges(partAuth);
  1071. }
  1072. success = commitTransaction();
  1073. return part;
  1074. } finally {
  1075. if (!success) {
  1076. rollbackTransaction();
  1077. }
  1078. }
  1079. }
  1080. private List<Partition> convertToParts(List<MPartition> mparts)
  1081. throws MetaException {
  1082. List<Partition> parts = new ArrayList<Partition>(mparts.size());
  1083. for (MPartition mp : mparts) {
  1084. parts.add(convertToPart(mp));
  1085. }
  1086. return parts;
  1087. }
  1088. // TODO:pc implement max
  1089. public List<String> listPartitionNames(String dbName, String tableName,
  1090. short max) throws MetaException {
  1091. List<String> pns = new ArrayList<String>();
  1092. boolean success = false;
  1093. try {
  1094. openTransaction();
  1095. LOG.debug("Executing getPartitionNames");
  1096. dbName = dbName.toLowerCase().trim();
  1097. tableName = tableName.toLowerCase().trim();
  1098. Query q = pm.newQuery(
  1099. "select partitionName from org.apache.hadoop.hive.metastore.model.MPartition "
  1100. + "where table.database.name == t1 && table.tableName == t2 "
  1101. + "order by partitionName asc");
  1102. q.declareParameters("java.lang.String t1, java.lang.String t2");
  1103. q.setResult("partitionName");
  1104. Collection names = (Collection) q.execute(dbName, tableName);
  1105. pns = new ArrayList<String>();
  1106. for (Iterator i = names.iterator(); i.hasNext();) {
  1107. pns.add((String) i.next());
  1108. }
  1109. success = commitTransaction();
  1110. } finally {
  1111. if (!success) {
  1112. rollbackTransaction();
  1113. }
  1114. }
  1115. return pns;
  1116. }
  1117. // TODO:pc implement max
  1118. private List<MPartition> listMPartitions(String dbName, String tableName,
  1119. int max) {
  1120. boolean success = false;
  1121. List<MPartition> mparts = null;
  1122. try {
  1123. openTransaction();
  1124. LOG.debug("Executing listMPartitions");
  1125. dbName = dbName.toLowerCase().trim();
  1126. tableName = tableName.toLowerCase().trim();
  1127. Query query = pm.newQuery(MPartition.class,
  1128. "table.tableName == t1 && table.database.name == t2");
  1129. query.declareParameters("java.lang.String t1, java.lang.String t2");
  1130. mparts = (List<MPartition>) query.execute(tableName, dbName);
  1131. LOG.debug("Done executing query for listMPartitions");
  1132. pm.retrieveAll(mparts);
  1133. success = commitTransaction();
  1134. LOG.debug("Done retrieving all objects for listMPartitions");
  1135. } finally {
  1136. if (!success) {
  1137. rollbackTransaction();
  1138. }
  1139. }
  1140. return mparts;
  1141. }
  1142. @Override
  1143. public List<Partition> getPartitionsByFilter(String dbName, String tblName,
  1144. String filter, short maxParts) throws MetaException, NoSuchObjectException {
  1145. openTransaction();
  1146. List<Partition> parts = convertToParts(listMPartitionsByFilter(dbName,
  1147. tblName, filter, maxParts));
  1148. commitTransaction();
  1149. return parts;
  1150. }
  1151. private String makeQueryFilterString(MTable mtable, String filter,
  1152. Map<String, String> params)
  1153. throws MetaException {
  1154. StringBuilder queryBuilder = new StringBuilder(
  1155. "table.tableName == t1 && table.database.name == t2");
  1156. if( filter != null && filter.length() > 0) {
  1157. Table table = convertToTable(mtable);
  1158. CharStream cs = new ANTLRNoCaseStringStream(filter);
  1159. FilterLexer lexer = new FilterLexer(cs);
  1160. CommonTokenStream tokens = new CommonTokenStream();
  1161. tokens.setTokenSource (lexer);
  1162. FilterParser parser = new FilterParser(tokens);
  1163. try {
  1164. parser.filter();
  1165. } catch(RecognitionException re) {
  1166. throw new MetaException("Error parsing partition filter : " + re);
  1167. }
  1168. String jdoFilter = parser.tree.generateJDOFilter(table, params);
  1169. if( jdoFilter.trim().length() > 0 ) {
  1170. queryBuilder.append(" && ( ");
  1171. queryBuilder.append(jdoFilter.trim());
  1172. queryBuilder.append(" )");
  1173. }
  1174. }
  1175. return queryBuilder.toString();
  1176. }
  1177. private String makeParameterDeclarationString(Map<String, String> params) {
  1178. //Create the parameter declaration string
  1179. StringBuilder paramDecl = new StringBuilder();
  1180. for(String key : params.keySet() ) {
  1181. paramDecl.append(", java.lang.String " + key);
  1182. }
  1183. return paramDecl.toString();
  1184. }
  1185. private List<MPartition> listMPartitionsByFilter(String dbName, String tableName,
  1186. String filter, short maxParts) throws MetaException, NoSuchObjectException{
  1187. boolean success = false;
  1188. List<MPartition> mparts = null;
  1189. try {
  1190. openTransaction();
  1191. LOG.debug("Executing listMPartitionsByFilter");
  1192. dbName = dbName.toLowerCase();
  1193. tableName = tableName.toLowerCase();
  1194. MTable mtable = getMTable(dbName, tableName);
  1195. if( mtable == null ) {
  1196. throw new NoSuchObjectException("Specified database/table does not exist : "
  1197. + dbName + "." + tableName);
  1198. }
  1199. Map<String, String> params = new HashMap<String, String>();
  1200. String queryFilterString =
  1201. makeQueryFilterString(mtable, filter, params);
  1202. Query query = pm.newQuery(MPartition.class,
  1203. queryFilterString);
  1204. if( maxParts >= 0 ) {
  1205. //User specified a row limit, set it on the Query
  1206. query.setRange(0, maxParts);
  1207. }
  1208. LOG.debug("Filter specified is " + filter + "," +
  1209. " JDOQL filter is " + queryFilterString);
  1210. params.put("t1", tableName.trim());
  1211. params.put("t2", dbName.trim());
  1212. String parameterDeclaration = makeParameterDeclarationString(params);
  1213. query.declareParameters(parameterDeclaration);
  1214. query.setOrdering("partitionName ascending");
  1215. mparts = (List<MPartition>) query.executeWithMap(params);
  1216. LOG.debug("Done executing query for listMPartitionsByFilter");
  1217. pm.retrieveAll(mparts);
  1218. success = commitTransaction();
  1219. LOG.debug("Done retrieving all objects for listMPartitionsByFilter");
  1220. } finally {
  1221. if (!success) {
  1222. rollbackTransaction();
  1223. }
  1224. }
  1225. return mparts;
  1226. }
  1227. @Override
  1228. public List<String> listPartitionNamesByFilter(String dbName, String tableName,
  1229. String filter, short maxParts) throws MetaException {
  1230. boolean success = false;
  1231. List<String> partNames = new ArrayList<String>();
  1232. try {
  1233. openTransaction();
  1234. LOG.debug("Executing listMPartitionsByFilter");
  1235. dbName = dbName.toLowerCase();
  1236. tableName = tableName.toLowerCase();
  1237. MTable mtable = getMTable(dbName, tableName);
  1238. if( mtable == null ) {
  1239. // To be consistent with the behavior of listPartitionNames, if the
  1240. // table or db does not exist, we return an empty list
  1241. return partNames;
  1242. }
  1243. Map<String, String> params = new HashMap<String, String>();
  1244. String queryFilterString =
  1245. makeQueryFilterString(mtable, filter, params);
  1246. Query query = pm.newQuery(
  1247. "select partitionName from org.apache.hadoop.hive.metastore.model.MPartition "
  1248. + "where " + queryFilterString);
  1249. if( maxParts >= 0 ) {
  1250. //User specified a row limit, set it on the Query
  1251. query.setRange(0, maxParts);
  1252. }
  1253. LOG.debug("Filter specified is " + filter + "," +
  1254. " JDOQL filter is " + queryFilterString);
  1255. LOG.debug("Parms is " + params);
  1256. params.put("t1", tableName.trim());
  1257. params.put("t2", dbName.trim());
  1258. String parameterDeclaration = makeParameterDeclarationString(params);
  1259. query.declareParameters(parameterDeclaration);
  1260. query.setOrdering("partitionName ascending");
  1261. query.setResult("partitionName");
  1262. Collection names = (Collection) query.executeWithMap(params);
  1263. partNames = new ArrayList<String>();
  1264. for (Iterator i = names.iterator(); i.hasNext();) {
  1265. partNames.add((String) i.next());
  1266. }
  1267. LOG.debug("Done executing query for listMPartitionNamesByFilter");
  1268. success = commitTransaction();
  1269. LOG.debug("Done retrieving all objects for listMPartitionNamesByFilter");
  1270. } finally {
  1271. if (!success) {
  1272. rollbackTransaction();
  1273. }
  1274. }
  1275. return partNames;
  1276. }
  1277. public void alterTable(String dbname, String name, Table newTable)
  1278. throws InvalidObjectException, MetaException {
  1279. boolean success = false;
  1280. try {
  1281. openTransaction();
  1282. name = name.toLowerCase();
  1283. dbname = dbname.toLowerCase();
  1284. MTable newt = convertToMTable(newTable);
  1285. if (newt == null) {
  1286. throw new InvalidObjectException("new table is invalid");
  1287. }
  1288. MTable oldt = getMTable(dbname, name);
  1289. if (oldt == null) {
  1290. throw new MetaException("table " + name + " doesn't exist");
  1291. }
  1292. // For now only alter name, owner, paramters, cols, bucketcols are allowed
  1293. oldt.setTableName(newt.getTableName().toLowerCase());
  1294. oldt.setParameters(newt.getParameters());
  1295. oldt.setOwner(newt.getOwner());
  1296. oldt.setSd(newt.getSd());
  1297. oldt.setDatabase(newt.getDatabase());
  1298. oldt.setRetention(newt.getRetention());
  1299. oldt.setPartitionKeys(newt.getPartitionKeys());
  1300. oldt.setTableType(newt.getTableType());
  1301. oldt.setLastAccessTime(newt.getLastAccessTime());
  1302. // commit the changes
  1303. success = commitTransaction();
  1304. } finally {
  1305. if (!success) {
  1306. rollbackTransaction();
  1307. }
  1308. }
  1309. }
  1310. public void alterIndex(String dbname, String baseTblName, String name, Index newIndex)
  1311. throws InvalidObjectException, MetaException {
  1312. boolean success = false;
  1313. try {
  1314. openTransaction();
  1315. name = name.toLowerCase();
  1316. baseTblName = baseTblName.toLowerCase();
  1317. dbname = dbname.toLowerCase();
  1318. MIndex newi = convertToMIndex(newIndex);
  1319. if (newi == null) {
  1320. throw new InvalidObjectException("new index is invalid");
  1321. }
  1322. MIndex oldi = getMIndex(dbname, baseTblName, name);
  1323. if (oldi == null) {
  1324. throw new MetaException("index " + name + " doesn't exist");
  1325. }
  1326. // For now only alter paramters are allowed
  1327. oldi.setParameters(newi.getParameters());
  1328. // commit the changes
  1329. success = commitTransaction();
  1330. } finally {
  1331. if (!success) {
  1332. rollbackTransaction();
  1333. }
  1334. }
  1335. }
  1336. public void alterPartition(String dbname, String name, Partition newPart)
  1337. throws InvalidObjectException, MetaException {
  1338. boolean success = false;
  1339. try {
  1340. openTransaction();
  1341. name = name.toLowerCase();
  1342. dbname = dbname.toLowerCase();
  1343. MPartition oldp = getMPartition(dbname, name, newPart.getValues());
  1344. MPartition newp = convertToMPart(newPart);
  1345. if (oldp == null || newp == null) {
  1346. throw new InvalidObjectException("partition does not exist.");
  1347. }
  1348. oldp.setParameters(newPart.getParameters());
  1349. copyMSD(newp.getSd(), oldp.getSd());
  1350. if (newp.getCreateTime() != oldp.getCreateTime()) {
  1351. oldp.setCreateTime(newp.getCreateTime());
  1352. }
  1353. if (newp.getLastAccessTime() != oldp.getLastAccessTime()) {
  1354. oldp.setLastAccessTime(newp.getLastAccessTime());
  1355. }
  1356. // commit the changes
  1357. success = commitTransaction();
  1358. } finally {
  1359. if (!success) {
  1360. rollbackTransaction();
  1361. }
  1362. }
  1363. }
  1364. private void copyMSD(MStorageDescriptor newSd, MStorageDescriptor oldSd) {
  1365. oldSd.setLocation(newSd.getLocation());
  1366. oldSd.setCols(newSd.getCols());
  1367. oldSd.setBucketCols(newSd.getBucketCols());
  1368. oldSd.setCompressed(newSd.isCompressed());
  1369. oldSd.setInputFormat(newSd.getInputFormat());
  1370. oldSd.setOutputFormat(newSd.getOutputFormat());
  1371. oldSd.setNumBuckets(newSd.getNumBuckets());
  1372. oldSd.getSerDeInfo().setName(newSd.getSerDeInfo().getName());
  1373. oldSd.getSerDeInfo().setSerializationLib(
  1374. newSd.getSerDeInfo().getSerializationLib());
  1375. oldSd.getSerDeInfo().setParameters(newSd.getSerDeInfo().getParameters());
  1376. }
  1377. @Override
  1378. public boolean addIndex(Index index) throws InvalidObjectException,
  1379. MetaException {
  1380. boolean commited = false;
  1381. try {
  1382. openTransaction();
  1383. MIndex idx = convertToMIndex(index);
  1384. pm.makePersistent(idx);
  1385. commited = commitTransaction();
  1386. return true;
  1387. } finally {
  1388. if (!commited) {
  1389. rollbackTransaction();
  1390. return false;
  1391. }
  1392. }
  1393. }
  1394. private MIndex convertToMIndex(Index index) throws InvalidObjectException,
  1395. MetaException {
  1396. StorageDescriptor sd = index.getSd();
  1397. if (sd == null) {
  1398. throw new InvalidObjectException("Storage descriptor is not defined for index.");
  1399. }
  1400. MStorageDescriptor msd = this.convertToMStorageDescriptor(sd);
  1401. MTable origTable = getMTable(index.getDbName(), index.getOrigTableName());
  1402. if (origTable == null) {
  1403. throw new InvalidObjectException(
  1404. "Original table does not exist for the given index.");
  1405. }
  1406. MTable indexTable = getMTable(index.getDbName(), index.getIndexTableName());
  1407. if (indexTable == null) {
  1408. throw new InvalidObjectException(
  1409. "Underlying index table does not exist for the given index.");
  1410. }
  1411. return new MIndex(index.getIndexName(), origTable, index.getCreateTime(),
  1412. index.getLastAccessTime(), index.getParameters(), indexTable, msd,
  1413. index.getIndexHandlerClass(), index.isDeferredRebuild());
  1414. }
  1415. @Override
  1416. public boolean dropIndex(String dbName, String origTableName, String indexName)
  1417. throws MetaException {
  1418. boolean success = false;
  1419. try {
  1420. openTransaction();
  1421. MIndex index = getMIndex(dbName, origTableName, indexName);
  1422. if (index != null) {
  1423. pm.deletePersistent(index);
  1424. }
  1425. success = commitTransaction();
  1426. } finally {
  1427. if (!success) {
  1428. rollbackTransaction();
  1429. }
  1430. }
  1431. return success;
  1432. }
  1433. private MIndex getMIndex(String dbName, String originalTblName, String indexName) throws MetaException {
  1434. MIndex midx = null;
  1435. boolean commited = false;
  1436. try {
  1437. openTransaction();
  1438. dbName = dbName.toLowerCase().trim();
  1439. originalTblName = originalTblName.toLowerCase().trim();
  1440. MTable mtbl = getMTable(dbName, originalTblName);
  1441. if (mtbl == null) {
  1442. commited = commitTransaction();
  1443. return null;
  1444. }
  1445. Query query = pm.newQuery(MIndex.class,
  1446. "origTable.tableName == t1 && origTable.database.name == t2 && indexName == t3");
  1447. query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
  1448. query.setUnique(true);
  1449. midx = (MIndex) query.execute(originalTblName, dbName, indexName);
  1450. pm.retrieve(midx);
  1451. commited = commitTransaction();
  1452. } finally {
  1453. if (!commited) {
  1454. rollbackTransaction();
  1455. }
  1456. }
  1457. return midx;
  1458. }
  1459. @Override
  1460. public Index getIndex(String dbName, String origTableName, String indexName)
  1461. throws MetaException {
  1462. openTransaction();
  1463. MIndex mIndex = this.getMIndex(dbName, origTableName, indexName);
  1464. Index ret = convertToIndex(mIndex);
  1465. commitTransaction();
  1466. return ret;
  1467. }
  1468. private Index convertToIndex(MIndex mIndex) throws MetaException {
  1469. if(mIndex == null) {
  1470. return null;
  1471. }
  1472. return new Index(
  1473. mIndex.getIndexName(),
  1474. mIndex.getIndexHandlerClass(),
  1475. mIndex.getOrigTable().getDatabase().getName(),
  1476. mIndex.getOrigTable().getTableName(),
  1477. mIndex.getCreateTime(),
  1478. mIndex.getLastAccessTime(),
  1479. mIndex.getIndexTable().getTableName(),
  1480. this.convertToStorageDescriptor(mIndex.getSd()),
  1481. mIndex.getParameters(),
  1482. mIndex.getDeferredRebuild());
  1483. }
  1484. @Override
  1485. public List<Index> getIndexes(String dbName, String origTableName, int max)
  1486. throws MetaException {
  1487. boolean success = false;
  1488. try {
  1489. openTransaction();
  1490. List<MIndex> mIndexList = listMIndexes(dbName, origTableName, max);
  1491. List<Index> indexes = new ArrayList<Index>(mIndexList.size());
  1492. for (MIndex midx : mIndexList) {
  1493. indexes.add(this.convertToIndex(midx));
  1494. }
  1495. success = commitTransaction();
  1496. return indexes;
  1497. } finally {
  1498. if (!success) {
  1499. rollbackTransaction();
  1500. }
  1501. }
  1502. }
  1503. private List<MIndex> listMIndexes(String dbName, String origTableName,
  1504. int max) {
  1505. boolean success = false;
  1506. List<MIndex> mindexes = null;
  1507. try {
  1508. openTransaction();
  1509. LOG.debug("Executing listMIndexes");
  1510. dbName = dbName.toLowerCase().trim();
  1511. origTableName = origTableName.toLowerCase().trim();
  1512. Query query = pm.newQuery(MIndex.class,
  1513. "origTable.tableName == t1 && origTable.database.name == t2");
  1514. query.declareParameters("java.lang.String t1, java.lang.String t2");
  1515. mindexes = (List<MIndex>) query.execute(origTableName, dbName);
  1516. LOG.debug("Done executing query for listMIndexes");
  1517. pm.retrieveAll(mindexes);
  1518. success = commitTransaction();
  1519. LOG.debug("Done retrieving all objects for listMIndexes");
  1520. } finally {
  1521. if (!success) {
  1522. rollbackTransaction();
  1523. }
  1524. }
  1525. return mindexes;
  1526. }
  1527. @Override
  1528. public List<String> listIndexNames(String dbName, String origTableName,
  1529. short max) throws MetaException {
  1530. List<String> pns = new ArrayList<String>();
  1531. boolean success = false;
  1532. try {
  1533. openTransaction();
  1534. LOG.debug("Executing listIndexNames");
  1535. dbName = dbName.toLowerCase().trim();
  1536. origTableName = origTableName.toLowerCase().trim();
  1537. Query q = pm.newQuery(
  1538. "select indexName from org.apache.hadoop.hive.metastore.model.MIndex "
  1539. + "where origTable.database.name == t1 && origTable.tableName == t2 "
  1540. + "order by indexName asc");
  1541. q.declareParameters("java.lang.String t1, java.lang.String t2");
  1542. q.setResult("indexName");
  1543. Collection names = (Collection) q.execute(dbName, origTableName);
  1544. pns = new ArrayList<String>();
  1545. for (Iterator i = names.iterator(); i.hasNext();) {
  1546. pns.add((String) i.next());
  1547. }
  1548. success = commitTransaction();
  1549. } finally {
  1550. if (!success) {
  1551. rollbackTransaction();
  1552. }
  1553. }
  1554. return pns;
  1555. }
  1556. @Override
  1557. public boolean addRole(String roleName, String ownerName)
  1558. throws InvalidObjectException, MetaException, NoSuchObjectException {
  1559. boolean success = false;
  1560. boolean commited = false;
  1561. try {
  1562. openTransaction();
  1563. MRole nameCheck = this.getMRole(roleName);
  1564. if (nameCheck != null) {
  1565. throw new InvalidObjectException("Role " + roleName + " already exists.");
  1566. }
  1567. int now = (int)(System.currentTimeMillis()/1000);
  1568. MRole mRole = new MRole(roleName, now,
  1569. ownerName);
  1570. pm.makePersistent(mRole);
  1571. commited = commitTransaction();
  1572. success = true;
  1573. } finally {
  1574. if (!commited) {
  1575. rollbackTransaction();
  1576. }
  1577. }
  1578. return success;
  1579. }
  1580. @Override
  1581. public boolean grantRole(Role role, String userName,
  1582. PrincipalType principalType, String grantor, PrincipalType grantorType,
  1583. boolean grantOption) throws MetaException, NoSuchObjectException,InvalidObjectException {
  1584. boolean success = false;
  1585. boolean commited = false;
  1586. try {
  1587. MRoleMap roleMap = null;
  1588. try {
  1589. roleMap = this.getMSecurityUserRoleMap(userName, principalType, role
  1590. .getRoleName());
  1591. } catch (Exception e) {
  1592. }
  1593. if (roleMap != null) {
  1594. throw new InvalidObjectException("Principal " + userName
  1595. + " already has the role " + role.getRoleName());
  1596. }
  1597. openTransaction();
  1598. MRole mRole = getMRole(role.getRoleName());
  1599. long now = System.currentTimeMillis()/1000;
  1600. MRoleMap roleMember = new MRoleMap(userName, principalType.toString(),
  1601. mRole, (int) now, grantor, grantorType.toString(), grantOption);
  1602. pm.makePersistent(roleMember);
  1603. commited = commitTransaction();
  1604. success = true;
  1605. } finally {
  1606. if (!commited) {
  1607. rollbackTransaction();
  1608. }
  1609. }
  1610. return success;
  1611. }
  1612. @Override
  1613. public boolean revokeRole(Role role, String userName, PrincipalType principalType) throws MetaException, NoSuchObjectException {
  1614. boolean success = false;
  1615. try {
  1616. openTransaction();
  1617. MRoleMap roleMember = getMSecurityUserRoleMap(userName, principalType,
  1618. role.getRoleName());
  1619. pm.deletePersistent(roleMember);
  1620. success = commitTransaction();
  1621. } finally {
  1622. if (!success) {
  1623. rollbackTransaction();
  1624. }
  1625. }
  1626. return success;
  1627. }
  1628. private MRoleMap getMSecurityUserRoleMap(String userName,
  1629. PrincipalType principalType, String roleName) {
  1630. MRoleMap mRoleMember = null;
  1631. boolean commited = false;
  1632. try {
  1633. openTransaction();
  1634. Query query = pm.newQuery(MRoleMap.class, "principalName == t1 && principalType == t2 && role.roleName == t3");
  1635. query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
  1636. query.setUnique(true);
  1637. mRoleMember = (MRoleMap) query.executeWithArray(userName, principalType.toString(), roleName);
  1638. pm.retrieve(mRoleMember);
  1639. commited = commitTransaction();
  1640. } finally {
  1641. if (!commited) {
  1642. rollbackTransaction();
  1643. }
  1644. }
  1645. return mRoleMember;
  1646. }
  1647. @Override
  1648. public boolean removeRole(String roleName) throws MetaException,
  1649. NoSuchObjectException {
  1650. boolean success = false;
  1651. try {
  1652. openTransaction();
  1653. MRole mRol = getMRole(roleName);
  1654. pm.retrieve(mRol);
  1655. if (mRol != null) {
  1656. // first remove all the membership, the membership that this role has
  1657. // been granted
  1658. List<MRoleMap> roleMap = listRoleMembers(mRol);
  1659. if (roleMap.size() > 0) {
  1660. pm.deletePersistentAll(roleMap);
  1661. }
  1662. List<MRoleMap> roleMember = listMSecurityPrincipalMembershipRole(mRol
  1663. .getRoleName(), PrincipalType.ROLE);
  1664. if (roleMember.size() > 0) {
  1665. pm.deletePersistentAll(roleMember);
  1666. }
  1667. // then remove all the grants
  1668. List<MGlobalPrivilege> userGrants = listPrincipalGlobalGrants(
  1669. mRol.getRoleName(), PrincipalType.ROLE);
  1670. if (userGrants.size() > 0) {
  1671. pm.deletePersistentAll(userGrants);
  1672. }
  1673. List<MDBPrivilege> dbGrants = listPrincipalAllDBGrant(mRol
  1674. .getRoleName(), PrincipalType.ROLE);
  1675. if (dbGrants.size() > 0) {
  1676. pm.deletePersistentAll(dbGrants);
  1677. }
  1678. List<MTablePrivilege> tabPartGrants = listPrincipalAllTableGrants(
  1679. mRol.getRoleName(), PrincipalType.ROLE);
  1680. if (tabPartGrants.size() > 0) {
  1681. pm.deletePersistentAll(tabPartGrants);
  1682. }
  1683. List<MPartitionPrivilege> partGrants = listPrincipalAllPartitionGrants(
  1684. mRol.getRoleName(), PrincipalType.ROLE);
  1685. if (partGrants.size() > 0) {
  1686. pm.deletePersistentAll(partGrants);
  1687. }
  1688. List<MTableColumnPrivilege> tblColumnGrants = listPrincipalAllTableColumnGrants(
  1689. mRol.getRoleName(), PrincipalType.ROLE);
  1690. if (tblColumnGrants.size() > 0) {
  1691. pm.deletePersistentAll(tblColumnGrants);
  1692. }
  1693. List<MPartitionColumnPrivilege> partColumnGrants = listPrincipalAllPartitionColumnGrants(
  1694. mRol.getRoleName(), PrincipalType.ROLE);
  1695. if (tblColumnGrants.size() > 0) {
  1696. pm.deletePersistentAll(partColumnGrants);
  1697. }
  1698. // finally remove the role
  1699. pm.deletePersistent(mRol);
  1700. }
  1701. success = commitTransaction();
  1702. } finally {
  1703. if (!success) {
  1704. rollbackTransaction();
  1705. }
  1706. }
  1707. return success;
  1708. }
  1709. private List<MRoleMap> listRoles(String userName,
  1710. List<String> groupNames) {
  1711. List<MRoleMap> ret = new ArrayList<MRoleMap>();
  1712. if(userName != null) {
  1713. ret.addAll(listRoles(userName, PrincipalType.USER));
  1714. }
  1715. if (groupNames != null) {
  1716. for (String groupName: groupNames) {
  1717. ret.addAll(listRoles(groupName, PrincipalType.GROUP));
  1718. }
  1719. }
  1720. return ret;
  1721. }
  1722. @SuppressWarnings("unchecked")
  1723. @Override
  1724. public List<MRoleMap> listRoles(String principalName,
  1725. PrincipalType principalType) {
  1726. boolean success = false;
  1727. List<MRoleMap> mRoleMember = null;
  1728. try {
  1729. openTransaction();
  1730. LOG.debug("Executing listRoles");
  1731. Query query = pm
  1732. .newQuery(
  1733. MRoleMap.class,
  1734. "principalName == t1 && principalType == t2");
  1735. query
  1736. .declareParameters("java.lang.String t1, java.lang.String t2");
  1737. query.setUnique(false);
  1738. mRoleMember = (List<MRoleMap>) query.executeWithArray(
  1739. principalName, principalType.toString());
  1740. LOG.debug("Done executing query for listMSecurityUserRoleMap");
  1741. pm.retrieveAll(mRoleMember);
  1742. success = commitTransaction();
  1743. LOG.debug("Done retrieving all objects for listMSecurityUserRoleMap");
  1744. } finally {
  1745. if (!success) {
  1746. rollbackTransaction();
  1747. }
  1748. }
  1749. return mRoleMember;
  1750. }
  1751. @SuppressWarnings("unchecked")
  1752. private List<MRoleMap> listMSecurityPrincipalMembershipRole(final String roleName,
  1753. final PrincipalType principalType) {
  1754. boolean success = false;
  1755. List<MRoleMap> mRoleMemebership = null;
  1756. try {
  1757. openTransaction();
  1758. LOG.debug("Executing listMSecurityPrincipalMembershipRole");
  1759. Query query = pm.newQuery(MRoleMap.class,
  1760. "principalName == t1 && principalType == t2");
  1761. query
  1762. .declareParameters("java.lang.String t1, java.lang.String t2");
  1763. mRoleMemebership = (List<MRoleMap>) query.execute(roleName, principalType.toString());
  1764. LOG
  1765. .debug("Done executing query for listMSecurityPrincipalMembershipRole");
  1766. pm.retrieveAll(mRoleMemebership);
  1767. success = commitTransaction();
  1768. LOG
  1769. .debug("Done retrieving all objects for listMSecurityPrincipalMembershipRole");
  1770. } finally {
  1771. if (!success) {
  1772. rollbackTransaction();
  1773. }
  1774. }
  1775. return mRoleMemebership;
  1776. }
  1777. public Role getRole(String roleName) throws NoSuchObjectException {
  1778. MRole mRole = this.getMRole(roleName);
  1779. if (mRole == null) {
  1780. throw new NoSuchObjectException(roleName + " role can not be found.");
  1781. }
  1782. Role ret = new Role(mRole.getRoleName(), mRole.getCreateTime(), mRole
  1783. .getOwnerName());
  1784. return ret;
  1785. }
  1786. private MRole getMRole(String roleName) {
  1787. MRole mrole = null;
  1788. boolean commited = false;
  1789. try {
  1790. openTransaction();
  1791. Query query = pm.newQuery(MRole.class, "roleName == t1");
  1792. query.declareParameters("java.lang.String t1");
  1793. query.setUnique(true);
  1794. mrole = (MRole) query.execute(roleName);
  1795. pm.retrieve(mrole);
  1796. commited = commitTransaction();
  1797. } finally {
  1798. if (!commited) {
  1799. rollbackTransaction();
  1800. }
  1801. }
  1802. return mrole;
  1803. }
  1804. public List<String> listRoleNames() {
  1805. boolean success = false;
  1806. try {
  1807. openTransaction();
  1808. LOG.debug("Executing listAllRoleNames");
  1809. Query query = pm.newQuery("select roleName from org.apache.hadoop.hive.metastore.model.MRole");
  1810. query.setResult("roleName");
  1811. Collection names = (Collection) query.execute();
  1812. List<String> roleNames = new ArrayList<String>();
  1813. for (Iterator i = names.iterator(); i.hasNext();) {
  1814. roleNames.add((String) i.next());
  1815. }
  1816. success = commitTransaction();
  1817. return roleNames;
  1818. } finally {
  1819. if (!success) {
  1820. rollbackTransaction();
  1821. }
  1822. }
  1823. }
  1824. @Override
  1825. public PrincipalPrivilegeSet getUserPrivilegeSet(String userName,
  1826. List<String> groupNames) throws InvalidObjectException, MetaException {
  1827. boolean commited = false;
  1828. PrincipalPrivilegeSet ret = new PrincipalPrivilegeSet();
  1829. try {
  1830. openTransaction();
  1831. if (userName != null) {
  1832. List<MGlobalPrivilege> user = this.listPrincipalGlobalGrants(userName, PrincipalType.USER);
  1833. if(user.size()>0) {
  1834. Map<String, List<PrivilegeGrantInfo>> userPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1835. List<PrivilegeGrantInfo> grantInfos = new ArrayList<PrivilegeGrantInfo>(user.size());
  1836. for (int i = 0; i < user.size(); i++) {
  1837. MGlobalPrivilege item = user.get(i);
  1838. grantInfos.add(new PrivilegeGrantInfo(item.getPrivilege(), item
  1839. .getCreateTime(), item.getGrantor(), getPrincipalTypeFromStr(item
  1840. .getGrantorType()), item.getGrantOption()));
  1841. }
  1842. userPriv.put(userName, grantInfos);
  1843. ret.setUserPrivileges(userPriv);
  1844. }
  1845. }
  1846. if (groupNames != null && groupNames.size() > 0) {
  1847. Map<String, List<PrivilegeGrantInfo>> groupPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1848. for(String groupName: groupNames) {
  1849. List<MGlobalPrivilege> group = this.listPrincipalGlobalGrants(groupName, PrincipalType.GROUP);
  1850. if(group.size()>0) {
  1851. List<PrivilegeGrantInfo> grantInfos = new ArrayList<PrivilegeGrantInfo>(group.size());
  1852. for (int i = 0; i < group.size(); i++) {
  1853. MGlobalPrivilege item = group.get(i);
  1854. grantInfos.add(new PrivilegeGrantInfo(item.getPrivilege(), item
  1855. .getCreateTime(), item.getGrantor(), getPrincipalTypeFromStr(item
  1856. .getGrantorType()), item.getGrantOption()));
  1857. }
  1858. groupPriv.put(groupName, grantInfos);
  1859. }
  1860. }
  1861. ret.setGroupPrivileges(groupPriv);
  1862. }
  1863. commited = commitTransaction();
  1864. } finally {
  1865. if (!commited) {
  1866. rollbackTransaction();
  1867. }
  1868. }
  1869. return ret;
  1870. }
  1871. public List<PrivilegeGrantInfo> getDBPrivilege(String dbName,
  1872. String principalName, PrincipalType principalType)
  1873. throws InvalidObjectException, MetaException {
  1874. dbName = dbName.toLowerCase().trim();
  1875. if (principalName != null) {
  1876. List<MDBPrivilege> userNameDbPriv = this.listPrincipalDBGrants(
  1877. principalName, principalType, dbName);
  1878. if (userNameDbPriv != null && userNameDbPriv.size() > 0) {
  1879. List<PrivilegeGrantInfo> grantInfos = new ArrayList<PrivilegeGrantInfo>(
  1880. userNameDbPriv.size());
  1881. for (int i = 0; i < userNameDbPriv.size(); i++) {
  1882. MDBPrivilege item = userNameDbPriv.get(i);
  1883. grantInfos.add(new PrivilegeGrantInfo(item.getPrivilege(), item
  1884. .getCreateTime(), item.getGrantor(), getPrincipalTypeFromStr(item
  1885. .getGrantorType()), item.getGrantOption()));
  1886. }
  1887. return grantInfos;
  1888. }
  1889. }
  1890. return new ArrayList<PrivilegeGrantInfo>(0);
  1891. }
  1892. @Override
  1893. public PrincipalPrivilegeSet getDBPrivilegeSet(String dbName,
  1894. String userName, List<String> groupNames) throws InvalidObjectException,
  1895. MetaException {
  1896. boolean commited = false;
  1897. dbName = dbName.toLowerCase().trim();
  1898. PrincipalPrivilegeSet ret = new PrincipalPrivilegeSet();
  1899. try {
  1900. openTransaction();
  1901. if (userName != null) {
  1902. Map<String, List<PrivilegeGrantInfo>> dbUserPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1903. dbUserPriv.put(userName, getDBPrivilege(dbName, userName,
  1904. PrincipalType.USER));
  1905. ret.setUserPrivileges(dbUserPriv);
  1906. }
  1907. if (groupNames != null && groupNames.size() > 0) {
  1908. Map<String, List<PrivilegeGrantInfo>> dbGroupPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1909. for (String groupName : groupNames) {
  1910. dbGroupPriv.put(groupName, getDBPrivilege(dbName, groupName,
  1911. PrincipalType.GROUP));
  1912. }
  1913. ret.setGroupPrivileges(dbGroupPriv);
  1914. }
  1915. List<MRoleMap> roles = listRoles(userName, groupNames);
  1916. if (roles != null && roles.size() > 0) {
  1917. Map<String, List<PrivilegeGrantInfo>> dbRolePriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1918. for (MRoleMap role : roles) {
  1919. String name = role.getRole().getRoleName();
  1920. dbRolePriv
  1921. .put(name, getDBPrivilege(dbName, name, PrincipalType.ROLE));
  1922. }
  1923. ret.setRolePrivileges(dbRolePriv);
  1924. }
  1925. commited = commitTransaction();
  1926. } finally {
  1927. if (!commited) {
  1928. rollbackTransaction();
  1929. }
  1930. }
  1931. return ret;
  1932. }
  1933. @Override
  1934. public PrincipalPrivilegeSet getPartitionPrivilegeSet(String dbName,
  1935. String tableName, String partition, String userName,
  1936. List<String> groupNames) throws InvalidObjectException, MetaException {
  1937. boolean commited = false;
  1938. PrincipalPrivilegeSet ret = new PrincipalPrivilegeSet();
  1939. tableName = tableName.toLowerCase().trim();
  1940. dbName = dbName.toLowerCase().trim();
  1941. try {
  1942. openTransaction();
  1943. if (userName != null) {
  1944. Map<String, List<PrivilegeGrantInfo>> partUserPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1945. partUserPriv.put(userName, getPartitionPrivilege(dbName,
  1946. tableName, partition, userName, PrincipalType.USER));
  1947. ret.setUserPrivileges(partUserPriv);
  1948. }
  1949. if (groupNames != null && groupNames.size() > 0) {
  1950. Map<String, List<PrivilegeGrantInfo>> partGroupPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1951. for (String groupName : groupNames) {
  1952. partGroupPriv.put(groupName, getPartitionPrivilege(dbName, tableName,
  1953. partition, groupName, PrincipalType.GROUP));
  1954. }
  1955. ret.setGroupPrivileges(partGroupPriv);
  1956. }
  1957. List<MRoleMap> roles = listRoles(userName, groupNames);
  1958. if (roles != null && roles.size() > 0) {
  1959. Map<String, List<PrivilegeGrantInfo>> partRolePriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1960. for (MRoleMap role : roles) {
  1961. String roleName = role.getRole().getRoleName();
  1962. partRolePriv.put(roleName, getPartitionPrivilege(dbName, tableName,
  1963. partition, roleName, PrincipalType.ROLE));
  1964. }
  1965. ret.setRolePrivileges(partRolePriv);
  1966. }
  1967. commited = commitTransaction();
  1968. } finally {
  1969. if (!commited) {
  1970. rollbackTransaction();
  1971. }
  1972. }
  1973. return ret;
  1974. }
  1975. @Override
  1976. public PrincipalPrivilegeSet getTablePrivilegeSet(String dbName,
  1977. String tableName, String userName, List<String> groupNames)
  1978. throws InvalidObjectException, MetaException {
  1979. boolean commited = false;
  1980. PrincipalPrivilegeSet ret = new PrincipalPrivilegeSet();
  1981. tableName = tableName.toLowerCase().trim();
  1982. dbName = dbName.toLowerCase().trim();
  1983. try {
  1984. openTransaction();
  1985. if (userName != null) {
  1986. Map<String, List<PrivilegeGrantInfo>> tableUserPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1987. tableUserPriv.put(userName, getTablePrivilege(dbName,
  1988. tableName, userName, PrincipalType.USER));
  1989. ret.setUserPrivileges(tableUserPriv);
  1990. }
  1991. if (groupNames != null && groupNames.size() > 0) {
  1992. Map<String, List<PrivilegeGrantInfo>> tableGroupPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  1993. for (String groupName : groupNames) {
  1994. tableGroupPriv.put(groupName, getTablePrivilege(dbName, tableName,
  1995. groupName, PrincipalType.GROUP));
  1996. }
  1997. ret.setGroupPrivileges(tableGroupPriv);
  1998. }
  1999. List<MRoleMap> roles = listRoles(userName, groupNames);
  2000. if (roles != null && roles.size() > 0) {
  2001. Map<String, List<PrivilegeGrantInfo>> tableRolePriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  2002. for (MRoleMap role : roles) {
  2003. String roleName = role.getRole().getRoleName();
  2004. tableRolePriv.put(roleName, getTablePrivilege(dbName, tableName,
  2005. roleName, PrincipalType.ROLE));
  2006. }
  2007. ret.setRolePrivileges(tableRolePriv);
  2008. }
  2009. commited = commitTransaction();
  2010. } finally {
  2011. if (!commited) {
  2012. rollbackTransaction();
  2013. }
  2014. }
  2015. return ret;
  2016. }
  2017. @Override
  2018. public PrincipalPrivilegeSet getColumnPrivilegeSet(String dbName,
  2019. String tableName, String partitionName, String columnName,
  2020. String userName, List<String> groupNames) throws InvalidObjectException,
  2021. MetaException {
  2022. tableName = tableName.toLowerCase().trim();
  2023. dbName = dbName.toLowerCase().trim();
  2024. columnName = columnName.toLowerCase().trim();
  2025. boolean commited = false;
  2026. PrincipalPrivilegeSet ret = new PrincipalPrivilegeSet();
  2027. try {
  2028. openTransaction();
  2029. if (userName != null) {
  2030. Map<String, List<PrivilegeGrantInfo>> columnUserPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  2031. columnUserPriv.put(userName, getColumnPrivilege(dbName, tableName,
  2032. columnName, partitionName, userName, PrincipalType.USER));
  2033. ret.setUserPrivileges(columnUserPriv);
  2034. }
  2035. if (groupNames != null && groupNames.size() > 0) {
  2036. Map<String, List<PrivilegeGrantInfo>> columnGroupPriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  2037. for (String groupName : groupNames) {
  2038. columnGroupPriv.put(groupName, getColumnPrivilege(dbName, tableName,
  2039. columnName, partitionName, groupName, PrincipalType.GROUP));
  2040. }
  2041. ret.setGroupPrivileges(columnGroupPriv);
  2042. }
  2043. List<MRoleMap> roles = listRoles(userName, groupNames);
  2044. if (roles != null && roles.size() > 0) {
  2045. Map<String, List<PrivilegeGrantInfo>> columnRolePriv = new HashMap<String, List<PrivilegeGrantInfo>>();
  2046. for (MRoleMap role : roles) {
  2047. String roleName = role.getRole().getRoleName();
  2048. columnRolePriv.put(roleName, getColumnPrivilege(dbName, tableName,
  2049. columnName, partitionName, roleName, PrincipalType.ROLE));
  2050. }
  2051. ret.setRolePrivileges(columnRolePriv);
  2052. }
  2053. commited = commitTransaction();
  2054. } finally {
  2055. if (!commited) {
  2056. rollbackTransaction();
  2057. }
  2058. }
  2059. return ret;
  2060. }
  2061. private List<PrivilegeGrantInfo> getPartitionPrivilege(String dbName,
  2062. String tableName, String partName, String principalName,
  2063. PrincipalType principalType) {
  2064. tableName = tableName.toLowerCase().trim();
  2065. dbName = dbName.toLowerCase().trim();
  2066. if (principalName != null) {
  2067. List<MPartitionPrivilege> userNameTabPartPriv = this
  2068. .listPrincipalPartitionGrants(principalName, principalType,
  2069. dbName, tableName, partName);
  2070. if (userNameTabPartPriv != null && userNameTabPartPriv.size() > 0) {
  2071. List<PrivilegeGrantInfo> grantInfos = new ArrayList<PrivilegeGrantInfo>(
  2072. userNameTabPartPriv.size());
  2073. for (int i = 0; i < userNameTabPartPriv.size(); i++) {
  2074. MPartitionPrivilege item = userNameTabPartPriv.get(i);
  2075. grantInfos.add(new PrivilegeGrantInfo(item.getPrivilege(), item
  2076. .getCreateTime(), item.getGrantor(),
  2077. getPrincipalTypeFromStr(item.getGrantorType()), item.getGrantOption()));
  2078. }
  2079. return grantInfos;
  2080. }
  2081. }
  2082. return new ArrayList<PrivilegeGrantInfo>(0);
  2083. }
  2084. private PrincipalType getPrincipalTypeFromStr(String str) {
  2085. return str == null ? null : PrincipalType.valueOf(str);
  2086. }
  2087. private List<PrivilegeGrantInfo> getTablePrivilege(String dbName,
  2088. String tableName, String principalName, PrincipalType principalType) {
  2089. tableName = tableName.toLowerCase().trim();
  2090. dbName = dbName.toLowerCase().trim();
  2091. if (principalName != null) {
  2092. List<MTablePrivilege> userNameTabPartPriv = this
  2093. .listAllTableGrants(principalName, principalType,
  2094. dbName, tableName);
  2095. if (userNameTabPartPriv != null && userNameTabPartPriv.size() > 0) {
  2096. List<PrivilegeGrantInfo> grantInfos = new ArrayList<PrivilegeGrantInfo>(
  2097. userNameTabPartPriv.size());
  2098. for (int i = 0; i < userNameTabPartPriv.size(); i++) {
  2099. MTablePrivilege item = userNameTabPartPriv.get(i);
  2100. grantInfos.add(new PrivilegeGrantInfo(item.getPrivilege(), item
  2101. .getCreateTime(), item.getGrantor(), getPrincipalTypeFromStr(item
  2102. .getGrantorType()), item.getGrantOption()));
  2103. }
  2104. return grantInfos;
  2105. }
  2106. }
  2107. return new ArrayList<PrivilegeGrantInfo>(0);
  2108. }
  2109. private List<PrivilegeGrantInfo> getColumnPrivilege(String dbName,
  2110. String tableName, String columnName, String partitionName,
  2111. String principalName, PrincipalType principalType) {
  2112. tableName = tableName.toLowerCase().trim();
  2113. dbName = dbName.toLowerCase().trim();
  2114. columnName = columnName.toLowerCase().trim();
  2115. if (partitionName == null) {
  2116. List<MTableColumnPrivilege> userNameColumnPriv = this
  2117. .listPrincipalTableColumnGrants(principalName, principalType,
  2118. dbName, tableName, columnName);
  2119. if (userNameColumnPriv != null && userNameColumnPriv.size() > 0) {
  2120. List<PrivilegeGrantInfo> grantInfos = new ArrayList<PrivilegeGrantInfo>(
  2121. userNameColumnPriv.size());
  2122. for (int i = 0; i < userNameColumnPriv.size(); i++) {
  2123. MTableColumnPrivilege item = userNameColumnPriv.get(i);
  2124. grantInfos.add(new PrivilegeGrantInfo(item.getPrivilege(), item
  2125. .getCreateTime(), item.getGrantor(), getPrincipalTypeFromStr(item
  2126. .getGrantorType()), item.getGrantOption()));
  2127. }
  2128. return grantInfos;
  2129. }
  2130. } else {
  2131. List<MPartitionColumnPrivilege> userNameColumnPriv = this
  2132. .listPrincipalPartitionColumnGrants(principalName,
  2133. principalType, dbName, tableName, partitionName, columnName);
  2134. if (userNameColumnPriv != null && userNameColumnPriv.size() > 0) {
  2135. List<PrivilegeGrantInfo> grantInfos = new ArrayList<PrivilegeGrantInfo>(
  2136. userNameColumnPriv.size());
  2137. for (int i = 0; i < userNameColumnPriv.size(); i++) {
  2138. MPartitionColumnPrivilege item = userNameColumnPriv.get(i);
  2139. grantInfos.add(new PrivilegeGrantInfo(item.getPrivilege(), item
  2140. .getCreateTime(), item.getGrantor(), getPrincipalTypeFromStr(item
  2141. .getGrantorType()), item.getGrantOption()));
  2142. }
  2143. return grantInfos;
  2144. }
  2145. }
  2146. return new ArrayList<PrivilegeGrantInfo>(0);
  2147. }
  2148. @Override
  2149. public boolean grantPrivileges(PrivilegeBag privileges) throws InvalidObjectException,
  2150. MetaException, NoSuchObjectException {
  2151. boolean committed = false;
  2152. int now = (int) (System.currentTimeMillis() / 1000);
  2153. try {
  2154. openTransaction();
  2155. List<Object> persistentObjs = new ArrayList<Object>();
  2156. List<HiveObjectPrivilege> privilegeList = privileges.getPrivileges();
  2157. if (privilegeList != null && privilegeList.size() > 0) {
  2158. Iterator<HiveObjectPrivilege> privIter = privilegeList.iterator();
  2159. Set<String> privSet = new HashSet<String>();
  2160. while (privIter.hasNext()) {
  2161. HiveObjectPrivilege privDef = privIter.next();
  2162. HiveObjectRef hiveObject = privDef.getHiveObject();
  2163. String privilegeStr = privDef.getGrantInfo().getPrivilege();
  2164. String[] privs = privilegeStr.split(",");
  2165. String userName = privDef.getPrincipalName();
  2166. PrincipalType principalType = privDef.getPrincipalType();
  2167. String grantor = privDef.getGrantInfo().getGrantor();
  2168. String grantorType = privDef.getGrantInfo().getGrantorType().toString();
  2169. boolean grantOption = privDef.getGrantInfo().isGrantOption();
  2170. privSet.clear();
  2171. if (hiveObject.getObjectType() == HiveObjectType.GLOBAL) {
  2172. List<MGlobalPrivilege> globalPrivs = this
  2173. .listPrincipalGlobalGrants(userName, principalType);
  2174. if (globalPrivs != null) {
  2175. for (MGlobalPrivilege priv : globalPrivs) {
  2176. if (priv.getGrantor().equalsIgnoreCase(grantor)) {
  2177. privSet.add(priv.getPrivilege());
  2178. }
  2179. }
  2180. }
  2181. for (String privilege : privs) {
  2182. if (privSet.contains(privilege)) {
  2183. throw new InvalidObjectException(privilege
  2184. + " is already granted by " + grantor);
  2185. }
  2186. MGlobalPrivilege mGlobalPrivs = new MGlobalPrivilege(userName,
  2187. principalType.toString(), privilege, now, grantor, grantorType, grantOption);
  2188. persistentObjs.add(mGlobalPrivs);
  2189. }
  2190. } else if (hiveObject.getObjectType() == HiveObjectType.DATABASE) {
  2191. MDatabase dbObj = getMDatabase(hiveObject.getDbName());
  2192. if (dbObj != null) {
  2193. List<MDBPrivilege> dbPrivs = this.listPrincipalDBGrants(
  2194. userName, principalType, hiveObject.getDbName());
  2195. if (dbPrivs != null) {
  2196. for (MDBPrivilege priv : dbPrivs) {
  2197. if (priv.getGrantor().equalsIgnoreCase(grantor)) {
  2198. privSet.add(priv.getPrivilege());
  2199. }
  2200. }
  2201. }
  2202. for (String privilege : privs) {
  2203. if (privSet.contains(privilege)) {
  2204. throw new InvalidObjectException(privilege
  2205. + " is already granted on database "
  2206. + hiveObject.getDbName() + " by " + grantor);
  2207. }
  2208. MDBPrivilege mDb = new MDBPrivilege(userName, principalType
  2209. .toString(), dbObj, privilege, now, grantor, grantorType, grantOption);
  2210. persistentObjs.add(mDb);
  2211. }
  2212. }
  2213. } else if (hiveObject.getObjectType() == HiveObjectType.TABLE) {
  2214. MTable tblObj = getMTable(hiveObject.getDbName(), hiveObject
  2215. .getObjectName());
  2216. if (tblObj != null) {
  2217. List<MTablePrivilege> tablePrivs = this
  2218. .listAllTableGrants(userName, principalType,
  2219. hiveObject.getDbName(), hiveObject.getObjectName());
  2220. if (tablePrivs != null) {
  2221. for (MTablePrivilege priv : tablePrivs) {
  2222. if (priv.getGrantor() != null
  2223. && priv.getGrantor().equalsIgnoreCase(grantor)) {
  2224. privSet.add(priv.getPrivilege());
  2225. }
  2226. }
  2227. }
  2228. for (String privilege : privs) {
  2229. if (privSet.contains(privilege)) {
  2230. throw new InvalidObjectException(privilege
  2231. + " is already granted on table ["
  2232. + hiveObject.getDbName() + ","
  2233. + hiveObject.getObjectName() + "] by " + grantor);
  2234. }
  2235. MTablePrivilege mTab = new MTablePrivilege(
  2236. userName, principalType.toString(), tblObj,
  2237. privilege, now, grantor, grantorType, grantOption);
  2238. persistentObjs.add(mTab);
  2239. }
  2240. }
  2241. } else if (hiveObject.getObjectType() == HiveObjectType.PARTITION) {
  2242. MPartition partObj = this.getMPartition(hiveObject.getDbName(),
  2243. hiveObject.getObjectName(), hiveObject.getPartValues());
  2244. String partName = null;
  2245. if (partObj != null) {
  2246. partName = partObj.getPartitionName();
  2247. List<MPartitionPrivilege> partPrivs = this
  2248. .listPrincipalPartitionGrants(userName,
  2249. principalType, hiveObject.getDbName(), hiveObject
  2250. .getObjectName(), partObj.getPartitionName());
  2251. if (partPrivs != null) {
  2252. for (MPartitionPrivilege priv : partPrivs) {
  2253. if (priv.getGrantor().equalsIgnoreCase(grantor)) {
  2254. privSet.add(priv.getPrivilege());
  2255. }
  2256. }
  2257. }
  2258. for (String privilege : privs) {
  2259. if (privSet.contains(privilege)) {
  2260. throw new InvalidObjectException(privilege
  2261. + " is already granted on partition ["
  2262. + hiveObject.getDbName() + ","
  2263. + hiveObject.getObjectName() + ","
  2264. + partName + "] by " + grantor);
  2265. }
  2266. MPartitionPrivilege mTab = new MPartitionPrivilege(userName,
  2267. principalType.toString(), partObj, privilege, now, grantor,
  2268. grantorType, grantOption);
  2269. persistentObjs.add(mTab);
  2270. }
  2271. }
  2272. } else if (hiveObject.getObjectType() == HiveObjectType.COLUMN) {
  2273. MTable tblObj = getMTable(hiveObject.getDbName(), hiveObject
  2274. .getObjectName());
  2275. if (tblObj != null) {
  2276. if (hiveObject.getPartValues() != null) {
  2277. MPartition partObj = null;
  2278. List<MPartitionColumnPrivilege> colPrivs = null;
  2279. partObj = this.getMPartition(hiveObject.getDbName(), hiveObject
  2280. .getObjectName(), hiveObject.getPartValues());
  2281. if (partObj == null) {
  2282. continue;
  2283. }
  2284. colPrivs = this.listPrincipalPartitionColumnGrants(
  2285. userName, principalType, hiveObject.getDbName(), hiveObject
  2286. .getObjectName(), partObj.getPartitionName(),
  2287. hiveObject.getColumnName());
  2288. if (colPrivs != null) {
  2289. for (MPartitionColumnPrivilege priv : colPrivs) {
  2290. if (priv.getGrantor().equalsIgnoreCase(grantor)) {
  2291. privSet.add(priv.getPrivilege());
  2292. }
  2293. }
  2294. }
  2295. for (String privilege : privs) {
  2296. if (privSet.contains(privilege)) {
  2297. throw new InvalidObjectException(privilege
  2298. + " is already granted on column "
  2299. + hiveObject.getColumnName() + " ["
  2300. + hiveObject.getDbName() + ","
  2301. + hiveObject.getObjectName() + ","
  2302. + partObj.getPartitionName() + "] by " + grantor);
  2303. }
  2304. MPartitionColumnPrivilege mCol = new MPartitionColumnPrivilege(userName,
  2305. principalType.toString(), partObj, hiveObject
  2306. .getColumnName(), privilege, now, grantor, grantorType,
  2307. grantOption);
  2308. persistentObjs.add(mCol);
  2309. }
  2310. } else {
  2311. List<MTableColumnPrivilege> colPrivs = null;
  2312. colPrivs = this.listPrincipalTableColumnGrants(
  2313. userName, principalType, hiveObject.getDbName(), hiveObject
  2314. .getObjectName(), hiveObject.getColumnName());
  2315. if (colPrivs != null) {
  2316. for (MTableColumnPrivilege priv : colPrivs) {
  2317. if (priv.getGrantor().equalsIgnoreCase(grantor)) {
  2318. privSet.add(priv.getPrivilege());
  2319. }
  2320. }
  2321. }
  2322. for (String privilege : privs) {
  2323. if (privSet.contains(privilege)) {
  2324. throw new InvalidObjectException(privilege
  2325. + " is already granted on column "
  2326. + hiveObject.getColumnName() + " ["
  2327. + hiveObject.getDbName() + ","
  2328. + hiveObject.getObjectName() + "] by " + grantor);
  2329. }
  2330. MTableColumnPrivilege mCol = new MTableColumnPrivilege(userName,
  2331. principalType.toString(), tblObj, hiveObject
  2332. .getColumnName(), privilege, now, grantor, grantorType,
  2333. grantOption);
  2334. persistentObjs.add(mCol);
  2335. }
  2336. }
  2337. }
  2338. }
  2339. }
  2340. }
  2341. if (persistentObjs.size() > 0) {
  2342. pm.makePersistentAll(persistentObjs);
  2343. }
  2344. committed = commitTransaction();
  2345. } finally {
  2346. if (!committed) {
  2347. rollbackTransaction();
  2348. }
  2349. }
  2350. return committed;
  2351. }
  2352. @Override
  2353. public boolean revokePrivileges(PrivilegeBag privileges)
  2354. throws InvalidObjectException, MetaException, NoSuchObjectException {
  2355. boolean committed = false;
  2356. try {
  2357. openTransaction();
  2358. List<Object> persistentObjs = new ArrayList<Object>();
  2359. List<HiveObjectPrivilege> privilegeList = privileges.getPrivileges();
  2360. if (privilegeList != null && privilegeList.size() > 0) {
  2361. Iterator<HiveObjectPrivilege> privIter = privilegeList.iterator();
  2362. while (privIter.hasNext()) {
  2363. HiveObjectPrivilege privDef = privIter.next();
  2364. HiveObjectRef hiveObject = privDef.getHiveObject();
  2365. String privilegeStr = privDef.getGrantInfo().getPrivilege();
  2366. if (privilegeStr == null || privilegeStr.trim().equals("")) {
  2367. continue;
  2368. }
  2369. String[] privs = privilegeStr.split(",");
  2370. String userName = privDef.getPrincipalName();
  2371. PrincipalType principalType = privDef.getPrincipalType();
  2372. if (hiveObject.getObjectType() == HiveObjectType.GLOBAL) {
  2373. List<MGlobalPrivilege> mSecUser = this.listPrincipalGlobalGrants(
  2374. userName, principalType);
  2375. boolean found = false;
  2376. if (mSecUser != null) {
  2377. for (String privilege : privs) {
  2378. for (MGlobalPrivilege userGrant : mSecUser) {
  2379. String userGrantPrivs = userGrant.getPrivilege();
  2380. if (privilege.equals(userGrantPrivs)) {
  2381. found = true;
  2382. persistentObjs.add(userGrant);
  2383. break;
  2384. }
  2385. }
  2386. if (!found) {
  2387. throw new InvalidObjectException(
  2388. "No user grant found for privileges " + privilege);
  2389. }
  2390. }
  2391. }
  2392. } else if (hiveObject.getObjectType() == HiveObjectType.DATABASE) {
  2393. MDatabase dbObj = getMDatabase(hiveObject.getDbName());
  2394. if (dbObj != null) {
  2395. String db = hiveObject.getDbName();
  2396. boolean found = false;
  2397. List<MDBPrivilege> dbGrants = this.listPrincipalDBGrants(
  2398. userName, principalType, db);
  2399. for (String privilege : privs) {
  2400. for (MDBPrivilege dbGrant : dbGrants) {
  2401. String dbGrantPriv = dbGrant.getPrivilege();
  2402. if (privilege.equals(dbGrantPriv)) {
  2403. found = true;
  2404. persistentObjs.add(dbGrant);
  2405. break;
  2406. }
  2407. }
  2408. if (!found) {
  2409. throw new InvalidObjectException(
  2410. "No database grant found for privileges " + privilege
  2411. + " on database " + db);
  2412. }
  2413. }
  2414. }
  2415. } else if (hiveObject.getObjectType() == HiveObjectType.TABLE) {
  2416. boolean found = false;
  2417. List<MTablePrivilege> tableGrants = this
  2418. .listAllTableGrants(userName, principalType,
  2419. hiveObject.getDbName(), hiveObject.getObjectName());
  2420. for (String privilege : privs) {
  2421. for (MTablePrivilege tabGrant : tableGrants) {
  2422. String tableGrantPriv = tabGrant.getPrivilege();
  2423. if (privilege.equalsIgnoreCase(tableGrantPriv)) {
  2424. found = true;
  2425. persistentObjs.add(tabGrant);
  2426. break;
  2427. }
  2428. }
  2429. if (!found) {
  2430. throw new InvalidObjectException("No grant (" + privilege
  2431. + ") found " + " on table " + hiveObject.getObjectName()
  2432. + ", database is " + hiveObject.getDbName());
  2433. }
  2434. }
  2435. } else if (hiveObject.getObjectType() == HiveObjectType.PARTITION) {
  2436. boolean found = false;
  2437. Table tabObj = this.getTable(hiveObject.getDbName(), hiveObject.getObjectName());
  2438. String partName = null;
  2439. if (hiveObject.getPartValues() != null) {
  2440. partName = Warehouse.makePartName(tabObj.getPartitionKeys(), hiveObject.getPartValues());
  2441. }
  2442. List<MPartitionPrivilege> partitionGrants = this
  2443. .listPrincipalPartitionGrants(userName, principalType,
  2444. hiveObject.getDbName(), hiveObject.getObjectName(), partName);
  2445. for (String privilege : privs) {
  2446. for (MPartitionPrivilege partGrant : partitionGrants) {
  2447. String partPriv = partGrant.getPrivilege();
  2448. if (partPriv.equalsIgnoreCase(privilege)) {
  2449. found = true;
  2450. persistentObjs.add(partGrant);
  2451. break;
  2452. }
  2453. }
  2454. if (!found) {
  2455. throw new InvalidObjectException("No grant (" + privilege
  2456. + ") found " + " on table " + tabObj.getTableName()
  2457. + ", partition is " + partName + ", database is " + tabObj.getDbName());
  2458. }
  2459. }
  2460. } else if (hiveObject.getObjectType() == HiveObjectType.COLUMN) {
  2461. Table tabObj = this.getTable(hiveObject.getDbName(), hiveObject
  2462. .getObjectName());
  2463. String partName = null;
  2464. if (hiveObject.getPartValues() != null) {
  2465. partName = Warehouse.makePartName(tabObj.getPartitionKeys(),
  2466. hiveObject.getPartValues());
  2467. }
  2468. if (partName != null) {
  2469. List<MPartitionColumnPrivilege> mSecCol = listPrincipalPartitionColumnGrants(
  2470. userName, principalType, hiveObject.getDbName(), hiveObject
  2471. .getObjectName(), partName, hiveObject.getColumnName());
  2472. boolean found = false;
  2473. if (mSecCol != null) {
  2474. for (String privilege : privs) {
  2475. for (MPartitionColumnPrivilege col : mSecCol) {
  2476. String colPriv = col.getPrivilege();
  2477. if (colPriv.equalsIgnoreCase(privilege)) {
  2478. found = true;
  2479. persistentObjs.add(col);
  2480. break;
  2481. }
  2482. }
  2483. if (!found) {
  2484. throw new InvalidObjectException("No grant (" + privilege
  2485. + ") found " + " on table " + tabObj.getTableName()
  2486. + ", partition is " + partName + ", column name = "
  2487. + hiveObject.getColumnName() + ", database is "
  2488. + tabObj.getDbName());
  2489. }
  2490. }
  2491. }
  2492. } else {
  2493. List<MTableColumnPrivilege> mSecCol = listPrincipalTableColumnGrants(
  2494. userName, principalType, hiveObject.getDbName(), hiveObject
  2495. .getObjectName(), hiveObject.getColumnName());
  2496. boolean found = false;
  2497. if (mSecCol != null) {
  2498. for (String privilege : privs) {
  2499. for (MTableColumnPrivilege col : mSecCol) {
  2500. String colPriv = col.getPrivilege();
  2501. if (colPriv.equalsIgnoreCase(privilege)) {
  2502. found = true;
  2503. persistentObjs.add(col);
  2504. break;
  2505. }
  2506. }
  2507. if (!found) {
  2508. throw new InvalidObjectException("No grant (" + privilege
  2509. + ") found " + " on table " + tabObj.getTableName()
  2510. + ", column name = "
  2511. + hiveObject.getColumnName() + ", database is "
  2512. + tabObj.getDbName());
  2513. }
  2514. }
  2515. }
  2516. }
  2517. }
  2518. }
  2519. }
  2520. if (persistentObjs.size() > 0) {
  2521. pm.deletePersistentAll(persistentObjs);
  2522. }
  2523. committed = commitTransaction();
  2524. } finally {
  2525. if (!committed) {
  2526. rollbackTransaction();
  2527. }
  2528. }
  2529. return committed;
  2530. }
  2531. @SuppressWarnings("unchecked")
  2532. private List<MRoleMap> listRoleMembers(
  2533. MRole mRol) {
  2534. boolean success = false;
  2535. List<MRoleMap> mRoleMemeberList = null;
  2536. try {
  2537. openTransaction();
  2538. LOG.debug("Executing listMSecurityUserRoleMember");
  2539. Query query = pm.newQuery(MRoleMap.class,
  2540. "role.roleName == t1");
  2541. query.declareParameters("java.lang.String t1");
  2542. query.setUnique(false);
  2543. mRoleMemeberList = (List<MRoleMap>) query.execute(
  2544. mRol.getRoleName());
  2545. LOG.debug("Done executing query for listMSecurityUserRoleMember");
  2546. pm.retrieveAll(mRoleMemeberList);
  2547. success = commitTransaction();
  2548. LOG.debug("Done retrieving all objects for listMSecurityUserRoleMember");
  2549. } finally {
  2550. if (!success) {
  2551. rollbackTransaction();
  2552. }
  2553. }
  2554. return mRoleMemeberList;
  2555. }
  2556. @SuppressWarnings("unchecked")
  2557. @Override
  2558. public List<MGlobalPrivilege> listPrincipalGlobalGrants(String principalName, PrincipalType principalType) {
  2559. boolean commited = false;
  2560. List<MGlobalPrivilege> userNameDbPriv = null;
  2561. try {
  2562. openTransaction();
  2563. if (principalName != null) {
  2564. Query query = pm.newQuery(MGlobalPrivilege.class,
  2565. "principalName == t1 && principalType == t2 ");
  2566. query.declareParameters(
  2567. "java.lang.String t1, java.lang.String t2");
  2568. userNameDbPriv = (List<MGlobalPrivilege>) query
  2569. .executeWithArray(principalName, principalType.toString());
  2570. pm.retrieveAll(userNameDbPriv);
  2571. }
  2572. commited = commitTransaction();
  2573. } finally {
  2574. if (!commited) {
  2575. rollbackTransaction();
  2576. }
  2577. }
  2578. return userNameDbPriv;
  2579. }
  2580. @SuppressWarnings("unchecked")
  2581. @Override
  2582. public List<MDBPrivilege> listPrincipalDBGrants(String principalName,
  2583. PrincipalType principalType, String dbName) {
  2584. boolean success = false;
  2585. List<MDBPrivilege> mSecurityDBList = null;
  2586. dbName = dbName.toLowerCase().trim();
  2587. try {
  2588. openTransaction();
  2589. LOG.debug("Executing listPrincipalDBGrants");
  2590. Query query = pm.newQuery(MDBPrivilege.class,
  2591. "principalName == t1 && principalType == t2 && database.name == t3");
  2592. query
  2593. .declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
  2594. mSecurityDBList = (List<MDBPrivilege>) query.executeWithArray(principalName, principalType.toString(), dbName);
  2595. LOG.debug("Done executing query for listPrincipalDBGrants");
  2596. pm.retrieveAll(mSecurityDBList);
  2597. success = commitTransaction();
  2598. LOG.debug("Done retrieving all objects for listPrincipalDBGrants");
  2599. } finally {
  2600. if (!success) {
  2601. rollbackTransaction();
  2602. }
  2603. }
  2604. return mSecurityDBList;
  2605. }
  2606. @SuppressWarnings("unchecked")
  2607. private List<MDBPrivilege> listPrincipalAllDBGrant(
  2608. String principalName, PrincipalType principalType) {
  2609. boolean success = false;
  2610. List<MDBPrivilege> mSecurityDBList = null;
  2611. try {
  2612. openTransaction();
  2613. LOG.debug("Executing listPrincipalAllDBGrant");
  2614. Query query = pm.newQuery(MDBPrivilege.class,
  2615. "principalName == t1 && principalType == t2");
  2616. query
  2617. .declareParameters("java.lang.String t1, java.lang.String t2");
  2618. mSecurityDBList = (List<MDBPrivilege>) query.execute(principalName, principalType.toString());
  2619. LOG.debug("Done executing query for listPrincipalAllDBGrant");
  2620. pm.retrieveAll(mSecurityDBList);
  2621. success = commitTransaction();
  2622. LOG.debug("Done retrieving all objects for listPrincipalAllDBGrant");
  2623. } finally {
  2624. if (!success) {
  2625. rollbackTransaction();
  2626. }
  2627. }
  2628. return mSecurityDBList;
  2629. }
  2630. @SuppressWarnings("unchecked")
  2631. public List<MTablePrivilege> listAllTableGrants(String dbName,
  2632. String tableName) {
  2633. boolean success = false;
  2634. tableName = tableName.toLowerCase().trim();
  2635. dbName = dbName.toLowerCase().trim();
  2636. List<MTablePrivilege> mSecurityTabList = null;
  2637. tableName = tableName.toLowerCase().trim();
  2638. dbName = dbName.toLowerCase().trim();
  2639. try {
  2640. openTransaction();
  2641. LOG.debug("Executing listAllTableGrants");
  2642. String queryStr = "table.tableName == t1 && table.database.name == t2";
  2643. Query query = pm.newQuery(
  2644. MTablePrivilege.class, queryStr);
  2645. query.declareParameters(
  2646. "java.lang.String t1, java.lang.String t2");
  2647. mSecurityTabList = (List<MTablePrivilege>) query
  2648. .executeWithArray(tableName, dbName);
  2649. LOG.debug("Done executing query for listAllTableGrants");
  2650. pm.retrieveAll(mSecurityTabList);
  2651. success = commitTransaction();
  2652. LOG
  2653. .debug("Done retrieving all objects for listAllTableGrants");
  2654. } finally {
  2655. if (!success) {
  2656. rollbackTransaction();
  2657. }
  2658. }
  2659. return mSecurityTabList;
  2660. }
  2661. @SuppressWarnings("unchecked")
  2662. public List<MPartitionPrivilege> listTableAllPartitionGrants(String dbName,
  2663. String tableName) {
  2664. tableName = tableName.toLowerCase().trim();
  2665. dbName = dbName.toLowerCase().trim();
  2666. boolean success = false;
  2667. List<MPartitionPrivilege> mSecurityTabPartList = null;
  2668. try {
  2669. openTransaction();
  2670. LOG.debug("Executing listTableAllPartitionGrants");
  2671. String queryStr = "partition.table.tableName == t1 && partition.table.database.name == t2";
  2672. Query query = pm.newQuery(
  2673. MPartitionPrivilege.class, queryStr);
  2674. query.declareParameters(
  2675. "java.lang.String t1, java.lang.String t2");
  2676. mSecurityTabPartList = (List<MPartitionPrivilege>) query
  2677. .executeWithArray(tableName, dbName);
  2678. LOG.debug("Done executing query for listTableAllPartitionGrants");
  2679. pm.retrieveAll(mSecurityTabPartList);
  2680. success = commitTransaction();
  2681. LOG
  2682. .debug("Done retrieving all objects for listTableAllPartitionGrants");
  2683. } finally {
  2684. if (!success) {
  2685. rollbackTransaction();
  2686. }
  2687. }
  2688. return mSecurityTabPartList;
  2689. }
  2690. @SuppressWarnings("unchecked")
  2691. public List<MTableColumnPrivilege> listTableAllColumnGrants(String dbName,
  2692. String tableName) {
  2693. boolean success = false;
  2694. List<MTableColumnPrivilege> mTblColPrivilegeList = null;
  2695. tableName = tableName.toLowerCase().trim();
  2696. dbName = dbName.toLowerCase().trim();
  2697. try {
  2698. openTransaction();
  2699. LOG.debug("Executing listTableAllColumnGrants");
  2700. String queryStr = "table.tableName == t1 && table.database.name == t2";
  2701. Query query = pm.newQuery(MTableColumnPrivilege.class, queryStr);
  2702. query.declareParameters("java.lang.String t1, java.lang.String t2");
  2703. mTblColPrivilegeList = (List<MTableColumnPrivilege>) query
  2704. .executeWithArray(tableName, dbName);
  2705. LOG.debug("Done executing query for listTableAllColumnGrants");
  2706. pm.retrieveAll(mTblColPrivilegeList);
  2707. success = commitTransaction();
  2708. LOG.debug("Done retrieving all objects for listTableAllColumnGrants");
  2709. } finally {
  2710. if (!success) {
  2711. rollbackTransaction();
  2712. }
  2713. }
  2714. return mTblColPrivilegeList;
  2715. }
  2716. @SuppressWarnings("unchecked")
  2717. public List<MPartitionColumnPrivilege> listTableAllPartitionColumnGrants(String dbName,
  2718. String tableName) {
  2719. boolean success = false;
  2720. tableName = tableName.toLowerCase().trim();
  2721. dbName = dbName.toLowerCase().trim();
  2722. List<MPartitionColumnPrivilege> mSecurityColList = null;
  2723. try {
  2724. openTransaction();
  2725. LOG.debug("Executing listTableAllPartitionColumnGrants");
  2726. String queryStr = "partition.table.tableName == t1 && partition.table.database.name == t2";
  2727. Query query = pm.newQuery(MPartitionColumnPrivilege.class, queryStr);
  2728. query.declareParameters("java.lang.String t1, java.lang.String t2");
  2729. mSecurityColList = (List<MPartitionColumnPrivilege>) query
  2730. .executeWithArray(tableName, dbName);
  2731. LOG.debug("Done executing query for listTableAllPartitionColumnGrants");
  2732. pm.retrieveAll(mSecurityColList);
  2733. success = commitTransaction();
  2734. LOG.debug("Done retrieving all objects for listTableAllPartitionColumnGrants");
  2735. } finally {
  2736. if (!success) {
  2737. rollbackTransaction();
  2738. }
  2739. }
  2740. return mSecurityColList;
  2741. }
  2742. @SuppressWarnings("unchecked")
  2743. public List<MPartitionColumnPrivilege> listPartitionAllColumnGrants(String dbName,
  2744. String tableName, String partName) {
  2745. boolean success = false;
  2746. tableName = tableName.toLowerCase().trim();
  2747. dbName = dbName.toLowerCase().trim();
  2748. List<MPartitionColumnPrivilege> mSecurityColList = null;
  2749. try {
  2750. openTransaction();
  2751. LOG.debug("Executing listPartitionAllColumnGrants");
  2752. String queryStr = "partition.table.tableName == t1 && partition.table.database.name == t2 && partition.partitionName == t3";
  2753. Query query = pm.newQuery(MPartitionColumnPrivilege.class, queryStr);
  2754. query.declareParameters(
  2755. "java.lang.String t1, java.lang.String t2, java.lang.String t3");
  2756. mSecurityColList = (List<MPartitionColumnPrivilege>) query
  2757. .executeWithArray(tableName, dbName, partName);
  2758. LOG.debug("Done executing query for listPartitionAllColumnGrants");
  2759. pm.retrieveAll(mSecurityColList);
  2760. success = commitTransaction();
  2761. LOG.debug("Done retrieving all objects for listPartitionAllColumnGrants");
  2762. } finally {
  2763. if (!success) {
  2764. rollbackTransaction();
  2765. }
  2766. }
  2767. return mSecurityColList;
  2768. }
  2769. @SuppressWarnings("unchecked")
  2770. private List<MDBPrivilege> listDatabaseGrants(String dbName) {
  2771. dbName = dbName.toLowerCase().trim();
  2772. boolean success = false;
  2773. try {
  2774. openTransaction();
  2775. LOG.debug("Executing listDatabaseGrants");
  2776. Query query = pm.newQuery(MDBPrivilege.class,
  2777. "database.name == t1");
  2778. query.declareParameters("java.lang.String t1");
  2779. List<MDBPrivilege> mSecurityDBList = (List<MDBPrivilege>) query
  2780. .executeWithArray(dbName);
  2781. LOG.debug("Done executing query for listDatabaseGrants");
  2782. pm.retrieveAll(mSecurityDBList);
  2783. success = commitTransaction();
  2784. LOG.debug("Done retrieving all objects for listDatabaseGrants");
  2785. return mSecurityDBList;
  2786. } finally {
  2787. if (!success) {
  2788. rollbackTransaction();
  2789. }
  2790. }
  2791. }
  2792. @SuppressWarnings("unchecked")
  2793. private List<MPartitionPrivilege> listPartitionGrants(String dbName, String tableName,
  2794. String partName) {
  2795. tableName = tableName.toLowerCase().trim();
  2796. dbName = dbName.toLowerCase().trim();
  2797. boolean success = false;
  2798. List<MPartitionPrivilege> mSecurityTabPartList = null;
  2799. try {
  2800. openTransaction();
  2801. LOG.debug("Executing listPartitionGrants");
  2802. Query query = pm.newQuery(MPartitionPrivilege.class,
  2803. "partition.table.tableName == t1 && partition.table.database.name == t2 && partition.partitionName == t3");
  2804. query.declareParameters(
  2805. "java.lang.String t1, java.lang.String t2, java.lang.String t3");
  2806. mSecurityTabPartList = (List<MPartitionPrivilege>) query
  2807. .executeWithArray(tableName, dbName, partName);
  2808. LOG.debug("Done executing query for listPartitionGrants");
  2809. pm.retrieveAll(mSecurityTabPartList);
  2810. success = commitTransaction();
  2811. LOG.debug("Done retrieving all objects for listPartitionGrants");
  2812. } finally {
  2813. if (!success) {
  2814. rollbackTransaction();
  2815. }
  2816. }
  2817. return mSecurityTabPartList;
  2818. }
  2819. @SuppressWarnings("unchecked")
  2820. public List<MTablePrivilege> listAllTableGrants(
  2821. String principalName, PrincipalType principalType, String dbName,
  2822. String tableName) {
  2823. tableName = tableName.toLowerCase().trim();
  2824. dbName = dbName.toLowerCase().trim();
  2825. boolean success = false;
  2826. List<MTablePrivilege> mSecurityTabPartList = null;
  2827. try {
  2828. openTransaction();
  2829. LOG.debug("Executing listAllTableGrants");
  2830. Query query = pm.newQuery(
  2831. MTablePrivilege.class,
  2832. "principalName == t1 && principalType == t2 && table.tableName == t3 && table.database.name == t4");
  2833. query.declareParameters(
  2834. "java.lang.String t1, java.lang.String t2, java.lang.String t3, java.lang.String t4");
  2835. mSecurityTabPartList = (List<MTablePrivilege>) query
  2836. .executeWithArray(principalName, principalType.toString(), tableName, dbName);
  2837. LOG.debug("Done executing query for listAllTableGrants");
  2838. pm.retrieveAll(mSecurityTabPartList);
  2839. success = commitTransaction();
  2840. LOG
  2841. .debug("Done retrieving all objects for listAllTableGrants");
  2842. } finally {
  2843. if (!success) {
  2844. rollbackTransaction();
  2845. }
  2846. }
  2847. return mSecurityTabPartList;
  2848. }
  2849. @SuppressWarnings("unchecked")
  2850. @Override
  2851. public List<MPartitionPrivilege> listPrincipalPartitionGrants(
  2852. String principalName, PrincipalType principalType, String dbName,
  2853. String tableName, String partName) {
  2854. boolean success = false;
  2855. tableName = tableName.toLowerCase().trim();
  2856. dbName = dbName.toLowerCase().trim();
  2857. List<MPartitionPrivilege> mSecurityTabPartList = null;
  2858. try {
  2859. openTransaction();
  2860. LOG.debug("Executing listMSecurityPrincipalPartitionGrant");
  2861. Query query = pm.newQuery(
  2862. MPartitionPrivilege.class,
  2863. "principalName == t1 && principalType == t2 && partition.table.tableName == t3 " +
  2864. "&& partition.table.database.name == t4 && partition.partitionName == t5");
  2865. query.declareParameters(
  2866. "java.lang.String t1, java.lang.String t2, java.lang.String t3, java.lang.String t4, " +
  2867. "java.lang.String t5");
  2868. mSecurityTabPartList = (List<MPartitionPrivilege>) query
  2869. .executeWithArray(principalName, principalType.toString(), tableName, dbName, partName);
  2870. LOG.debug("Done executing query for listMSecurityPrincipalPartitionGrant");
  2871. pm.retrieveAll(mSecurityTabPartList);
  2872. success = commitTransaction();
  2873. LOG.debug("Done retrieving all objects for listMSecurityPrincipalPartitionGrant");
  2874. } finally {
  2875. if (!success) {
  2876. rollbackTransaction();
  2877. }
  2878. }
  2879. return mSecurityTabPartList;
  2880. }
  2881. @SuppressWarnings("unchecked")
  2882. @Override
  2883. public List<MTableColumnPrivilege> listPrincipalTableColumnGrants(
  2884. String principalName, PrincipalType principalType, String dbName,
  2885. String tableName, String columnName) {
  2886. boolean success = false;
  2887. tableName = tableName.toLowerCase().trim();
  2888. dbName = dbName.toLowerCase().trim();
  2889. columnName = columnName.toLowerCase().trim();
  2890. List<MTableColumnPrivilege> mSecurityColList = null;
  2891. try {
  2892. openTransaction();
  2893. LOG.debug("Executing listPrincipalTableColumnGrants");
  2894. String queryStr = "principalName == t1 && principalType == t2 && " +
  2895. "table.tableName == t3 && table.database.name == t4 && columnName == t5 ";
  2896. Query query = pm.newQuery(MTableColumnPrivilege.class, queryStr);
  2897. query
  2898. .declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3, " +
  2899. "java.lang.String t4, java.lang.String t5");
  2900. mSecurityColList = (List<MTableColumnPrivilege>) query.executeWithArray(
  2901. principalName, principalType.toString(), tableName, dbName, columnName);
  2902. LOG.debug("Done executing query for listPrincipalTableColumnGrants");
  2903. pm.retrieveAll(mSecurityColList);
  2904. success = commitTransaction();
  2905. LOG
  2906. .debug("Done retrieving all objects for listPrincipalTableColumnGrants");
  2907. } finally {
  2908. if (!success) {
  2909. rollbackTransaction();
  2910. }
  2911. }
  2912. return mSecurityColList;
  2913. }
  2914. @SuppressWarnings("unchecked")
  2915. public List<MPartitionColumnPrivilege> listPrincipalPartitionColumnGrants(
  2916. String principalName, PrincipalType principalType, String dbName,
  2917. String tableName, String partitionName, String columnName) {
  2918. boolean success = false;
  2919. tableName = tableName.toLowerCase().trim();
  2920. dbName = dbName.toLowerCase().trim();
  2921. columnName = columnName.toLowerCase().trim();
  2922. List<MPartitionColumnPrivilege> mSecurityColList = null;
  2923. try {
  2924. openTransaction();
  2925. LOG.debug("Executing listPrincipalPartitionColumnGrants");
  2926. Query query = pm
  2927. .newQuery(
  2928. MPartitionColumnPrivilege.class,
  2929. "principalName == t1 && principalType == t2 && partition.table.tableName == t3 " +
  2930. "&& partition.table.database.name == t4 && partition.partitionName == t5 && columnName == t6");
  2931. query
  2932. .declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3, " +
  2933. "java.lang.String t4, java.lang.String t5, java.lang.String t6");
  2934. mSecurityColList = (List<MPartitionColumnPrivilege>) query
  2935. .executeWithArray(principalName, principalType.toString(), tableName,
  2936. dbName, partitionName, columnName);
  2937. LOG.debug("Done executing query for listPrincipalPartitionColumnGrants");
  2938. pm.retrieveAll(mSecurityColList);
  2939. success = commitTransaction();
  2940. LOG
  2941. .debug("Done retrieving all objects for listPrincipalPartitionColumnGrants");
  2942. } finally {
  2943. if (!success) {
  2944. rollbackTransaction();
  2945. }
  2946. }
  2947. return mSecurityColList;
  2948. }
  2949. @SuppressWarnings("unchecked")
  2950. private List<MTablePrivilege> listPrincipalAllTableGrants(
  2951. String principalName, PrincipalType principalType) {
  2952. boolean success = false;
  2953. List<MTablePrivilege> mSecurityTabPartList = null;
  2954. try {
  2955. openTransaction();
  2956. LOG.debug("Executing listPrincipalAllTableGrants");
  2957. Query query = pm.newQuery(MTablePrivilege.class,
  2958. "principalName == t1 && principalType == t2");
  2959. query.declareParameters("java.lang.String t1, java.lang.String t2");
  2960. mSecurityTabPartList = (List<MTablePrivilege>) query.execute(
  2961. principalName, principalType.toString());
  2962. LOG
  2963. .debug("Done executing query for listPrincipalAllTableGrants");
  2964. pm.retrieveAll(mSecurityTabPartList);
  2965. success = commitTransaction();
  2966. LOG
  2967. .debug("Done retrieving all objects for listPrincipalAllTableGrants");
  2968. } finally {
  2969. if (!success) {
  2970. rollbackTransaction();
  2971. }
  2972. }
  2973. return mSecurityTabPartList;
  2974. }
  2975. @SuppressWarnings("unchecked")
  2976. private List<MPartitionPrivilege> listPrincipalAllPartitionGrants(
  2977. String principalName, PrincipalType principalType) {
  2978. boolean success = false;
  2979. List<MPartitionPrivilege> mSecurityTabPartList = null;
  2980. try {
  2981. openTransaction();
  2982. LOG.debug("Executing listPrincipalAllPartitionGrants");
  2983. Query query = pm.newQuery(MPartitionPrivilege.class,
  2984. "principalName == t1 && principalType == t2");
  2985. query.declareParameters("java.lang.String t1, java.lang.String t2");
  2986. mSecurityTabPartList = (List<MPartitionPrivilege>) query.execute(
  2987. principalName, principalType.toString());
  2988. LOG
  2989. .debug("Done executing query for listPrincipalAllPartitionGrants");
  2990. pm.retrieveAll(mSecurityTabPartList);
  2991. success = commitTransaction();
  2992. LOG
  2993. .debug("Done retrieving all objects for listPrincipalAllPartitionGrants");
  2994. } finally {
  2995. if (!success) {
  2996. rollbackTransaction();
  2997. }
  2998. }
  2999. return mSecurityTabPartList;
  3000. }
  3001. @SuppressWarnings("unchecked")
  3002. private List<MTableColumnPrivilege> listPrincipalAllTableColumnGrants(
  3003. String principalName, PrincipalType principalType) {
  3004. boolean success = false;
  3005. List<MTableColumnPrivilege> mSecurityColumnList = null;
  3006. try {
  3007. openTransaction();
  3008. LOG.debug("Executing listPrincipalAllTableColumnGrants");
  3009. Query query = pm.newQuery(MTableColumnPrivilege.class,
  3010. "principalName == t1 && principalType == t2");
  3011. query
  3012. .declareParameters("java.lang.String t1, java.lang.String t2");
  3013. mSecurityColumnList = (List<MTableColumnPrivilege>) query.execute(
  3014. principalName, principalType.toString());
  3015. LOG.debug("Done executing query for listPrincipalAllTableColumnGrants");
  3016. pm.retrieveAll(mSecurityColumnList);
  3017. success = commitTransaction();
  3018. LOG.debug("Done retrieving all objects for listPrincipalAllTableColumnGrants");
  3019. } finally {
  3020. if (!success) {
  3021. rollbackTransaction();
  3022. }
  3023. }
  3024. return mSecurityColumnList;
  3025. }
  3026. @SuppressWarnings("unchecked")
  3027. private List<MPartitionColumnPrivilege> listPrincipalAllPartitionColumnGrants(
  3028. String principalName, PrincipalType principalType) {
  3029. boolean success = false;
  3030. List<MPartitionColumnPrivilege> mSecurityColumnList = null;
  3031. try {
  3032. openTransaction();
  3033. LOG.debug("Executing listPrincipalAllTableColumnGrants");
  3034. Query query = pm.newQuery(MPartitionColumnPrivilege.class,
  3035. "principalName == t1 && principalType == t2");
  3036. query
  3037. .declareParameters("java.lang.String t1, java.lang.String t2");
  3038. mSecurityColumnList = (List<MPartitionColumnPrivilege>) query.execute(
  3039. principalName, principalType.toString());
  3040. LOG.debug("Done executing query for listPrincipalAllTableColumnGrants");
  3041. pm.retrieveAll(mSecurityColumnList);
  3042. success = commitTransaction();
  3043. LOG.debug("Done retrieving all objects for listPrincipalAllTableColumnGrants");
  3044. } finally {
  3045. if (!success) {
  3046. rollbackTransaction();
  3047. }
  3048. }
  3049. return mSecurityColumnList;
  3050. }
  3051. }