Search in sources :

Example 1 with DomainDataRegion

use of org.hibernate.cache.spi.DomainDataRegion in project hibernate-orm by hibernate.

the class BasicStructuredCachingOfConvertedValueTest method basicCacheStructureTest.

@Test
@TestForIssue(jiraKey = "HHH-9615")
@SuppressWarnings("unchecked")
public void basicCacheStructureTest() {
    EntityPersister persister = sessionFactory().getMetamodel().entityPersisters().get(Address.class.getName());
    DomainDataRegion region = persister.getCacheAccessStrategy().getRegion();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // test during store...
    PostalAreaConverter.clearCounts();
    Session session = openSession();
    session.getTransaction().begin();
    session.save(new Address(1, "123 Main St.", null, PostalArea._78729));
    session.getTransaction().commit();
    session.close();
    {
        inSession(s -> {
            final EntityDataAccess entityDataAccess = region.getEntityDataAccess(persister.getNavigableRole());
            final Object cacheKey = entityDataAccess.generateCacheKey(1, persister, sessionFactory(), null);
            final Object cachedItem = entityDataAccess.get(s, cacheKey);
            final Map<String, ?> state = (Map) cachedItem;
            // this is the point of the Jira.. that this "should be" the converted value
            assertThat(state.get("postalArea"), instanceOf(PostalArea.class));
        });
    }
    assertThat(PostalAreaConverter.toDatabaseCallCount, is(1));
    assertThat(PostalAreaConverter.toDomainCallCount, is(0));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // test during load...
    PostalAreaConverter.clearCounts();
    sessionFactory().getCache().evictAll();
    session = openSession();
    session.getTransaction().begin();
    Address address = session.get(Address.class, 1);
    session.getTransaction().commit();
    session.close();
    {
        inSession(s -> {
            final EntityDataAccess entityDataAccess = region.getEntityDataAccess(persister.getNavigableRole());
            final Object cacheKey = entityDataAccess.generateCacheKey(1, persister, sessionFactory(), null);
            final Object cachedItem = entityDataAccess.get(s, cacheKey);
            final Map<String, ?> state = (Map) cachedItem;
            // this is the point of the Jira.. that this "should be" the converted value
            assertThat(state.get("postalArea"), instanceOf(PostalArea.class));
        });
    }
    assertThat(PostalAreaConverter.toDatabaseCallCount, is(0));
    assertThat(PostalAreaConverter.toDomainCallCount, is(1));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // cleanup
    session = openSession();
    session.getTransaction().begin();
    session.delete(address);
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CoreMatchers.is(org.hamcrest.CoreMatchers.is) EntityPersister(org.hibernate.persister.entity.EntityPersister) BaseNonConfigCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase) AvailableSettings(org.hibernate.cfg.AvailableSettings) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) Session(org.hibernate.Session) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) Test(org.junit.Test) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) TestForIssue(org.hibernate.testing.TestForIssue) Map(java.util.Map) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) Map(java.util.Map) Session(org.hibernate.Session) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with DomainDataRegion

use of org.hibernate.cache.spi.DomainDataRegion in project hibernate-orm by hibernate.

the class EnabledCaching method prime.

@Override
public void prime(Set<DomainDataRegionConfig> cacheRegionConfigs) {
    for (DomainDataRegionConfig regionConfig : cacheRegionConfigs) {
        final DomainDataRegion region = getRegionFactory().buildDomainDataRegion(regionConfig, this);
        regionsByName.put(region.getName(), region);
        if (!StringTypeDescriptor.INSTANCE.areEqual(region.getName(), regionConfig.getRegionName())) {
            throw new HibernateException(String.format(Locale.ROOT, "Region [%s] returned from RegionFactory [%s] was named differently than requested name.  Expecting `%s`, but found `%s`", region, getRegionFactory().getClass().getName(), regionConfig.getRegionName(), region.getName()));
        }
        for (EntityDataCachingConfig entityAccessConfig : regionConfig.getEntityCaching()) {
            final EntityDataAccess entityDataAccess = entityAccessMap.put(entityAccessConfig.getNavigableRole(), region.getEntityDataAccess(entityAccessConfig.getNavigableRole()));
            legacySecondLevelCacheNames.add(StringHelper.qualifyConditionally(getSessionFactory().getSessionFactoryOptions().getCacheRegionPrefix(), region.getName()));
        }
        if (regionConfig.getNaturalIdCaching().isEmpty()) {
            legacyNaturalIdAccessesForRegion.put(region.getName(), Collections.emptySet());
        } else {
            final HashSet<NaturalIdDataAccess> accesses = new HashSet<>();
            for (NaturalIdDataCachingConfig naturalIdAccessConfig : regionConfig.getNaturalIdCaching()) {
                final NaturalIdDataAccess naturalIdDataAccess = naturalIdAccessMap.put(naturalIdAccessConfig.getNavigableRole(), region.getNaturalIdDataAccess(naturalIdAccessConfig.getNavigableRole()));
                accesses.add(naturalIdDataAccess);
            }
            legacyNaturalIdAccessesForRegion.put(region.getName(), accesses);
        }
        for (CollectionDataCachingConfig collectionAccessConfig : regionConfig.getCollectionCaching()) {
            final CollectionDataAccess collectionDataAccess = collectionAccessMap.put(collectionAccessConfig.getNavigableRole(), region.getCollectionDataAccess(collectionAccessConfig.getNavigableRole()));
            legacySecondLevelCacheNames.add(StringHelper.qualifyConditionally(getSessionFactory().getSessionFactoryOptions().getCacheRegionPrefix(), region.getName()));
        }
    }
}
Also used : NaturalIdDataCachingConfig(org.hibernate.cache.cfg.spi.NaturalIdDataCachingConfig) EntityDataCachingConfig(org.hibernate.cache.cfg.spi.EntityDataCachingConfig) HibernateException(org.hibernate.HibernateException) DomainDataRegionConfig(org.hibernate.cache.cfg.spi.DomainDataRegionConfig) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess) CollectionDataCachingConfig(org.hibernate.cache.cfg.spi.CollectionDataCachingConfig) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with DomainDataRegion

