Skip to content

Package: EntityCache

EntityCache

nameinstructionbranchcomplexitylinemethod
EntityCache()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
contains(Class, Object)
M: 8 C: 16
67%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 4
100%
M: 0 C: 1
100%
contains(Class, Object, URI)
M: 8 C: 22
73%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
evict(Class)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
evict(Class, Object, URI)
M: 8 C: 23
74%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
evict(URI)
M: 3 C: 15
83%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
get(Class, Object, URI)
M: 10 C: 23
70%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 0 C: 5
100%
M: 0 C: 1
100%
getMapForId(URI, Object)
M: 8 C: 25
76%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$evict$0(Class, Object, Map)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
put(Object, Object, URI)
M: 8 C: 63
89%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 0 C: 14
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.cache;
16:
17: import java.net.URI;
18: import java.util.Collections;
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: class EntityCache {
23:
24: // TODO Think about locking on context level, so that the whole cache doesn't have to be locked when being accessed
25:
26: private static final String DEFAULT_CONTEXT_BASE = "http://defaultContext";
27:
28: final Map<URI, Map<Object, Map<Class<?>, Object>>> repoCache;
29: final URI defaultContext;
30:
31: EntityCache() {
32: repoCache = new HashMap<>();
33: this.defaultContext = URI.create(DEFAULT_CONTEXT_BASE + System.currentTimeMillis());
34: }
35:
36: void put(Object identifier, Object entity, URI context) {
37:• assert identifier != null;
38:• assert entity != null;
39:
40: final Class<?> cls = entity.getClass();
41:• final URI ctx = context != null ? context : defaultContext;
42:
43: Map<Object, Map<Class<?>, Object>> ctxMap;
44:• if (!repoCache.containsKey(ctx)) {
45: ctxMap = new HashMap<>();
46: repoCache.put(ctx, ctxMap);
47: } else {
48: ctxMap = repoCache.get(ctx);
49: }
50: Map<Class<?>, Object> individualMap;
51:• if (!ctxMap.containsKey(identifier)) {
52: individualMap = new HashMap<>();
53: ctxMap.put(identifier, individualMap);
54: } else {
55: individualMap = ctxMap.get(identifier);
56: }
57: individualMap.put(cls, entity);
58: }
59:
60: <T> T get(Class<T> cls, Object identifier, URI context) {
61:• assert cls != null;
62:• assert identifier != null;
63:
64:• final URI ctx = context != null ? context : defaultContext;
65: final Map<Class<?>, Object> m = getMapForId(ctx, identifier);
66: return cls.cast(m.getOrDefault(cls, null));
67: }
68:
69: boolean contains(Class<?> cls, Object identifier) {
70:• assert cls != null;
71:• assert identifier != null;
72: final Map<Class<?>, Object> m = getMapForId(defaultContext, identifier);
73: return m.containsKey(cls);
74: }
75:
76: boolean contains(Class<?> cls, Object identifier, URI context) {
77:• assert cls != null;
78:• assert identifier != null;
79:• if (context == null) {
80: return contains(cls, identifier);
81: }
82:
83: final Map<Class<?>, Object> m = getMapForId(context, identifier);
84: return m.containsKey(cls);
85: }
86:
87: void evict(Class<?> cls, Object identifier, URI context) {
88:• assert cls != null;
89:• assert identifier != null;
90:
91:• final URI ctx = context != null ? context : defaultContext;
92: final Map<Class<?>, Object> m = getMapForId(ctx, identifier);
93: m.remove(cls);
94: }
95:
96: void evict(URI context) {
97:• if (context == null) {
98: context = defaultContext;
99: }
100:• if (!repoCache.containsKey(context)) {
101: return;
102: }
103: repoCache.get(context).clear();
104: }
105:
106: void evict(Class<?> cls) {
107:• for (Map.Entry<URI, Map<Object, Map<Class<?>, Object>>> e : repoCache.entrySet()) {
108: final Map<Object, Map<Class<?>, Object>> m = e.getValue();
109: m.forEach((key, value) -> value.remove(cls));
110: }
111: }
112:
113: private Map<Class<?>, Object> getMapForId(URI context, Object identifier) {
114:• assert context != null;
115:• assert identifier != null;
116:
117:• if (!repoCache.containsKey(context)) {
118: return Collections.emptyMap();
119: }
120: final Map<Object, Map<Class<?>, Object>> ctxMap = repoCache.get(context);
121: return ctxMap.getOrDefault(identifier, Collections.emptyMap());
122: }
123: }