-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityInstanceLoader.java
156 lines (135 loc) · 6.37 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright (C) 2020 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 cz.cvut.kbss.ontodriver.model.NamedResource;
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;
final CacheManager cache;
private final AxiomDescriptorFactory descriptorFactory;
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);
/**
* Loads entity reference.
* <p>
* I.e., the object may contain only the identifier, other attributes could be loaded lazily. However, if a
* corresponding entity is already cached, it can be returned instead.
*
* @param loadingParameters Reference loading parameters. Note that cache bypassing and forced eager loading
* configuration is ignored
* @param <T> Entity type
* @return Loaded entity reference, possibly {@code null}
*/
abstract <T> T loadReference(LoadingParameters<T> loadingParameters);
<U extends T, T> U loadInstance(LoadingParameters<T> loadingParameters, EntityType<U> et) {
final URI identifier = loadingParameters.getIdentifier();
final Descriptor descriptor = loadingParameters.getDescriptor();
if (isCached(loadingParameters, et)) {
return loadCached(et, 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);
}
}
<T> boolean isCached(LoadingParameters<T> loadingParameters, EntityType<? extends T> et) {
return !loadingParameters.shouldBypassCache() &&
cache.contains(et.getJavaType(), loadingParameters.getIdentifier(), loadingParameters.getDescriptor());
}
<T> T loadCached(EntityType<T> et, URI identifier, Descriptor descriptor) {
final T cached = cache.get(et.getJavaType(), identifier, descriptor);
entityBuilder.populateQueryAttributes(cached, et);
return cached;
}
<T> T loadReferenceInstance(LoadingParameters<T> loadingParameters, EntityType<? extends T> et) {
final URI identifier = loadingParameters.getIdentifier();
final Axiom<NamedResource> typeAxiom = descriptorFactory.createForReferenceLoading(identifier, et);
try {
final boolean contains =
storageConnection.contains(typeAxiom, loadingParameters.getDescriptor().getContexts());
return contains ? entityBuilder.createEntityInstance(identifier, et) : null;
} catch (OntoDriverException e) {
throw new StorageAccessException(e);
} catch (InstantiationException | IllegalAccessException e) {
throw new EntityReconstructionException(e);
}
}
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();
}
}