Search in sources :

Example 1 with DefaultRuntimeProperties

use of org.apache.cayenne.configuration.DefaultRuntimeProperties in project cayenne by apache.

the class DataContextFactoryTest method testCreateDataContextWithDedicatedCache.

@Test
public void testCreateDataContextWithDedicatedCache() throws Exception {
    final EventManager eventManager = new MockEventManager();
    final DataDomain domain = new DataDomain("d1");
    domain.setSharedCacheEnabled(false);
    Module testModule = binder -> {
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(DataDomain.class).toInstance(domain);
        binder.bind(EventManager.class).toInstance(eventManager);
        binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
        binder.bind(RuntimeProperties.class).toInstance(new DefaultRuntimeProperties(Collections.<String, String>emptyMap()));
        binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
        binder.bind(ObjectStoreFactory.class).to(DefaultObjectStoreFactory.class);
        binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
        binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DataContextFactory factory = new DataContextFactory();
    injector.injectMembers(factory);
    DataContext c3 = (DataContext) factory.createContext();
    assertNotNull(c3.getObjectStore().getDataRowCache());
    assertNull(domain.getSharedSnapshotCache());
    assertNotSame(c3.getObjectStore().getDataRowCache(), domain.getSharedSnapshotCache());
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DataContext(org.apache.cayenne.access.DataContext) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) TransactionManager(org.apache.cayenne.tx.TransactionManager) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) ObjectMapRetainStrategy(org.apache.cayenne.access.ObjectMapRetainStrategy) ObjectStoreFactory(org.apache.cayenne.configuration.ObjectStoreFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataDomain(org.apache.cayenne.access.DataDomain) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Test(org.junit.Test) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Assert(org.junit.Assert) Collections(java.util.Collections) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) DataDomain(org.apache.cayenne.access.DataDomain) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DataContext(org.apache.cayenne.access.DataContext) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Module(org.apache.cayenne.di.Module) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 2 with DefaultRuntimeProperties

use of org.apache.cayenne.configuration.DefaultRuntimeProperties in project cayenne by apache.

the class DataContextSerializationIT method testSerializeWithLocalCache.

@Test
public void testSerializeWithLocalCache() throws Exception {
    createSingleArtistDataSet();
    // manually assemble a DataContext with local cache....
    DataDomain domain = context.getParentDataDomain();
    DataRowStore snapshotCache = new DataRowStore(domain.getName(), new DefaultRuntimeProperties(domain.getProperties()), domain.getEventManager());
    Map<Object, Persistent> map = new HashMap<>();
    DataContext localCacheContext = new DataContext(domain, new ObjectStore(snapshotCache, map));
    localCacheContext.setValidatingObjectsOnCommit(domain.isValidatingObjectsOnCommit());
    localCacheContext.setUsingSharedSnapshotCache(false);
    assertNotSame(domain.getSharedSnapshotCache(), localCacheContext.getObjectStore().getDataRowCache());
    DataContext deserializedContext = Util.cloneViaSerialization(localCacheContext);
    assertNotSame(localCacheContext, deserializedContext);
    assertNotSame(localCacheContext.getObjectStore(), deserializedContext.getObjectStore());
    assertSame(localCacheContext.getParentDataDomain(), deserializedContext.getParentDataDomain());
    assertNotSame(localCacheContext.getObjectStore().getDataRowCache(), deserializedContext.getObjectStore().getDataRowCache());
    assertNotSame(deserializedContext.getParentDataDomain().getSharedSnapshotCache(), deserializedContext.getObjectStore().getDataRowCache());
    Artist a = Cayenne.objectForPK(deserializedContext, Artist.class, 33001);
    assertNotNull(a);
    a.setArtistName(a.getArtistName() + "___");
    // this blows per CAY-796
    deserializedContext.commitChanges();
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) HashMap(java.util.HashMap) Persistent(org.apache.cayenne.Persistent) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Test(org.junit.Test)

Example 3 with DefaultRuntimeProperties

use of org.apache.cayenne.configuration.DefaultRuntimeProperties in project cayenne by apache.

the class DataRowStoreIT method testMaxSize.

/**
 * Tests LRU cache behavior.
 */
