DatabaseOps { public static void compact(DatasetGraphSwitchable container) { checkSupportsAdmin(container); synchronized(compactionLock) { Path base = container.getContainerPath(); Path db1 = findLocation(base, dbPrefix); Location loc1 = IOX.asLocation(db1); Location loc1a = ((DatasetGraphTDB)container.get()).getLocation(); if ( loc1a.isMem() ) {} if ( ! loc1a.exists() ) throw new TDBException("No such location: "+loc1a); if ( ! loc1.equals(loc1a) ) throw new TDBException("Inconsistent (not latested?) : "+loc1a+" : "+loc1); 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); } }  static String backup(DatasetGraphSwitchable container); static void compact(DatasetGraphSwitchable container); static final String dbPrefix; static final String SEP; }
@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)); }); 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() { 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)); }); 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)) ); }