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;
|
});
|
}
|
}
|