wenzhenghu commented on code in PR #65126: URL: https://github.com/apache/doris/pull/65126#discussion_r3541940592
########## fe/fe-core/src/main/java/org/apache/doris/datasource/metacache/NameCacheValue.java: ########## @@ -0,0 +1,113 @@ +// 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 org.apache.doris.datasource.metacache; + +import org.apache.doris.common.Pair; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; + +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * Immutable snapshot of remote/local names and the case-insensitive remote-name index. + */ +public final class NameCacheValue { + private final ImmutableList<Pair<String, String>> names; + private final ImmutableMap<String, String> lowerCaseToRemoteName; + + private NameCacheValue(List<Pair<String, String>> names) { + // Deep-copy each pair so callers cannot mutate the snapshot through reused Pair instances. + this.names = ImmutableList.copyOf(copyPairs(names)); + // Build the lower-case index with last-write-wins semantics so the snapshot does not + // introduce case-conflict validation beyond what the catalog-aware loader already enforces. + Map<String, String> indexBuilder = new java.util.HashMap<>(); + for (Pair<String, String> pair : this.names) { + indexBuilder.put(pair.key().toLowerCase(Locale.ROOT), pair.key()); + } + lowerCaseToRemoteName = ImmutableMap.copyOf(indexBuilder); + } + + public static NameCacheValue of(List<Pair<String, String>> names) { + return new NameCacheValue(Objects.requireNonNull(names, "names can not be null")); + } + + public static NameCacheValue empty() { + return of(ImmutableList.of()); + } + + public List<Pair<String, String>> names() { + // Return fresh Pair objects even though the list itself is immutable, so callers cannot + // mutate the published snapshot through reused Pair instances. + return ImmutableList.copyOf(copyPairs(names)); + } + + public String remoteNameOfLocalName(String localName) { + for (Pair<String, String> pair : names) { + if (pair.value().equals(localName)) { + return pair.key(); + } + } Review Comment: Thank you, this suggestion makes good sense. I went ahead and optimized this in the PR. `NameCacheValue` now precomputes immutable local-name indexes, so `remoteNameOfLocalName()` uses a `localName -> remoteName` map and `containsLocalName()` uses a local-name set. That removes the linear scan from these hot-path lookups while keeping the snapshot immutable. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
