/** Does the location exist (and is a directory, and is accessible) */ public boolean exists() { File f = new File(getDirectoryPath()) ; return f.exists() && f.isDirectory() && f.canRead() ; }
public String getSubDirectory(String dirname) { return getSubLocation(dirname).getDirectoryPath() ; }
@Override public String toString() { return "Transaction: " + id + " : Mode=" + mode + " : State=" + state + " : " + basedsg.getLocation().getDirectoryPath() ; }
/** * Resolve a Location and file path: Location.getPath only handles file names withing * the location, not paths. */ public static String resolve(Location location, String pathStr) { Path path = Paths.get(pathStr); if ( path.getNameCount() == 0 ) return location.getDirectoryPath(); else if ( path.getNameCount() == 1 ) return location.getPath(pathStr); Path locationPath = IOX.asPath(location); return locationPath.resolve(pathStr).toAbsolutePath().toString(); }
public DatasetGraphTDB build(Location location, StoreParams params) { // Ensure that there is global synchronization synchronized (DatasetBuilderStd.class) { log.debug("Build database: "+location.getDirectoryPath()) ; checkLocation(location) ; return _build(location, params, true, null) ; } }
private void takeLock(int pid) { File lockFile = getLockFile(); checkLockFileForWrite(lockFile); // Write our PID to the lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(lockFile))) { writer.write(Integer.toString(pid)); writer.close(); } catch (IOException e) { throw new TDBException("Failed to obtain a lock on the location " + location.getDirectoryPath(), e); } // Mark lock for deletion on JVM exit // This does not guarantee that the lock file gets cleaned up because // such deletions only succeed for normal JVM termination but it should // clean up the lock for normal JVM terminations lockFile.deleteOnExit(); }
/** Look at a directory and see if it is a new area */ public static boolean isNewDatabaseArea(Location location) { StoreConnection sConn = StoreConnection.getExisting(location) ; if ( sConn != null ) // Already has a StoreConnection return false ; if ( location.isMem() ) return true ; File d = new File(location.getDirectoryPath()) ; if ( !d.exists() ) return true ; FileFilter ff = fileFilterNewDB ; File[] entries = d.listFiles(ff) ; return entries.length == 0 ; }
/** Convert a {@link Location} to a {@link Path}. */ public static Path asPath(Location location) { if ( location.isMem() ) throw new RuntimeIOException("Location is a memory location: "+location); return Paths.get(location.getDirectoryPath()); }
private static void checkLocation(Location location) { if ( location.isMem() ) return ; String dirname = location.getDirectoryPath() ; File dir = new File(dirname) ; // File location. if ( ! dir.exists() ) error(log, "Does not exist: "+dirname) ; if ( ! dir.isDirectory() ) error(log, "Not a directory: "+dirname) ; if ( ! dir.canRead() ) error(log, "Directory not readable: "+dirname) ; if ( ! dir.canWrite() ) error(log, "Directory not writeable: "+dirname) ; }
/** 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) ; }
public void clearDirectory() { FileOps.clearDirectory(loc.getDirectoryPath()) ; }
@BeforeClass static public void beforeClass() { FileOps.ensureDir(STORE.getDirectoryPath()); }
public /* for testing */ static void init$() { if ( initialized ) return ; initialized = true ; if ( location == null ) location = Location.create(FusekiWebapp.dirSystemDatabase.toString()) ; if ( ! location.isMem() ) FileOps.ensureDir(location.getDirectoryPath()) ; // Force it into the store connection as a low footprint if ( StoreConnection.getExisting(location) != null ) Fuseki.serverLog.warn("System database already in the StoreConnection cache") ; StoreConnection.make(location, systemDatabaseParams) ; dataset = TDBFactory.createDataset(location) ; dsg = (DatasetGraphTransaction)(dataset.asDatasetGraph()) ; dsg.getContext().set(TDB.symUnionDefaultGraph, false) ; } }
synchronized /*package*/ void notifyClose(Transaction txn) { // Caution - not called if "Transactional.end() is not called." if ( txn.getState() == TxnState.ACTIVE ) { // The application error case for begin(WRITE)...end() is handled in Trasnaction.close(). // This is internal checking. String x = txn.getBaseDataset().getLocation().getDirectoryPath() ; syslog.warn("close: Transaction not commited or aborted: Transaction: "+txn.getTxnId()+" @ "+x) ; // Force abort then close txn.abort() ; txn.close() ; return ; } noteTxnClose(txn) ; }
@After public void afterTest() { FileStore.resetTracked(); FileOps.clearDirectory(STORE.getDirectoryPath()); }
@Test public void params_choose_new_persist_3() { // new database, location defined, application modified. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); StoreParamsCodec.write(loc, pLoc); // Clear. StoreParams p = Build.decideStoreParams(loc, true, pApp, pLoc, pDft) ; // Check location still has a pLoc. String fn = loc.getPath(StoreParamsConst.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertFalse(StoreParams.sameValues(pLoc, p)) ; assertEquals(0, p.getBlockSize().intValue()) ; // Location assertEquals(12, p.getNodeMissCacheSize().intValue()) ; // Application }
@Test public void params_choose_new_persist_1() { // new database, app defined. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); // Clear. StoreParams p = Build.decideStoreParams(loc, true, pApp, null, pDft) ; // Check location now has a pLoc. String fn = loc.getPath(StoreParamsConst.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc2, p)) ; }
@Test public void params_choose_new_persist_1() { // new database, app defined. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); // Clear. StoreParams p = Build.decideStoreParams(loc, true, pApp, null, pDft) ; // Check location now has a pLoc. String fn = loc.getPath(StoreParamsConst.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc2, p)) ; }
@Test public void params_choose_new_persist_2() { // new database, location defined. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); StoreParamsCodec.write(loc, pLoc); // Clear. StoreParams p = Build.decideStoreParams(loc, true, null, pLoc, pDft) ; // Check location still has a pLoc. String fn = loc.getPath(StoreParamsConst.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc, p)) ; }
@Test public void params_choose_new_persist_2() { // new database, location defined. Location loc = Location.create(DIR) ; FileOps.clearAll(loc.getDirectoryPath()); StoreParamsCodec.write(loc, pLoc); // Clear. StoreParams p = Build.decideStoreParams(loc, true, null, pLoc, pDft) ; // Check location still has a pLoc. String fn = loc.getPath(StoreParamsConst.TDB_CONFIG_FILE) ; assertTrue(FileOps.exists(fn)) ; StoreParams pLoc2 = StoreParamsCodec.read(loc) ; assertTrue(StoreParams.sameValues(pLoc, p)) ; }