This is an automated email from the ASF dual-hosted git repository. haonan pushed a commit to branch devicePathCache in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit c0216c71ec543929f514641a081834ba296f912d Author: HTHou <[email protected]> AuthorDate: Wed Apr 19 10:35:42 2023 +0800 Add a DevicePath cache to avoid init duplicated partial paths --- .../db/metadata/cache/DataNodeDevicePathCache.java | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/cache/DataNodeDevicePathCache.java b/server/src/main/java/org/apache/iotdb/db/metadata/cache/DataNodeDevicePathCache.java new file mode 100644 index 0000000000..0649c27b5b --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/metadata/cache/DataNodeDevicePathCache.java @@ -0,0 +1,65 @@ +/* + * 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.iotdb.db.metadata.cache; + +import org.apache.iotdb.commons.exception.IllegalPathException; +import org.apache.iotdb.commons.path.PartialPath; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** This class is for reducing duplicated Device PartialPath splits in write process. */ +public class DataNodeDevicePathCache { + + private final Map<String, PartialPath> devicePathCache; + + private DataNodeDevicePathCache() { + devicePathCache = new ConcurrentHashMap<>(); + } + + public static DataNodeDevicePathCache getInstance() { + return DataNodeDevicePathCache.DataNodeDevicePathCacheHolder.INSTANCE; + } + + /** singleton pattern. */ + private static class DataNodeDevicePathCacheHolder { + private static final DataNodeDevicePathCache INSTANCE = new DataNodeDevicePathCache(); + } + + public PartialPath get(String deviceId) throws IllegalPathException { + try { + return devicePathCache.computeIfAbsent( + deviceId, + path -> { + try { + return new PartialPath(deviceId); + } catch (IllegalPathException e) { + throw new IllegalArgumentException(e); + } + }); + } catch (IllegalArgumentException e) { + throw new IllegalPathException(deviceId); + } + } + + public void cleanUp() { + devicePathCache.clear(); + } +}