use of org.hibernate.cache.spi.DomainDataRegion in project hibernate-orm by hibernate.

the class DeprecatedNaturalIdCacheStatisticsImpl method getElementCountOnDisk.

@Override
public long getElementCountOnDisk() {
    long count = 0;
    HashSet<Region> processedRegions = null;
    for (NaturalIdDataAccess accessStrategy : accessStrategies) {
        final DomainDataRegion region = accessStrategy.getRegion();
        if (ExtendedStatisticsSupport.class.isInstance(region)) {
        }
        if (region instanceof ExtendedStatisticsSupport) {
            if (processedRegions == null) {
                processedRegions = new HashSet<>();
            }
            if (processedRegions.add(region)) {
                count += ((ExtendedStatisticsSupport) region).getElementCountOnDisk();
            }
        }
    }
    if (count == 0) {
        return NO_EXTENDED_STAT_SUPPORT_RETURN;
    }
    return count;
}
Also used : ExtendedStatisticsSupport(org.hibernate.cache.spi.ExtendedStatisticsSupport) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) Region(org.hibernate.cache.spi.Region) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion)

Example 4 with DomainDataRegion

use of org.hibernate.cache.spi.DomainDataRegion in project hibernate-orm by hibernate.

the class DeprecatedNaturalIdCacheStatisticsImpl method getSizeInMemory.

@Override
public long getSizeInMemory() {
    long count = 0;
    HashSet<Region> processedRegions = null;
    for (NaturalIdDataAccess accessStrategy : accessStrategies) {
        final DomainDataRegion region = accessStrategy.getRegion();
        if (ExtendedStatisticsSupport.class.isInstance(region)) {
        }
        if (region instanceof ExtendedStatisticsSupport) {
            if (processedRegions == null) {
                processedRegions = new HashSet<>();
            }
            if (processedRegions.add(region)) {
                count += ((ExtendedStatisticsSupport) region).getElementCountOnDisk();
            }
        }
    }
    if (count == 0) {
        return NO_EXTENDED_STAT_SUPPORT_RETURN;
    }
    return count;
}
Also used : ExtendedStatisticsSupport(org.hibernate.cache.spi.ExtendedStatisticsSupport) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) Region(org.hibernate.cache.spi.Region) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion)

Example 5 with DomainDataRegion

use of org.hibernate.cache.spi.DomainDataRegion in project hibernate-orm by hibernate.

the class DeprecatedNaturalIdCacheStatisticsImpl method getElementCountInMemory.

@Override
public long getElementCountInMemory() {
    long count = 0;
    HashSet<Region> processedRegions = null;
    for (NaturalIdDataAccess accessStrategy : accessStrategies) {
        final DomainDataRegion region = accessStrategy.getRegion();
        if (ExtendedStatisticsSupport.class.isInstance(region)) {
        }
        if (region instanceof ExtendedStatisticsSupport) {
            if (processedRegions == null) {
                processedRegions = new HashSet<>();
            }
            if (processedRegions.add(region)) {
                count += ((ExtendedStatisticsSupport) region).getElementCountInMemory();
            }
        }
    }
    if (count == 0) {
        return NO_EXTENDED_STAT_SUPPORT_RETURN;
    }
    return count;
}
Also used : ExtendedStatisticsSupport(org.hibernate.cache.spi.ExtendedStatisticsSupport) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion) Region(org.hibernate.cache.spi.Region) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) DomainDataRegion(org.hibernate.cache.spi.DomainDataRegion)

Aggregations

DomainDataRegion (org.hibernate.cache.spi.DomainDataRegion)6 NaturalIdDataAccess (org.hibernate.cache.spi.access.NaturalIdDataAccess)4 ExtendedStatisticsSupport (org.hibernate.cache.spi.ExtendedStatisticsSupport)3 Region (org.hibernate.cache.spi.Region)3 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)3 Map (java.util.Map)2 CoreMatchers.instanceOf (org.hamcrest.CoreMatchers.instanceOf)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Session (org.hibernate.Session)2 AvailableSettings (org.hibernate.cfg.AvailableSettings)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 TestForIssue (org.hibernate.testing.TestForIssue)2 CachingRegionFactory (org.hibernate.testing.cache.CachingRegionFactory)2 BaseNonConfigCoreFunctionalTestCase (org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 HibernateException (org.hibernate.HibernateException)1 CollectionDataCachingConfig (org.hibernate.cache.cfg.spi.CollectionDataCachingConfig)1