001/**
002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.gnu.org/licenses/lgpl-3.0.txt
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package dev.tinyflow.core.chain.repository;
017
018import dev.tinyflow.core.chain.ChainState;
019import dev.tinyflow.core.util.MapUtil;
020
021import java.util.EnumSet;
022import java.util.Map;
023import java.util.concurrent.ConcurrentHashMap;
024
025public class InMemoryChainStateRepository implements ChainStateRepository {
026    private static final Map<String, ChainState> chainStateMap = new ConcurrentHashMap<>();
027
028    @Override
029    public ChainState load(String instanceId) {
030        return MapUtil.computeIfAbsent(chainStateMap, instanceId, k -> {
031            ChainState state = new ChainState();
032            state.setInstanceId(instanceId);
033            return state;
034        });
035    }
036
037    @Override
038    public boolean tryUpdate(ChainState chainState, EnumSet<ChainStateField> fields) {
039        chainStateMap.put(chainState.getInstanceId(), chainState);
040        return true;
041    }
042}