use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testL2CacheAfterReadApplicationIdentity.
/**
* Test for storage of an object in the L2 cache from a query or from getObjectById.
*/
public void testL2CacheAfterReadApplicationIdentity() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
try {
// Create some data we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.pinAll(true, Vote.class);
tx.begin();
Vote vote1 = new Vote(1, "Vote 1");
pm.makePersistent(vote1);
Vote vote2 = new Vote(2, "Vote 2");
pm.makePersistent(vote2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run optimistic L2 test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
l2Cache.evictAll();
assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
// Try getObjectById
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Vote vote1 = pm.getObjectById(Vote.class, 1);
Vote vote2 = pm.getObjectById(Vote.class, 2);
assertNotNull(vote1);
assertNotNull(vote2);
assertEquals("Incorrect number of pinned objects after getObjectById", 2, l2Cache.getNumberOfPinnedObjects());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error updating object that was retrieved from the L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
l2Cache.evictAll();
assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
// Try Query
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery(Vote.class);
Collection votes = (Collection) query.execute();
Iterator iter = votes.iterator();
while (iter.hasNext()) {
iter.next();
}
assertEquals("Incorrect number of pinned objects after Query", 2, l2Cache.getNumberOfPinnedObjects());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error retrieving object from the L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(cachePMF, Vote.class);
cachePMF.close();
}
}
use of javax.jdo.datastore.DataStoreCache in project hive by apache.
the class ObjectStore method getPMF.
private static synchronized PersistenceManagerFactory getPMF() {
if (pmf == null) {
Configuration conf = MetastoreConf.newMetastoreConf();
DataSourceProvider dsp = DataSourceProviderFactory.getDataSourceProvider(conf);
if (dsp == null) {
pmf = JDOHelper.getPersistenceManagerFactory(prop);
} else {
try {
DataSource ds = dsp.create(conf);
Map<Object, Object> dsProperties = new HashMap<>();
// Any preexisting datanucleus property should be passed along
dsProperties.putAll(prop);
dsProperties.put("datanucleus.ConnectionFactory", ds);
dsProperties.put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
pmf = JDOHelper.getPersistenceManagerFactory(dsProperties);
} catch (SQLException e) {
LOG.warn("Could not create PersistenceManagerFactory using " + "connection pool properties, will fall back", e);
pmf = JDOHelper.getPersistenceManagerFactory(prop);
}
}
DataStoreCache dsc = pmf.getDataStoreCache();
if (dsc != null) {
String objTypes = MetastoreConf.getVar(conf, ConfVars.CACHE_PINOBJTYPES);
LOG.info("Setting MetaStore object pin classes with hive.metastore.cache.pinobjtypes=\"{}\"", objTypes);
if (org.apache.commons.lang.StringUtils.isNotEmpty(objTypes)) {
String[] typeTokens = objTypes.toLowerCase().split(",");
for (String type : typeTokens) {
type = type.trim();
if (PINCLASSMAP.containsKey(type)) {
dsc.pinAll(true, PINCLASSMAP.get(type));
} else {
LOG.warn("{} is not one of the pinnable object types: {}", type, org.apache.commons.lang.StringUtils.join(PINCLASSMAP.keySet(), " "));
}
}
}
} else {
LOG.warn("PersistenceManagerFactory returned null DataStoreCache object. Unable to initialize object pin types defined by hive.metastore.cache.pinobjtypes");
}
}
return pmf;
}
use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class EmbeddedMixedTest method testNestedEmbeddedPCObjectDetachAttach.
/**
* Test for detaching/attaching an nested embedded PC object.
* @throws Exception
*/
public void testNestedEmbeddedPCObjectDetachAttach() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Persist some objects
Object cameraId = null;
try {
tx.begin();
Memory memory = new Memory(Memory.COMPACT_FLASH, 64, 3.3);
memory.setChip(new Chip(12));
DigitalCamera camera = new DigitalCamera("Canon", "Powerzoom A40", memory);
pm.makePersistent(camera);
tx.commit();
cameraId = pm.getObjectId(camera);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object(s) and detach them
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup(FetchPlan.ALL);
pm.getFetchPlan().setMaxFetchDepth(2);
DigitalCamera detachedCamera = null;
try {
tx.begin();
// Retrieve the object with both embedded subobjects
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
assertTrue("Unable to retrieve object with embedded object(s)", camera != null);
assertTrue("Retrieved object with embedded object(s) has incorrect make field", camera.getMake().equals("Canon"));
assertTrue("Retrieved object with embedded object(s) has no memory", camera.getMemory() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", camera.getMemory().getType() == Memory.COMPACT_FLASH);
assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
assertEquals("Retrieved chip has wrong thickness", 12, camera.getMemory().getChip().getThickness());
detachedCamera = (DigitalCamera) pm.detachCopy(camera);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while retrieving/detaching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the detached object(s) and check them
assertNotNull("Detached Computer is null!", detachedCamera);
assertTrue("Unable to retrieve object with embedded object(s)", detachedCamera != null);
assertTrue("Retrieved object with embedded object(s) has incorrect make field", detachedCamera.getMake().equals("Canon"));
assertTrue("Retrieved object with embedded object(s) has no memory", detachedCamera.getMemory() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", detachedCamera.getMemory().getType() == Memory.COMPACT_FLASH);
assertTrue("Retrieved object with embedded object(s) has no chip", detachedCamera.getMemory().getChip() != null);
assertEquals("Retrieved chip has wrong thickness", 12, detachedCamera.getMemory().getChip().getThickness());
// Update some objects
detachedCamera.getMemory().getChip().setThickness(15);
// Attach the objects
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup(FetchPlan.ALL);
pm.getFetchPlan().setMaxFetchDepth(2);
try {
tx.begin();
DigitalCamera camera = (DigitalCamera) pm.makePersistent(detachedCamera);
assertTrue("Unable to retrieve object with embedded object(s)", camera != null);
assertTrue("Retrieved object with embedded object(s) has incorrect make field", camera.getMake().equals("Canon"));
assertTrue("Retrieved object with embedded object(s) has no memory", camera.getMemory() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", camera.getMemory().getType() == Memory.COMPACT_FLASH);
assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while attaching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object(s) and re-check updated objects
DataStoreCache cache = pmf.getDataStoreCache();
cache.evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup(FetchPlan.ALL);
pm.getFetchPlan().setMaxFetchDepth(2);
try {
tx.begin();
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
assertTrue("Unable to retrieve object with embedded object(s)", camera != null);
assertTrue("Retrieved object with embedded object(s) has incorrect make field", camera.getMake().equals("Canon"));
assertTrue("Retrieved object with embedded object(s) has no memory", camera.getMemory() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", camera.getMemory().getType() == Memory.COMPACT_FLASH);
assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while retrieving/detaching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(DigitalCamera.class);
}
}
use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testOptimisticTransactionWithL2Cache.
/**
* Test use of optimistic txns and L2 cache. Tests that we store and retrieve the version correctly
* when using L2 cached objects
*/
public void testOptimisticTransactionWithL2Cache() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
// All Trade1 get pinned
l2Cache.pinAll(true, Trade1.class);
// Create some data we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
Object version = null;
try {
tx.begin();
Trade1 trade = new Trade1("Woody Woodpecker", 500.0, new java.util.Date());
pm.makePersistent(trade);
// trade will be pinned since we have all Trade1 objects being pinned
tx.commit();
id = JDOHelper.getObjectId(trade);
version = JDOHelper.getVersion(trade);
assertNotNull("Version of persisted object is null!!", JDOHelper.getVersion(trade));
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run optimistic L2 test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Start new PM so we know "woody" isnt present in the L1 cache and has to get from L2
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Trade1 trade = (Trade1) pm.getObjectById(id);
assertNotNull("Version of retrieved L2 cached object is null!!", JDOHelper.getVersion(trade));
assertEquals("Versions of original/retrieved objects are different so wasn't L2 cached correctly", version, JDOHelper.getVersion(trade));
trade.setPerson("Donald Duck");
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error updating object that was retrieved from the L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(cachePMF, Trade1.class);
cachePMF.close();
;
}
}
use of javax.jdo.datastore.DataStoreCache in project tests by datanucleus.
the class CacheTest method testL2CachedObject.
/**
* Test for storage of an object in the L2 cache, and whether it has its fields populated.
*/
public void testL2CachedObject() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
try {
// Create some data we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object woodyId = null;
Object woodlessId = null;
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
// All Employees/Managers get pinned
l2Cache.pinAll(true, Employee.class);
tx.begin();
final Employee woody = new Employee(1, "Woody", null, "woody@woodpecker.com", 13, "serial 1", new Integer(10));
final Employee woodless = new Employee(2, "Woodless", "Woodpecker", "woodless@woodpecker.com", 14, "serial 2", new Integer(11));
Manager bart = new Manager(3, "Bart", "Simpson", "bart@simpson.com", 3, "serial 3");
woody.setManager(bart);
pm.makePersistent(woody);
woody.setLastName("Woodpecker");
pm.makePersistent(woodless);
woodyId = pm.getObjectId(woody);
woodlessId = pm.getObjectId(woodless);
// Woody, Woodless, and Bart will all be pinned since we have all Employee objects being pinned
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run multithread test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
assertEquals("Incorrect number of pinned objects : should have been 3 but is " + l2Cache.getNumberOfPinnedObjects(), 3, l2Cache.getNumberOfPinnedObjects());
assertTrue("Level 2 Cache returned that it is empty yet should have pinned object(s)!", !l2Cache.isEmpty());
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = (Employee) pm.getObjectById(woodyId, false);
assertTrue("Object retrieved from L2 cache was null", woody != null);
assertTrue("Field of object retrieved from L2 cache was null!", woody.getLastName() != null);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error encountered accessing object from L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
l2Cache.evict(woodlessId);
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet should have 2", 2, l2Cache.getNumberOfPinnedObjects());
// Clear the cache and check if the objects are released
l2Cache.evictAll();
assertTrue("Level 2 Cache returned that it is not empty yet we just cleared it!", l2Cache.isEmpty());
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet we just cleared it!", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", 0, l2Cache.getNumberOfUnpinnedObjects());
} finally {
clearEmployeeData(cachePMF);
cachePMF.close();
}
}
Aggregations