hachikuji commented on a change in pull request #10005: URL: https://github.com/apache/kafka/pull/10005#discussion_r569780361
########## File path: core/src/main/scala/kafka/server/metadata/LocalConfigRepository.scala ########## @@ -0,0 +1,93 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 kafka.server.metadata + +import java.util +import java.util.Properties +import java.util.concurrent.ConcurrentHashMap +import java.util.function.BiFunction + +import org.apache.kafka.common.config.ConfigResource +import org.apache.kafka.common.config.ConfigResource.Type + +import scala.jdk.CollectionConverters._ + +/** + * A ConfigRepository that stores configurations locally. + */ +class LocalConfigRepository extends ConfigRepository { + val configMap = new ConcurrentHashMap[ConfigResource, util.HashMap[String, String]] + + def setTopicConfig(topic: String, key: String, value: String): Unit = { + setConfig(new ConfigResource(Type.TOPIC, topic), key, value) + } + + def setBrokerConfig(id: Int, key: String, value: String): Unit = { + setConfig(new ConfigResource(Type.BROKER, id.toString()), key, value) + } + + def setConfig(configResource: ConfigResource, key: String, value: String): Unit = { + configMap.compute(configResource, new BiFunction[ConfigResource, util.HashMap[String, String], util.HashMap[String, String]] { + override def apply(resource: ConfigResource, + curConfigs: util.HashMap[String, String]): util.HashMap[String, String] = { + if (value == null) { + if (curConfigs == null) { + null + } else { + val newConfigs = new util.HashMap[String, String](curConfigs) + newConfigs.remove(key) + if (newConfigs.isEmpty) { + null + } else { + newConfigs + } + } + } else { + if (curConfigs == null) { + val newConfigs = new util.HashMap[String, String](1) + newConfigs.put(key, value) + newConfigs + } else { + val newConfigs = new util.HashMap[String, String](curConfigs.size() + 1) + newConfigs.putAll(curConfigs) + newConfigs.put(key, value) + newConfigs + } + } + } + }) + } + + def config(configResource: ConfigResource): Properties = { Review comment: nit: can we be consistent on pluralization? I think I tend to see `config` as plural already, so maybe we can use `topicConfig` and `brokerConfig`. Really we could even pull this up to `ConfigRepository`. Maybe something like this: ```scala trait ConfigRepository { def config(configResource: ConfigResource): Properties def topicConfig(topic: String): Properties = { config(new ConfigResource(Type.TOPIC, topic)) } def brokerConfig(id: Int): Properties = { config(new ConfigResource(Type.BROKER, id.toString())) } } ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org