Skip to content

Commit aa6b1ad

Browse files
committed
Few improvements for OntologyLoader
1 parent be4f5d8 commit aa6b1ad

2 files changed

Lines changed: 38 additions & 64 deletions

File tree

src/ubic/basecode/ontology/jena/AbstractOntologyService.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,21 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
102102
return;
103103
}
104104

105+
String ontologyUrl = getOntologyUrl();
106+
String ontologyName = getOntologyName();
107+
String cacheName = getCacheName();
108+
105109
boolean loadOntology = isEnabled();
106110

107111
// If loading ontologies is disabled in the configuration, return
108112
if ( !forceLoad && !loadOntology ) {
109113
log.debug( "Loading {} is disabled (force=false, Configuration load.{}=false)",
110-
this, getOntologyName() );
114+
this, ontologyName );
111115
return;
112116
}
113117

114118
// Detect configuration problems.
115-
if ( StringUtils.isBlank( this.getOntologyUrl() ) ) {
119+
if ( StringUtils.isBlank( ontologyUrl ) ) {
116120
throw new IllegalStateException( "URL not defined for %s: ontology cannot be loaded. (" + this + ")" );
117121
}
118122

@@ -145,8 +149,8 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
145149
.toSet();
146150

147151
//Checks if the current ontology has changed since it was last loaded.
148-
boolean changed = OntologyLoader.hasChanged( getCacheName() );
149-
boolean indexExists = OntologyIndexer.getSubjectIndex( getCacheName() ) != null;
152+
boolean changed = OntologyLoader.hasChanged( cacheName );
153+
boolean indexExists = OntologyIndexer.getSubjectIndex( cacheName ) != null;
150154
boolean forceReindexing = forceLoad && forceIndexing;
151155

152156
/*
@@ -158,7 +162,7 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
158162
if ( checkIfInterrupted() )
159163
return;
160164

161-
index = OntologyIndexer.indexOntology( getCacheName(), model, force );
165+
index = OntologyIndexer.indexOntology( cacheName, model, force );
162166

163167
// if interrupted, we don't need to replace the model and clear the *old* cache
164168
if ( checkIfInterrupted() )
@@ -172,7 +176,7 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
172176
this.index = index;
173177
this.isInitialized = true;
174178
// now that the terms have been replaced, we can clear old caches
175-
OntologyLoader.deleteOldCache( getCacheName() );
179+
OntologyLoader.deleteOldCache( cacheName );
176180
} finally {
177181
lock.unlock();
178182
}

src/ubic/basecode/ontology/jena/OntologyLoader.java

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.URL;
3636
import java.net.URLConnection;
3737
import java.nio.file.Files;
38+
import java.nio.file.Paths;
3839
import java.nio.file.StandardCopyOption;
3940

4041
/**
@@ -44,7 +45,7 @@
4445
*/
4546
public class OntologyLoader {
4647

47-
private static Logger log = LoggerFactory.getLogger( OntologyLoader.class );
48+
private static final Logger log = LoggerFactory.getLogger( OntologyLoader.class );
4849
private static final int MAX_CONNECTION_TRIES = 3;
4950
private static final String OLD_CACHE_SUFFIX = ".old";
5051
private static final String TMP_CACHE_SUFFIX = ".tmp";
@@ -126,26 +127,21 @@ public static OntModel loadMemoryModel( String url, String cacheName ) {
126127
}
127128

128129
if ( urlc != null ) {
129-
try ( InputStream in = urlc.getInputStream(); ) {
130+
try ( InputStream in = urlc.getInputStream() ) {
130131
Reader reader;
131132
if ( cacheName != null ) {
132133
// write tmp to disk
133134
File tempFile = getTmpDiskCachePath( cacheName );
134-
if ( tempFile == null ) {
135-
reader = new InputStreamReader( in );
136-
} else {
137-
tempFile.getParentFile().mkdirs();
138-
Files.copy( in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
139-
reader = new FileReader( tempFile );
140-
}
135+
tempFile.getParentFile().mkdirs();
136+
Files.copy( in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
137+
reader = new FileReader( tempFile );
141138

142139
} else {
143140
// Skip the cache
144141
reader = new InputStreamReader( in );
145142
}
146143

147-
assert reader != null;
148-
try ( BufferedReader buf = new BufferedReader( reader ); ) {
144+
try ( BufferedReader buf = new BufferedReader( reader ) ) {
149145
model.read( buf, url );
150146
}
151147

@@ -164,13 +160,8 @@ public static OntModel loadMemoryModel( String url, String cacheName ) {
164160
if ( model.isEmpty() ) {
165161
// Attempt to load from disk cache
166162

167-
if ( f == null ) {
168-
throw new RuntimeException(
169-
"Ontology cache directory required to load from disk: ontology.cache.dir" );
170-
}
171-
172163
if ( f.exists() && !f.isDirectory() ) {
173-
try ( BufferedReader buf = new BufferedReader( new FileReader( f ) ); ) {
164+
try ( BufferedReader buf = new BufferedReader( new FileReader( f ) ) ) {
174165
model.read( buf, url );
175166
// We successfully loaded the cached ontology. Copy the loaded ontology to oldFile
176167
// so that we don't recreate indices during initialization based on a false change in
@@ -190,18 +181,14 @@ public static OntModel loadMemoryModel( String url, String cacheName ) {
190181
} else {
191182
// Model was successfully loaded into memory from URL with given cacheName
192183
// Save cache to disk (rename temp file)
193-
log.info( "Caching ontology to disk: " + cacheName );
194-
if ( f != null ) {
195-
try {
196-
// Need to compare previous to current so instead of overwriting we'll move the old file
197-
f.createNewFile();
198-
Files.move( f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
199-
Files.move( tempFile.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING );
200-
} catch ( IOException e ) {
201-
log.error( e.getMessage(), e );
202-
}
203-
} else {
204-
log.warn( "Ontology cache directory required to save to disk: ontology.cache.dir" );
184+
log.info( "Caching ontology to disk: " + cacheName + " under " + f.getAbsolutePath() );
185+
try {
186+
// Need to compare previous to current so instead of overwriting we'll move the old file
187+
f.createNewFile();
188+
Files.move( f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
189+
Files.move( tempFile.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING );
190+
} catch ( IOException e ) {
191+
log.error( e.getMessage(), e );
205192
}
206193
}
207194

@@ -226,8 +213,7 @@ public static boolean hasChanged( String cacheName ) {
226213
// in the worst case scenario.
227214
// In this case consider using NIO for higher-performance IO using Channels and Buffers.
228215
// Ex. Use a 4MB Memory-Mapped IO operation.
229-
if ( newFile != null && oldFile != null )
230-
changed = !FileUtils.contentEquals( newFile, oldFile );
216+
changed = !FileUtils.contentEquals( newFile, oldFile );
231217
} catch ( IOException e ) {
232218
log.error( e.getMessage() );
233219
}
@@ -236,18 +222,12 @@ public static boolean hasChanged( String cacheName ) {
236222

237223
}
238224

239-
public static boolean deleteOldCache( String cacheName ) {
240-
File f = getOldDiskCachePath( cacheName );
241-
if ( f != null )
242-
return f.delete();
243-
return false;
225+
public static void deleteOldCache( String cacheName ) {
226+
getOldDiskCachePath( cacheName ).delete();
244227
}
245228

246229
/**
247230
* Get model that is entirely in memory.
248-
*
249-
* @param url
250-
* @return
251231
*/
252232
private static OntModel getMemoryModel( String url ) {
253233
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM_TRANS_INF );
@@ -262,42 +242,32 @@ private static OntModel getMemoryModel( String url ) {
262242
}
263243

264244
/**
265-
* @param name
266-
* @return
245+
* Obtain the path for the ontology cache.
267246
*/
268247
public static File getDiskCachePath( String name ) {
269248
String ontologyDir = Configuration.getString( "ontology.cache.dir" ); // e.g., /something/gemmaData/ontologyCache
270-
if ( StringUtils.isBlank( ontologyDir ) || StringUtils.isBlank( name ) ) {
271-
return null;
249+
if ( StringUtils.isBlank( ontologyDir ) ) {
250+
throw new IllegalArgumentException( "The 'ontology.cache.dir' configuration must be set to cache ontologies." );
251+
}
252+
if ( StringUtils.isBlank( name ) ) {
253+
throw new IllegalArgumentException( "The ontology must have a suitable name for being loaded from cache." );
272254
}
273255

274256
if ( !new File( ontologyDir ).exists() ) {
275257
new File( ontologyDir ).mkdirs();
276258
}
277259

278-
assert ontologyDir != null;
279-
280-
String path = ontologyDir + File.separator + "ontology" + File.separator + name;
281-
282-
File indexFile = new File( path );
283-
284-
return indexFile;
260+
return Paths.get( ontologyDir, "ontology", name ).toFile();
285261
}
286262

287-
static File getOldDiskCachePath( String name ) {
263+
private static File getOldDiskCachePath( String name ) {
288264
File indexFile = getDiskCachePath( name );
289-
if ( indexFile == null ) {
290-
return null;
291-
}
292265
return new File( indexFile.getAbsolutePath() + OLD_CACHE_SUFFIX );
293266

294267
}
295268

296-
static File getTmpDiskCachePath( String name ) {
269+
private static File getTmpDiskCachePath( String name ) {
297270
File indexFile = getDiskCachePath( name );
298-
if ( indexFile == null ) {
299-
return null;
300-
}
301271
return new File( indexFile.getAbsolutePath() + TMP_CACHE_SUFFIX );
302272

303273
}

0 commit comments

Comments
 (0)