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
package com.iplatform.gather.support;
 
import com.walker.connector.Address;
import com.walker.db.DatabaseException;
import com.walker.db.TableInfo;
import com.walker.store.AbstractMetaDataEngine;
import com.walker.store.Repository;
import com.walker.store.repo.AbstractRepository;
import com.walker.store.repo.DatabaseRepository;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 测试元数据引擎
 * @author shikeying
 * @date 2015年12月17日
 *
 */
public class TestMetaDataEngine extends AbstractMetaDataEngine {
 
//    protected transient final Log logger = LogFactory.getLog(getClass());
    
    // key = storeId
    private final Map<String, Repository> cached = new ConcurrentHashMap<String, Repository>(2);
    
    // key = storeId
    private final Map<String, List<String>> tableCache = new ConcurrentHashMap<String, List<String>>();
    
    private final ConcurrentHashMap<String, Address> usingAddress = new ConcurrentHashMap<String, Address>();
    
    @Override
    public void saveNewAddress(String storeId, Address address) throws DatabaseException {
        AbstractRepository repo = (AbstractRepository)cached.get(storeId);
        if(repo == null){
            repo = new DatabaseRepository();
            repo.setId(storeId);
            cached.put(storeId, repo);
        }
        repo.addAddress(address);
        logger.debug("---- 缓存更新后,数据库: " + repo.getAddressList());
        
        // 同时,更新记录,对于当前存储,那个库正在被使用
        usingAddress.put(storeId, address);
        logger.debug("usingAddress.size = " + usingAddress.size());
    }
 
    @Override
    public void saveNewTable(String storeId, Address addr, String destTableName) throws DatabaseException {
        List<String> list = tableCache.get(storeId);
        if(list == null){
            list = new ArrayList<String>();
            tableCache.put(storeId, list);
        }
        if(!list.contains(destTableName)){
            list.add(destTableName);
        }
    }
 
    @Override
    public synchronized int getTableSize(Address address) {
        // 测试,查找数据库中的元数据
        return this.getDatabaseMetaEngine().getTableSize(address);
    }
 
    @Override
    public int getDatabaseSize(Address address) {
        return 1;
    }
 
    @Override
    public boolean isExistDatabase(String storeId, Address address) {
        List<Address> list = null;
        for(Repository repo : cached.values()){
            list = repo.getAddressList();
            for(Address addr : list){
                logger.debug("+++++++++ 要比较的地址:" + addr + ", 源地址:" + address);
                if(addr.equals(address)){
                    return true;
                }
            }
        }
        return false;
    }
 
    @Override
    public boolean isExistTable(String storeId, Address address, String tableName) {
        List<String> list = tableCache.get(storeId);
        if(list == null){
            list = new ArrayList<String>();
            tableCache.put(storeId, list);
        }
        return list.contains(tableName);
    }
 
    @Override
    public Address getUsingAddress(String storeId) {
        logger.debug("====== usingAddress: " + usingAddress.size());
        for(Address a : usingAddress.values()){
            logger.debug("2-- " + a);
        }
        return usingAddress.get(storeId);
    }
 
    @Override
    public List<String> getFields(Address address, String tableName) {
        return this.getDatabaseMetaEngine().getFields(address, tableName);
    }
 
    @Override
    public Map<String, TableInfo> getTableRows(String storeId, long metaDbId, List<String> tableNameList) {
        return null;
    }
 
}