Search in sources :

Example 11 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class ConcurrentCache method insertIntoFreeSlot.

/**
 * Insert a {@code CacheEntry} into a free slot in the {@code
 * ReplacementPolicy}'s internal data structure, and return a {@code
 * Cacheable} that the caller can reuse. The entry must have been locked
 * before this method is called.
 *
 * @param key the identity of the object being inserted
 * @param entry the entry that is being inserted
 * @return a {@code Cacheable} object that the caller can reuse
 * @throws StandardException if an error occurs while inserting the entry
 * or while allocating a new {@code Cacheable}
 */
private Cacheable insertIntoFreeSlot(Object key, CacheEntry entry) throws StandardException {
    try {
        replacementPolicy.insertEntry(entry);
    } catch (StandardException se) {
        // Failed to insert the entry into the replacement policy. Make
        // sure that it's also removed from the hash table.
        removeEntry(key);
        throw se;
    }
    Cacheable free = entry.getCacheable();
    if (free == null) {
        // We didn't get a reusable cacheable. Create a new one.
        free = holderFactory.newCacheable(this);
    }
    entry.keep(true);
    return free;
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) Cacheable(org.apache.derby.iapi.services.cache.Cacheable)

Example 12 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class ConcurrentCache method cleanEntry.

/**
 * Clean an entry in the cache.
 *
 * @param entry the entry to clean
 * @exception StandardException if an error occurs while cleaning
 */
void cleanEntry(CacheEntry entry) throws StandardException {
    // Fetch the cacheable while having exclusive access to the entry.
    // Release the lock before cleaning to avoid blocking others.
    Cacheable item;
    entry.lock();
    try {
        item = entry.getCacheable();
        if (item == null) {
            // nothing to do
            return;
        }
        entry.keep(false);
    } finally {
        entry.unlock();
    }
    cleanAndUnkeepEntry(entry, item);
}
Also used : Cacheable(org.apache.derby.iapi.services.cache.Cacheable)

Example 13 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class ConcurrentCache method cleanCache.

/**
 * Clean all dirty objects matching a partial key. If no key is specified,
 * clean all dirty objects in the cache.
 *
 * @param partialKey the partial (or exact) key to match, or
 * <code>null</code> to match all keys
 */
private void cleanCache(Matchable partialKey) throws StandardException {
    for (CacheEntry entry : cache.values()) {
        final Cacheable dirtyObject;
        entry.lock();
        try {
            if (!entry.isValid()) {
                // no need to clean an invalid entry
                continue;
            }
            Cacheable c = entry.getCacheable();
            if (partialKey != null && !partialKey.match(c.getIdentity())) {
                // don't clean objects that don't match the partial key
                continue;
            }
            if (!c.isDirty()) {
                // already clean
                continue;
            }
            // Increment the keep count for this entry to prevent others
            // from removing it. Then release the lock on the entry to
            // avoid blocking others when the object is cleaned.
            entry.keep(false);
            dirtyObject = c;
        } finally {
            entry.unlock();
        }
        // Clean the object and decrement the keep count.
        cleanAndUnkeepEntry(entry, dirtyObject);
    }
}
Also used : Cacheable(org.apache.derby.iapi.services.cache.Cacheable)

Example 14 with Cacheable

use of org.apache.derby.iapi.services.cache.Cacheable in project derby by apache.

the class ConcurrentCache method removeEntry.

/**
 * Remove an entry from the cache. Its <code>Cacheable</code> is cleared
 * and made available for other entries. This method should only be called
 * if the entry is present in the cache and locked by the current thread.
 *
 * @param key the identity of the entry to remove
 */
private void removeEntry(Object key) {
    CacheEntry entry = cache.remove(key);
    Cacheable c = entry.getCacheable();
    if (c != null && c.getIdentity() != null) {
        // The cacheable should not have an identity when it has been
        // removed.
        c.clearIdentity();
    }
    entry.free();
}
Also used : Cacheable(org.apache.derby.iapi.services.cache.Cacheable)

Aggregations

Cacheable (org.apache.derby.iapi.services.cache.Cacheable)14 CacheManager (org.apache.derby.iapi.services.cache.CacheManager)2 PrivilegedActionException (java.security.PrivilegedActionException)1 GeneratedClass (org.apache.derby.iapi.services.loader.GeneratedClass)1 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)1 GenericPreparedStatement (org.apache.derby.impl.sql.GenericPreparedStatement)1 StandardException (org.apache.derby.shared.common.error.StandardException)1