Skip to content

Commit db72c30

Browse files
committed
tdb: Make the temporary TDB directory configurable
Only delete temporary directories that were created by the service
1 parent a90ca40 commit db72c30

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public class TdbOntologyService extends AbstractOntologyService {
2828
@Nullable
2929
private Dataset dataset;
3030

31-
/**
32-
* Temporary d
33-
*/
31+
@Nullable
3432
private Path tempDir;
3533

34+
private boolean deleteTempDir = false;
35+
3636
/**
3737
* @param readOnly open the TDB database in read-only mode, allowing multiple JVMs to share a common TDB. For this
3838
* to work safely, all the TDB files must not be writable. Additionally, a temporary directory with
@@ -57,14 +57,27 @@ protected OntologyModel loadModel( boolean processImports, LanguageLevel languag
5757
try ( Stream<Path> z = Files.list( tdbDir ) ) {
5858
filesToLink = z.collect( Collectors.toSet() );
5959
}
60-
tempDir = Files.createTempDirectory( getOntologyName() + ".tdb" );
60+
if ( tempDir == null ) {
61+
log.info( "Creating temporary directory for read-only TDB model." );
62+
tempDir = Files.createTempDirectory( getOntologyName() + ".tdb" );
63+
deleteTempDir = true;
64+
} else if ( !Files.exists( tempDir ) ) {
65+
log.info( "Creating temporary directory at {} for read-only TDB model.", tempDir );
66+
Files.createDirectories( tempDir );
67+
deleteTempDir = true;
68+
} else {
69+
log.info( "Reusing existing temporary directory at {} for read-only TDB model. Note that the directory will not be removed.", tempDir );
70+
// never delete a temporary directory that wasn't created by this service (I've learned this the hard way!)
71+
deleteTempDir = false;
72+
}
6173
for ( Path p : filesToLink ) {
6274
Files.copy( p, tempDir.resolve( p.getFileName() ) );
6375
}
6476
} finally {
6577
loc.getLock().release();
6678
}
6779
log.info( "Reading read-only TDB model from {}.", tempDir );
80+
assert tempDir != null;
6881
dataset = TDBFactory.createDataset( tempDir.toString() );
6982
} else {
7083
dataset = TDBFactory.createDataset( tdbDir.toString() );
@@ -78,6 +91,19 @@ protected OntologyModel loadModelFromStream( InputStream is, boolean processImpo
7891
throw new UnsupportedOperationException( "TDB cannot be loaded from an input stream." );
7992
}
8093

94+
/**
95+
* Set the temporary directory used when opening the TDB in read-only mode.
96+
* <p>
97+
* This can only be set once, before the first model is loaded. Note that the directory will be removed when the
98+
* service is closed.
99+
*/
100+
public void setTempDir( Path tempDir ) {
101+
if ( this.tempDir != null ) {
102+
throw new IllegalStateException( "Temporary directory has already been set." );
103+
}
104+
this.tempDir = tempDir;
105+
}
106+
81107
@Override
82108
public void close() throws Exception {
83109
try {
@@ -86,7 +112,7 @@ public void close() throws Exception {
86112
if ( dataset != null ) {
87113
TDBFactory.release( dataset );
88114
}
89-
if ( tempDir != null ) {
115+
if ( tempDir != null && deleteTempDir ) {
90116
PathUtils.delete( tempDir );
91117
}
92118
}

0 commit comments

Comments
 (0)