@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."); } }
/** * Finds the entity with the JDO persistence manager. * @return the entity */ @Override public Object fetchEntity(Object entity, String[] fetch) { Object id = pm.getObjectId(entity); if (id == null) return null; return pm.getObjectById(id); }
public Object getObjectId(Object pc) { return getPM().getObjectId(pc); }
private String newIdentifierFor(final Object pojo, final Type type) { return type == Type.TRANSIENT ? UUID.randomUUID().toString() : JdoObjectIdSerializer.toOidIdentifier(getPersistenceManager().getObjectId(pojo)); }
public ObjectAdapter mapPersistent(final Persistable pojo) { if (persistenceManager.getObjectId(pojo) == null) { return null; } final RootOid oid = createPersistentOrViewModelOid(pojo); final ObjectAdapter adapter = mapRecreatedPojo(oid, pojo); return adapter; }
Boolean findOrphanedPrivilegesCore(PersistenceManager pm) { //Perform a SQL query to get things that look like orphans List<MSentryPrivilege> results = getAllMSentryPrivilegesCore(pm); List<Object> idList = new ArrayList<>(results.size()); for (MSentryPrivilege orphan : results) { idList.add(pm.getObjectId(orphan)); } if (idList.isEmpty()) { return false; } //For each potential orphan, verify it's really a orphan. // Moment an orphan is identified return 1 indicating an orphan is found. pm.refreshAll(); // Try to ensure we really have correct objects for (Object id : idList) { MSentryPrivilege priv = (MSentryPrivilege) pm.getObjectById(id); if (priv.getRoles().isEmpty()) { return true; } } return false; }
public static Object makePersistent( PersistenceManager pm, Object object, boolean detach ) { pm.makePersistent( object ); Object id = pm.getObjectId( object ); Object persistentObject = pm.getObjectById( id ); if ( detach ) { persistentObject = pm.detachCopy( persistentObject ); } return persistentObject; }
@Override public void runWorker() { for (int i = 0; i < N; i++) { TestSuper o = new TestSuper(i, ID, new long[]{i}); pm.makePersistent(o); oids.add(pm.getObjectId(o)); n++; } } }
@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."); } }
public static void removeObject( PersistenceManager pm, Object o ) { Transaction tx = pm.currentTransaction(); try { tx.begin(); o = pm.getObjectById( pm.getObjectId( o ) ); pm.deletePersistent( o ); tx.commit(); } finally { rollbackIfActive( tx ); } }
@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."); } }
@Test public void testNewInstanceWithOidFail2() { ZooClass c1 = ZooJdoHelper.schema(pm).getClass(TestClassTiny.class); TestClassTiny t = new TestClassTiny(); pm.makePersistent(t); try { c1.newInstance((Long)pm.getObjectId(t)); fail(); } catch (IllegalArgumentException e) { //good } }
@Test public void testQuery() { PersistenceManager pm = TestTools.openPM(); pm.currentTransaction().begin(); TestClass t = new TestClass(); t.setString("Hello"); pm.makePersistent(t); Object oid = pm.getObjectId(t); pm.currentTransaction().commit(); TestTools.closePM(); ZooQuery.main(new String[]{TestTools.getDbName(), "select", "from", ZooPC.class.getName()}); String out = ZooQuery.getStringOutput(); //System.out.println("out=" + out); assertTrue(out.contains(Util.oidToString(oid))); assertTrue(out.contains("Querying database done.")); }
@Override public void runWorker() { //TODO use repeatUntilSuccess() ? for (int i = 0; i < N; i++) { TestSuper o = new TestSuper(i, ID, new long[]{i}); pm.makePersistent(o); oids.add(pm.getObjectId(o)); n++; if (n % COMMIT_INTERVAL == 0) { pm.currentTransaction().commit(); pm.currentTransaction().begin(); } } pm.currentTransaction().commit(); pm.currentTransaction().begin(); } }
@Test public void testPc2HandleWithNew() { final int I = 123; TestClassTiny t1 = new TestClassTiny(); t1.setInt(I); pm.makePersistent(t1); long oid1 = (Long) pm.getObjectId(t1); try { //handles on new/dirty Java objects are not supported ZooJdoHelper.schema(pm).getHandle(oid1); fail(); } catch (UnsupportedOperationException e) { //good } // assertEquals(t1.getInt(), hdl.getValue("_int")); // assertTrue(t1 == hdl.getJavaObject()); }
@Test public void testQueryEasy() { PersistenceManager pm = TestTools.openPM(); pm.currentTransaction().begin(); TestClass t = new TestClass(); t.setString("HelloEasy"); pm.makePersistent(t); Object oid = pm.getObjectId(t); pm.currentTransaction().commit(); TestTools.closePM(); ZooQuery.main(new String[]{TestTools.getDbName(), "select", "from", "TestClass"}); String out = ZooQuery.getStringOutput(); //System.out.println("out=" + out); assertTrue(out.contains(Util.oidToString(oid))); assertTrue(out.contains("Querying database done.")); }
/** * This is relevant for JDOOptimisticVerificationExceptions. We return the Handle in the * Exception, but for convenience, JDOHelper and PM should return the proper OID if required. */ @Test public void testHandleWithJdoObjectID() { ZooClass c1 = ZooJdoHelper.schema(pm).getClass(TestClassTiny.class); ZooHandle hdl1 = c1.newInstance(); long oid = hdl1.getOid(); assertEquals(oid, JDOHelper.getObjectId(hdl1)); assertEquals(oid, pm.getObjectId(hdl1)); try { //converting a new Handle to an object is not allowed/supported hdl1.getJavaObject(); fail(); } catch (UnsupportedOperationException e) { //good } }
private Object[] initProjectBranch() { TestTools.defineSchema(ProjectBranchPCv1.class); PersistenceManager pm = TestTools.openPM(); pm.currentTransaction().begin(); Object[] oids = new Object[10]; UserPC[] users = new UserPC[2]; users[0] = new UserPC(333+0, "userPB"+0); users[1] = new UserPC(333+1, "userPB"+1); for (int i = 0; i < oids.length; i++) { ProjectBranchPCv1 a1 = new ProjectBranchPCv1(234, "PB"+i, users[i%2]); pm.makePersistent(a1); oids[i] = pm.getObjectId(a1); } pm.currentTransaction().commit(); TestTools.closePM(); return oids; }
@Test public void testPc2Handle() { final int I = 123; //to avoid activation of t1 TestClassTiny t1 = new TestClassTiny(); t1.setInt(I); pm.makePersistent(t1); long oid1 = (Long) pm.getObjectId(t1); pm.currentTransaction().commit(); pm.currentTransaction().begin(); ZooHandle hdl = ZooJdoHelper.schema(pm).getHandle(oid1); assertEquals(I, hdl.getValue("_int")); //'I' avoids activation of t1 assertTrue(t1 == hdl.getJavaObject()); }
@Test public void testGo2Pc() { final int I = 123; //to avoid activation of t1 TestClassTiny t1 = new TestClassTiny(); t1.setInt(I); pm.makePersistent(t1); long oid1 = (Long) pm.getObjectId(t1); pm.currentTransaction().commit(); TestTools.closePM(); pm = TestTools.openPM(); pm.currentTransaction().begin(); ZooHandle hdl = ZooJdoHelper.schema(pm).getHandle(oid1); assertEquals(I, hdl.getValue("_int")); //'I' avoids activation of t1 //no load the PCI t1 = (TestClassTiny) pm.getObjectById(oid1); assertEquals(I, t1.getInt()); //activation of t1 //check identity assertNotNull(t1); assertTrue(t1 == hdl.getJavaObject()); }