package com.iplatform.gather.cache;
import com.walker.connector.Address;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 缓存使用的'存储对象'定义
* @author shikeying
* @date 2015年12月29日
*
*/
public class StoreObject {
private static final Boolean DUMMY = new Boolean(true);
// private String id;
/* 通过address缓存数据库信息 */
private final Map
dbMap = new ConcurrentHashMap();
/* 通过MetaDbId缓存数据库信息 */
private final Map dbMetaIdMap = new ConcurrentHashMap();
/**
* 返回当前存储正在使用的数据库信息
* @return
*/
public Address getUsingAddress(){
for(Map.Entry entry : dbMap.entrySet()){
if(entry.getValue().isUsing()){
return entry.getKey();
}
}
return null;
}
/**
* 返回给定的数据库中,元数据记录了有多少表
* @param address
* @return
*/
public int getTableSize(Address address){
int i = 0;
Address a = null;
for(Map.Entry entry : dbMap.entrySet()){
a = entry.getKey();
if(a.equals(address)){
i = i + entry.getValue().getTableSize();
}
}
return i;
}
/**
* 返回当前存储项目中,在给定主机上有几个数据库
* @param address
* @return
*/
public int getDatabaseSize(Address address){
int i = 0;
Address a = null;
for(Map.Entry entry : dbMap.entrySet()){
a = entry.getKey();
if(a.getUrl().equals(address.getUrl()) && a.getPort() == address.getPort()){
i++;
}
}
return i;
}
/**
* 根据输入的数据库地址信息,找到对应的metaDbId
* @param address
* @return
*/
public Long getMetaDbId(Address address){
DbCache d = dbMap.get(address);
if(d == null){
return null;
}
for(Map.Entry entry : dbMetaIdMap.entrySet()){
if(entry.getValue().equals(d)){
return entry.getKey();
}
}
return null;
}
/**
* 根据元数据中数据库ID,查找缓存的数据库基本信息
* @param dbId
* @return
*/
public Address getDbCache(long dbId){
DbCache db = dbMetaIdMap.get(dbId);
if(db == null){
return null;
}
Address address = new Address();
address.setUrl(db.getIp());
address.setPort(db.getPort());
address.setService(db.getDatabaseName());
// address.setAuthentication(db.get);
return address;
}
public void addDb(Address address, long metaDbId){
// 添加新数据库时,先把其他数据库设置为'不可用'状态
DbCache d = dbMap.get(address);
for(DbCache dc : dbMap.values()){
dc.setUsing(false);
}
if(d == null){
d = new DbCache();
d.setDatabaseName(address.getServiceName());
d.setIp(address.getUrl());
d.setPort(address.getPort());
d.setMetaDbId(metaDbId);
// d.setUsing(address.isUsing());
dbMap.put(address, d);
dbMetaIdMap.put(metaDbId, d);
} else {
// 如果已经缓存,不再添加
}
d.setUsing(true); // 新创建的库,肯定正在被使用
}
public void removeDb(Address address){
dbMap.remove(address);
}
/**
* 该方法允许通过表dc_meta_table
中,通过db_id字段作为查询条件来找到dbCache对象,
* 然后通过该对象来添加其表信息。
* 此方法在系统缓存对象中调用:DcMetaCacheProvider
* @param dbId
* @param tableName
*/
public void addTable(long dbId, String tableName){
DbCache d = dbMetaIdMap.get(dbId);
if(d == null){
throw new IllegalArgumentException("存储缓存中,未找到数据库信息。dbId = " + dbId + ", tableName = " + tableName);
}
d.addTable(tableName);
}
public void addTable(Address address, String tableName){
DbCache d = dbMap.get(address);
if(d == null){
throw new IllegalArgumentException("存储缓存中,未找到数据库信息。address = " + address + ", tableName = " + tableName);
}
d.addTable(tableName);
}
public void removeTable(Address address, String tableName){
DbCache d = dbMap.get(address);
if(d == null){
throw new IllegalArgumentException("存储缓存中,未找到数据库信息。address = " + address + ", tableName = " + tableName);
}
d.removeTable(tableName);
}
public boolean isExistDatabase(Address address){
return dbMap.get(address) != null;
}
public boolean isExistTable(String storeId, Address address, String tableName){
DbCache d = dbMap.get(address);
if(d == null){
throw new IllegalArgumentException("存储缓存中,未找到数据库信息。address = " + address + ", tableName = " + tableName);
}
return d.isExistTable(tableName);
}
private class DbCache{
private String databaseName;
private String ip;
private int port = 0;
private long metaDbId; // 元数据:数据库ID
private ConcurrentHashMap tables = new ConcurrentHashMap();
private boolean using = false;
public boolean isUsing() {
return using;
}
public void setUsing(boolean using) {
this.using = using;
}
// public long getMetaDbId() {
// return metaDbId;
// }
public void setMetaDbId(long metaDbId) {
this.metaDbId = metaDbId;
}
public String getDatabaseName() {
return databaseName;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public void addTable(String tableName){
tables.putIfAbsent(tableName, DUMMY);
}
public void removeTable(String tableName){
tables.remove(tableName);
}
public int getTableSize(){
return tables.size();
}
public boolean isExistTable(String tableName){
return tables.get(tableName) != null;
}
@Override
public boolean equals(Object obj){
if(obj != null && obj instanceof DbCache){
DbCache d = (DbCache)obj;
if(d.metaDbId == this.metaDbId){
return true;
}
}
return false;
}
private int hashCode = 0;
@Override
public int hashCode(){
if(hashCode == 0){
hashCode = new Long(this.metaDbId).hashCode();
}
return hashCode;
}
}
}