Search in sources :

Example 16 with TimeWrapper

use of org.vcell.util.TimeWrapper in project vcell by virtualcell.

the class DBCacheTable method put.

private synchronized TimeWrapper put(Object key, TimeWrapper timeWrapper) throws CacheException {
    long dataMemSize = timeWrapper.getSize();
    if (dataMemSize >= maxMemSize) {
        throw new CacheException("data item " + key + " with memSize=" + dataMemSize + " too large, maxCacheSize=" + maxMemSize);
    }
    // 
    if (dataMemSize + currMemSize >= maxMemSize) {
        resize(Math.min((long) (maxMemSize * cleanupFraction), maxMemSize - dataMemSize));
    }
    TimeWrapper oldTimeWrapper = (TimeWrapper) hashTable.put(key, timeWrapper);
    currMemSize += dataMemSize;
    if (oldTimeWrapper != null) {
        currMemSize -= oldTimeWrapper.getSize();
    }
    if (currMemSize < 0 || currMemSize >= maxMemSize) {
        throw new CacheException("Error: adding data item " + key + ". currMemSize=" + currMemSize + " maxMemSize=" + maxMemSize);
    }
    return oldTimeWrapper;
}
Also used : TimeWrapper(org.vcell.util.TimeWrapper) CacheException(org.vcell.util.CacheException)

Example 17 with TimeWrapper

use of org.vcell.util.TimeWrapper in project vcell by virtualcell.

the class DBCacheTable method putUnprotected.

public void putUnprotected(KeyValue key, Cacheable cacheable) throws CacheException {
    if (!((cacheable instanceof Cloneable) || (cacheable instanceof Immutable) || (cacheable instanceof Serializable))) {
        throw new CacheException("put:Object not Cloneable, Immutable or Serializable");
    }
    if (key == null) {
        throw new CacheException("put: key == null");
    }
    long dataSize = 1000;
    try {
        byte[] objData = org.vcell.util.BeanUtils.toSerialized(cacheable);
        dataSize = objData.length;
    } catch (IOException e) {
        e.printStackTrace(System.out);
        throw new CacheException(e.getMessage());
    }
    TimeWrapper oldTimeWrapper = put(key, new TimeWrapper(new DbObjectWrapper(cacheable), dataSize, key));
    // 
    if (oldTimeWrapper != null) {
        System.out.println("replacing object ALREADY IN DATABASE_CACHE " + oldTimeWrapper.getObject() + " at key " + key);
    }
    // 
    // checking to see if same object (using compareEqual) already in hash
    // 
    // if (cacheable instanceof cbit.vcell.model.Species){
    // Enumeration enum1 = hashTable.elements();
    // while (enum1.hasMoreElements()){
    // TimeWrapper timeWrapper = (TimeWrapper) enum1.nextElement();
    // DbObjectWrapper objWrapper = (DbObjectWrapper) timeWrapper.getObject();
    // Cacheable cacheObj = objWrapper.getWorkingCopy();
    // if (cacheable != cacheObj && cacheable.compareEqual(cacheObj)){
    // //	throw new RuntimeException("DBCacheTable.put("+cacheable+"), already in cache as ("+cacheObj+")");
    // System.out.println("DBCacheTable.put("+cacheable+"), already in cache as ("+cacheObj+")");
    // }
    // }
    // }
    // System.out.print("put(cacheable="+cacheable+") ");
    show();
}
Also used : Immutable(org.vcell.util.Immutable) TimeWrapper(org.vcell.util.TimeWrapper) Serializable(java.io.Serializable) CacheException(org.vcell.util.CacheException) IOException(java.io.IOException)

Example 18 with TimeWrapper

use of org.vcell.util.TimeWrapper in project vcell by virtualcell.

the class DBCacheTable method get.

public synchronized Cacheable get(KeyValue key) {
    TimeWrapper timeWrapper = (TimeWrapper) hashTable.get(key);
    if (timeWrapper != null) {
        DbObjectWrapper objWrapper = (DbObjectWrapper) timeWrapper.getObject();
        Cacheable cacheable = objWrapper.getWorkingCopy();
        // System.out.println("DBCacheTable.get: "+cacheable.getClass().getName());
        return cacheable;
    } else {
        // System.out.println("DBCacheTable.get: null");
        return null;
    }
}
Also used : TimeWrapper(org.vcell.util.TimeWrapper) Cacheable(org.vcell.util.Cacheable)

Aggregations

TimeWrapper (org.vcell.util.TimeWrapper)18 Enumeration (java.util.Enumeration)8 CacheException (org.vcell.util.CacheException)4 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Cacheable (org.vcell.util.Cacheable)2 Immutable (org.vcell.util.Immutable)2