疯狂的狮子Li
2024-07-27 bd872f624a043b00af1124607929e04230dbd212
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright 2006-2023 www.anyline.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 
 
package org.anyline.proxy;
 
import org.anyline.cache.CacheProvider;
import org.anyline.data.param.ConfigStore;
import org.anyline.data.runtime.DataRuntime;
import org.anyline.entity.OriginRow;
import org.anyline.metadata.*;
import org.anyline.metadata.graph.EdgeTable;
import org.anyline.metadata.graph.VertexTable;
import org.anyline.util.ConfigTable;
import org.anyline.util.encrypt.MD5Util;
 
import java.util.LinkedHashMap;
import java.util.List;
 
public class CacheProxy {
 
    private static final ThreadLocal<OriginRow> thread_caches = new ThreadLocal<>();
    private static final ThreadLocal<OriginRow> thread_names = new ThreadLocal<>();
    private static OriginRow application_caches = new OriginRow();
    private static OriginRow application_names = new OriginRow();
    public static CacheProvider provider;
    public CacheProxy() {}
    public static void init(CacheProvider provider) {
        CacheProxy.provider = provider;
    }
    private static OriginRow caches(){
        OriginRow result = new OriginRow();
        if(ConfigTable.METADATA_CACHE_SCOPE == 1){
            result = thread_caches.get();
            if(null == result){
                result = new OriginRow();
                thread_caches.set(result);
            }
        }else if(ConfigTable.METADATA_CACHE_SCOPE == 9){
            if(application_caches.isExpire(ConfigTable.METADATA_CACHE_SECOND*1000)){
                application_caches = new OriginRow();
            }
            result = application_caches;
        }
        return result;
    }
    private static OriginRow names(){
        OriginRow result = new OriginRow();
        if(ConfigTable.METADATA_CACHE_SCOPE == 1){
            result = thread_names.get();
            if(null == result){
                result = new OriginRow();
                thread_names.set(result);
            }
        }else if(ConfigTable.METADATA_CACHE_SCOPE == 9){
            if(application_names.isExpire(ConfigTable.METADATA_CACHE_SECOND*1000)){
                application_names = new OriginRow();
            }
            result = application_names;
        }
        return result;
    }
/*
 
    private static Map<String, DataRow> cache_columns = new HashMap<>();
    private static Map<String, Map<String, String>>  cache_names = new HashMap<>();
    private static Map<String, DataRow> cache_table_maps = new HashMap<>();
    private static Map<String, DataRow> cache_view_maps = new HashMap<>();
*/
 
 
    public static String key(DataRuntime runtime, String flag, boolean greedy, Catalog catalog, Schema schema, String pattern, int types, ConfigStore configs){
        StringBuilder key = new StringBuilder();
        key.append(runtime.datasource()).append("_").append(flag).append("_").append(greedy).append("_");
        if(null != catalog){
            key.append(catalog.getName());
        }
        key.append("_");
        if(null != schema){
            key.append(schema.getName());
        }
        key.append("_").append(pattern).append("_").append(types);
        if(null != configs){
            key.append(MD5Util.crypto(configs.json()));;
        }
        return key.toString().toUpperCase();
    }
    public static String key(DataRuntime runtime, String flag, boolean greedy, Table table){
        StringBuilder key = new StringBuilder();
        key.append(runtime.datasource()).append("_").append(flag).append("_").append(greedy).append("_");
        String name = table.getName();
        String catalog = table.getCatalogName();
        String schema = table.getSchemaName();
        if(null != catalog){
            key.append(catalog);
        }
        key.append("_");
        if(null != schema){
            key.append(schema);
        }
        key.append("_").append(name);
        return key.toString().toUpperCase();
    }
    public static String key(DataRuntime runtime, String flag, boolean greedy, Catalog catalog, Schema schema, String pattern){
        StringBuilder key = new StringBuilder();
        key.append(runtime.datasource()).append("_").append(flag).append("_").append(greedy).append("_");
        if(null != catalog){
            key.append(catalog.getName());
        }
        key.append("_");
        if(null != schema){
            key.append(schema.getName());
        }
        key.append("_").append(pattern);
        return key.toString().toUpperCase();
    }
    public static String name(String key) {
        return names().getString(key.toUpperCase());
    }
    public static void name(String key, String origin) {
        names().put(key.toUpperCase(), origin);
    }
    public static  <T extends Table> List<T> tables(String cache){
        List<T> tables = (List<T>)caches().get(cache);
        return tables;
    }
    public static  <T extends MasterTable> List<T> masterTables(String cache){
        List<T> tables = (List<T>)caches().get(cache);
        return tables;
    }
    public static  <T extends EdgeTable> List<T> edgeTables(String cache){
        List<T> tables = (List<T>)caches().get(cache);
        return tables;
    }
    public static  <T extends VertexTable> List<T> vertexTables(String cache){
        List<T> tables = (List<T>)caches().get(cache);
        return tables;
    }
    public static  <T extends Table> void tables(String cache, List<T> tables){
        caches().put(cache, tables);
    }
 
    public static  <T extends View> List<T> views(String cache){
        List<T> view = (List<T>)caches().get(cache);
        return view;
    }
    public static  <T extends View> void views(String cache, List<T> view){
        caches().put(cache, view);
    }
 
