All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hundsun.lightdb.unisql.utils.SqlSequenceUtils Maven / Gradle / Ivy

There is a newer version: 24.1.7.0-beta-2
Show newest version
package com.hundsun.lightdb.unisql.utils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: sql-convert-runtime
 * @description: 获取sql执行顺序的工具类
 * @author: liangdong30629
 * @date: 2024-06-12 14:00
 **/
public class SqlSequenceUtils {
    /**
     * 这是一个静态缓存,用于维护trace_id内sql顺序号(从1开始)
     *
     * @see {@link com.google.common.cache.LoadingCache}
     */
    private static final LoadingCache SQL_SEQUENCE_LOCAL_CACHE = CacheBuilder.newBuilder()
            .maximumSize(10000)
            .build(new CacheLoader() {

                @Override
                public AtomicInteger load(String key) throws Exception {
                    return new AtomicInteger(1);
                }
            });

    /**
     * 在同一个trace_id内下一个sql执行顺序号
     *
     * @param traceId 全链路日志id
     * @return 下一个sql执行顺序号
     * @throws RuntimeException 如果获取序列号时发生异常
     * @see {@link SqlSequenceUtils#SQL_SEQUENCE_LOCAL_CACHE}
     */
    public static int nextSequenceId(String traceId) {
        try {
            return SQL_SEQUENCE_LOCAL_CACHE.get(traceId).getAndIncrement();
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy