private static DatasetGraphTDB unwrap(DatasetGraph datasetGraph) { DatasetGraph dsg = datasetGraph; if ( dsg instanceof DatasetGraphSwitchable ) dsg = ((DatasetGraphSwitchable)datasetGraph).get(); if ( dsg instanceof DatasetGraphTDB ) return ((DatasetGraphTDB)dsg); return null; }
/** Stop managing a DatasetGraph. Use with great care. */ public static synchronized void expel(DatasetGraph dsg, boolean force) { Location locContainer = null; Location locStorage = null; if ( dsg instanceof DatasetGraphSwitchable ) { locContainer = ((DatasetGraphSwitchable)dsg).getLocation(); dsg = ((DatasetGraphSwitchable)dsg).getWrapped(); } if ( dsg instanceof DatasetGraphTDB ) locStorage = ((DatasetGraphTDB)dsg).getLocation(); DatabaseConnection.internalExpel(locContainer, force); StoreConnection.internalExpel(locStorage, force); }
private static void checkSupportsAdmin(DatasetGraphSwitchable container) { if ( ! container.hasContainerPath() ) throw new TDBException("Dataset does not support admin operations"); }
DatasetGraphTDB dsgCurrent = (DatasetGraphTDB)container.get(); if ( ! dsgCurrent.getLocation().equals(loc1) ) throw new TDBException("Inconsistent locations for base : "+dsgCurrent.getLocation()+" , "+dsgCurrent.getLocation()); if ( ! container.change(dsgCurrent, dsgCompact) ) { Log.warn(DatabaseOps.class, "Inconistent: old datasetgraph not as expected"); container.set(dsgCompact);
public static void compact(DatasetGraphSwitchable container) { checkSupportsAdmin(container); synchronized(compactionLock) { Path base = container.getContainerPath(); Path db1 = findLocation(base, dbPrefix); Location loc1 = IOX.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 latested?) : "+loc1a+" : "+loc1); // -- Checks // Version int v = IOX.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 = IOX.asLocation(db2); LOG.debug(String.format("Compact %s -> %s\n", db1.getFileName(), db2.getFileName())); compact(container, loc1, loc2); } }
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() ; } }
/** Return the location of a DatasetGraph if it is backed by TDB, else null. */ public static Location location(DatasetGraph datasetGraph) { DatasetGraphSwitchable dsg = requireSwitchable(datasetGraph); if ( dsg == null ) return null ; return dsg.getLocation(); }
public static String backup(DatasetGraphSwitchable container) { checkSupportsAdmin(container); Path dbPath = container.getContainerPath(); Path backupDir = dbPath.resolve(BACKUPS_DIR); if ( ! Files.exists(backupDir) ) IOX.createDirectory(backupDir); DatasetGraph dsg = container; // // Per backup source lock. // synchronized(activeBackups) { // // Atomically check-and-set // if ( activeBackups.contains(dsg) ) // Log.warn(Fuseki.serverLog, "Backup already in progress") ; // activeBackups.add(dsg) ; // } Pair<OutputStream, Path> x = openUniqueFileForWriting(backupDir, BACKUPS_FN, "nq.gz"); try (OutputStream out2 = x.getLeft(); OutputStream out1 = new GZIPOutputStream(out2, 8 * 1024) ; OutputStream out = new BufferedOutputStream(out1)) { Txn.executeRead(dsg, ()->RDFDataMgr.write(out, dsg, Lang.NQUADS)); } catch (IOException e) { throw IOX.exception(e) ; } return x.getRight().toString(); }
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 = IOX.asPath(location); // Scan for DBs Path db = findLocation(path, dbPrefix); if ( db == null ) { db = path.resolve(dbPrefix+SEP+startCount); IOX.createDirectory(db); } Location loc2 = IOX.asLocation(db); DatasetGraphTDB dsg = StoreConnection.connectCreate(loc2, params).getDatasetGraphTDB(); DatasetGraphSwitchable appDSG = new DatasetGraphSwitchable(path, location, dsg); return appDSG; }
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() ; } }
private DatasetGraphTDB getDSG() { return ((DatasetGraphTDB)(getx().get())); }
/** Stop managing a DatasetGraph. Use with great care. */ public static synchronized void expel(DatasetGraph dsg) { Location locContainer = null; Location locStorage = null; if ( dsg instanceof DatasetGraphSwitchable ) { locContainer = ((DatasetGraphSwitchable)dsg).getLocation(); dsg = ((DatasetGraphSwitchable)dsg).getWrapped(); } if ( dsg instanceof DatasetGraphTDB ) locStorage = ((DatasetGraphTDB)dsg).getLocation(); if ( locContainer != null ) DatabaseConnection.internalExpel(locContainer, false); StoreConnection.internalExpel(locStorage, false); }
@Test public void createGraphEmbed() { String f = dirAssem + "/tdb-graph-embed.ttl" ; Object thing = null ; try { thing = AssemblerUtils.build(f, JA.Model) ; } catch (AssemblerException e) { e.getCause().printStackTrace(System.err) ; throw e ; } assertTrue(thing instanceof Model) ; Graph graph = ((Model)thing).getGraph() ; assertTrue(graph instanceof GraphViewSwitchable) ; DatasetGraphSwitchable ds = ((GraphViewSwitchable)graph).getDataset() ; if ( ds != null ) ds.close() ; }
@Test public void compact_dsg_1() { DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir); DatasetGraphSwitchable dsgs = (DatasetGraphSwitchable)dsg; DatasetGraph dsg1 = dsgs.get(); Location loc1 = ((DatasetGraphTDB)dsg1).getLocation(); Txn.executeWrite(dsg, ()-> { dsg.add(quad2) ; dsg.add(quad1) ; }) ; DatabaseMgr.compact(dsg); assertFalse(StoreConnection.isSetup(loc1)); DatasetGraph dsg2 = dsgs.get(); Location loc2 = ((DatasetGraphTDB)dsg2).getLocation(); assertNotEquals(dsg1, dsg2); assertNotEquals(loc1, loc2); Txn.executeRead(dsg, ()-> { assertTrue(dsg.contains(quad2)) ; assertTrue(dsg.contains(quad1)) ; }) ; // dsg1 was closed and expelled. We must carefully reopen its storage only. DatasetGraph dsgOld = StoreConnection.connectCreate(loc1).getDatasetGraph(); Txn.executeWrite(dsgOld, ()->dsgOld.delete(quad2)); Txn.executeRead(dsg, ()->assertTrue(dsg.contains(quad2)) ); Txn.executeRead(dsg2, ()->assertTrue(dsg2.contains(quad2)) ) ; }
@Test public void createGraphEmbed() { String f = dirAssem + "/tdb-graph-embed.ttl" ; Object thing = null ; try { thing = AssemblerUtils.build(f, JA.Model) ; } catch (AssemblerException e) { e.getCause().printStackTrace(System.err) ; throw e ; } assertTrue(thing instanceof Model) ; Graph graph = ((Model)thing).getGraph() ; assertTrue(graph instanceof GraphViewSwitchable) ; DatasetGraphSwitchable ds = ((GraphViewSwitchable)graph).getDataset() ; if ( ds != null ) ds.close() ; }
@Test public void compact_dsg_1() { DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir); DatasetGraphSwitchable dsgs = (DatasetGraphSwitchable)dsg; DatasetGraph dsg1 = dsgs.get(); Location loc1 = ((DatasetGraphTDB)dsg1).getLocation(); Txn.executeWrite(dsg, ()-> { dsg.add(quad2) ; dsg.add(quad1) ; }) ; DatabaseMgr.compact(dsg); assertFalse(StoreConnection.isSetup(loc1)); DatasetGraph dsg2 = dsgs.get(); Location loc2 = ((DatasetGraphTDB)dsg2).getLocation(); assertNotEquals(dsg1, dsg2); assertNotEquals(loc1, loc2); Txn.executeRead(dsg, ()-> { assertTrue(dsg.contains(quad2)) ; assertTrue(dsg.contains(quad1)) ; }) ; // dsg1 was closed and expelled. We must carefully reopen its storage only. DatasetGraph dsgOld = StoreConnection.connectCreate(loc1).getDatasetGraph(); Txn.executeWrite(dsgOld, ()->dsgOld.delete(quad2)); Txn.executeRead(dsg, ()->assertTrue(dsg.contains(quad2)) ); Txn.executeRead(dsg2, ()->assertTrue(dsg2.contains(quad2)) ) ; }
@Test public void compact_graph_2() { // graphs across compaction. DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir); Graph g = dsg.getDefaultGraph(); DatasetGraphSwitchable dsgs = (DatasetGraphSwitchable)dsg; DatasetGraph dsg1 = dsgs.get(); Location loc1 = ((DatasetGraphTDB)dsg1).getLocation(); Txn.executeWrite(dsg, ()-> { dsg.add(quad2) ; dsg.add(quad1) ; }) ; DatabaseMgr.compact(dsg); Txn.executeRead(dsg, ()-> { assertEquals(2, g.size()); assertTrue(g.contains(triple2)); }) ; // Check is not attached to the old graph. DatasetGraph dsgOld = StoreConnection.connectCreate(loc1).getDatasetGraph(); Txn.executeWrite(dsgOld, ()->dsgOld.getDefaultGraph().delete(triple2)); Txn.executeRead(dsg, ()->assertTrue(g.contains(triple2)) ); Txn.executeWrite(dsg, ()->g.add(triple3)); Txn.executeRead(dsgOld, ()->assertFalse(dsgOld.getDefaultGraph().contains(triple3)) ); }
@Test public void compact_graph_2() { // graphs across compaction. DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir); Graph g = dsg.getDefaultGraph(); DatasetGraphSwitchable dsgs = (DatasetGraphSwitchable)dsg; DatasetGraph dsg1 = dsgs.get(); Location loc1 = ((DatasetGraphTDB)dsg1).getLocation(); Txn.executeWrite(dsg, ()-> { dsg.add(quad2) ; dsg.add(quad1) ; }) ; DatabaseMgr.compact(dsg); Txn.executeRead(dsg, ()-> { assertEquals(2, g.size()); assertTrue(g.contains(triple2)); }) ; // Check is not attached to the old graph. DatasetGraph dsgOld = StoreConnection.connectCreate(loc1).getDatasetGraph(); Txn.executeWrite(dsgOld, ()->dsgOld.getDefaultGraph().delete(triple2)); Txn.executeRead(dsg, ()->assertTrue(g.contains(triple2)) ); Txn.executeWrite(dsg, ()->g.add(triple3)); Txn.executeRead(dsgOld, ()->assertFalse(dsgOld.getDefaultGraph().contains(triple3)) ); }
@Test public void compact_prefixes_3() { // prefixes axcross compaction. DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir); Graph g = dsg.getDefaultGraph(); Txn.executeWrite(dsg, ()-> g.getPrefixMapping().setNsPrefix("ex", "http://example/") ); DatasetGraphSwitchable dsgs = (DatasetGraphSwitchable)dsg; DatasetGraph dsg1 = dsgs.get(); Location loc1 = ((DatasetGraphTDB)dsg1).getLocation(); DatabaseMgr.compact(dsg); Txn.executeRead(dsg, ()-> { assertEquals("ex", g.getPrefixMapping().getNsURIPrefix("http://example/")); assertEquals("http://example/", g.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"))); }
@Test public void compact_prefixes_3() { // prefixes axcross compaction. DatasetGraph dsg = DatabaseMgr.connectDatasetGraph(dir); Graph g = dsg.getDefaultGraph(); Txn.executeWrite(dsg, ()-> g.getPrefixMapping().setNsPrefix("ex", "http://example/") ); DatasetGraphSwitchable dsgs = (DatasetGraphSwitchable)dsg; DatasetGraph dsg1 = dsgs.get(); Location loc1 = ((DatasetGraphTDB)dsg1).getLocation(); DatabaseMgr.compact(dsg); Txn.executeRead(dsg, ()-> { assertEquals("ex", g.getPrefixMapping().getNsURIPrefix("http://example/")); assertEquals("http://example/", g.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"))); }