public Object getFieldValue(_Fields field) { switch (field) { case NAME: return getName(); case DESCRIPTION: return getDescription(); case LOCATION_URI: return getLocationUri(); case CREATE_TIME: return getCreateTime(); } throw new IllegalStateException(); }
@VisibleForTesting public static void createDefaultCatalog(RawStore ms, Warehouse wh) throws MetaException, InvalidOperationException { try { Catalog defaultCat = ms.getCatalog(DEFAULT_CATALOG_NAME); // Null check because in some test cases we get a null from ms.getCatalog. if (defaultCat !=null && defaultCat.getLocationUri().equals("TBD")) { // One time update issue. When the new 'hive' catalog is created in an upgrade the // script does not know the location of the warehouse. So we need to update it. LOG.info("Setting location of default catalog, as it hasn't been done after upgrade"); defaultCat.setLocationUri(wh.getWhRoot().toString()); ms.alterCatalog(defaultCat.getName(), defaultCat); } } catch (NoSuchObjectException e) { Catalog cat = new Catalog(DEFAULT_CATALOG_NAME, wh.getWhRoot().toString()); long time = System.currentTimeMillis() / 1000; cat.setCreateTime((int) time); cat.setDescription(Warehouse.DEFAULT_CATALOG_COMMENT); ms.createCatalog(cat); } }
/** * Build the database path based on catalog name and database name. This should only be used * when a database is being created or altered. If you just want to find out the path a * database is already using call {@link #getDatabasePath(Database)}. If the passed in * database already has a path set that will be used. If not the location will be built using * catalog's path and the database name. * @param cat catalog the database is in * @param db database object * @return Path representing the directory for the database * @throws MetaException when the file path cannot be properly determined from the configured * file system. */ public Path determineDatabasePath(Catalog cat, Database db) throws MetaException { if (db.isSetLocationUri()) { return getDnsPath(new Path(db.getLocationUri())); } if (cat == null || cat.getName().equalsIgnoreCase(DEFAULT_CATALOG_NAME)) { if (db.getName().equalsIgnoreCase(DEFAULT_DATABASE_NAME)) { return getWhRoot(); } else { return new Path(getWhRoot(), dbDirFromDbName(db)); } } else { return new Path(getDnsPath(new Path(cat.getLocationUri())), dbDirFromDbName(db)); } }
@Override public void alterCatalog(String catName, Catalog cat) throws MetaException, InvalidOperationException { if (!cat.getName().equals(catName)) { throw new InvalidOperationException("You cannot change a catalog's name"); } boolean committed = false; try { MCatalog mCat = getMCatalog(catName); if (org.apache.commons.lang.StringUtils.isNotBlank(cat.getLocationUri())) { mCat.setLocationUri(cat.getLocationUri()); } if (org.apache.commons.lang.StringUtils.isNotBlank(cat.getDescription())) { mCat.setDescription(cat.getDescription()); } openTransaction(); pm.makePersistent(mCat); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }
} finally { if (success) { wh.deleteDir(wh.getDnsPath(new Path(cat.getLocationUri())), false, false, false); } else { ms.rollbackTransaction();
private MCatalog catToMCat(Catalog cat) { MCatalog mCat = new MCatalog(); mCat.setName(normalizeIdentifier(cat.getName())); if (cat.isSetDescription()) { mCat.setDescription(cat.getDescription()); } mCat.setLocationUri(cat.getLocationUri()); mCat.setCreateTime(cat.getCreateTime()); return mCat; }
if (catalog.getLocationUri() == null) { throw new InvalidObjectException("You must specify a path for the catalog"); Path catPath = new Path(catalog.getLocationUri()); boolean madeDir = false; Map<String, String> transactionalListenersResponses = Collections.emptyMap(); "Default database for catalog " + catalog.getName(), catalog.getLocationUri(), Collections.emptyMap()); db.setCatalogName(catalog.getName());
.build(); client.createCatalog(cat); File dir = new File(cat.getLocationUri()); Assert.assertTrue(dir.exists() && dir.isDirectory()); Assert.assertTrue(catNames[i].equalsIgnoreCase(cat.getName())); Assert.assertEquals(description[i], cat.getDescription()); Assert.assertEquals(location[i], cat.getLocationUri()); Assert.assertTrue("Create time of catalog should be set", cat.isSetCreateTime()); Assert.assertTrue("Create time of catalog should be non-zero", cat.getCreateTime() > 0); File dir = new File(cat.getLocationUri()); Assert.assertTrue(dir.exists() && dir.isDirectory()); Assert.assertEquals("file:" + cat.getLocationUri(), db.getLocationUri()); client.alterCatalog(catNames[0], newCat); Catalog fetchedNewCat = client.getCatalog(catNames[0]); Assert.assertEquals(newLocation, fetchedNewCat.getLocationUri()); Assert.assertEquals(description[0], fetchedNewCat.getDescription()); client.alterCatalog(catNames[1], newCat); fetchedNewCat = client.getCatalog(catNames[1]); Assert.assertEquals(location[1], fetchedNewCat.getLocationUri()); Assert.assertEquals(newDescription, fetchedNewCat.getDescription());
String expectedLocation = new File(cat.getLocationUri(), dbNames[0] + ".db").toURI().toString(); Assert.assertEquals(expectedLocation, fetched.getLocationUri() + "/"); String db0Location = new URI(fetched.getLocationUri()).getPath();
@Test public void catalogs() throws MetaException, NoSuchObjectException { final String names[] = {"cat1", "cat2"}; final String locations[] = {"loc1", "loc2"}; final String descriptions[] = {"description 1", "description 2"}; for (int i = 0; i < names.length; i++) { Catalog cat = new CatalogBuilder() .setName(names[i]) .setLocation(locations[i]) .setDescription(descriptions[i]) .build(); objectStore.createCatalog(cat); } List<String> fetchedNames = objectStore.getCatalogs(); Assert.assertEquals(3, fetchedNames.size()); for (int i = 0; i < names.length - 1; i++) { Assert.assertEquals(names[i], fetchedNames.get(i)); Catalog cat = objectStore.getCatalog(fetchedNames.get(i)); Assert.assertEquals(names[i], cat.getName()); Assert.assertEquals(descriptions[i], cat.getDescription()); Assert.assertEquals(locations[i], cat.getLocationUri()); } Catalog cat = objectStore.getCatalog(fetchedNames.get(2)); Assert.assertEquals(DEFAULT_CATALOG_NAME, cat.getName()); Assert.assertEquals(Warehouse.DEFAULT_CATALOG_COMMENT, cat.getDescription()); // Location will vary by system. for (int i = 0; i < names.length; i++) objectStore.dropCatalog(names[i]); fetchedNames = objectStore.getCatalogs(); Assert.assertEquals(1, fetchedNames.size()); }
new File(MetaStoreTestUtils.getTestWarehouseDir(tableNames[i])).toURI().toString() new File(cat.getLocationUri() + File.separatorChar + dbName + ".db", tableNames[i]).toURI().toString();
@VisibleForTesting public static void createDefaultCatalog(RawStore ms, Warehouse wh) throws MetaException, InvalidOperationException { try { Catalog defaultCat = ms.getCatalog(DEFAULT_CATALOG_NAME); // Null check because in some test cases we get a null from ms.getCatalog. if (defaultCat !=null && defaultCat.getLocationUri().equals("TBD")) { // One time update issue. When the new 'hive' catalog is created in an upgrade the // script does not know the location of the warehouse. So we need to update it. LOG.info("Setting location of default catalog, as it hasn't been done after upgrade"); defaultCat.setLocationUri(wh.getWhRoot().toString()); ms.alterCatalog(defaultCat.getName(), defaultCat); } } catch (NoSuchObjectException e) { Catalog cat = new Catalog(DEFAULT_CATALOG_NAME, wh.getWhRoot().toString()); cat.setDescription(Warehouse.DEFAULT_CATALOG_COMMENT); ms.createCatalog(cat); } }
/** * Build the database path based on catalog name and database name. This should only be used * when a database is being created or altered. If you just want to find out the path a * database is already using call {@link #getDatabasePath(Database)}. If the passed in * database already has a path set that will be used. If not the location will be built using * catalog's path and the database name. * @param cat catalog the database is in * @param db database object * @return Path representing the directory for the database * @throws MetaException when the file path cannot be properly determined from the configured * file system. */ public Path determineDatabasePath(Catalog cat, Database db) throws MetaException { if (db.isSetLocationUri()) { return getDnsPath(new Path(db.getLocationUri())); } if (cat == null || cat.getName().equalsIgnoreCase(DEFAULT_CATALOG_NAME)) { if (db.getName().equalsIgnoreCase(DEFAULT_DATABASE_NAME)) { return getWhRoot(); } else { return new Path(getWhRoot(), dbDirFromDbName(db)); } } else { return new Path(getDnsPath(new Path(cat.getLocationUri())), dbDirFromDbName(db)); } }
@Override public void alterCatalog(String catName, Catalog cat) throws MetaException, InvalidOperationException { if (!cat.getName().equals(catName)) { throw new InvalidOperationException("You cannot change a catalog's name"); } boolean committed = false; try { MCatalog mCat = getMCatalog(catName); if (org.apache.commons.lang.StringUtils.isNotBlank(cat.getLocationUri())) { mCat.setLocationUri(cat.getLocationUri()); } if (org.apache.commons.lang.StringUtils.isNotBlank(cat.getDescription())) { mCat.setDescription(cat.getDescription()); } openTransaction(); pm.makePersistent(mCat); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }
private MCatalog catToMCat(Catalog cat) { MCatalog mCat = new MCatalog(); mCat.setName(normalizeIdentifier(cat.getName())); if (cat.isSetDescription()) { mCat.setDescription(cat.getDescription()); } mCat.setLocationUri(cat.getLocationUri()); return mCat; }
} finally { if (success) { wh.deleteDir(wh.getDnsPath(new Path(cat.getLocationUri())), false, false, false); } else { ms.rollbackTransaction();
if (catalog.getLocationUri() == null) { throw new InvalidObjectException("You must specify a path for the catalog"); Path catPath = new Path(catalog.getLocationUri()); boolean madeDir = false; Map<String, String> transactionalListenersResponses = Collections.emptyMap(); catalog.getName(), catalog.getLocationUri(), Collections.emptyMap()); db.setCatalogName(catalog.getName()); create_database_core(ms, db);