shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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<Address, DbCache> dbMap = new ConcurrentHashMap<Address, DbCache>();
    
    /* 通过MetaDbId缓存数据库信息 */
    private final Map<Long, DbCache> dbMetaIdMap = new ConcurrentHashMap<Long, DbCache>();
    
    /**
     * 返回当前存储正在使用的数据库信息
     * @return
     */
    public Address getUsingAddress(){
        for(Map.Entry<Address, DbCache> 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<Address, DbCache> 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<Address, DbCache> 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<Long, DbCache> 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);
    }
    
    /**
     * 该方法允许通过表<code>dc_meta_table</code>中,通过db_id字段作为查询条件来找到dbCache对象,<br>
     * 然后通过该对象来添加其表信息。</p>
     * 此方法在系统缓存对象中调用:<code>DcMetaCacheProvider</code>
     * @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<String, Boolean> tables = new ConcurrentHashMap<String, Boolean>();
 
        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;
        }
    }
    
}