gu.sql2java.TableCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-manager Show documentation
Show all versions of sql2java-manager Show documentation
sql2java manager class package for accessing database
package gu.sql2java;
import static com.google.common.base.Preconditions.*;
import static gu.sql2java.SimpleLog.*;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableMap;
import gu.sql2java.TableManager.Action;
import gu.sql2java.exception.ObjectRetrievalException;
import gu.sql2java.exception.RuntimeDaoException;
class TableCache extends ColumnCache
implements TableListener{
private final Map> indexCachers;
/**
* constructor
* @param metaData meta data for table
* @param updateStrategy cache update strategy,{@link Constant#DEFAULT_STRATEGY} be used if {@code null}
* @param maximumSize maximum capacity of cache ,{@link Constant#DEFAULT_CACHE_MAXIMUMSIZE } be used if {@code null} or <=0,see also {@link CacheBuilder#maximumSize(long)}
* @param duration cache data expired time,{@link Constant#DEFAULT_DURATION} be used if {@code null} or <=0,see also {@link CacheBuilder#expireAfterWrite(long, TimeUnit)}
* @param unit time unit for {@code duration},{@link Constant#DEFAULT_TIME_UNIT} be used if {@code null} ,see also {@link CacheBuilder#expireAfterWrite(long, TimeUnit)}
*/
public TableCache(RowMetaData metaData,UpdateStrategy updateStrategy,Long maximumSize, Long duration, TimeUnit unit) {
super(metaData,null,updateStrategy,maximumSize, duration, unit);
ImmutableMap.Builder> builder = ImmutableMap.builder();
for(IndexMetaData index : metaData.getUniqueIndices().values()){
builder.put(
index.name,
new ColumnCache(
this.metaData,
index.name,
this.updateStrategy,
this.maximumSize,
this.duration,
this.unit));
}
indexCachers = builder.build();
manager.bindForeignKeyListenerForDeleteRule();
}
/** 注册侦听器 */
public void registerListener() {
manager.registerListener(this);
if(debug){
log("REGISTER LISTENER FOR INDEX AND PRIMARY KEY of " + metaData.tablename);
}
}
/** 注销侦听器 */
public void unregisterListener() {
manager.unregisterListener(this);
}
/**
* force update bean to all caches that excluding specified by {@link #recursive}
* @param bean
*/
private void updateAll(B bean){
update(bean, UpdateStrategy.always);
for(IKeyCache cache : indexCachers.values()){
cache.update(bean, UpdateStrategy.always);
}
}
/**
* return record (B) that unique indexed by 'keys'
* @param indexName index name
* @param keys values for index key
* @return B instance if found
* @throws ObjectRetrievalException not found record
*/
public B getBeanByIndex(String indexName,Object... keys)throws ObjectRetrievalException{
return checkNotNull(indexCachers.get(indexName),"INVALID indexName %s",indexName).getBean(keys);
}
/**
* return record (B) that unique indexed by 'keys'
* @param indexName index name
* @param keys values for index key
* @return B instance if found,or null
*/
public B getBeanByIndexUnchecked(String indexName,Object... keys){
return checkNotNull(indexCachers.get(indexName),"INVALID indexName %s",indexName).getBeanUnchecked(keys);
}
/**
* wrap the 'action' for updating cache while retrieve data from database
* @param action
* @return {@link Action} instance
*/
public Action wrap(Action action){
if(action == null
|| action instanceof TableCache.CacheWrapper
|| action instanceof BaseTableManager.DeleteBeanAction){
return action;
}
return new CacheWrapper(action);
}
//////////////////////////////////////////////
// TableListener IMPLEMENTATION
//////////////////////////////////////////////
@Override
public void afterUpdate(B bean) {
update(bean);
for(IKeyCache cache : indexCachers.values()){
cache.update(bean);
}
}
@Override
public void afterInsert(B bean) {
update(bean);
for(IKeyCache cache : indexCachers.values()){
cache.update(bean);
}
}
@Override
public void afterDelete(B bean) {
B mem = remove(bean);
for(IKeyCache cache : indexCachers.values()){
if(cache.hasValidKey(bean)){
cache.remove(bean);
}else if(cache.hasValidKey(mem)){
cache.remove(mem);
}
}
}
@Override
public void beforeInsert(B bean) throws RuntimeDaoException {}
@Override
public void beforeUpdate(B bean) throws RuntimeDaoException {}
@Override
public void beforeDelete(B bean) throws RuntimeDaoException {}
@Override
public void done() throws RuntimeDaoException {}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((indexCachers == null) ? 0 : indexCachers.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (!(obj instanceof TableCache)) {
return false;
}
TableCache> other = (TableCache>) obj;
if (indexCachers == null) {
if (other.indexCachers != null) {
return false;
}
} else if (!indexCachers.equals(other.indexCachers)) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("TableCache [super=");
builder.append(super.toString());
builder.append(",indexCachers=");
builder.append(indexCachers);
builder.append("]");
return builder.toString();
}
private class CacheWrapper implements Action{
private final Action action;
public CacheWrapper(Actionaction){
this.action = checkNotNull(action,"action is null");
}
@Override
public void call(B bean) {
action.call(bean);
updateAll(bean);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy