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

Hyshmily / hotkey / 28848089351

07 Jul 2026 07:05AM UTC coverage: 90.138% (-0.3%) from 90.477%
28848089351

push

github

Hyshmily
refactor and test: refactor Lz4CacheCompressor and fix the test

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

1315 of 1527 branches covered (86.12%)

Branch coverage included in aggregate %.

17 of 18 new or added lines in 1 file covered. (94.44%)

19 existing lines in 1 file now uncovered.

3721 of 4060 relevant lines covered (91.65%)

4.23 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 static org.apache.lucene.util.RamUsageEstimator.NUM_BYTES_OBJECT_REF;
19
import static org.apache.lucene.util.RamUsageEstimator.shallowSizeOf;
20

21
import com.github.benmanes.caffeine.cache.Weigher;
22
import io.github.hyshmily.hotkey.cache.annotationsupporter.NullValue;
23
import io.github.hyshmily.hotkey.model.CacheEntry;
24
import java.util.Collection;
25
import java.util.Map;
26
import org.jspecify.annotations.NonNull;
27

28
/**
29
 * Heap-weight estimator for {@link CacheEntry} values backed by Lucene's {@link
30
 * org.apache.lucene.util.RamUsageEstimator}.
31
 *
32
 * <p>Used when {@code hotkey.local.cache.max-weight} is set. Delegates object header / field /
33
 * alignment calculations to Lucene's {@code RamUsageEstimator} (which auto-detects compressed
34
 * OOPs, object alignment, and JVM pointer sizes), then adds conservative estimates for
35
 * variable-length data and Caffeine internal metadata.
36
 */
UNCOV
37
@SuppressWarnings("all")
×
38
public enum DefaultWeigher implements Weigher<String, Object> {
UNCOV
39
  INSTANCE;
×
40

41
  private static final int ENTRY_OVERHEAD = 512;
42
  private static final int COLLECTION_ELEMENT_WEIGHT = 200;
43
  private static final int MAP_ENTRY_WEIGHT = 350;
44

45
  @Override
46
  public int weigh(@NonNull String key, @NonNull Object value) {
UNCOV
47
    long keyWeight = shallowSizeOf(key) + ((long) key.length() << 1);
×
UNCOV
48
    long total = keyWeight + valueOf(value) + ENTRY_OVERHEAD;
×
UNCOV
49
    return (int) Math.min(total, Integer.MAX_VALUE);
×
50
  }
51

52
  private static long valueOf(Object v) {
53
    if (v instanceof CacheEntry ce) {
×
UNCOV
54
      return shallowSizeOf(ce) + valueOf(ce.getValue());
×
55
    }
UNCOV
56
    if (v instanceof NullValue) {
×
UNCOV
57
      return shallowSizeOf(v);
×
58
    }
UNCOV
59
    if (v instanceof String s) {
×
UNCOV
60
      return shallowSizeOf(s) + ((long) s.length() << 1);
×
61
    }
UNCOV
62
    if (v instanceof byte[] b) {
×
UNCOV
63
      return shallowSizeOf(b);
×
64
    }
UNCOV
65
    if (v instanceof Collection<?> c) {
×
UNCOV
66
      return shallowSizeOf(c) + (long) Math.max(1, c.size()) * COLLECTION_ELEMENT_WEIGHT;
×
67
    }
UNCOV
68
    if (v instanceof Map<?, ?> m) {
×
UNCOV
69
      return shallowSizeOf(m) + (long) Math.max(1, m.size()) * MAP_ENTRY_WEIGHT;
×
70
    }
UNCOV
71
    if (v instanceof Object[] a) {
×
UNCOV
72
      return shallowSizeOf(a) + (long) a.length * NUM_BYTES_OBJECT_REF;
×
73
    }
UNCOV
74
    return shallowSizeOf(v) + 1024;
×
75
  }
76
}
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