1. Project Clover database Wed Nov 12 2025 05:07:35 UTC
  2. Package guru.mikelue.foxglove.jdbc

File MetaDataCache.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
53% of files have more coverage

Code metrics

2
15
3
1
76
60
6
0.4
5
3
2

Classes

Class Line # Actions
MetaDataCache 15 15 0% 6 2
0.990%
 

Contributing tests

This file is covered by 39 tests. .

Source view

1    package guru.mikelue.foxglove.jdbc;
2   
3    import java.sql.Connection;
4    import java.sql.SQLException;
5    import java.util.List;
6    import java.util.Map;
7    import java.util.concurrent.ConcurrentHashMap;
8   
9    import org.apache.commons.lang3.Validate;
10   
11    import guru.mikelue.foxglove.ColumnMeta;
12   
13    import static java.util.stream.Collectors.toSet;
14   
 
15    class MetaDataCache {
16    private final static Map<String, Map<String, List<ColumnMeta>>> globalCache =
17    new ConcurrentHashMap<>();
18   
19    private final Map<String, List<ColumnMeta>> cacheByInstance;
20    private final String connUrl;
21   
 
22  62 toggle MetaDataCache(Connection conn)
23    {
24  62 try {
25  62 connUrl = conn.getMetaData().getURL();
26    } catch (SQLException e) {
27  0 throw new RuntimeJdbcException(e);
28    }
29   
30  62 if (connUrl == null) {
31  3 cacheByInstance = new ConcurrentHashMap<>();
32    } else {
33  59 cacheByInstance = globalCache.computeIfAbsent(
34    connUrl,
35    key -> new ConcurrentHashMap<>()
36    );
37    }
38    }
39   
 
40  61 toggle void loadMetadata(List<JdbcTableFacet> facets, Connection conn)
41    {
42  61 for (var facet: facets) {
43  91 var metaOfColumnsOfDb = cacheByInstance.computeIfAbsent(
44    facet.tableName(),
45    name -> {
46  9 try {
47  9 return MetaUtils.getColumnMetaList(conn.getMetaData(), name);
48    } catch (SQLException e) {
49  0 throw new RuntimeJdbcException(e);
50    }
51    }
52    );
53   
54    /*
55    * Checks if all configured columns exist in database
56    */
57  91 var setOfColumnNamesOfDb = metaOfColumnsOfDb.stream()
58    .map(meta -> meta.name().toLowerCase())
59    .collect(toSet());
60   
61  91 for (var checkedName: facet.getConfiguredNamesOfColumn()) {
62  123 Validate.isTrue(
63    setOfColumnNamesOfDb.contains(checkedName),
64    "Configured column not found in table[%s]: \"%s\"",
65    facet.tableName(), checkedName
66    );
67    }
68    // :~)
69    }
70    }
71   
 
72  88 toggle List<ColumnMeta> getMetaOfColumns(String tableName)
73    {
74  88 return cacheByInstance.get(tableName);
75    }
76    }