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

mybatis / mybatis-3 / 2520

31 Dec 2024 02:20AM UTC coverage: 87.301% (-0.009%) from 87.31%
2520

push

github

web-flow
Merge pull request #3354 from hazendaz/next

Add comments to unused fields in tests (indirectly required) and remove unused field

3521 of 4267 branches covered (82.52%)

9377 of 10741 relevant lines covered (87.3%)

0.87 hits per line

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

90.48
/src/main/java/org/apache/ibatis/cache/decorators/WeakCache.java
1
/*
2
 *    Copyright 2009-2024 the original author or authors.
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
 *       https://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 org.apache.ibatis.cache.decorators;
17

18
import java.lang.ref.ReferenceQueue;
19
import java.lang.ref.WeakReference;
20
import java.util.Deque;
21
import java.util.LinkedList;
22
import java.util.concurrent.locks.ReentrantLock;
23

24
import org.apache.ibatis.cache.Cache;
25

26
/**
27
 * Weak Reference cache decorator.
28
 * <p>
29
 * Thanks to Dr. Heinz Kabutz for his guidance here.
30
 *
31
 * @author Clinton Begin
32
 */
33
public class WeakCache implements Cache {
34
  private final Deque<Object> hardLinksToAvoidGarbageCollection;
35
  private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
36
  private final Cache delegate;
37
  private int numberOfHardLinks;
38
  private final ReentrantLock lock = new ReentrantLock();
1✔
39

40
  public WeakCache(Cache delegate) {
1✔
41
    this.delegate = delegate;
1✔
42
    this.numberOfHardLinks = 256;
1✔
43
    this.hardLinksToAvoidGarbageCollection = new LinkedList<>();
1✔
44
    this.queueOfGarbageCollectedEntries = new ReferenceQueue<>();
1✔
45
  }
1✔
46

47
  @Override
48
  public String getId() {
49
    return delegate.getId();
×
50
  }
51

52
  @Override
53
  public int getSize() {
54
    removeGarbageCollectedItems();
1✔
55
    return delegate.getSize();
1✔
56
  }
57

58
  public void setSize(int size) {
59
    this.numberOfHardLinks = size;
×
60
  }
×
61

62
  @Override
63
  public void putObject(Object key, Object value) {
64
    removeGarbageCollectedItems();
1✔
65
    delegate.putObject(key, new WeakEntry(key, value, queueOfGarbageCollectedEntries));
1✔
66
  }
1✔
67

68
  @Override
69
  public Object getObject(Object key) {
70
    Object result = null;
1✔
71
    @SuppressWarnings("unchecked") // assumed delegate cache is totally managed by this cache
72
    WeakReference<Object> weakReference = (WeakReference<Object>) delegate.getObject(key);
1✔
73
    if (weakReference != null) {
1✔
74
      result = weakReference.get();
1✔
75
      if (result == null) {
1!
76
        delegate.removeObject(key);
×
77
      } else {
78
        lock.lock();
1✔
79
        try {
80
          hardLinksToAvoidGarbageCollection.addFirst(result);
1✔
81
          if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
1✔
82
            hardLinksToAvoidGarbageCollection.removeLast();
1✔
83
          }
84
        } finally {
85
          lock.unlock();
1✔
86
        }
87
      }
88
    }
89
    return result;
1✔
90
  }
91

92
  @Override
93
  public Object removeObject(Object key) {
94
    removeGarbageCollectedItems();
1✔
95
    @SuppressWarnings("unchecked")
96
    WeakReference<Object> weakReference = (WeakReference<Object>) delegate.removeObject(key);
1✔
97
    return weakReference == null ? null : weakReference.get();
1!
98
  }
99

100
  @Override
101
  public void clear() {
102
    lock.lock();
1✔
103
    try {
104
      hardLinksToAvoidGarbageCollection.clear();
1✔
105
    } finally {
106
      lock.unlock();
1✔
107
    }
108
    removeGarbageCollectedItems();
1✔
109
    delegate.clear();
1✔
110
  }
1✔
111

112
  private void removeGarbageCollectedItems() {
113
    WeakEntry sv;
114
    while ((sv = (WeakEntry) queueOfGarbageCollectedEntries.poll()) != null) {
1✔
115
      delegate.removeObject(sv.key);
1✔
116
    }
117
  }
1✔
118

119
  private static class WeakEntry extends WeakReference<Object> {
120
    private final Object key;
121

122
    private WeakEntry(Object key, Object value, ReferenceQueue<Object> garbageCollectionQueue) {
123
      super(value, garbageCollectionQueue);
1✔
124
      this.key = key;
1✔
125
    }
1✔
126
  }
127

128
}
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

© 2025 Coveralls, Inc