Skip to content

Package: EntityInstanceLoader$EntityInstanceLoaderBuilder

EntityInstanceLoader$EntityInstanceLoaderBuilder

nameinstructionbranchcomplexitylinemethod
EntityInstanceLoader.EntityInstanceLoaderBuilder()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
cache(CacheManager)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
connection(Connection)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
descriptorFactory(AxiomDescriptorFactory)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
entityBuilder(EntityConstructor)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
metamodel(MetamodelImpl)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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.oom;
16:
17: import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
18: import cz.cvut.kbss.jopa.model.MetamodelImpl;
19: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
20: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
21: import cz.cvut.kbss.jopa.oom.exceptions.EntityReconstructionException;
22: import cz.cvut.kbss.jopa.sessions.CacheManager;
23: import cz.cvut.kbss.jopa.sessions.LoadingParameters;
24: import cz.cvut.kbss.ontodriver.Connection;
25: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
26: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
27: import cz.cvut.kbss.ontodriver.model.Axiom;
28: import cz.cvut.kbss.ontodriver.model.NamedResource;
29:
30: import java.net.URI;
31: import java.util.Collection;
32: import java.util.Objects;
33:
34: /**
35: * Root of the entity loading strategies.
36: */
37: abstract class EntityInstanceLoader {
38:
39: final Connection storageConnection;
40: final MetamodelImpl metamodel;
41:
42: final CacheManager cache;
43: private final AxiomDescriptorFactory descriptorFactory;
44: final EntityConstructor entityBuilder;
45:
46: EntityInstanceLoader(EntityInstanceLoaderBuilder builder) {
47: assert builder.storageConnection != null;
48: assert builder.metamodel != null;
49: assert builder.cache != null;
50: assert builder.descriptorFactory != null;
51: assert builder.entityBuilder != null;
52:
53: this.storageConnection = builder.storageConnection;
54: this.metamodel = builder.metamodel;
55: this.cache = builder.cache;
56: this.descriptorFactory = builder.descriptorFactory;
57: this.entityBuilder = builder.entityBuilder;
58: }
59:
60: /**
61: * Loads entity based on the specified loading parameters.
62: *
63: * @param loadingParameters Instance loading parameters
64: * @return The loaded instance (possibly {@code null})
65: */
66: abstract <T> T loadEntity(LoadingParameters<T> loadingParameters);
67:
68: /**
69: * Loads entity reference.
70: * <p>
71: * I.e., the object may contain only the identifier, other attributes could be loaded lazily. However, if a
72: * corresponding entity is already cached, it can be returned instead.
73: *
74: * @param loadingParameters Reference loading parameters. Note that cache bypassing and forced eager loading
75: * configuration is ignored
76: * @param <T> Entity type
77: * @return Loaded entity reference, possibly {@code null}
78: */
79: abstract <T> T loadReference(LoadingParameters<T> loadingParameters);
80:
81: <T> T loadInstance(LoadingParameters<T> loadingParameters, EntityType<? extends T> et) {
82: final URI identifier = loadingParameters.getIdentifier();
83: final Descriptor descriptor = loadingParameters.getDescriptor();
84: if (isCached(loadingParameters, et)) {
85: return cache.get(et.getJavaType(), identifier, descriptor);
86: }
87: final AxiomDescriptor axiomDescriptor = descriptorFactory.createForEntityLoading(loadingParameters, et);
88: try {
89: final Collection<Axiom<?>> axioms = storageConnection.find(axiomDescriptor);
90: return axioms.isEmpty() ? null : entityBuilder.reconstructEntity(identifier, et, descriptor, axioms);
91: } catch (OntoDriverException e) {
92: throw new StorageAccessException(e);
93: } catch (InstantiationException | IllegalAccessException e) {
94: throw new EntityReconstructionException(e);
95: }
96: }
97:
98: <T> boolean isCached(LoadingParameters<T> loadingParameters, EntityType<? extends T> et) {
99: return !loadingParameters.shouldBypassCache() &&
100: cache.contains(et.getJavaType(), loadingParameters.getIdentifier(), loadingParameters.getDescriptor());
101: }
102:
103: <T> T loadReferenceInstance(LoadingParameters<T> loadingParameters, EntityType<? extends T> et) {
104: final URI identifier = loadingParameters.getIdentifier();
105: final Axiom<NamedResource> typeAxiom = descriptorFactory.createForReferenceLoading(identifier, et);
106: try {
107: final boolean contains =
108: storageConnection.contains(typeAxiom, loadingParameters.getDescriptor().getContext());
109: return contains ? entityBuilder.createEntityInstance(identifier, et) : null;
110: } catch (OntoDriverException e) {
111: throw new StorageAccessException(e);
112: } catch (InstantiationException | IllegalAccessException e) {
113: throw new EntityReconstructionException(e);
114: }
115: }
116:
117: abstract static class EntityInstanceLoaderBuilder {
118: private Connection storageConnection;
119: private MetamodelImpl metamodel;
120: private CacheManager cache;
121:
122: private AxiomDescriptorFactory descriptorFactory;
123: private EntityConstructor entityBuilder;
124:
125: EntityInstanceLoaderBuilder connection(Connection connection) {
126: this.storageConnection = Objects.requireNonNull(connection);
127: return this;
128: }
129:
130: EntityInstanceLoaderBuilder metamodel(MetamodelImpl metamodel) {
131: this.metamodel = metamodel;
132: return this;
133: }
134:
135: EntityInstanceLoaderBuilder descriptorFactory(AxiomDescriptorFactory factory) {
136: this.descriptorFactory = factory;
137: return this;
138: }
139:
140: EntityInstanceLoaderBuilder entityBuilder(EntityConstructor builder) {
141: this.entityBuilder = builder;
142: return this;
143: }
144:
145: EntityInstanceLoaderBuilder cache(CacheManager cache) {
146: this.cache = cache;
147: return this;
148: }
149:
150: abstract EntityInstanceLoader build();
151: }
152: }