Search in sources :

Example 1 with Cachable

use of org.teiid.cache.Cachable in project teiid by teiid.

the class TestSessionAwareCache method testSessionSpecfic.

@Test
public void testSessionSpecfic() {
    SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
    CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Cachable result = Mockito.mock(Cachable.class);
    id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    cache.put(id, Determinism.SESSION_DETERMINISTIC, result, null);
    // make sure that in the case of session specific; we do not call prepare
    // as session is local only call for distributed
    Mockito.verify(result, times(0)).prepare((BufferManager) anyObject());
    Object c = cache.get(id);
    Mockito.verify(result, times(0)).restore((BufferManager) anyObject());
    assertTrue(result == c);
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ParseInfo(org.teiid.query.parser.ParseInfo) Cachable(org.teiid.cache.Cachable) Test(org.junit.Test)

Example 2 with Cachable

use of org.teiid.cache.Cachable in project teiid by teiid.

the class TestSessionAwareCache method testTtl.

@Test
public void testTtl() {
    SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
    CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Cachable result = Mockito.mock(Cachable.class);
    // make sure defaults are returned
    assertNull(cache.computeTtl(id, result, null));
    assertEquals(Long.valueOf(1), cache.computeTtl(id, result, 1l));
    AccessInfo ai = new AccessInfo();
    Mockito.stub(result.getAccessInfo()).toReturn(ai);
    Table t = new Table();
    t.setProperty(DataModifiable.DATA_TTL, "2");
    ai.addAccessedObject(t);
    assertEquals(Long.valueOf(2), cache.computeTtl(id, result, null));
    Table t1 = new Table();
    Schema s = new Schema();
    t1.setParent(s);
    s.setProperty(DataModifiable.DATA_TTL, "0");
    ai.addAccessedObject(t1);
    // ensure that the min and the parent are used
    assertEquals(Long.valueOf(0), cache.computeTtl(id, result, null));
}
Also used : Table(org.teiid.metadata.Table) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) Schema(org.teiid.metadata.Schema) ParseInfo(org.teiid.query.parser.ParseInfo) Cachable(org.teiid.cache.Cachable) Test(org.junit.Test)

Example 3 with Cachable

use of org.teiid.cache.Cachable in project teiid by teiid.

the class TestSessionAwareCache method testNoScope.

@Test
public void testNoScope() {
    SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
    CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Cachable result = Mockito.mock(Cachable.class);
    Mockito.stub(result.prepare((BufferManager) anyObject())).toReturn(true);
    Mockito.stub(result.restore((BufferManager) anyObject())).toReturn(true);
    cache.put(id, Determinism.VDB_DETERMINISTIC, result, null);
    // make sure that in the case of session specific; we do not call prepare
    // as session is local only call for distributed
    Mockito.verify(result, times(1)).prepare((BufferManager) anyObject());
    id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Object c = cache.get(id);
    Mockito.verify(result, times(1)).restore((BufferManager) anyObject());
    assertTrue(result == c);
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ParseInfo(org.teiid.query.parser.ParseInfo) Cachable(org.teiid.cache.Cachable) Test(org.junit.Test)

Example 4 with Cachable

use of org.teiid.cache.Cachable in project teiid by teiid.

the class TestSessionAwareCache method testUserSpecfic.

@Test
public void testUserSpecfic() {
    SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
    CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Cachable result = Mockito.mock(Cachable.class);
    Mockito.stub(result.prepare((BufferManager) anyObject())).toReturn(true);
    Mockito.stub(result.restore((BufferManager) anyObject())).toReturn(true);
    cache.put(id, Determinism.USER_DETERMINISTIC, result, null);
    // make sure that in the case of session specific; we do not call prepare
    // as session is local only call for distributed
    Mockito.verify(result, times(1)).prepare((BufferManager) anyObject());
    id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    Object c = cache.get(id);
    Mockito.verify(result, times(1)).restore((BufferManager) anyObject());
    assertTrue(result == c);
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ParseInfo(org.teiid.query.parser.ParseInfo) Cachable(org.teiid.cache.Cachable) Test(org.junit.Test)

Example 5 with Cachable

use of org.teiid.cache.Cachable in project teiid by teiid.

the class SessionAwareCache method get.

public T get(CacheID id) {
    this.totalRequests.getAndIncrement();
    id.setSessionId(id.originalSessionId);
    T result = localCache.get(id);
    if (result == null) {
        id.setSessionId(null);
        id.setUserName(id.originalUserName);
        result = distributedCache.get(id);
        if (result == null) {
            id.setUserName(null);
            result = distributedCache.get(id);
        }
        if (result instanceof Cachable) {
            Cachable c = (Cachable) result;
            if (!c.restore(this.bufferManager)) {
                result = null;
            }
        }
    }
    if (result != null) {
        if (result instanceof Cachable) {
            Cachable c = (Cachable) result;
            AccessInfo info = c.getAccessInfo();
            if (info != null && !info.validate(type == Type.RESULTSET, modTime)) {
                // $NON-NLS-1$
                LogManager.logTrace(LogConstants.CTX_DQP, "Invalidating cache entry", id);
                if (id.getSessionId() == null) {
                    this.distributedCache.remove(id);
                } else {
                    this.localCache.remove(id);
                }
                return null;
            }
        }
        // $NON-NLS-1$
        LogManager.logTrace(LogConstants.CTX_DQP, "Cache hit for", id);
        cacheHit.getAndIncrement();
    } else {
        // $NON-NLS-1$
        LogManager.logTrace(LogConstants.CTX_DQP, "Cache miss for", id);
    }
    return result;
}
Also used : Cachable(org.teiid.cache.Cachable)

Aggregations

Cachable (org.teiid.cache.Cachable)9 Test (org.junit.Test)6 CacheID (org.teiid.dqp.internal.process.SessionAwareCache.CacheID)6 ParseInfo (org.teiid.query.parser.ParseInfo)6 AbstractMetadataRecord (org.teiid.metadata.AbstractMetadataRecord)1 Schema (org.teiid.metadata.Schema)1 Table (org.teiid.metadata.Table)1