use of org.apache.jena.tdb2.store.DatasetGraphSwitchable in project jena by apache.
the class TestDatabaseOps method compact_prefixes_3_test.
private void compact_prefixes_3_test() {
// prefixes across compaction.
DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir);
Graph g = dsg.getDefaultGraph();
Txn.executeWrite(dsg, () -> g.getPrefixMapping().setNsPrefix("ex", "http://example/"));
Txn.executeRead(dsg, () -> {
assertEquals("ex", g.getPrefixMapping().getNsURIPrefix("http://example/"));
assertEquals("http://example/", g.getPrefixMapping().getNsPrefixURI("ex"));
});
DatasetGraphSwitchable dsgs = (DatasetGraphSwitchable) dsg;
assertNotNull("DatasetGraphSwitchable created", dsgs.getLocation());
DatasetGraph dsg1 = dsgs.get();
Location loc1 = ((DatasetGraphTDB) dsg1).getLocation();
// Before
int x1 = Txn.calculateRead(dsg, () -> dsg.prefixes().size());
assertTrue("Prefxies count", x1 > 0);
// HERE
DatabaseMgr.compact(dsgs, false);
// After
int x2 = Txn.calculateRead(dsg, () -> dsg.prefixes().size());
assertEquals("Before and after prefix count", x1, x2);
Graph g2 = dsgs.getDefaultGraph();
Txn.executeRead(dsgs, () -> {
assertEquals("ex", g2.getPrefixMapping().getNsURIPrefix("http://example/"));
assertEquals("http://example/", g2.getPrefixMapping().getNsPrefixURI("ex"));
});
// Check is not attached to the old graph.
DatasetGraph dsgOld = StoreConnection.connectCreate(loc1).getDatasetGraph();
Txn.executeWrite(dsgOld, () -> dsgOld.getDefaultGraph().getPrefixMapping().removeNsPrefix("ex"));
Txn.executeRead(dsg, () -> assertEquals("http://example/", g.getPrefixMapping().getNsPrefixURI("ex")));
Txn.executeWrite(dsg, () -> g.getPrefixMapping().setNsPrefix("ex2", "http://exampl2/"));
Txn.executeRead(dsgOld, () -> assertNull(dsgOld.getDefaultGraph().getPrefixMapping().getNsPrefixURI("ex")));
}
use of org.apache.jena.tdb2.store.DatasetGraphSwitchable in project jena by apache.
the class DatabaseOps method createSwitchable.
private static DatasetGraphSwitchable createSwitchable(Location location, StoreParams params) {
if (location.isMem()) {
DatasetGraph dsg = StoreConnection.connectCreate(location).getDatasetGraph();
return new DatasetGraphSwitchable(null, location, dsg);
}
// Exists?
if (!location.exists())
throw new TDBException("No such location: " + location);
Path path = IO_DB.asPath(location);
// Scan for DBs
Path db = findLocation(path, dbPrefix);
if (db == null) {
db = path.resolve(dbPrefix + SEP + startCount);
IOX.createDirectory(db);
}
Location loc2 = IO_DB.asLocation(db);
DatasetGraphTDB dsg = StoreConnection.connectCreate(loc2, params).getDatasetGraphTDB();
DatasetGraphSwitchable appDSG = new DatasetGraphSwitchable(path, location, dsg);
return appDSG;
}
use of org.apache.jena.tdb2.store.DatasetGraphSwitchable in project jena by apache.
the class TestTDBAssembler method testGraph.
private static void testGraph(String assemblerFile, boolean named) {
Object thing = null;
try {
thing = AssemblerUtils.build(assemblerFile, VocabTDB2.tGraphTDB);
} catch (AssemblerException e) {
e.getCause().printStackTrace(System.err);
throw e;
}
assertTrue(thing instanceof Model);
Graph graph = ((Model) thing).getGraph();
assertTrue(graph instanceof GraphViewSwitchable);
DatasetGraphSwitchable dsg = ((GraphViewSwitchable) graph).getDataset();
if (dsg != null)
dsg.close();
}
use of org.apache.jena.tdb2.store.DatasetGraphSwitchable in project jena by apache.
the class DatabaseOps method compact.
// XXX Later - switch in a recording dataset, not block writers, and reply after
// switch over before releasing the new dataset to the container.
// Maybe copy indexes and switch the DSG over (drop switchable).
/**
* Copy the latest version from one location to another.
*/
private static void compact(DatasetGraphSwitchable container, Location loc1, Location loc2) {
if (loc1.isMem() || loc2.isMem())
throw new TDBException("Compact involves a memory location: " + loc1 + " : " + loc2);
copyFiles(loc1, loc2);
StoreConnection srcConn = StoreConnection.connectExisting(loc1);
if (srcConn == null)
throw new TDBException("No database at location : " + loc1);
if (!(container.get() instanceof DatasetGraphTDB))
throw new TDBException("Not a TDB2 database in DatasetGraphSwitchable");
DatasetGraphTDB dsgCurrent = (DatasetGraphTDB) container.get();
if (!dsgCurrent.getLocation().equals(loc1))
throw new TDBException("Inconsistent locations for base : " + dsgCurrent.getLocation() + " , " + dsgCurrent.getLocation());
DatasetGraphTDB dsgBase = srcConn.getDatasetGraphTDB();
if (dsgBase != dsgCurrent)
throw new TDBException("Inconsistent datasets : " + dsgCurrent.getLocation() + " , " + dsgBase.getLocation());
TransactionalSystem txnSystem = dsgBase.getTxnSystem();
TransactionCoordinator txnMgr = dsgBase.getTxnSystem().getTxnMgr();
// Stop update. On exit there are no writers and none will start until switched over.
txnMgr.tryBlockWriters();
// txnMgr.begin(WRITE, false) will now bounce.
// Copy the latest generation.
DatasetGraphTDB dsgCompact = StoreConnection.connectCreate(loc2).getDatasetGraphTDB();
CopyDSG.copy(dsgBase, dsgCompact);
TransactionCoordinator txnMgr2 = dsgCompact.getTxnSystem().getTxnMgr();
txnMgr2.startExclusiveMode();
txnMgr.startExclusiveMode();
// Switch.
if (!container.change(dsgCurrent, dsgCompact)) {
Log.warn(DatabaseOps.class, "Inconistent: old datasetgraph not as expected");
container.set(dsgCompact);
}
txnMgr2.finishExclusiveMode();
// New database running.
// Clean-up.
// txnMgr.finishExclusiveMode();
// Don't call : txnMgr.startWriters();
StoreConnection.release(dsgBase.getLocation());
}
use of org.apache.jena.tdb2.store.DatasetGraphSwitchable in project jena by apache.
the class DatabaseOps method compact.
public static void compact(DatasetGraphSwitchable container, boolean shouldDeleteOld) {
checkSupportsAdmin(container);
synchronized (compactionLock) {
Path base = container.getContainerPath();
Path db1 = findLocation(base, dbPrefix);
if (db1 == null)
throw new TDBException("No location: (" + base + ", " + dbPrefix + ")");
Location loc1 = IO_DB.asLocation(db1);
// -- Checks
Location loc1a = ((DatasetGraphTDB) container.get()).getLocation();
if (loc1a.isMem()) {
}
if (!loc1a.exists())
throw new TDBException("No such location: " + loc1a);
// Is this the same database location?
if (!loc1.equals(loc1a))
throw new TDBException("Inconsistent (not latest?) : " + loc1a + " : " + loc1);
// -- Checks
// Version
int v = IO_DB.extractIndex(db1.getFileName().toString(), dbPrefix, SEP);
String next = FilenameUtils.filename(dbPrefix, SEP, v + 1);
Path db2 = db1.getParent().resolve(next);
IOX.createDirectory(db2);
Location loc2 = IO_DB.asLocation(db2);
LOG.debug(String.format("Compact %s -> %s\n", db1.getFileName(), db2.getFileName()));
compact(container, loc1, loc2);
if (shouldDeleteOld) {
Path loc1Path = IO_DB.asPath(loc1);
LOG.debug("Deleting old database after successful compaction (old db path='" + loc1Path + "')...");
try (Stream<Path> walk = Files.walk(loc1Path)) {
walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (IOException ex) {
throw IOX.exception(ex);
}
}
}
}
Aggregations