PageRenderTime 172ms CodeModel.GetById 68ms app.highlight 90ms 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

Large files files are truncated, but you can click here to view the full file

   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
  19package org.apache.hadoop.hive.metastore;
  20
  21import java.util.ArrayList;
  22import java.util.Collection;
  23import java.util.Collections;
  24import java.util.HashMap;
  25import java.util.HashSet;
  26import java.util.Iterator;
  27import java.util.List;
  28import java.util.Map;
  29import java.util.Set;
  30import java.util.Map.Entry;
  31import java.util.Properties;
  32import java.util.concurrent.locks.Lock;
  33import java.util.concurrent.locks.ReentrantLock;
  34
  35import javax.jdo.JDOHelper;
  36import javax.jdo.JDOObjectNotFoundException;
  37import javax.jdo.PersistenceManager;
  38import javax.jdo.PersistenceManagerFactory;
  39import javax.jdo.Query;
  40import javax.jdo.Transaction;
  41import javax.jdo.datastore.DataStoreCache;
  42
  43import org.antlr.runtime.CharStream;
  44import org.antlr.runtime.CommonTokenStream;
  45import org.antlr.runtime.RecognitionException;
  46import org.apache.commons.logging.Log;
  47import org.apache.commons.logging.LogFactory;
  48import org.apache.hadoop.conf.Configurable;
  49import org.apache.hadoop.conf.Configuration;
  50import org.apache.hadoop.hive.common.FileUtils;
  51import org.apache.hadoop.hive.conf.HiveConf;
  52import org.apache.hadoop.hive.metastore.api.Database;
  53import org.apache.hadoop.hive.metastore.api.FieldSchema;
  54import org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege;
  55import org.apache.hadoop.hive.metastore.api.HiveObjectRef;
  56import org.apache.hadoop.hive.metastore.api.HiveObjectType;
  57import org.apache.hadoop.hive.metastore.api.Index;
  58import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
  59import org.apache.hadoop.hive.metastore.api.MetaException;
  60import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
  61import org.apache.hadoop.hive.metastore.api.Order;
  62import org.apache.hadoop.hive.metastore.api.Partition;
  63import org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet;
  64import org.apache.hadoop.hive.metastore.api.PrincipalType;
  65import org.apache.hadoop.hive.metastore.api.PrivilegeBag;
  66import org.apache.hadoop.hive.metastore.api.PrivilegeGrantInfo;
  67import org.apache.hadoop.hive.metastore.api.Role;
  68import org.apache.hadoop.hive.metastore.api.SerDeInfo;
  69import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
  70import org.apache.hadoop.hive.metastore.api.Table;
  71import org.apache.hadoop.hive.metastore.api.Type;
  72import org.apache.hadoop.hive.metastore.model.MDatabase;
  73import org.apache.hadoop.hive.metastore.model.MFieldSchema;
  74import org.apache.hadoop.hive.metastore.model.MIndex;
  75import org.apache.hadoop.hive.metastore.model.MOrder;
  76import org.apache.hadoop.hive.metastore.model.MPartition;
  77import org.apache.hadoop.hive.metastore.model.MDBPrivilege;
  78import org.apache.hadoop.hive.metastore.model.MPartitionColumnPrivilege;
  79import org.apache.hadoop.hive.metastore.model.MPartitionPrivilege;
  80import org.apache.hadoop.hive.metastore.model.MRole;
  81import org.apache.hadoop.hive.metastore.model.MTableColumnPrivilege;
  82import org.apache.hadoop.hive.metastore.model.MGlobalPrivilege;
  83import org.apache.hadoop.hive.metastore.model.MRoleMap;
  84import org.apache.hadoop.hive.metastore.model.MSerDeInfo;
  85import org.apache.hadoop.hive.metastore.model.MStorageDescriptor;
  86import org.apache.hadoop.hive.metastore.model.MTable;
  87import org.apache.hadoop.hive.metastore.model.MTablePrivilege;
  88import org.apache.hadoop.hive.metastore.model.MType;
  89import org.apache.hadoop.hive.metastore.parser.FilterLexer;
  90import org.apache.hadoop.hive.metastore.parser.FilterParser;
  91import org.apache.hadoop.hive.metastore.parser.ExpressionTree.ANTLRNoCaseStringStream;
  92import org.apache.hadoop.util.StringUtils;
  93
  94/**
  95 * This class is the interface between the application logic and the database
  96 * store that contains the objects. Refrain putting any logic in mode.M* objects
  97 * or in this file as former could be auto generated and this class would need
  98 * to be made into a interface that can read both from a database and a
  99 * filestore.
 100 */
 101public class ObjectStore implements RawStore, Configurable {
 102  private static Properties prop = null;
 103  private static PersistenceManagerFactory pmf = null;
 104
 105  private static Lock pmfPropLock = new ReentrantLock();
 106  private static final Log LOG = LogFactory.getLog(ObjectStore.class.getName());
 107
 108  private static enum TXN_STATUS {
 109    NO_STATE, OPEN, COMMITED, ROLLBACK
 110  }
 111
 112  private static final Map<String, Class> PINCLASSMAP;
 113  static {
 114    Map<String, Class> map = new HashMap();
 115    map.put("table", MTable.class);
 116    map.put("storagedescriptor", MStorageDescriptor.class);
 117    map.put("serdeinfo", MSerDeInfo.class);
 118    map.put("partition", MPartition.class);
 119    map.put("database", MDatabase.class);
 120    map.put("type", MType.class);
 121    map.put("fieldschema", MFieldSchema.class);
 122    map.put("order", MOrder.class);
 123    PINCLASSMAP = Collections.unmodifiableMap(map);
 124  }
 125
 126  private boolean isInitialized = false;
 127  private PersistenceManager pm = null;
 128  private Configuration hiveConf;
 129  int openTrasactionCalls = 0;
 130  private Transaction currentTransaction = null;
 131  private TXN_STATUS transactionStatus = TXN_STATUS.NO_STATE;
 132
 133  public ObjectStore() {
 134  }
 135
 136  public Configuration getConf() {
 137    return hiveConf;
 138  }
 139
 140  /**
 141   * Called whenever this object is instantiated using ReflectionUils, and also
 142   * on connection retries. In cases of connection retries, conf will usually
 143   * contain modified values.
 144   */
 145  @SuppressWarnings("nls")
 146  public void setConf(Configuration conf) {
 147    // Although an instance of ObjectStore is accessed by one thread, there may
 148    // be many threads with ObjectStore instances. So the static variables
 149    // pmf and prop need to be protected with locks.
 150    pmfPropLock.lock();
 151    try {
 152      isInitialized = false;
 153      hiveConf = conf;
 154      Properties propsFromConf = getDataSourceProps(conf);
 155      boolean propsChanged = !propsFromConf.equals(prop);
 156
 157      if (propsChanged) {
 158        pmf = null;
 159        prop = null;
 160      }
 161
 162      assert(!isActiveTransaction());
 163      shutdown();
 164      // Always want to re-create pm as we don't know if it were created by the
 165      // most recent instance of the pmf
 166      pm = null;
 167      openTrasactionCalls = 0;
 168      currentTransaction = null;
 169      transactionStatus = TXN_STATUS.NO_STATE;
 170
 171      initialize(propsFromConf);
 172
 173      if (!isInitialized) {
 174        throw new RuntimeException(
 175        "Unable to create persistence manager. Check dss.log for details");
 176      } else {
 177        LOG.info("Initialized ObjectStore");
 178      }
 179    } finally {
 180      pmfPropLock.unlock();
 181    }
 182  }
 183
 184  private ClassLoader classLoader;
 185  {
 186    classLoader = Thread.currentThread().getContextClassLoader();
 187    if (classLoader == null) {
 188      classLoader = ObjectStore.class.getClassLoader();
 189    }
 190  }
 191
 192  @SuppressWarnings("nls")
 193  private void initialize(Properties dsProps) {
 194    LOG.info("ObjectStore, initialize called");
 195    prop = dsProps;
 196    pm = getPersistenceManager();
 197    isInitialized = pm != null;
 198    return;
 199  }
 200
 201  /**
 202   * Properties specified in hive-default.xml override the properties specified
 203   * in jpox.properties.
 204   */
 205  @SuppressWarnings("nls")
 206  private static Properties getDataSourceProps(Configuration conf) {
 207    Properties prop = new Properties();
 208
 209    Iterator<Map.Entry<String, String>> iter = conf.iterator();
 210    while (iter.hasNext()) {
 211      Map.Entry<String, String> e = iter.next();
 212      if (e.getKey().contains("datanucleus") || e.getKey().contains("jdo")) {
 213        Object prevVal = prop.setProperty(e.getKey(), conf.get(e.getKey()));
 214        if (LOG.isDebugEnabled()
 215            && !e.getKey().equals(HiveConf.ConfVars.METASTOREPWD.varname)) {
 216          LOG.debug("Overriding " + e.getKey() + " value " + prevVal
 217              + " from  jpox.properties with " + e.getValue());
 218        }
 219      }
 220    }
 221
 222    if (LOG.isDebugEnabled()) {
 223      for (Entry<Object, Object> e : prop.entrySet()) {
 224        if (!e.getKey().equals(HiveConf.ConfVars.METASTOREPWD.varname)) {
 225          LOG.debug(e.getKey() + " = " + e.getValue());
 226        }
 227      }
 228    }
 229    return prop;
 230  }
 231
 232  private static PersistenceManagerFactory getPMF() {
 233    if (pmf == null) {
 234      pmf = JDOHelper.getPersistenceManagerFactory(prop);
 235      DataStoreCache dsc = pmf.getDataStoreCache();
 236      if (dsc != null) {
 237        HiveConf conf = new HiveConf(ObjectStore.class);
 238        String objTypes = HiveConf.getVar(conf, HiveConf.ConfVars.METASTORE_CACHE_PINOBJTYPES);
 239        LOG.info("Setting MetaStore object pin classes with hive.metastore.cache.pinobjtypes=\"" + objTypes + "\"");
 240        if (objTypes != null && objTypes.length() > 0) {
 241          objTypes = objTypes.toLowerCase();
 242          String[] typeTokens = objTypes.split(",");
 243          for (String type : typeTokens) {
 244            type = type.trim();
 245            if (PINCLASSMAP.containsKey(type)) {
 246              dsc.pinAll(true, PINCLASSMAP.get(type));
 247            }
 248            else {
 249              LOG.warn(type + " is not one of the pinnable object types: " + org.apache.commons.lang.StringUtils.join(PINCLASSMAP.keySet(), " "));
 250            }
 251          }
 252        }
 253      } else {
 254        LOG.warn("PersistenceManagerFactory returned null DataStoreCache object. Unable to initialize object pin types defined by hive.metastore.cache.pinobjtypes");
 255      }
 256    }
 257    return pmf;
 258  }
 259
 260  private PersistenceManager getPersistenceManager() {
 261    return getPMF().getPersistenceManager();
 262  }
 263
 264  public void shutdown() {
 265    if (pm != null) {
 266      pm.close();
 267    }
 268  }
 269
 270  /**
 271   * Opens a new one or the one already created Every call of this function must
 272   * have corresponding commit or rollback function call
 273   *
 274   * @return an active transaction
 275   */
 276
 277  public boolean openTransaction() {
 278    openTrasactionCalls++;
 279    if (openTrasactionCalls == 1) {
 280      currentTransaction = pm.currentTransaction();
 281      currentTransaction.begin();
 282      transactionStatus = TXN_STATUS.OPEN;
 283    } else {
 284      // something is wrong since openTransactionCalls is greater than 1 but
 285      // currentTransaction is not active
 286      assert ((currentTransaction != null) && (currentTransaction.isActive()));
 287    }
 288    return currentTransaction.isActive();
 289  }
 290
 291  /**
 292   * if this is the commit of the first open call then an actual commit is
 293   * called.
 294   *
 295   * @return Always returns true
 296   */
 297  @SuppressWarnings("nls")
 298  public boolean commitTransaction() {
 299    if (TXN_STATUS.ROLLBACK == transactionStatus) {
 300      return false;
 301    }
 302    if (openTrasactionCalls <= 0) {
 303      throw new RuntimeException("commitTransaction was called but openTransactionCalls = "
 304          + openTrasactionCalls + ". This probably indicates that there are unbalanced " +
 305          		"calls to openTransaction/commitTransaction");
 306    }
 307    if (!currentTransaction.isActive()) {
 308      throw new RuntimeException(
 309          "Commit is called, but transaction is not active. Either there are"
 310              + " mismatching open and close calls or rollback was called in the same trasaction");
 311    }
 312    openTrasactionCalls--;
 313    if ((openTrasactionCalls == 0) && currentTransaction.isActive()) {
 314      transactionStatus = TXN_STATUS.COMMITED;
 315      currentTransaction.commit();
 316    }
 317    return true;
 318  }
 319
 320  /**
 321   * @return true if there is an active transaction. If the current transaction
 322   *         is either committed or rolled back it returns false
 323   */
 324  public boolean isActiveTransaction() {
 325    if (currentTransaction == null) {
 326      return false;
 327    }
 328    return currentTransaction.isActive();
 329  }
 330
 331  /**
 332   * Rolls back the current transaction if it is active
 333   */
 334  public void rollbackTransaction() {
 335    if (openTrasactionCalls < 1) {
 336      return;
 337    }
 338    openTrasactionCalls = 0;
 339    if (currentTransaction.isActive()
 340        && transactionStatus != TXN_STATUS.ROLLBACK) {
 341      transactionStatus = TXN_STATUS.ROLLBACK;
 342      // could already be rolled back
 343      currentTransaction.rollback();
 344    }
 345  }
 346
 347  public void createDatabase(Database db) throws InvalidObjectException, MetaException {
 348    boolean commited = false;
 349    MDatabase mdb = new MDatabase();
 350    mdb.setName(db.getName().toLowerCase());
 351    mdb.setLocationUri(db.getLocationUri());
 352    mdb.setDescription(db.getDescription());
 353    mdb.setParameters(db.getParameters());
 354    try {
 355      openTransaction();
 356      pm.makePersistent(mdb);
 357      commited = commitTransaction();
 358    } finally {
 359      if (!commited) {
 360        rollbackTransaction();
 361      }
 362    }
 363  }
 364
 365  @SuppressWarnings("nls")
 366  private MDatabase getMDatabase(String name) throws NoSuchObjectException {
 367    MDatabase mdb = null;
 368    boolean commited = false;
 369    try {
 370      openTransaction();
 371      name = name.toLowerCase().trim();
 372      Query query = pm.newQuery(MDatabase.class, "name == dbname");
 373      query.declareParameters("java.lang.String dbname");
 374      query.setUnique(true);
 375      mdb = (MDatabase) query.execute(name);
 376      pm.retrieve(mdb);
 377      commited = commitTransaction();
 378    } finally {
 379      if (!commited) {
 380        rollbackTransaction();
 381      }
 382    }
 383    if (mdb == null) {
 384      throw new NoSuchObjectException("There is no database named " + name);
 385    }
 386    return mdb;
 387  }
 388
 389  public Database getDatabase(String name) throws NoSuchObjectException {
 390    MDatabase mdb = null;
 391    boolean commited = false;
 392    try {
 393      openTransaction();
 394      mdb = getMDatabase(name);
 395      commited = commitTransaction();
 396    } finally {
 397      if (!commited) {
 398        rollbackTransaction();
 399      }
 400    }
 401    Database db = new Database();
 402    db.setName(mdb.getName());
 403    db.setDescription(mdb.getDescription());
 404    db.setLocationUri(mdb.getLocationUri());
 405    db.setParameters(mdb.getParameters());
 406    return db;
 407  }
 408  
 409  /**
 410   * Alter the database object in metastore. Currently only the parameters
 411   * of the database can be changed.
 412   * @param dbName the database name
 413   * @param db the Hive Database object
 414   * @throws MetaException
 415   * @throws NoSuchObjectException
 416   */
 417  public boolean alterDatabase(String dbName, Database db)
 418    throws MetaException, NoSuchObjectException {
 419
 420    MDatabase mdb = null;
 421    boolean committed = false;
 422    try {
 423      mdb = getMDatabase(dbName);
 424      // currently only allow changing database parameters
 425      mdb.setParameters(db.getParameters());
 426      openTransaction();
 427      pm.makePersistent(mdb);
 428      committed = commitTransaction();
 429    } finally {
 430      if (!committed) {
 431        rollbackTransaction();
 432        return false;
 433      }
 434    }
 435    return true;
 436  }
 437
 438  public boolean dropDatabase(String dbname) throws NoSuchObjectException, MetaException {
 439    boolean success = false;
 440    LOG.info("Dropping database " + dbname + " along with all tables");
 441    dbname = dbname.toLowerCase();
 442    try {
 443      openTransaction();
 444
 445      // first drop tables
 446      for (String tableName : getAllTables(dbname)) {
 447        dropTable(dbname, tableName);
 448      }
 449
 450      // then drop the database
 451      MDatabase db = getMDatabase(dbname);
 452      pm.retrieve(db);
 453      if (db != null) {
 454        List<MDBPrivilege> dbGrants = this.listDatabaseGrants(dbname);
 455        if (dbGrants != null && dbGrants.size() > 0) {
 456          pm.deletePersistentAll(dbGrants);
 457        }
 458        pm.deletePersistent(db);
 459      }
 460      success = commitTransaction();
 461    } finally {
 462      if (!success) {
 463        rollbackTransaction();
 464      }
 465    }
 466    return success;
 467  }
 468
 469
 470  public List<String> getDatabases(String pattern) throws MetaException {
 471    boolean commited = false;
 472    List<String> databases = null;
 473    try {
 474      openTransaction();
 475      // Take the pattern and split it on the | to get all the composing
 476      // patterns
 477      String[] subpatterns = pattern.trim().split("\\|");
 478      String query = "select name from org.apache.hadoop.hive.metastore.model.MDatabase where (";
 479      boolean first = true;
 480      for (String subpattern : subpatterns) {
 481        subpattern = "(?i)" + subpattern.replaceAll("\\*", ".*");
 482        if (!first) {
 483          query = query + " || ";
 484        }
 485        query = query + " name.matches(\"" + subpattern + "\")";
 486        first = false;
 487      }
 488      query = query + ")";
 489
 490      Query q = pm.newQuery(query);
 491      q.setResult("name");
 492      q.setOrdering("name ascending");
 493      Collection names = (Collection) q.execute();
 494      databases = new ArrayList<String>();
 495      for (Iterator i = names.iterator(); i.hasNext();) {
 496        databases.add((String) i.next());
 497      }
 498      commited = commitTransaction();
 499    } finally {
 500      if (!commited) {
 501        rollbackTransaction();
 502      }
 503    }
 504    return databases;
 505  }
 506
 507  public List<String> getAllDatabases() throws MetaException {
 508    return getDatabases(".*");
 509  }
 510
 511  private MType getMType(Type type) {
 512    List<MFieldSchema> fields = new ArrayList<MFieldSchema>();
 513    if (type.getFields() != null) {
 514      for (FieldSchema field : type.getFields()) {
 515        fields.add(new MFieldSchema(field.getName(), field.getType(), field
 516            .getComment()));
 517      }
 518    }
 519    return new MType(type.getName(), type.getType1(), type.getType2(), fields);
 520  }
 521
 522  private Type getType(MType mtype) {
 523    List<FieldSchema> fields = new ArrayList<FieldSchema>();
 524    if (mtype.getFields() != null) {
 525      for (MFieldSchema field : mtype.getFields()) {
 526        fields.add(new FieldSchema(field.getName(), field.getType(), field
 527            .getComment()));
 528      }
 529    }
 530    Type ret = new Type();
 531    ret.setName(mtype.getName());
 532    ret.setType1(mtype.getType1());
 533    ret.setType2(mtype.getType2());
 534    ret.setFields(fields);
 535    return ret;
 536  }
 537
 538  public boolean createType(Type type) {
 539    boolean success = false;
 540    MType mtype = getMType(type);
 541    boolean commited = false;
 542    try {
 543      openTransaction();
 544      pm.makePersistent(mtype);
 545      commited = commitTransaction();
 546      success = true;
 547    } finally {
 548      if (!commited) {
 549        rollbackTransaction();
 550      }
 551    }
 552    return success;
 553  }
 554
 555  public Type getType(String typeName) {
 556    Type type = null;
 557    boolean commited = false;
 558    try {
 559      openTransaction();
 560      Query query = pm.newQuery(MType.class, "name == typeName");
 561      query.declareParameters("java.lang.String typeName");
 562      query.setUnique(true);
 563      MType mtype = (MType) query.execute(typeName.trim());
 564      pm.retrieve(type);
 565      if (mtype != null) {
 566        type = getType(mtype);
 567      }
 568      commited = commitTransaction();
 569    } finally {
 570      if (!commited) {
 571        rollbackTransaction();
 572      }
 573    }
 574    return type;
 575  }
 576
 577  public boolean dropType(String typeName) {
 578    boolean success = false;
 579    boolean commited = false;
 580    try {
 581      openTransaction();
 582      Query query = pm.newQuery(MType.class, "name == typeName");
 583      query.declareParameters("java.lang.String typeName");
 584      query.setUnique(true);
 585      MType type = (MType) query.execute(typeName.trim());
 586      pm.retrieve(type);
 587      pm.deletePersistent(type);
 588      commited = commitTransaction();
 589      success = true;
 590    } catch (JDOObjectNotFoundException e) {
 591      commited = commitTransaction();
 592      LOG.debug("type not found " + typeName, e);
 593    } finally {
 594      if (!commited) {
 595        rollbackTransaction();
 596      }
 597    }
 598    return success;
 599  }
 600
 601  public void createTable(Table tbl) throws InvalidObjectException, MetaException {
 602    boolean commited = false;
 603    try {
 604      openTransaction();
 605      MTable mtbl = convertToMTable(tbl);
 606      pm.makePersistent(mtbl);
 607      PrincipalPrivilegeSet principalPrivs = tbl.getPrivileges();
 608      List<Object> toPersistPrivObjs = new ArrayList<Object>();
 609      if (principalPrivs != null) {
 610        int now = (int)(System.currentTimeMillis()/1000);
 611
 612        Map<String, List<PrivilegeGrantInfo>> userPrivs = principalPrivs.getUserPrivileges();
 613        putPersistentPrivObjects(mtbl, toPersistPrivObjs, now, userPrivs, PrincipalType.USER);
 614
 615        Map<String, List<PrivilegeGrantInfo>> groupPrivs = principalPrivs.getGroupPrivileges();
 616        putPersistentPrivObjects(mtbl, toPersistPrivObjs, now, groupPrivs, PrincipalType.GROUP);
 617        
 618        Map<String, List<PrivilegeGrantInfo>> rolePrivs = principalPrivs.getRolePrivileges();
 619        putPersistentPrivObjects(mtbl, toPersistPrivObjs, now, rolePrivs, PrincipalType.ROLE);
 620      }
 621      pm.makePersistentAll(toPersistPrivObjs);
 622      commited = commitTransaction();
 623    } finally {
 624      if (!commited) {
 625        rollbackTransaction();
 626      }
 627    }
 628  }
 629
 630  /**
 631   * Convert PrivilegeGrantInfo from privMap to MTablePrivilege, and add all of
 632   * them to the toPersistPrivObjs. These privilege objects will be persisted as
 633   * part of createTable.
 634   * 
 635   * @param mtbl
 636   * @param toPersistPrivObjs
 637   * @param now
 638   * @param privMap
 639   * @param type
 640   */
 641  private void putPersistentPrivObjects(MTable mtbl, List<Object> toPersistPrivObjs,
 642      int now, Map<String, List<PrivilegeGrantInfo>> privMap, PrincipalType type) {
 643    if (privMap != null) {
 644      for (Map.Entry<String, List<PrivilegeGrantInfo>> entry : privMap
 645          .entrySet()) {
 646        String principalName = entry.getKey();
 647        List<PrivilegeGrantInfo> privs = entry.getValue();
 648        for (int i = 0; i < privs.size(); i++) {
 649          PrivilegeGrantInfo priv = privs.get(i);
 650          if (priv == null) {
 651            continue;
 652          }
 653          MTablePrivilege mTblSec = new MTablePrivilege(
 654              principalName, type.toString(), mtbl, priv.getPrivilege(),
 655              now, priv.getGrantor(), priv.getGrantorType().toString(), priv
 656                  .isGrantOption());
 657          toPersistPrivObjs.add(mTblSec);
 658        }
 659      }
 660    }
 661  }
 662
 663  public boolean dropTable(String dbName, String tableName) throws MetaException {
 664    boolean success = false;
 665    try {
 666      openTransaction();
 667      MTable tbl = getMTable(dbName, tableName);
 668      pm.retrieve(tbl);
 669      if (tbl != null) {
 670        // first remove all the partitions
 671        List<MTablePrivilege> tabGrants = listAllTableGrants(dbName, tableName);
 672        if (tabGrants != null && tabGrants.size() > 0) {
 673          pm.deletePersistentAll(tabGrants);
 674        }
 675        List<MTableColumnPrivilege> tblColGrants = listTableAllColumnGrants(dbName,
 676            tableName);
 677        if (tblColGrants != null && tblColGrants.size() > 0) {
 678          pm.deletePersistentAll(tblColGrants);
 679        }
 680
 681        List<MPartitionPrivilege> partGrants = this.listTableAllPartitionGrants(dbName, tableName);
 682        if (partGrants != null && partGrants.size() > 0) {
 683          pm.deletePersistentAll(partGrants);
 684        }
 685        
 686        List<MPartitionColumnPrivilege> partColGrants = listTableAllPartitionColumnGrants(dbName,
 687            tableName);
 688        if (partColGrants != null && partColGrants.size() > 0) {
 689          pm.deletePersistentAll(partColGrants);
 690        }
 691        pm.deletePersistentAll(listMPartitions(dbName, tableName, -1));
 692        // then remove the table
 693        pm.deletePersistentAll(tbl);
 694      }
 695      success = commitTransaction();
 696    } finally {
 697      if (!success) {
 698        rollbackTransaction();
 699      }
 700    }
 701    return success;
 702  }
 703  
 704  public Table getTable(String dbName, String tableName) throws MetaException {
 705    boolean commited = false;
 706    Table tbl = null;
 707    try {
 708      openTransaction();
 709      tbl = convertToTable(getMTable(dbName, tableName));
 710      commited = commitTransaction();
 711    } finally {
 712      if (!commited) {
 713        rollbackTransaction();
 714      }
 715    }
 716    return tbl;
 717  }
 718
 719  public List<String> getTables(String dbName, String pattern)
 720      throws MetaException {
 721    boolean commited = false;
 722    List<String> tbls = null;
 723    try {
 724      openTransaction();
 725      dbName = dbName.toLowerCase().trim();
 726      // Take the pattern and split it on the | to get all the composing
 727      // patterns
 728      String[] subpatterns = pattern.trim().split("\\|");
 729      String query =
 730        "select tableName from org.apache.hadoop.hive.metastore.model.MTable "
 731        + "where database.name == dbName && (";
 732      boolean first = true;
 733      for (String subpattern : subpatterns) {
 734        subpattern = "(?i)" + subpattern.replaceAll("\\*", ".*");
 735        if (!first) {
 736          query = query + " || ";
 737        }
 738        query = query + " tableName.matches(\"" + subpattern + "\")";
 739        first = false;
 740      }
 741      query = query + ")";
 742
 743      Query q = pm.newQuery(query);
 744      q.declareParameters("java.lang.String dbName");
 745      q.setResult("tableName");
 746      q.setOrdering("tableName ascending");
 747      Collection names = (Collection) q.execute(dbName);
 748      tbls = new ArrayList<String>();
 749      for (Iterator i = names.iterator(); i.hasNext();) {
 750        tbls.add((String) i.next());
 751      }
 752      commited = commitTransaction();
 753    } finally {
 754      if (!commited) {
 755        rollbackTransaction();
 756      }
 757    }
 758    return tbls;
 759  }
 760
 761  public List<String> getAllTables(String dbName) throws MetaException {
 762    return getTables(dbName, ".*");
 763  }
 764
 765  private MTable getMTable(String db, String table) {
 766    MTable mtbl = null;
 767    boolean commited = false;
 768    try {
 769      openTransaction();
 770      db = db.toLowerCase().trim();
 771      table = table.toLowerCase().trim();
 772      Query query = pm.newQuery(MTable.class, "tableName == table && database.name == db");
 773      query.declareParameters("java.lang.String table, java.lang.String db");
 774      query.setUnique(true);
 775      mtbl = (MTable) query.execute(table, db);
 776      pm.retrieve(mtbl);
 777      commited = commitTransaction();
 778    } finally {
 779      if (!commited) {
 780        rollbackTransaction();
 781      }
 782    }
 783    return mtbl;
 784  }
 785
 786  private Table convertToTable(MTable mtbl) throws MetaException {
 787    if (mtbl == null) {
 788      return null;
 789    }
 790    String tableType = mtbl.getTableType();
 791    if (tableType == null) {
 792      // for backwards compatibility with old metastore persistence
 793      if (mtbl.getViewOriginalText() != null) {
 794        tableType = TableType.VIRTUAL_VIEW.toString();
 795      } else if ("TRUE".equals(mtbl.getParameters().get("EXTERNAL"))) {
 796        tableType = TableType.EXTERNAL_TABLE.toString();
 797      } else {
 798        tableType = TableType.MANAGED_TABLE.toString();
 799      }
 800    }
 801    return new Table(mtbl.getTableName(), mtbl.getDatabase().getName(), mtbl
 802        .getOwner(), mtbl.getCreateTime(), mtbl.getLastAccessTime(), mtbl
 803        .getRetention(), convertToStorageDescriptor(mtbl.getSd()),
 804        convertToFieldSchemas(mtbl.getPartitionKeys()), mtbl.getParameters(),
 805        mtbl.getViewOriginalText(), mtbl.getViewExpandedText(),
 806        tableType);
 807  }
 808
 809  private MTable convertToMTable(Table tbl) throws InvalidObjectException,
 810      MetaException {
 811    if (tbl == null) {
 812      return null;
 813    }
 814    MDatabase mdb = null;
 815    try {
 816      mdb = getMDatabase(tbl.getDbName());
 817    } catch (NoSuchObjectException e) {
 818      LOG.error(StringUtils.stringifyException(e));
 819      throw new InvalidObjectException("Database " + tbl.getDbName()
 820          + " doesn't exist.");
 821    }
 822
 823    // If the table has property EXTERNAL set, update table type
 824    // accordingly
 825    String tableType = tbl.getTableType();
 826    boolean isExternal = "TRUE".equals(tbl.getParameters().get("EXTERNAL"));
 827    if (TableType.MANAGED_TABLE.toString().equals(tableType)) {
 828      if (isExternal) {
 829        tableType = TableType.EXTERNAL_TABLE.toString();
 830      }
 831    }
 832    if (TableType.EXTERNAL_TABLE.toString().equals(tableType)) {
 833      if (!isExternal) {
 834        tableType = TableType.MANAGED_TABLE.toString();
 835      }
 836    }
 837
 838    return new MTable(tbl.getTableName().toLowerCase(), mdb,
 839        convertToMStorageDescriptor(tbl.getSd()), tbl.getOwner(), tbl
 840            .getCreateTime(), tbl.getLastAccessTime(), tbl.getRetention(),
 841        convertToMFieldSchemas(tbl.getPartitionKeys()), tbl.getParameters(),
 842        tbl.getViewOriginalText(), tbl.getViewExpandedText(),
 843        tableType);
 844  }
 845
 846  private List<MFieldSchema> convertToMFieldSchemas(List<FieldSchema> keys) {
 847    List<MFieldSchema> mkeys = null;
 848    if (keys != null) {
 849      mkeys = new ArrayList<MFieldSchema>(keys.size());
 850      for (FieldSchema part : keys) {
 851        mkeys.add(new MFieldSchema(part.getName().toLowerCase(),
 852            part.getType(), part.getComment()));
 853      }
 854    }
 855    return mkeys;
 856  }
 857
 858  private List<FieldSchema> convertToFieldSchemas(List<MFieldSchema> mkeys) {
 859    List<FieldSchema> keys = null;
 860    if (mkeys != null) {
 861      keys = new ArrayList<FieldSchema>(mkeys.size());
 862      for (MFieldSchema part : mkeys) {
 863        keys.add(new FieldSchema(part.getName(), part.getType(), part
 864            .getComment()));
 865      }
 866    }
 867    return keys;
 868  }
 869
 870  private List<MOrder> convertToMOrders(List<Order> keys) {
 871    List<MOrder> mkeys = null;
 872    if (keys != null) {
 873      mkeys = new ArrayList<MOrder>(keys.size());
 874      for (Order part : keys) {
 875        mkeys.add(new MOrder(part.getCol().toLowerCase(), part.getOrder()));
 876      }
 877    }
 878    return mkeys;
 879  }
 880
 881  private List<Order> convertToOrders(List<MOrder> mkeys) {
 882    List<Order> keys = null;
 883    if (mkeys != null) {
 884      keys = new ArrayList<Order>();
 885      for (MOrder part : mkeys) {
 886        keys.add(new Order(part.getCol(), part.getOrder()));
 887      }
 888    }
 889    return keys;
 890  }
 891
 892  private SerDeInfo converToSerDeInfo(MSerDeInfo ms) throws MetaException {
 893    if (ms == null) {
 894      throw new MetaException("Invalid SerDeInfo object");
 895    }
 896    return new SerDeInfo(ms.getName(), ms.getSerializationLib(), ms
 897        .getParameters());
 898  }
 899
 900  private MSerDeInfo converToMSerDeInfo(SerDeInfo ms) throws MetaException {
 901    if (ms == null) {
 902      throw new MetaException("Invalid SerDeInfo object");
 903    }
 904    return new MSerDeInfo(ms.getName(), ms.getSerializationLib(), ms
 905        .getParameters());
 906  }
 907
 908  // MSD and SD should be same objects. Not sure how to make then same right now
 909  // MSerdeInfo *& SerdeInfo should be same as well
 910  private StorageDescriptor convertToStorageDescriptor(MStorageDescriptor msd)
 911      throws MetaException {
 912    if (msd == null) {
 913      return null;
 914    }
 915    return new StorageDescriptor(convertToFieldSchemas(msd.getCols()), msd
 916        .getLocation(), msd.getInputFormat(), msd.getOutputFormat(), msd
 917        .isCompressed(), msd.getNumBuckets(), converToSerDeInfo(msd
 918        .getSerDeInfo()), msd.getBucketCols(), convertToOrders(msd
 919        .getSortCols()), msd.getParameters());
 920  }
 921
 922  private MStorageDescriptor convertToMStorageDescriptor(StorageDescriptor sd)
 923      throws MetaException {
 924    if (sd == null) {
 925      return null;
 926    }
 927    return new MStorageDescriptor(convertToMFieldSchemas(sd.getCols()), sd
 928        .getLocation(), sd.getInputFormat(), sd.getOutputFormat(), sd
 929        .isCompressed(), sd.getNumBuckets(), converToMSerDeInfo(sd
 930        .getSerdeInfo()), sd.getBucketCols(),
 931        convertToMOrders(sd.getSortCols()), sd.getParameters());
 932  }
 933
 934  public boolean addPartition(Partition part) throws InvalidObjectException,
 935      MetaException {
 936    boolean success = false;
 937    boolean commited = false;
 938    try {
 939      MTable table = this.getMTable(part.getDbName(), part.getTableName());
 940      List<MTablePrivilege> tabGrants = null;
 941      List<MTableColumnPrivilege> tabColumnGrants = null;
 942      if ("TRUE".equalsIgnoreCase(table.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) {
 943        tabGrants = this.listAllTableGrants(part
 944            .getDbName(), part.getTableName());
 945        tabColumnGrants = this.listTableAllColumnGrants(
 946            part.getDbName(), part.getTableName());
 947      }
 948      openTransaction();
 949      MPartition mpart = convertToMPart(part);
 950      pm.makePersistent(mpart);
 951
 952      int now = (int)(System.currentTimeMillis()/1000);
 953      List<Object> toPersist = new ArrayList<Object>();
 954      if (tabGrants != null) {
 955        for (MTablePrivilege tab: tabGrants) {
 956          MPartitionPrivilege partGrant = new MPartitionPrivilege(tab
 957              .getPrincipalName(), tab.getPrincipalType(),
 958              mpart, tab.getPrivilege(), now, tab.getGrantor(), tab
 959                  .getGrantorType(), tab.getGrantOption());
 960          toPersist.add(partGrant);
 961        }
 962      }
 963      
 964      if (tabColumnGrants != null) {
 965        for (MTableColumnPrivilege col : tabColumnGrants) {
 966          MPartitionColumnPrivilege partColumn = new MPartitionColumnPrivilege(col
 967              .getPrincipalName(), col.getPrincipalType(), mpart, col
 968              .getColumnName(), col.getPrivilege(), now, col.getGrantor(), col
 969              .getGrantorType(), col.getGrantOption());
 970          toPersist.add(partColumn);
 971        }
 972        
 973        if (toPersist.size() > 0) {
 974          pm.makePersistentAll(toPersist);
 975        }
 976      }
 977
 978      commited = commitTransaction();
 979      success = true;
 980    } finally {
 981      if (!commited) {
 982        rollbackTransaction();
 983      }
 984    }
 985    return success;
 986  }
 987
 988  public Partition getPartition(String dbName, String tableName,
 989      List<String> part_vals) throws NoSuchObjectException, MetaException {
 990    openTransaction();
 991    Partition part = convertToPart(getMPartition(dbName, tableName, part_vals));
 992    commitTransaction();
 993    if(part == null) {
 994      throw new NoSuchObjectException("partition values="
 995          + part_vals.toString());
 996    }
 997    return part;
 998  }
 999  
1000  private MPartition getMPartition(String dbName, String tableName,
1001      List<String> part_vals) throws MetaException {
1002    MPartition mpart = null;
1003    boolean commited = false;
1004    try {
1005      openTransaction();
1006      dbName = dbName.toLowerCase().trim();
1007      tableName = tableName.toLowerCase().trim();
1008      MTable mtbl = getMTable(dbName, tableName);
1009      if (mtbl == null) {
1010        commited = commitTransaction();
1011        return null;
1012      }
1013      // Change the query to use part_vals instead of the name which is
1014      // redundant
1015      String name = Warehouse.makePartName(convertToFieldSchemas(mtbl
1016          .getPartitionKeys()), part_vals);
1017      Query query = pm.newQuery(MPartition.class,
1018          "table.tableName == t1 && table.database.name == t2 && partitionName == t3");
1019      query.declareParameters("java.lang.String t1, java.lang.String t2, java.lang.String t3");
1020      query.setUnique(true);
1021      mpart = (MPartition) query.execute(tableName, dbName, name);
1022      pm.retrieve(mpart);
1023      commited = commitTransaction();
1024    } finally {
1025      if (!commited) {
1026        rollbackTransaction();
1027      }
1028    }
1029    return mpart;
1030  }
1031
1032  private MPartition convertToMPart(Partition part)
1033      throws InvalidObjectException, MetaException {
1034    if (part == null) {
1035      return null;
1036    }
1037    MTable mt = getMTable(part.getDbName(), part.getTableName());
1038    if (mt == null) {
1039      throw new InvalidObjectException(
1040          "Partition doesn't have a valid table or database name");
1041    }
1042    return new MPartition(Warehouse.makePartName(convertToFieldSchemas(mt
1043        .getPartitionKeys()), part.getValues()), mt, part.getValues(), part
1044        .getCreateTime(), part.getLastAccessTime(),
1045        convertToMStorageDescriptor(part.getSd()), part.getParameters());
1046  }
1047
1048  private Partition convertToPart(MPartition mpart) throws MetaException {
1049    if (mpart == null) {
1050      return null;
1051    }
1052    return new Partition(mpart.getValues(), mpart.getTable().getDatabase()
1053        .getName(), mpart.getTable().getTableName(), mpart.getCreateTime(),
1054        mpart.getLastAccessTime(), convertToStorageDescriptor(mpart.getSd()),
1055        mpart.getParameters());
1056  }
1057
1058  public boolean dropPartition(String dbName, String tableName,
1059      List<String> part_vals) throws MetaException {
1060    boolean success = false;
1061    try {
1062      openTransaction();
1063      MPartition part = getMPartition(dbName, tableName, part_vals);
1064      if (part != null) {
1065        List<MFieldSchema> schemas = part.getTable().getPartitionKeys();
1066        List<String> colNames = new ArrayList<String>();
1067        for (MFieldSchema col: schemas) {
1068          colNames.add(col.getName());
1069        }
1070        String partName = FileUtils.makePartName(colNames, part_vals);
1071        
1072        List<MPartitionPrivilege> partGrants = listPartitionGrants(
1073            dbName, tableName, partName);
1074
1075        if (partGrants != null && partGrants.size() > 0) {
1076          pm.deletePersistentAll(partGrants);
1077        }
1078
1079        List<MPartitionColumnPrivilege> partColumnGrants = listPartitionAllColumnGrants(
1080            dbName, tableName, partName);
1081        if (partColumnGrants != null && partColumnGrants.size() > 0) {
1082          pm.deletePersistentAll(partColumnGrants);
1083        }
1084
1085        pm.deletePersistent(part);
1086      }
1087      success = commitTransaction();
1088    } finally {
1089      if (!success) {
1090        rollbackTransaction();
1091      }
1092    }
1093    return success;
1094  }
1095
1096  public List<Partition> getPartitions(String dbName, String tableName, int max)
1097      throws MetaException {
1098    openTransaction();
1099    List<Partition> parts = convertToParts(listMPartitions(dbName, tableName,
1100        max));
1101    commitTransaction();
1102    return parts;
1103  }
1104
1105  @Override
1106  public List<Partition> getPartitionsWithAuth(String dbName, String tblName,
1107      short maxParts, String userName, List<String> groupNames)
1108      throws MetaException, NoSuchObjectException, InvalidObjectException {
1109    boolean success = false;
1110    try {
1111      openTransaction();
1112      List<MPartition> mparts = listMPartitions(dbName, tblName, maxParts);
1113      List<Partition> parts = new ArrayList<Partition>(mparts.size());
1114      if (mparts != null && mparts.size()>0) {
1115        for (MPartition mpart : mparts) {
1116          MTable mtbl = mpart.getTable();
1117          Partition part = convertToPart(mpart);
1118          parts.add(part);
1119
1120          if ("TRUE".equalsIgnoreCase(mtbl.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) {
1121            String partName = Warehouse.makePartName(this.convertToFieldSchemas(mtbl
1122                .getPartitionKeys()), part.getValues());
1123            PrincipalPrivilegeSet partAuth = this.getPartitionPrivilegeSet(dbName,
1124                tblName, partName, userName, groupNames);
1125            part.setPrivileges(partAuth);
1126          }
1127        }
1128      }
1129      success =  commitTransaction();
1130      return parts;
1131    } finally {
1132      if (!success) {
1133        rollbackTransaction();
1134      }
1135    }
1136  }
1137  
1138  @Override
1139  public Partition getPartitionWithAuth(String dbName, String tblName,
1140      List<String> partVals, String user_name, List<String> group_names)
1141      throws NoSuchObjectException, MetaException, InvalidObjectException {
1142    boolean success = false;
1143    try {
1144      openTransaction();
1145      MPartition mpart = getMPartition(dbName, tblName, partVals);
1146      if (mpart == null) {
1147        commitTransaction();
1148        throw new NoSuchObjectException("partition values="
1149            + partVals.toString());
1150      }
1151      Partition part = null;
1152      MTable mtbl = mpart.getTable();
1153      part = convertToPart(mpart);
1154      if ("TRUE".equalsIgnoreCase(mtbl.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) {
1155        String partName = Warehouse.makePartName(this.convertToFieldSchemas(mtbl
1156            .getPartitionKeys()), partVals);
1157        PrincipalPrivilegeSet partAuth = this.getPartitionPrivilegeSet(dbName,
1158            tblName, partName, user_name, group_names);
1159        part.setPrivileges(partAuth);
1160      }
1161
1162      success = commitTransaction();
1163      return part;
1164    } finally {
1165      if (!success) {
1166        rollbackTransaction();
1167      }
1168    }
1169  }
1170
1171
1172  private List<Partition> convertToParts(List<MPartition> mparts)
1173      throws MetaException {
1174    List<Partition> parts = new ArrayList<Partition>(mparts.size());
1175    for (MPartition mp : mparts) {
1176      parts.add(convertToPart(mp));
1177    }
1178    return parts;
1179  }
1180
1181  // TODO:pc implement max
1182  public List<String> listPartitionNames(String dbName, String tableName,
1183      short max) throws MetaException {
1184    List<String> pns = new ArrayList<String>();
1185    boolean success = false;
1186    try {
1187      openTransaction();
1188      LOG.debug("Executing getPartitionNames");
1189      dbName = dbName.toLowerCase().trim();
1190      tableName = tableName.toLowerCase().trim();
1191      Query q = pm.newQuery(
1192          "select partitionName from org.apache.hadoop.hive.metastore.model.MPartition "
1193          + "where table.database.name == t1 && table.tableName == t2 "
1194          + "order by partitionName asc");
1195      q.declareParameters("java.lang.String t1, java.lang.String t2");
1196      q.setResult("partitionName");
1197      Collection names = (Collection) q.execute(dbName, tableName);
1198      pns = new ArrayList<String>();
1199      for (Iterator i = names.iterator(); i.hasNext();) {
1200        pns.add((String) i.next());
1201      }
1202      success = commitTransaction();
1203    } finally {
1204      if (!success) {
1205        rollbackTransaction();
1206      }
1207    }
1208    return pns;
1209  }
1210
1211  // TODO:pc implement max
1212  private List<MPartition> listMPartitions(String dbName, String tableName,
1213      int max) {
1214    boolean success = false;
1215    List<MPartition> mparts = null;
1216    try {
1217      openTransaction();
1218      LOG.debug("Executing listMPartitions");
1219      dbName = dbName.toLowerCase().trim();
1220      tableName = tableName.toLowerCase().trim();
1221      Query query = pm.newQuery(MPartition.class,
1222          "table.tableName == t1 && table.database.name == t2");
1223      query.declareParameters("java.lang.String t1, java.lang.String t2");
1224      mparts = (List<MPartition>) query.execute(tableName, dbName);
1225      LOG.debug("Done executing query for listMPartitions");
1226      pm.retrieveAll(mparts);
1227      success = commitTransaction();
1228      LOG.debug("Done retrieving all objects for listMPartitions");
1229    } finally {
1230      if (!success) {
1231        rollbackTransaction();
1232      }
1233    }
1234    return mparts;
1235  }
1236
1237  @Override
1238  public List<Partition> getPartitionsByFilter(String dbName, String tblName,
1239      String filter, short maxParts) throws MetaException, NoSuchObjectException {
1240    openTransaction();
1241    List<Partition> parts = convertToParts(listMPartitionsByFilter(dbName,
1242        tblName, filter, maxParts));
1243    commitTransaction();
1244    return parts;
1245  }
1246
1247  private String makeQueryFilterString(MTable mtable, String filter,
1248      Map<String, String> params)
1249      throws MetaException {
1250    StringBuilder queryBuilder = new StringBuilder(
1251        "table.tableName == t1 && table.database.name == t2");
1252
1253    if( filter != null && filter.length() > 0) {
1254
1255      Table table = convertToTable(mtable);
1256
1257      CharStream cs = new ANTLRNoCaseStringStream(filter);
1258      FilterLexer lexer = new FilterLexer(cs);
1259
1260      CommonTokenStream tokens = new CommonTokenStream();
1261      tokens.setTokenSource (lexer);
1262
1263      FilterParser parser = new FilterParser(tokens);
1264
1265      try {
1266        parser.filter();
1267      } catch(RecognitionException re) {
1268        throw new MetaException("Error parsing partition filter : " + re);
1269      }
1270
1271      String jdoFilter = parser.tree.generateJDOFilter(table, params);
1272
1273      if( jdoFilter.trim().length() > 0 ) {
1274        queryBuilder.append(" && ( ");
1275        queryBuilder.append(jdoFilter.trim());
1276        queryBuilder.append(" )");
1277      }
1278    }
1279
1280    return queryBuilder.toString();
1281  }
1282
1283  private String makeParameterDeclarationString(Map<String, String> params) {
1284    //Create the parameter declaration string
1285    StringBuilder paramDecl = new StringBuilder();
1286    for(String key : params.keySet() ) {
1287      paramDecl.append(", java.lang.String  " + key);
1288    }
1289    return paramDecl.toString();
1290  }
1291
1292  private List<MPartition> listMPartitionsByFilter(String dbName, String tableName,
1293      String filter, short maxParts) throws MetaException, NoSuchObjectException{
1294    boolean success = false;
1295    List<MPartition> mparts = null;
1296    try {
1297      openTransaction();
1298      LOG.debug("Executing listMPartitionsByFilter");
1299      dbName = dbName.toLowerCase();
1300      tableName = tableName.toLowerCase();
1301
1302      MTable mtable = getMTable(dbName, tableName);
1303      if( mtable == null ) {
1304        throw new NoSuchObjectException("Specified database/table does not exist : "
1305            + dbName + "." + tableName);
1306      }
1307      Map<String, String> params = new HashMap<String, String>();
1308      String queryFilterString =
1309        makeQueryFilterString(mtable, filter, params);
1310
1311      Query query = pm.newQuery(MPartition.class,
1312          queryFilterString);
1313
1314      if( maxParts >= 0 ) {
1315        //User specified a row limit, set it on the Query
1316        query.setRange(0, maxParts);
1317      }
1318
1319      LOG.debug("Filter specified is " + filter + "," +
1320             " JDOQL filter is " + queryFilterString);
1321
1322      params.put("t1", tableName.trim());
1323      params.put("t2", dbName.trim());
1324
1325      String parameterDeclaration = makeParameterDeclarationString(params);
1326      query.declareParameters(parameterDeclaration);
1327      query.setOrdering("partitionName ascending");
1328
1329      mparts = (List<MPartition>) query.executeWithMap(params);
1330
1331      LOG.debug("Done executing query for listMPartitionsByFilter");
1332      pm.retrieveAll(mparts);
1333      success = commitTransaction();
1334      LOG.debug("Done retrieving all objects for listMPartitionsByFilter");
1335    } finally {
1336      if (!success) {
1337        rollbackTransaction();
1338      }
1339    }
1340    return mparts;
1341  }
1342
1343  @Override
1344  public List<String> listPartitionNamesByFilter(String dbName, String tableName,
1345      String filter, short maxParts) throws MetaException {
1346    boolean success = false;
1347    List<String> partNames = new ArrayList<String>();
1348    try {
1349      openTransaction();
1350      LOG.debug("Executing listMPartitionsByFilter");
1351      dbName = dbName.toLowerCase();
1352      tableName = tableName.toLowerCase();
1353
1354      MTable mtable = getMTable(dbName, tableName);
1355      if( mtable == null ) {
1356        // To be consistent with the behavior of listPartitionNames, if the
1357        // table or db does not exist, we return an empty list
1358        return partNames;
1359      }
1360      Map<String, String> params = new HashMap<String, String>();
1361      String queryFilterString =
1362        makeQueryFilterString(mtable, filter, params);
1363
1364      Query query = pm.newQuery(
1365          "select partitionName from org.apache.hadoop.hive.metastore.model.MPartition "
1366          + "where " + queryFilterString);
1367
1368      if( maxParts >= 0 ) {
1369        //User specified a row limit, set it on the Query
1370        query.setRange(0, maxParts);
1371      }
1372
1373      LOG.debug("Filter specified is " + filter + "," +
1374          " JDOQL filter is " + queryFilterString);
1375      LOG.debug("Parms is " + params);
1376
1377      params.put("t1", tableName.trim());
1378      params.put("t2", dbName.trim());
1379
1380      String parameterDeclaration = makeParameterDeclarationString(params);
1381      query.declareParameters(parameterDeclaration);
1382      query.setOrdering("partitionName ascending");
1383      query.setResult("partitionName");
1384
1385      Collection names = (Collection) query.executeWithMap(params);
1386      partNames = new ArrayList<String>();
1387      for (Iterator i = names.iterator(); i.hasNext();) {
1388        partNames.add((String) i.next());
1389      }
1390
1391      LOG.debug("Done executing query for listMPartitionNamesByFilter");
1392      success = commitTransaction();
1393      LOG.debug("Done retrieving all objects for listMPartitionNamesByFilter");
1394    } finally {
1395      if (!success) {
1396        rollbackTransaction();
1397      }
1398    }
1399    return partNames;
1400  }
1401
1402  public void alterTable(String dbname, String name, Table newTable)
1403      throws InvalidObjectException, MetaException {
1404    boolean success = false;
1405    try {
1406      openTransaction();
1407      name = name.toLowerCase();
1408      dbname = dbname.toLowerCase();
1409      MTable newt = convertToMTable(newTable);
1410      if (newt == null) {
1411        throw new InvalidObjectException("new table is invalid");
1412      }
1413
1414      MTable oldt = getMTable(dbname, name);
1415      if (oldt == null) {
1416        throw new MetaException("table " + name + " doesn't exist");
1417      }
1418
1419      // For now only alter name, owner, paramters, cols, bucketcols are allowed
1420      oldt.setTableName(newt.getTableName().toLowerCase());
1421      oldt.setParameters(newt.getParameters());
1422      oldt.setOwner(newt.getOwner());
1423      oldt.setSd(newt.getSd());
1424      oldt.setDatabase(newt.getDatabase());
1425      oldt.setRetention(newt.getRetention());
1426      oldt.setPartitionKeys(newt.getPartitionKeys());
1427      oldt.setTableType(newt.getTableType());
1428      oldt.setLastAccessTime(newt.getLastAccessTime());
1429
1430      // commit the changes
1431      success = commitTransaction();
1432    } finally {
1433      if (!success) {
1434        rollbackTransaction();
1435      }
1436    }
1437  }
1438
1439  public void alterIndex(String dbname, String baseTblName, String name, Index newIndex)
1440      throws InvalidObjectException, MetaException {
1441    boolean success = false;
1442    try {
1443      openTransaction();
1444      name = name.toLowerCase();
1445      baseTblName = baseTblName.toLowerCase();
1446      dbname = dbname.toLowerCase();
1447      MIndex newi = convertToMIndex(newIndex);
1448      if (newi == null) {
1449        throw new InvalidObjectException("new index is invalid");
1450      }
1451
1452      MIndex oldi = getMIndex(dbname, baseTblName, name);
1453      if (oldi == null) {
1454        throw new MetaException("index " + name + " doesn't exist");
1455      }
1456
1457      // For now only alter paramters are allowed
1458      oldi.setParameters(newi.getParameters());
1459
1460      // commit the changes
1461      success = commitTransaction();
1462    } finally {
1463      if (!success) {
1464        rollbackTransaction();
1465      }
1466    }
1467  }
1468
1469  publi

Large files files are truncated, but you can click here to view the full file