• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Hyshmily / hotkey / 28835396755

07 Jul 2026 01:38AM UTC coverage: 89.623% (-0.7%) from 90.336%
28835396755

push

github

Hyshmily
fix and feat : fix known bugs and accept lz4 to wrap value for smaller memory,"wrap key" needs to consider

Signed-off-by: Hyshmily <cxm8607@outlook.com>

1325 of 1552 branches covered (85.37%)

Branch coverage included in aggregate %.

167 of 236 new or added lines in 12 files covered. (70.76%)

3745 of 4105 relevant lines covered (91.23%)

4.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/common/src/main/java/io/github/hyshmily/hotkey/cache/codec/DefaultWeigher.java
1
/*
2
 * Copyright 2026 Hyshmily. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.hyshmily.hotkey.cache.codec;
17

18
import com.github.benmanes.caffeine.cache.Weigher;
19
import io.github.hyshmily.hotkey.model.CacheEntry;
20
import org.jspecify.annotations.NonNull;
21

22
/**
23
 * Rough heap-weight estimator for {@link CacheEntry} values.
24
 *
25
 * <p>Used when {@code hotkey.local.cache.max-weight} is set. {@code String} values
26
 * are weighted by their UTF‑8 byte length (estimated without allocation).
27
 * Raw {@code byte[]} values are weighted by their length. All other types get
28
 * a flat {@code 1024} byte estimate. The {@link CacheEntry} wrapper overhead
29
 * (~200 bytes) is added on top.
30
 */
31
@SuppressWarnings("all")
32
public final class DefaultWeigher implements Weigher<String, Object> {
33

NEW
34
  public static final DefaultWeigher INSTANCE = new DefaultWeigher();
×
35
  private static final int ENTRY_OVERHEAD = 200;
36

37
  private DefaultWeigher() {}
38

39
  @Override
40
  public int weigh(String key, @NonNull Object value) {
NEW
41
    int valueWeight = valueOf(value);
×
NEW
42
    return (key.length() << 1) + valueWeight + ENTRY_OVERHEAD;
×
43
  }
44

45
  private static int valueOf(Object v) {
NEW
46
    if (v instanceof String s) return utf8Length(s);
×
NEW
47
    if (v instanceof byte[] b) return b.length;
×
NEW
48
    if (v instanceof CacheEntry ce) return valueOf(ce.getValue());
×
NEW
49
    return 1024;
×
50
  }
51

52
  /**
53
   * Estimates the number of bytes a {@link String} would occupy when
54
   * encoded in UTF‑8, without actually allocating a byte array.
55
   */
56
  private static int utf8Length(String s) {
NEW
57
    if (isAscii(s)) {
×
NEW
58
      return s.length();
×
59
    }
NEW
60
    int length = 0;
×
NEW
61
    final int len = s.length();
×
NEW
62
    for (int i = 0; i < len; i++) {
×
NEW
63
      char c = s.charAt(i);
×
NEW
64
      if (c < 0x80) {
×
NEW
65
        length++;
×
NEW
66
      } else if (c < 0x800) {
×
NEW
67
        length += 2;
×
NEW
68
      } else if (Character.isSurrogate(c)) {
×
NEW
69
        length += 4;
×
NEW
70
        i++;
×
71
      } else {
NEW
72
        length += 3;
×
73
      }
74
    }
NEW
75
    return length;
×
76
  }
77

78
  private static boolean isAscii(String s) {
NEW
79
    for (int i = 0; i < s.length(); i++) {
×
NEW
80
      if (s.charAt(i) > 0x7F) {
×
NEW
81
        return false;
×
82
      }
83
    }
NEW
84
    return true;
×
85
  }
86
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc