Repository: jena Updated Branches: refs/heads/master 4a2c5d29c -> 7169c67c8
JENA-988 : Test for whether a disk location is in use or not. Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/8c315984 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/8c315984 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/8c315984 Branch: refs/heads/master Commit: 8c315984418b12ffb348ca8bd087922f66d6e386 Parents: b328842 Author: Andy Seaborne <[email protected]> Authored: Mon Aug 10 11:30:12 2015 +0100 Committer: Andy Seaborne <[email protected]> Committed: Mon Aug 10 11:34:09 2015 +0100 ---------------------------------------------------------------------- .../java/org/apache/jena/tdb/TDBFactory.java | 32 +++++++++++-- .../org/apache/jena/tdb/TestTDBFactory.java | 47 ++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/8c315984/jena-tdb/src/main/java/org/apache/jena/tdb/TDBFactory.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/main/java/org/apache/jena/tdb/TDBFactory.java b/jena-tdb/src/main/java/org/apache/jena/tdb/TDBFactory.java index cf2f94a..26ea8c1 100644 --- a/jena-tdb/src/main/java/org/apache/jena/tdb/TDBFactory.java +++ b/jena-tdb/src/main/java/org/apache/jena/tdb/TDBFactory.java @@ -18,6 +18,9 @@ package org.apache.jena.tdb; +import java.io.File ; + +import org.apache.jena.atlas.lib.FileOps ; import org.apache.jena.query.Dataset ; import org.apache.jena.query.DatasetFactory ; import org.apache.jena.sparql.core.DatasetGraph ; @@ -125,9 +128,32 @@ public class TDBFactory return null ; } - /** test whether a location has a TDB database (pragmatic test). */ - private static boolean freshLocation(Location location) { - return TDBInternal.isNewDatabaseArea(location) ; + /** Test whether a location already has a TDB database or whether a call to TDBFactory + * will cause a new, fresh TDB database to be created (pragmatic tests). + * The directory may be empty, or not exist. + * Existing databases return "true". + */ + public static boolean inUseLocation(String directory) { + return inUseLocation(Location.create(directory)) ; + } + + /** Test whether a location already has a TDB database or whether a call to TDBFactory + * will cause a new, fresh TDB database to be created (pragmatic tests). + * The directory may be empty, or not exist. + * Existing databases return "true". + */ + public static boolean inUseLocation(Location location) { + if ( location.isMemUnique() ) + return false ; + if ( location.isMem() ) + return StoreConnection.getExisting(location) != null ; + String dirname = location.getDirectoryPath() ; + File d = new File(dirname) ; + + if ( ! d.exists() ) + // TDB autocreates directories one level. + return ! FileOps.exists(d.getParent()) ; + return ! TDBInternal.isNewDatabaseArea(location) ; } /** Set the {@link StoreParams} for specific Location. http://git-wip-us.apache.org/repos/asf/jena/blob/8c315984/jena-tdb/src/test/java/org/apache/jena/tdb/TestTDBFactory.java ---------------------------------------------------------------------- diff --git a/jena-tdb/src/test/java/org/apache/jena/tdb/TestTDBFactory.java b/jena-tdb/src/test/java/org/apache/jena/tdb/TestTDBFactory.java index 31af20a..1672c5e 100644 --- a/jena-tdb/src/test/java/org/apache/jena/tdb/TestTDBFactory.java +++ b/jena-tdb/src/test/java/org/apache/jena/tdb/TestTDBFactory.java @@ -40,6 +40,7 @@ public class TestTDBFactory extends BaseTest @Before public void before() { + StoreConnection.reset() ; FileOps.clearDirectory(DIR) ; } @@ -104,5 +105,51 @@ public class TestTDBFactory extends BaseTest assertNotSame(dgBase1, dgBase2) ; } + + @Test public void testTDBFresh01() { + boolean b = TDBFactory.inUseLocation(DIR) ; + assertFalse("Expect false before any creation attempted", b) ; + } + + @Test public void testTDBFresh02() { + boolean b = TDBFactory.inUseLocation(DIR) ; + assertFalse("Expect false before any creation attempted", b) ; + TDBFactory.createDataset(DIR) ; + b = TDBFactory.inUseLocation(DIR) ; + assertTrue("Expected true after creation attempted", b) ; + StoreConnection.expel(Location.create(DIR), true); + } + + @Test public void testTDBFresh03() { + boolean b = TDBFactory.inUseLocation(DIR) ; + assertFalse("Expect true before any creation attempted", b) ; + TDBFactory.createDataset(DIR) ; + b = TDBFactory.inUseLocation(DIR) ; + assertTrue("Expected true after creation attempted", b) ; + StoreConnection.expel(Location.create(DIR), true); + b = TDBFactory.inUseLocation(DIR) ; + assertTrue("Expected true even after StoreConenction reset", b) ; + } + @Test public void testTDBFresh11() { + Location loc = Location.mem() ; + boolean b = TDBFactory.inUseLocation(loc) ; + assertFalse("Expect false before any creation attempted", b) ; + } + + @Test public void testTDBFresh22() { + Location loc = Location.mem() ; + boolean b = TDBFactory.inUseLocation(loc) ; + TDBFactory.createDataset(loc) ; + b = TDBFactory.inUseLocation(loc) ; + assertFalse("Expected false for a unique memory location", b) ; + } + + @Test public void testTDBFresh23() { + Location loc = Location.mem("FOO") ; + boolean b = TDBFactory.inUseLocation(loc) ; + TDBFactory.createDataset(loc) ; + b = TDBFactory.inUseLocation(loc) ; + assertTrue("Expected true for a named memory location", b) ; + } }
