|
| 1 | +/* |
| 2 | + * Copyright © 2021 DQOps (support@dqops.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.dqops.metadata.labels; |
| 18 | + |
| 19 | +import com.dqops.metadata.sources.PhysicalTableName; |
| 20 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 21 | +import org.springframework.context.annotation.Scope; |
| 22 | +import org.springframework.stereotype.Component; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +/** |
| 28 | + * A global container of all labels, collected from all levels (connection, table, column). |
| 29 | + */ |
| 30 | +@Component |
| 31 | +@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) |
| 32 | +public class GlobalLabelsContainerImpl implements GlobalLabelsContainer { |
| 33 | + private final LabelCountContainer connectionLabels = new LabelCountContainer(); |
| 34 | + private final LabelCountContainer tableLabels = new LabelCountContainer(); |
| 35 | + private final LabelCountContainer columnLabels = new LabelCountContainer(); |
| 36 | + |
| 37 | + private final HashMap<String, LabelCountContainer> importedConnectionLabels = new HashMap<>(); |
| 38 | + private final HashMap<TableLabelsKey, LabelCountContainer> importedTableLabels = new HashMap<>(); |
| 39 | + private final HashMap<TableLabelsKey, LabelCountContainer> importedColumnLabels = new HashMap<>(); |
| 40 | + private final Object lock = new Object(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns the global labels container for labels defined on a connection level (assigned to the connection object). |
| 44 | + * @return Connection level labels. |
| 45 | + */ |
| 46 | + @Override |
| 47 | + public LabelCountContainer getConnectionLabels() { |
| 48 | + return connectionLabels; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the global labels container for labels defined on a table level (assigned to the table object). |
| 53 | + * @return Table level labels. |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public LabelCountContainer getTableLabels() { |
| 57 | + return tableLabels; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the global labels container for labels defined on a column level (assigned to the column object). |
| 62 | + * @return Column level labels. |
| 63 | + */ |
| 64 | + @Override |
| 65 | + public LabelCountContainer getColumnLabels() { |
| 66 | + return columnLabels; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Imports new labels from a connection level. |
| 71 | + * @param connectionName Connection name. |
| 72 | + * @param newLabels New labels or null if the connection was removed and the old labels should be unregistered. |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public void importConnectionLabels(String connectionName, LabelCountContainer newLabels) { |
| 76 | + synchronized (this.lock) { |
| 77 | + LabelCountContainer oldLabels = this.importedConnectionLabels.get(connectionName); |
| 78 | + if (Objects.equals(oldLabels, newLabels)) { |
| 79 | + // no change |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (oldLabels != null) { |
| 84 | + this.connectionLabels.subtractCountsFromContainer(oldLabels); |
| 85 | + this.importedConnectionLabels.remove(connectionName); |
| 86 | + } |
| 87 | + |
| 88 | + if (newLabels != null && !newLabels.isEmpty()) { |
| 89 | + this.connectionLabels.addCountsFromContainer(oldLabels); |
| 90 | + this.importedConnectionLabels.put(connectionName, newLabels); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Imports new labels from a table level. |
| 97 | + * @param connectionName Connection name. |
| 98 | + * @param schemaTableName Schema and table name. |
| 99 | + * @param newLabels New labels or null if the table was removed and the old labels should be unregistered. |
| 100 | + */ |
| 101 | + @Override |
| 102 | + public void importTableLabels(String connectionName, PhysicalTableName schemaTableName, LabelCountContainer newLabels) { |
| 103 | + TableLabelsKey tableLabelsKey = new TableLabelsKey(connectionName, schemaTableName); |
| 104 | + |
| 105 | + synchronized (this.lock) { |
| 106 | + LabelCountContainer oldLabels = this.importedTableLabels.get(tableLabelsKey); |
| 107 | + if (Objects.equals(oldLabels, newLabels)) { |
| 108 | + // no change |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (oldLabels != null) { |
| 113 | + this.tableLabels.subtractCountsFromContainer(oldLabels); |
| 114 | + this.importedTableLabels.remove(tableLabelsKey); |
| 115 | + } |
| 116 | + |
| 117 | + if (newLabels != null && !newLabels.isEmpty()) { |
| 118 | + this.tableLabels.addCountsFromContainer(oldLabels); |
| 119 | + this.importedTableLabels.put(tableLabelsKey, newLabels); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Imports new labels from a column level, but aggregated for a whole table. |
| 126 | + * @param connectionName Connection name. |
| 127 | + * @param schemaTableName Schema and table name. |
| 128 | + * @param newLabels New labels or null if the table was removed and the old labels should be unregistered. |
| 129 | + */ |
| 130 | + @Override |
| 131 | + public void importColumnLabels(String connectionName, PhysicalTableName schemaTableName, LabelCountContainer newLabels) { |
| 132 | + TableLabelsKey tableLabelsKey = new TableLabelsKey(connectionName, schemaTableName); |
| 133 | + |
| 134 | + synchronized (this.lock) { |
| 135 | + LabelCountContainer oldLabels = this.importedColumnLabels.get(tableLabelsKey); |
| 136 | + if (Objects.equals(oldLabels, newLabels)) { |
| 137 | + // no change |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + if (oldLabels != null) { |
| 142 | + this.columnLabels.subtractCountsFromContainer(oldLabels); |
| 143 | + this.importedColumnLabels.remove(tableLabelsKey); |
| 144 | + } |
| 145 | + |
| 146 | + if (newLabels != null && !newLabels.isEmpty()) { |
| 147 | + this.columnLabels.addCountsFromContainer(oldLabels); |
| 148 | + this.importedColumnLabels.put(tableLabelsKey, newLabels); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments