/** Create a FileSet given Location (directory) and name within the directory */ public FileSet(String directory, String basename) { initFileSet(Location.create(directory), basename) ; }
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) ; } }
public String getSubDirectory(String dirname) { return getSubLocation(dirname).getDirectoryPath() ; }
@Test public void location_lock_dir_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { writer.write(Integer.toString(-1234)); // Fake PID that would never // be valid } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertFalse(lock.canObtain()); }
@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 location_lock_dir_01() { Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Try to obtain the lock lock.obtain(); Assert.assertTrue(lock.isLocked()); Assert.assertTrue(lock.isOwned()); // Release the lock lock.release(); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); }
/** 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()); }
/** 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() ; }
/** * Return a StoreConnection backed by in-memory * datastructures (for testing). */ public static StoreConnection createMemUncached() { DatasetGraphTDB dsg = DatasetBuilderStd.create(Location.mem(), null); return new StoreConnection(dsg); } }
public String filename(String ext) { return location.getPath(basename, ext) ; } }
/** * Gets whether the location is lockable i.e. is it an on-disk location * where we can use a lock file to prevent multi-process access and the * potential data corruption that ensues * * @return True if the location is lockable */ public boolean canLock() { return !location.isMem(); }
/** 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) ; }
/** * 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(); }
LocationLock lock = location.getLock(); if (lock.canLock()) { if (!lock.canObtain()) { int here = ProcessUtils.getPid(0); throw new TDBException("Process ID "+here+" can't open database at location " + location.getDirectoryPath() + " because it is already locked by the process with PID " + lock.getOwner() + ". "+ "TDB databases do not permit concurrent usage across JVMs so in order to prevent possible data corruption you cannot open this location from the JVM that does not own the lock for the dataset"); throw new TDBException("Process ID "+here+" failed to open database at location " + location.getDirectoryPath() + " because it is already locked by the process with PID " + lock.getOwner() + ". "+ "TDB databases do not permit concurrent usage across JVMs so in order to prevent possible data corruption you cannot open this location from the JVM that does not own the lock for the dataset"); if (!location.isMemUnique())
/** Stop managing a location. Use with great care (testing only). */ public static synchronized void expel(Location location, boolean force) { StoreConnection sConn = cache.get(location) ; if (sConn == null) return ; if (!force && sConn.transactionManager.activeTransactions()) throw new TDBTransactionException("Can't expel: Active transactions for location: " + location) ; // No transactions at this point (or we don't care and are clearing up forcefully.) sConn.transactionManager.closedown() ; sConn.baseDSG.close() ; sConn.isValid = false ; cache.remove(location) ; ChannelManager.release(sConn.transactionManager.getJournal().getFilename()) ; // Release the lock if (SystemTDB.DiskLocationMultiJvmUsagePrevention) { if (location.getLock().isOwned()) { location.getLock().release(); } else if (location.getLock().canLock()) { SystemTDB.errlog.warn("Location " + location.getDirectoryPath() + " was not locked, if another JVM accessed this location simultaneously data corruption may have occurred"); } } }
@Test public void location_lock_dir_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { writer.write(Integer.toString(-1234)); // Fake PID that would never be valid } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertFalse(lock.canObtain()); }