刘光辉
15 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package jnpf.bizcommon.audit.service.support;
 
import java.util.Locale;
 
/** 为同名字段附加物理表作用域的可持久化复合键。 */
public final class AuditDiffKey {
 
    private static final String SCOPED_PREFIX = "@table:";
 
    private AuditDiffKey() {
    }
 
    /**
     * 长度前缀让表名和字段名的边界无歧义,同时只使用 PostgreSQL JSONB 可接受的可打印字符。
     */
    public static String of(String targetTable, String field) {
        String normalizedField = normalize(field);
        String normalizedTable = normalize(targetTable);
        if (normalizedTable == null) {
            return normalizedField == null ? "" : normalizedField;
        }
        String effectiveField = normalizedField == null ? "" : normalizedField;
        return SCOPED_PREFIX + normalizedTable.length() + ":" + normalizedTable + ":" + effectiveField;
    }
 
    public static boolean isScoped(String key) {
        if (key == null || !key.startsWith(SCOPED_PREFIX)) {
            return false;
        }
        int lengthEnd = key.indexOf(':', SCOPED_PREFIX.length());
        if (lengthEnd < 0) {
            return false;
        }
        try {
            int tableLength = Integer.parseInt(key.substring(SCOPED_PREFIX.length(), lengthEnd));
            long fieldSeparator = (long) lengthEnd + 1L + tableLength;
            return tableLength >= 0 && fieldSeparator < key.length()
                    && key.charAt((int) fieldSeparator) == ':';
        } catch (NumberFormatException ignored) {
            return false;
        }
    }
 
    /** 只用于兼容已加载到内存的旧键;返回值不会再携带旧 NUL 分隔符。 */
    public static String normalizeStored(String key) {
        if (key == null) {
            return null;
        }
        int separator = key.indexOf(Character.MIN_VALUE);
        if (separator < 0) {
            return key;
        }
        return of(key.substring(0, separator), key.substring(separator + 1));
    }
 
    private static String normalize(String value) {
        if (value == null) {
            return null;
        }
        String normalized = value.trim().toLowerCase(Locale.ROOT);
        return normalized.isEmpty() ? null : normalized;
    }
}