@Test
public void testMaxSize() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put(Constants.SNAPSHOT_CACHE_SIZE_PROPERTY, String.valueOf(2));
    cache = new DataRowStore("cacheXYZ", new DefaultRuntimeProperties(props), null);
    assertEquals(2, cache.maximumSize());
    assertEquals(0, cache.size());
    ObjectId key1 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 1);
    Map<ObjectId, DataRow> diff1 = new HashMap<>();
    diff1.put(key1, new DataRow(1));
    ObjectId key2 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 2);
    Map<ObjectId, DataRow> diff2 = new HashMap<>();
    diff2.put(key2, new DataRow(1));
    ObjectId key3 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 3);
    Map<ObjectId, DataRow> diff3 = new HashMap<>();
    diff3.put(key3, new DataRow(1));
    cache.processSnapshotChanges(this, diff1, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(1, cache.size());
    cache.processSnapshotChanges(this, diff2, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(2, cache.size());
    // this addition must overflow the cache, and throw out the first item
    cache.processSnapshotChanges(this, diff3, Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList(), Collections.<ObjectId>emptyList());
    assertEquals(2, cache.size());
    assertNotNull(cache.getCachedSnapshot(key2));
    assertNotNull(cache.getCachedSnapshot(key3));
    assertNull(cache.getCachedSnapshot(key1));
}
Also used : HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 4 with DefaultRuntimeProperties

use of org.apache.cayenne.configuration.DefaultRuntimeProperties in project cayenne by apache.

the class DataContextSharedCacheEmpiricIT method setUp.

@Before
public void setUp() throws Exception {
    eventManager = new DefaultEventManager();
    DataRowStore cache = new DataRowStore("cacheTest", new DefaultRuntimeProperties(Collections.<String, String>emptyMap()), eventManager);
    c1 = new DataContext(runtime.getDataDomain(), objectStoreFactory.createObjectStore(cache));
    c2 = new DataContext(runtime.getDataDomain(), objectStoreFactory.createObjectStore(cache));
    // prepare a single artist record
    TableHelper tArtist = new TableHelper(dbHelper, "ARTIST");
    tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
    tArtist.insert(1, "version1");
}
Also used : DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) TableHelper(org.apache.cayenne.test.jdbc.TableHelper) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Before(org.junit.Before)

Example 5 with DefaultRuntimeProperties

use of org.apache.cayenne.configuration.DefaultRuntimeProperties in project cayenne by apache.

the class DataDomainIT method testShutdownCache.

@Test
public void testShutdownCache() {
    DataDomain domain = new DataDomain("X");
    final boolean[] cacheShutdown = new boolean[1];
    DefaultEventManager eventManager = new DefaultEventManager();
    try {
        DataRowStore cache = new DataRowStore("Y", new DefaultRuntimeProperties(Collections.<String, String>emptyMap()), eventManager) {

            @Override
            public void shutdown() {
                cacheShutdown[0] = true;
            }
        };
        domain.setSharedSnapshotCache(cache);
        domain.shutdown();
    } finally {
        eventManager.shutdown();
    }
    assertTrue(cacheShutdown[0]);
}
Also used : DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Test(org.junit.Test)

Aggregations

DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)6 Test (org.junit.Test)5 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 DataContext (org.apache.cayenne.access.DataContext)2 DataDomain (org.apache.cayenne.access.DataDomain)2 DataRowStoreFactory (org.apache.cayenne.access.DataRowStoreFactory)2 DefaultDataRowStoreFactory (org.apache.cayenne.access.DefaultDataRowStoreFactory)2 DefaultObjectMapRetainStrategy (org.apache.cayenne.access.DefaultObjectMapRetainStrategy)2 ObjectMapRetainStrategy (org.apache.cayenne.access.ObjectMapRetainStrategy)2 MapQueryCache (org.apache.cayenne.cache.MapQueryCache)2 QueryCache (org.apache.cayenne.cache.QueryCache)2 DefaultObjectStoreFactory (org.apache.cayenne.configuration.DefaultObjectStoreFactory)2 ObjectStoreFactory (org.apache.cayenne.configuration.ObjectStoreFactory)2 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)2 DIBootstrap (org.apache.cayenne.di.DIBootstrap)2 Injector (org.apache.cayenne.di.Injector)2 Module (org.apache.cayenne.di.Module)2 DefaultEventManager (org.apache.cayenne.event.DefaultEventManager)2 EventBridge (org.apache.cayenne.event.EventBridge)2