Search in sources :

Example 6 with DatasetGraphSwitchable

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")));
}
Also used : DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) DatasetGraphSwitchable(org.apache.jena.tdb2.store.DatasetGraphSwitchable) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Location(org.apache.jena.dboe.base.file.Location) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Example 7 with DatasetGraphSwitchable

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;
}
Also used : TDBException(org.apache.jena.tdb2.TDBException) DatasetGraphSwitchable(org.apache.jena.tdb2.store.DatasetGraphSwitchable) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Location(org.apache.jena.dboe.base.file.Location) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Example 8 with DatasetGraphSwitchable

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();
}
Also used : Graph(org.apache.jena.graph.Graph) GraphViewSwitchable(org.apache.jena.tdb2.store.GraphViewSwitchable) AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) Model(org.apache.jena.rdf.model.Model) DatasetGraphSwitchable(org.apache.jena.tdb2.store.DatasetGraphSwitchable)

Example 9 with DatasetGraphSwitchable

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());
}
Also used : TDBException(org.apache.jena.tdb2.TDBException) TransactionalSystem(org.apache.jena.dboe.transaction.txn.TransactionalSystem) TransactionCoordinator(org.apache.jena.dboe.transaction.txn.TransactionCoordinator) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Example 10 with DatasetGraphSwitchable

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);
            }
        }
    }
}
Also used : TDBException(org.apache.jena.tdb2.TDBException) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) Location(org.apache.jena.dboe.base.file.Location) DatasetGraphTDB(org.apache.jena.tdb2.store.DatasetGraphTDB)

Aggregations

DatasetGraphSwitchable (org.apache.jena.tdb2.store.DatasetGraphSwitchable)14 DatasetGraphTDB (org.apache.jena.tdb2.store.DatasetGraphTDB)10 Location (org.apache.jena.dboe.base.file.Location)8 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)6 Graph (org.apache.jena.graph.Graph)4 ConfigTest (org.apache.jena.tdb2.ConfigTest)4 Test (org.junit.Test)4 TDBException (org.apache.jena.tdb2.TDBException)3 AssemblerException (org.apache.jena.assembler.exceptions.AssemblerException)2 Model (org.apache.jena.rdf.model.Model)2 GraphViewSwitchable (org.apache.jena.tdb2.store.GraphViewSwitchable)2 ModDataset (arq.cmdline.ModDataset)1 RuntimeIOException (org.apache.jena.atlas.RuntimeIOException)1 CmdException (org.apache.jena.cmd.CmdException)1 TransactionCoordinator (org.apache.jena.dboe.transaction.txn.TransactionCoordinator)1 TransactionalSystem (org.apache.jena.dboe.transaction.txn.TransactionalSystem)1 Dataset (org.apache.jena.query.Dataset)1 JenaException (org.apache.jena.shared.JenaException)1