public Long addStock(String symbol) throws NotLoggedInException { Stock stock = new Stock(getUser(), symbol); checkLoggedIn(); PersistenceManager pm = getPersistenceManager(); try { pm.makePersistent(stock); } finally { pm.close(); } return stock.getId(); }
// Your upload handle would look like public void doPost(HttpServletRequest req, HttpServletResponse res) { // Get the image representation ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iter = upload.getItemIterator(req); FileItemStream imageItem = iter.next(); InputStream imgStream = imageItem.openStream(); // construct our entity objects Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream)); MyImage myImage = new MyImage(imageItem.getName(), imageBlob); // persist image PersistenceManager pm = PMF.get().getPersistenceManager(); pm.makePersistent(myImage); pm.close(); // respond to query res.setContentType("text/plain"); res.getOutputStream().write("OK!".getBytes()); }
@Override public void addRuntimeStat(RuntimeStat stat) throws MetaException { LOG.debug("runtimeStat: " + stat); MRuntimeStat mStat = MRuntimeStat.fromThrift(stat); boolean committed = false; openTransaction(); try { pm.makePersistent(mStat); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }
@Override public void dropWMTriggerToPoolMapping(String resourcePlanName, String triggerName, String poolPath, String ns) throws NoSuchObjectException, InvalidOperationException, MetaException { boolean commited = false; try { openTransaction(); MWMResourcePlan resourcePlan = getMWMResourcePlan(resourcePlanName, ns, true); MWMPool pool = getPool(resourcePlan, poolPath); MWMTrigger trigger = getTrigger(resourcePlan, triggerName); pool.getTriggers().remove(trigger); trigger.getPools().remove(pool); pm.makePersistent(pool); pm.makePersistent(trigger); commited = commitTransaction(); } finally { rollbackAndCleanup(commited, (Query)null); } }
@Override public void createWMTriggerToPoolMapping(String resourcePlanName, String triggerName, String poolPath, String ns) throws AlreadyExistsException, NoSuchObjectException, InvalidOperationException, MetaException { boolean commited = false; try { openTransaction(); MWMResourcePlan resourcePlan = getMWMResourcePlan(resourcePlanName, ns, true); MWMPool pool = getPool(resourcePlan, poolPath); MWMTrigger trigger = getTrigger(resourcePlan, triggerName); pool.getTriggers().add(trigger); trigger.getPools().add(pool); pm.makePersistent(pool); pm.makePersistent(trigger); commited = commitTransaction(); } finally { rollbackAndCleanup(commited, (Query)null); } }
@Override public boolean addToken(String tokenId, String delegationToken) { LOG.debug("Begin executing addToken"); boolean committed = false; MDelegationToken token; try{ openTransaction(); token = getTokenFrom(tokenId); if (token == null) { // add Token, only if it already doesn't exist pm.makePersistent(new MDelegationToken(tokenId, delegationToken)); } committed = commitTransaction(); } finally { if(!committed) { rollbackTransaction(); } } LOG.debug("Done executing addToken with status : {}", committed); return committed && (token == null); }
@Override public void createCatalog(Catalog cat) throws MetaException { LOG.debug("Creating catalog " + cat.getName()); boolean committed = false; MCatalog mCat = catToMCat(cat); try { openTransaction(); pm.makePersistent(mCat); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }
@Override public void createFunction(Function func) throws InvalidObjectException, MetaException { boolean committed = false; try { openTransaction(); MFunction mfunc = convertToMFunction(func); pm.makePersistent(mfunc); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }
@Override public boolean createType(Type type) { boolean success = false; MType mtype = getMType(type); boolean commited = false; try { openTransaction(); pm.makePersistent(mtype); commited = commitTransaction(); success = true; } finally { if (!commited) { rollbackTransaction(); } } return success; }
@Override public int addMasterKey(String key) throws MetaException{ LOG.debug("Begin executing addMasterKey"); boolean committed = false; MMasterKey masterKey = new MMasterKey(key); try{ openTransaction(); pm.makePersistent(masterKey); committed = commitTransaction(); } finally { if(!committed) { rollbackTransaction(); } } LOG.debug("Done executing addMasterKey with status : {}", committed); if (committed) { return ((IntIdentity)pm.getObjectId(masterKey)).getKey(); } else { throw new MetaException("Failed to add master key."); } }
@Override public boolean addRole(String roleName, String ownerName) throws InvalidObjectException, MetaException, NoSuchObjectException { boolean success = false; boolean commited = false; try { openTransaction(); MRole nameCheck = this.getMRole(roleName); if (nameCheck != null) { throw new InvalidObjectException("Role " + roleName + " already exists."); } int now = (int)(System.currentTimeMillis()/1000); MRole mRole = new MRole(roleName, now, ownerName); pm.makePersistent(mRole); commited = commitTransaction(); success = true; } finally { if (!commited) { rollbackTransaction(); } } return success; }
@Override public Table markPartitionForEvent(String catName, String dbName, String tblName, Map<String,String> partName, PartitionEventType evtType) throws MetaException, UnknownTableException, InvalidPartitionException, UnknownPartitionException { LOG.debug("Begin executing markPartitionForEvent"); boolean success = false; Table tbl = null; try{ openTransaction(); tbl = getTable(catName, dbName, tblName, null); // Make sure dbName and tblName are valid. if(null == tbl) { throw new UnknownTableException("Table: "+ tblName + " is not found."); } pm.makePersistent(new MPartitionEvent(catName, dbName,tblName,getPartitionStr(tbl, partName), evtType.getValue())); success = commitTransaction(); LOG.debug("Done executing markPartitionForEvent"); } finally { if(!success) { rollbackTransaction(); } } return tbl; }
@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 void writeMTableColumnStatistics(Table table, MTableColumnStatistics mStatsObj, MTableColumnStatistics oldStats) throws NoSuchObjectException, MetaException, InvalidObjectException, InvalidInputException { String colName = mStatsObj.getColName(); QueryWrapper queryWrapper = new QueryWrapper(); try { LOG.info("Updating table level column statistics for table={}" + " colName={}", Warehouse.getCatalogQualifiedTableName(table), colName); validateTableCols(table, Lists.newArrayList(colName)); if (oldStats != null) { StatObjectConverter.setFieldsIntoOldStats(mStatsObj, oldStats); } else { if (sqlGenerator.getDbProduct().equals(DatabaseProduct.POSTGRES) && mStatsObj.getBitVector() == null) { // workaround for DN bug in persisting nulls in pg bytea column // instead set empty bit vector with header. mStatsObj.setBitVector(new byte[] {'H','L'}); } pm.makePersistent(mStatsObj); } } finally { queryWrapper.close(); } }
@Override public void addSerde(SerDeInfo serde) throws AlreadyExistsException, MetaException { boolean committed = false; try { openTransaction(); if (getMSerDeInfo(serde.getName()) != null) { throw new AlreadyExistsException("Serde with name " + serde.getName() + " already exists"); } MSerDeInfo mSerde = convertToMSerDeInfo(serde); pm.makePersistent(mSerde); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }
@Override public void createISchema(ISchema schema) throws AlreadyExistsException, MetaException, NoSuchObjectException { boolean committed = false; MISchema mSchema = convertToMISchema(schema); try { openTransaction(); if (getMISchema(schema.getCatName(), schema.getDbName(), schema.getName()) != null) { throw new AlreadyExistsException("Schema with name " + schema.getDbName() + "." + schema.getName() + " already exists"); } pm.makePersistent(mSchema); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }
@Override public void createWMTrigger(WMTrigger trigger) throws AlreadyExistsException, NoSuchObjectException, InvalidOperationException, MetaException { boolean commited = false; try { openTransaction(); MWMResourcePlan resourcePlan = getMWMResourcePlan( trigger.getResourcePlanName(), trigger.getNs(), true); MWMTrigger mTrigger = new MWMTrigger(resourcePlan, normalizeIdentifier(trigger.getTriggerName()), trigger.getTriggerExpression(), trigger.getActionExpression(), null, trigger.isSetIsInUnmanaged() && trigger.isIsInUnmanaged()); pm.makePersistent(mTrigger); commited = commitTransaction(); } catch (Exception e) { checkForConstraintException(e, "Trigger already exists, use alter: "); throw e; } finally { rollbackAndCleanup(commited, (Query)null); } }
@Override public void createDatabase(Database db) throws InvalidObjectException, MetaException { boolean commited = false; MDatabase mdb = new MDatabase(); assert db.getCatalogName() != null; mdb.setCatalogName(normalizeIdentifier(db.getCatalogName())); assert mdb.getCatalogName() != null; mdb.setName(db.getName().toLowerCase()); mdb.setLocationUri(db.getLocationUri()); mdb.setDescription(db.getDescription()); mdb.setParameters(db.getParameters()); mdb.setOwnerName(db.getOwnerName()); PrincipalType ownerType = db.getOwnerType(); mdb.setOwnerType((null == ownerType ? PrincipalType.USER.name() : ownerType.name())); mdb.setCreateTime(db.getCreateTime()); try { openTransaction(); pm.makePersistent(mdb); commited = commitTransaction(); } finally { if (!commited) { rollbackTransaction(); } } }
@Override public void createPool(WMPool pool) throws AlreadyExistsException, NoSuchObjectException, InvalidOperationException, MetaException { boolean commited = false; try { openTransaction(); MWMResourcePlan resourcePlan = getMWMResourcePlan( pool.getResourcePlanName(), pool.getNs(), true); if (!poolParentExists(resourcePlan, pool.getPoolPath())) { throw new NoSuchObjectException("Pool path is invalid, the parent does not exist"); } String policy = pool.getSchedulingPolicy(); if (!MetaStoreUtils.isValidSchedulingPolicy(policy)) { throw new InvalidOperationException("Invalid scheduling policy " + policy); } MWMPool mPool = new MWMPool(resourcePlan, pool.getPoolPath(), pool.getAllocFraction(), pool.getQueryParallelism(), policy); pm.makePersistent(mPool); commited = commitTransaction(); } catch (Exception e) { checkForConstraintException(e, "Pool already exists: "); throw e; } finally { rollbackAndCleanup(commited, (Query)null); } }
@Override public void addSchemaVersion(SchemaVersion schemaVersion) throws AlreadyExistsException, NoSuchObjectException, MetaException { boolean committed = false; MSchemaVersion mSchemaVersion = convertToMSchemaVersion(schemaVersion); try { openTransaction(); // Make sure it doesn't already exist if (getMSchemaVersion(schemaVersion.getSchema().getCatName(), schemaVersion.getSchema().getDbName(), schemaVersion.getSchema().getSchemaName(), schemaVersion.getVersion()) != null) { throw new AlreadyExistsException("Schema name " + schemaVersion.getSchema() + " version " + schemaVersion.getVersion() + " already exists"); } // Make sure the referenced Schema exists if (getMISchema(schemaVersion.getSchema().getCatName(), schemaVersion.getSchema().getDbName(), schemaVersion.getSchema().getSchemaName()) == null) { throw new NoSuchObjectException("Schema " + schemaVersion.getSchema() + " does not exist"); } pm.makePersistent(mSchemaVersion); committed = commitTransaction(); } finally { if (!committed) { rollbackTransaction(); } } }