private StoreConnection(DatasetGraphTDB dsg, ProcessFileLock fileLock) { this.datasetGraph = dsg ; this.location = dsg.getLocation() ; this.lock = fileLock; }
/** 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); }
/** 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); }
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); } }
if ( ! dsgCurrent.getLocation().equals(loc1) ) throw new TDBException("Inconsistent locations for base : "+dsgCurrent.getLocation()+" , "+dsgCurrent.getLocation()); throw new TDBException("Inconsistent datasets : "+dsgCurrent.getLocation()+" , "+dsgBase.getLocation()); StoreConnection.release(dsgBase.getLocation());
@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_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"))); }