package org.apache.seatunnel.connectors.seatunnel.rocksdb.util;

import java.util.concurrent.ConcurrentHashMap;

public class Singleton {
    private final ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
    private volatile static Singleton singleton;  //1:volatile修饰

    private Singleton() {
    }
    public static Singleton getSingleton() {
        if (singleton == null) {  //2:减少不要同步，优化性能
            synchronized (Singleton.class) {  // 3：同步，线程安全
                if (singleton == null) {
                    singleton = new Singleton();  //4：创建singleton 对象
                }
            }
        }
        return singleton;
    }



    public Integer getCount(String key) {
        Integer integer = map.get(key);
        if (integer==null){
            return 0;
        }
        return integer;

    }

    public void inc(String key) {
        map.put(key, map.computeIfAbsent(key, k -> 0) + 1);
    }

    public void remove(String key) {
        map.remove(key);
    }
    private static class Inner {
        private static final Singleton instance = new Singleton();
    }
}

