Search in sources :

Example 6 with Cachable

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

the class SessionAwareCache method put.

public void put(CacheID id, Determinism determinismLevel, T t, Long ttl) {
    cachePuts.incrementAndGet();
    if (determinismLevel.compareTo(Determinism.SESSION_DETERMINISTIC) <= 0) {
        id.setSessionId(id.originalSessionId);
        // $NON-NLS-1$
        LogManager.logTrace(LogConstants.CTX_DQP, "Adding to session/local cache", id);
        ttl = computeTtl(id, t, ttl);
        if (ttl != null && ttl == 0) {
            return;
        }
        this.localCache.put(id, t, ttl);
    } else {
        boolean insert = true;
        id.setSessionId(null);
        if (determinismLevel == Determinism.USER_DETERMINISTIC) {
            id.setUserName(id.originalUserName);
        } else {
            id.setUserName(null);
        }
        if (t instanceof Cachable) {
            Cachable c = (Cachable) t;
            ttl = computeTtl(id, t, ttl);
            if (ttl != null && ttl == 0) {
                return;
            }
            insert = c.prepare(this.bufferManager);
        }
        if (insert) {
            // $NON-NLS-1$
            LogManager.logTrace(LogConstants.CTX_DQP, "Adding to global/distributed cache", id);
            this.distributedCache.put(id, t, ttl);
        }
    }
}
Also used : Cachable(org.teiid.cache.Cachable)

Example 7 with Cachable

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

the class SessionAwareCache method computeTtl.

Long computeTtl(CacheID id, T t, Long ttl) {
    if (!(t instanceof Cachable) || type != Type.RESULTSET) {
        return ttl;
    }
    Cachable c = (Cachable) t;
    AccessInfo info = c.getAccessInfo();
    if (info == null) {
        return ttl;
    }
    Set<Object> objects = info.getObjectsAccessed();
    if (objects == null) {
        return ttl;
    }
    long computedTtl = Long.MAX_VALUE;
    for (Object o : objects) {
        if (!(o instanceof AbstractMetadataRecord)) {
            continue;
        }
        AbstractMetadataRecord amr = (AbstractMetadataRecord) o;
        Long l = getDataTtl(amr);
        if (l == null || l < 0) {
            continue;
        }
        if (l == 0) {
            if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                LogManager.logDetail(LogConstants.CTX_DQP, "Not adding cache entry", id, "since", amr.getFullName(), "has caching disabled");
            }
            return Long.valueOf(0);
        }
        computedTtl = Math.min(l, computedTtl);
    }
    if (ttl != null) {
        ttl = Math.min(ttl, computedTtl);
    } else if (computedTtl != Long.MAX_VALUE) {
        ttl = computedTtl;
    }
    return ttl;
}
Also used : AbstractMetadataRecord(org.teiid.metadata.AbstractMetadataRecord) Cachable(org.teiid.cache.Cachable)

Example 8 with Cachable

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

the class TestSessionAwareCache method testVDBRemoval.

@Test
public void testVDBRemoval() {
    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);
    id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    cache.put(id, Determinism.VDB_DETERMINISTIC, result, null);
    Object c = cache.get(id);
    assertTrue(result == c);
    cache.clearForVDB("vdb-name", "1");
    assertNull(cache.get(id));
}
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 9 with Cachable

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

the class TestSessionAwareCache method testRemove.

@Test
public void testRemove() {
    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);
    id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
    cache.put(id, Determinism.VDB_DETERMINISTIC, result, null);
    Object c = cache.get(id);
    assertTrue(result == c);
    assertTrue(cache.remove(id, Determinism.VDB_DETERMINISTIC) != null);
    assertNull(cache.get(id));
    // session scope
    cache.put(id, Determinism.SESSION_DETERMINISTIC, result, null);
    assertTrue(cache.get(id) != null);
    assertTrue(cache.remove(id, Determinism.SESSION_DETERMINISTIC) != null);
    assertNull(cache.get(id));
}
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)

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