zhou zhou
12 小时以前 25f0001a7e76d0565fa9de0651f1177b9f61472f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.vincent.rsf.server.manager.partition;
 
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
 
/**
 * Thread-local table routing context for ASN history log tables.
 */
public final class AsnLogTableRoutingContext {
 
    private static final ThreadLocal<Map<String, String>> ROUTES = ThreadLocal.withInitial(HashMap::new);
 
    private AsnLogTableRoutingContext() {
    }
 
    public static String getTable(String logicalTable) {
        return ROUTES.get().get(logicalTable);
    }
 
    public static <T> T withTable(String logicalTable, String actualTable, Supplier<T> supplier) {
        Map<String, String> routes = ROUTES.get();
        String previous = routes.put(logicalTable, actualTable);
        try {
            return supplier.get();
        } finally {
            if (previous == null) {
                routes.remove(logicalTable);
                if (routes.isEmpty()) {
                    ROUTES.remove();
                }
            } else {
                routes.put(logicalTable, previous);
            }
        }
    }
 
    public static void withTable(String logicalTable, String actualTable, Runnable runnable) {
        withTable(logicalTable, actualTable, () -> {
            runnable.run();
            return null;
        });
    }
}