    public static  void cache(String cache, Object value){
        caches().put(cache, value);
    }
 
/*
    public static <T extends Table> void name(DriverAdapter adapter, List<T> tables) {
        if(null != tables) {
            for (Table table : tables) {
                name(adapter, table.getCatalog(), table.getSchema(), table.getName(), table.getName());
            }
        }
    }*/
/*
    private static String key(DriverAdapter adapter, Catalog catalog, Schema schema) {
        String key = null;
        String catalog_name = null;
        String schema_name = null;
        if(null != catalog && adapter.supportCatalog()) {
            catalog_name = catalog.getName();
        }
        if(null != schema && adapter.supportSchema()) {
            schema_name = schema.getName();
        }
        if(null != catalog_name) {
            key = catalog_name;
        }
        if(null != schema_name) {
            if(null != key) {
                key += "_" + schema_name;
            }else{
                key = schema_name;
            }
        }
        if(null != key) {
            key = key.toUpperCase();
        }else{
            key = "ALL";
        }
        return key;
    }
    private static String key(DriverAdapter adapter, Catalog catalog, Schema schema, Table table) {
        String table_name = null;
        if(null != table) {
            table_name = table.getName();
        }
        String key = key(adapter, catalog, schema);
        if(null != table_name) {
            if(null != key) {
                key += ":" + table_name;
            }else{
                key = table_name;
            }
        }
        if(null != key) {
            key = key.toUpperCase();
        }
        return key;
    }*//*
    public static void name(DriverAdapter adapter, Catalog catalog, Schema schema, String name, String origin) {
        String group_key = key(adapter, catalog, schema);
        Map<String, String> maps = cache_names.get(group_key);
        if(null == maps) {
            maps = new HashMap<>();
            cache_names.put(group_key, maps);
        }
        String name_key = (group_key + ":" + name).toUpperCase();
        maps.put(name_key, origin);
    }
    public static Map<String, String> names(DriverAdapter adapter, Catalog catalog, Schema schema) {
        return cache_names.get(key(adapter, catalog, schema));
    }
    public static String name(DriverAdapter adapter, boolean greedy, Catalog catalog, Schema schema, String name) {
        if(null == name) {
            return null;
        }
        String group_key = key(adapter, catalog, schema);
        Map<String, String> maps = cache_names.get(group_key);
        if(null != maps) {
            String name_key = (group_key + ":" + name).toUpperCase();
            String origin = maps.get(name_key);
            if(null != origin) {
                return origin;
            }
        }
        if(greedy) {
            for (Map<String, String> names : cache_names.values()) {
                for(String item:names.keySet()) {
                    if(item.endsWith((":"+name).toUpperCase())) {
                        return names.get(item);
                    }
                }
            }
        }
        return null;
    }*/
    /*public static String datasource(String datasource) {
        if(null == datasource || "common".equalsIgnoreCase(datasource)) {
            //datasource = DataSourceHolder.curDataSource();
        }
        if(null == datasource) {
            datasource = "default";
        }
        return datasource.toUpperCase();
    }
*//*
    public static String tableName(String datasource, String name) {
        DataRow row = cache_table_maps.get(datasource(datasource));
        if(null != row) {
            return row.getString(name);
        }
        return name;
    }
    public static String viewName(String datasource, String name) {
        DataRow row = cache_view_maps.get(datasource(datasource));
        if(null != row) {
            return row.getString(name);
        }
        return name;
    }
    public static void setTableMaps(String datasource, DataRow maps) {
        cache_table_maps.put(datasource(datasource), maps);
    }
    public static void setViewMaps(String datasource, DataRow maps) {
        cache_view_maps.put(datasource(datasource), maps);
    }*/
 
 
   /* public static DataRow getTableMaps(String datasource) {
        DataRow row = cache_table_maps.get(datasource(datasource));
        if(null == row) {
            row = new DataRow();
            cache_table_maps.put(datasource(datasource), row);
        }
        return row;
    }*/
 
  /*  public static DataRow getViewMaps(String datasource) {
        DataRow row = cache_view_maps.get(datasource(datasource));
        if(null == row) {
            row = new DataRow();
            cache_view_maps.put(datasource(datasource), row);
        }
        return row;
    }*/
 
 
    public static <T extends Column> LinkedHashMap<String, T> columns(String key) {
        return (LinkedHashMap<String, T>) caches().get(key);
    }
    public static <T extends Tag> LinkedHashMap<String, T> tags(String key) {
        return (LinkedHashMap<String, T>) caches().get(key);
    }
 
 
   /* public static <T extends Column> void columns(DriverAdapter adapter, String datasource, Table table, LinkedHashMap<String, T> columns) {
        if(null == table) {
            return;
        }
        String cache = ConfigTable.getString("METADATA_CACHE_KEY");
        String key = datasource(datasource) + "_COLUMNS_" + key(adapter, table.getCatalog(), table.getSchema(), table);
        key = key.toUpperCase();
        if(null != provider && BasicUtil.isNotEmpty(cache) && !ConfigTable.IS_CACHE_DISABLED) {
            provider.put(cache, key, columns);
        }else{
            DataRow static_cache = new DataRow();
            static_cache.put("keys", columns);
            cache_columns.put(key, static_cache);
        }
    } */
 
    public static void clear() {/*
        if(null != provider && !ConfigTable.IS_CACHE_DISABLED) {
            String cache = ConfigTable.METADATA_CACHE_KEY;
            if(BasicUtil.isNotEmpty(cache)) {
                provider.clear(cache);
            }
        }else{
            cache_columns.clear();
        }*/
 
        caches().clear();
        names().clear();
    }
 
}