001package gu.sql2java; 002 003import static com.google.common.base.Preconditions.*; 004import static gu.sql2java.SimpleLog.*; 005 006import java.util.Map; 007import java.util.concurrent.TimeUnit; 008 009import com.google.common.collect.ImmutableMap; 010 011import gu.sql2java.exception.ObjectRetrievalException; 012 013public class TableCache<B extends BaseBean> extends ColumnCache<B>{ 014 private final Map<String, ColumnCache<B>> indexCachers; 015 public TableCache(RowMetaData metaData){ 016 this(metaData, DEFAULT_CACHE_MAXIMUMSIZE, DEFAULT_DURATION, DEFAULT_TIME_UNIT); 017 } 018 public TableCache(RowMetaData metaData, long maximumSize){ 019 this(metaData, maximumSize, DEFAULT_DURATION, DEFAULT_TIME_UNIT); 020 } 021 public TableCache(RowMetaData metaData,long maximumSize, long durationMinutes){ 022 this(metaData,maximumSize, durationMinutes, DEFAULT_TIME_UNIT); 023 } 024 public TableCache(RowMetaData metaData,long maximumSize, long duration, TimeUnit unit) { 025 this(metaData,DEFAULT_STRATEGY,maximumSize, duration, unit); 026 } 027 /** 028 * @param metaData 029 * @param updateStrategy 030 * @param maximumSize 031 * @param duration 032 * @param unit 033 * @see ColumnCache#ColumnCache(RowMetaData, String, gu.sql2java.Constant.UpdateStrategy, long, long, TimeUnit) 034 */ 035 public TableCache(RowMetaData metaData,UpdateStrategy updateStrategy,long maximumSize, long duration, TimeUnit unit) { 036 super(metaData,null,updateStrategy,maximumSize, duration, unit); 037 ImmutableMap.Builder<String, ColumnCache<B>> builder = ImmutableMap.builder(); 038 for(IndexMetaData index : metaData.getUniqueIndices().values()){ 039 builder.put( 040 index.name, 041 new ColumnCache<B>( 042 this.metaData, 043 index.name, 044 this.updateStrategy, 045 this.maximumSize, 046 this.duration, 047 this.unit)); 048 } 049 indexCachers = builder.build(); 050 manager.bindForeignKeyListenerForDeleteRule(); 051 } 052 /** ×¢²áÕìÌýÆ÷ */ 053 public void registerListener() { 054 for(ColumnCache<B> cache : indexCachers.values()){ 055 manager.registerListener(cache); 056 if(debug){ 057 log("REGISTER LISTENER FOR INDEX %s of %s", cache.indexName,metaData.tablename); 058 } 059 } 060 manager.registerListener(this); 061 if(debug){ 062 log("REGISTER LISTENER FOR INDEX PRIMARY KEY of %s",metaData.tablename); 063 } 064 } 065 /** ×¢ÏúÕìÌýÆ÷ */ 066 public void unregisterListener() { 067 manager.unregisterListener(this); 068 for(ColumnCache<B> cache : indexCachers.values()){ 069 manager.unregisterListener(cache); 070 } 071 } 072 073 void updateAll(B bean){ 074 this.update(bean); 075 for(ColumnCache<B> cache : indexCachers.values()){ 076 cache.update(bean); 077 } 078 } 079 public B getBeanByIndex(String indexName,Object... keys)throws ObjectRetrievalException{ 080 return checkNotNull(indexCachers.get(indexName),"INVALID indexName %s",indexName).getBean(keys); 081 } 082 public B getBeanByIndexUnchecked(String indexName,Object... keys){ 083 return checkNotNull(indexCachers.get(indexName),"INVALID indexName %s",indexName).getBeanUnchecked(keys); 084 } 085 @Override 086 public int hashCode() { 087 final int prime = 31; 088 int result = super.hashCode(); 089 result = prime * result + ((indexCachers == null) ? 0 : indexCachers.hashCode()); 090 return result; 091 } 092 @Override 093 public boolean equals(Object obj) { 094 if (this == obj) { 095 return true; 096 } 097 if (!super.equals(obj)) { 098 return false; 099 } 100 if (!(obj instanceof TableCache)) { 101 return false; 102 } 103 TableCache<?> other = (TableCache<?>) obj; 104 if (indexCachers == null) { 105 if (other.indexCachers != null) { 106 return false; 107 } 108 } else if (!indexCachers.equals(other.indexCachers)) { 109 return false; 110 } 111 return true; 112 } 113 @Override 114 public String toString() { 115 StringBuilder builder = new StringBuilder(); 116 builder.append("TableCache [super="); 117 builder.append(super.toString()); 118 builder.append(",indexCachers="); 119 builder.append(indexCachers); 120 builder.append("]"); 121 return builder.toString(); 122 } 123}