-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityInstanceLoader.java
123 lines (105 loc) · 4.83 KB
/
EntityInstanceLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.oom;
import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
import cz.cvut.kbss.jopa.model.MetamodelImpl;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.metamodel.EntityType;
import cz.cvut.kbss.jopa.oom.exceptions.EntityReconstructionException;
import cz.cvut.kbss.jopa.sessions.CacheManager;
import cz.cvut.kbss.jopa.sessions.LoadingParameters;
import cz.cvut.kbss.ontodriver.Connection;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.model.Axiom;
import java.net.URI;
import java.util.Collection;
import java.util.Objects;
/**
* Root of the entity loading strategies.
*/
abstract class EntityInstanceLoader {
final Connection storageConnection;
final MetamodelImpl metamodel;
private final CacheManager cache;
private final AxiomDescriptorFactory descriptorFactory;
private final EntityConstructor entityBuilder;
EntityInstanceLoader(EntityInstanceLoaderBuilder builder) {
assert builder.storageConnection != null;
assert builder.metamodel != null;
assert builder.cache != null;
assert builder.descriptorFactory != null;
assert builder.entityBuilder != null;
this.storageConnection = builder.storageConnection;
this.metamodel = builder.metamodel;
this.cache = builder.cache;
this.descriptorFactory = builder.descriptorFactory;
this.entityBuilder = builder.entityBuilder;
}
/**
* Loads entity based on the specified loading parameters.
*
* @param loadingParameters Instance loading parameters
* @return The loaded instance (possibly {@code null})
*/
abstract <T> T loadEntity(LoadingParameters<T> loadingParameters);
<T> T loadInstance(LoadingParameters<T> loadingParameters, EntityType<? extends T> et) {
final URI identifier = loadingParameters.getIdentifier();
final Descriptor descriptor = loadingParameters.getDescriptor();
if (isCached(loadingParameters, et, descriptor)) {
return cache.get(et.getJavaType(), identifier, descriptor);
}
final AxiomDescriptor axiomDescriptor = descriptorFactory.createForEntityLoading(loadingParameters, et);
try {
final Collection<Axiom<?>> axioms = storageConnection.find(axiomDescriptor);
return axioms.isEmpty() ? null : entityBuilder.reconstructEntity(identifier, et, descriptor, axioms);
} catch (OntoDriverException e) {
throw new StorageAccessException(e);
} catch (InstantiationException | IllegalAccessException e) {
throw new EntityReconstructionException(e);
}
}
private <T> boolean isCached(LoadingParameters<T> loadingParameters, EntityType<? extends T> et,
Descriptor descriptor) {
return !loadingParameters.shouldBypassCache() &&
cache.contains(et.getJavaType(), loadingParameters.getIdentifier(), descriptor);
}
abstract static class EntityInstanceLoaderBuilder {
private Connection storageConnection;
private MetamodelImpl metamodel;
private CacheManager cache;
private AxiomDescriptorFactory descriptorFactory;
private EntityConstructor entityBuilder;
EntityInstanceLoaderBuilder connection(Connection connection) {
this.storageConnection = Objects.requireNonNull(connection);
return this;
}
EntityInstanceLoaderBuilder metamodel(MetamodelImpl metamodel) {
this.metamodel = metamodel;
return this;
}
EntityInstanceLoaderBuilder descriptorFactory(AxiomDescriptorFactory factory) {
this.descriptorFactory = factory;
return this;
}
EntityInstanceLoaderBuilder entityBuilder(EntityConstructor builder) {
this.entityBuilder = builder;
return this;
}
EntityInstanceLoaderBuilder cache(CacheManager cache) {
this.cache = cache;
return this;
}
abstract EntityInstanceLoader build();
}
}