Search in sources :

Example 1 with MemoryDatabase

use of org.syncany.database.MemoryDatabase in project syncany by syncany.

the class DownOperation method persistMuddyMultiChunks.

/**
	 * Identifies and persists 'muddy' multichunks to the local database. Muddy multichunks are multichunks
	 * that have been referenced by DIRTY database versions and might be reused in future database versions when
	 * the other client cleans up its mess (performs another 'up').
	 */
private void persistMuddyMultiChunks(Entry<String, DatabaseBranch> winnersBranch, DatabaseBranches allStitchedBranches, Map<DatabaseVersionHeader, File> databaseVersionLocations) throws StorageException, IOException, SQLException {
    // Find dirty database versions (from other clients!) and load them from files
    Map<DatabaseVersionHeader, Collection<MultiChunkEntry>> muddyMultiChunksPerDatabaseVersion = new HashMap<>();
    Set<DatabaseVersionHeader> winnersDatabaseVersionHeaders = Sets.newHashSet(winnersBranch.getValue().getAll());
    for (String otherClientName : allStitchedBranches.getClients()) {
        boolean isLocalMachine = config.getMachineName().equals(otherClientName);
        if (!isLocalMachine) {
            DatabaseBranch otherClientBranch = allStitchedBranches.getBranch(otherClientName);
            Set<DatabaseVersionHeader> otherClientDatabaseVersionHeaders = Sets.newHashSet(otherClientBranch.getAll());
            SetView<DatabaseVersionHeader> otherMuddyDatabaseVersionHeaders = Sets.difference(otherClientDatabaseVersionHeaders, winnersDatabaseVersionHeaders);
            boolean hasMuddyDatabaseVersionHeaders = otherMuddyDatabaseVersionHeaders.size() > 0;
            if (hasMuddyDatabaseVersionHeaders) {
                logger.log(Level.INFO, "DIRTY database version headers of " + otherClientName + ":  " + otherMuddyDatabaseVersionHeaders);
                for (DatabaseVersionHeader muddyDatabaseVersionHeader : otherMuddyDatabaseVersionHeaders) {
                    MemoryDatabase muddyMultiChunksDatabase = new MemoryDatabase();
                    File localFileForMuddyDatabaseVersion = databaseVersionLocations.get(muddyDatabaseVersionHeader);
                    VectorClock fromVersion = muddyDatabaseVersionHeader.getVectorClock();
                    VectorClock toVersion = muddyDatabaseVersionHeader.getVectorClock();
                    logger.log(Level.INFO, "  - Loading " + muddyDatabaseVersionHeader + " from file " + localFileForMuddyDatabaseVersion);
                    databaseSerializer.load(muddyMultiChunksDatabase, localFileForMuddyDatabaseVersion, fromVersion, toVersion, DatabaseReadType.FULL);
                    boolean hasMuddyMultiChunks = muddyMultiChunksDatabase.getMultiChunks().size() > 0;
                    if (hasMuddyMultiChunks) {
                        muddyMultiChunksPerDatabaseVersion.put(muddyDatabaseVersionHeader, muddyMultiChunksDatabase.getMultiChunks());
                    }
                }
            }
        }
    }
    // Add muddy multichunks to 'multichunks_muddy' database table
    boolean hasMuddyMultiChunks = muddyMultiChunksPerDatabaseVersion.size() > 0;
    if (hasMuddyMultiChunks) {
        localDatabase.writeMuddyMultiChunks(muddyMultiChunksPerDatabaseVersion);
    }
}
Also used : HashMap(java.util.HashMap) VectorClock(org.syncany.database.VectorClock) MemoryDatabase(org.syncany.database.MemoryDatabase) Collection(java.util.Collection) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) CleanupRemoteFile(org.syncany.plugins.transfer.files.CleanupRemoteFile) File(java.io.File)

Example 2 with MemoryDatabase

use of org.syncany.database.MemoryDatabase in project syncany by syncany.

the class XmlDatabaseDaoTest method testWriteAndReadMultipleDatabaseVersions.

@Test
public void testWriteAndReadMultipleDatabaseVersions() throws IOException {
    MemoryDatabase writtenDatabase = new MemoryDatabase();
    List<DatabaseVersion> writtenDatabaseVersions = new ArrayList<DatabaseVersion>();
    for (int i = 0; i < 10; i++) {
        DatabaseVersion basedOnDatabaseVersion = (i > 0) ? writtenDatabaseVersions.get(i - 1) : null;
        DatabaseVersion newDatabaseVersion = createDatabaseVersion(basedOnDatabaseVersion);
        // Some random chunks
        newDatabaseVersion.addChunk(new ChunkEntry(new ChunkChecksum(TestFileUtil.createRandomArray(20)), 32 * 1024));
        newDatabaseVersion.addChunk(new ChunkEntry(new ChunkChecksum(TestFileUtil.createRandomArray(20)), 32 * 1024));
        newDatabaseVersion.addChunk(new ChunkEntry(new ChunkChecksum(TestFileUtil.createRandomArray(20)), 32 * 1024));
        // Add to database
        writtenDatabase.addDatabaseVersion(newDatabaseVersion);
        // Add to test array
        writtenDatabaseVersions.add(newDatabaseVersion);
    }
    // Write database to disk, read it again, and compare them
    writeReadAndCompareDatabase(writtenDatabase);
}
Also used : ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) MemoryDatabase(org.syncany.database.MemoryDatabase) ArrayList(java.util.ArrayList) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 3 with MemoryDatabase

use of org.syncany.database.MemoryDatabase in project syncany by syncany.

the class XmlDatabaseDaoTest method writeReadAndCompareDatabase.

private MemoryDatabase writeReadAndCompareDatabase(MemoryDatabase writtenDatabase) throws IOException {
    File writtenDatabaseFile = new File(tempDir + "/db-" + Math.random() + "-" + Math.abs(new Random().nextInt(Integer.MAX_VALUE)));
    TestDatabaseUtil.writeDatabaseFileToDisk(writtenDatabase, writtenDatabaseFile, null);
    MemoryDatabase readDatabase = TestDatabaseUtil.readDatabaseFileFromDisk(writtenDatabaseFile, null);
    TestAssertUtil.assertDatabaseEquals(writtenDatabase, readDatabase);
    return readDatabase;
}
Also used : Random(java.util.Random) MemoryDatabase(org.syncany.database.MemoryDatabase) File(java.io.File)

Example 4 with MemoryDatabase

use of org.syncany.database.MemoryDatabase in project syncany by syncany.

the class XmlDatabaseDaoTest method testWriteAndReadChunks.

@Test
public void testWriteAndReadChunks() throws IOException {
    // Prepare
    MemoryDatabase newDatabase = new MemoryDatabase();
    DatabaseVersion newDatabaseVersion = createDatabaseVersion();
    // Create chunks
    ChunkEntry chunkA1 = new ChunkEntry(new ChunkChecksum(new byte[] { 1, 2, 3, 4, 5, 7, 8, 9, 0 }), 12);
    ChunkEntry chunkA2 = new ChunkEntry(new ChunkChecksum(new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 }), 34);
    ChunkEntry chunkA3 = new ChunkEntry(new ChunkChecksum(new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }), 56);
    ChunkEntry chunkA4 = new ChunkEntry(new ChunkChecksum(new byte[] { 2, 2, 2, 2, 2, 2, 2, 2, 2 }), 78);
    newDatabaseVersion.addChunk(chunkA1);
    newDatabaseVersion.addChunk(chunkA2);
    newDatabaseVersion.addChunk(chunkA3);
    newDatabaseVersion.addChunk(chunkA4);
    // Add database version
    newDatabase.addDatabaseVersion(newDatabaseVersion);
    // Write database to disk, read it again, and compare them
    MemoryDatabase loadedDatabase = writeReadAndCompareDatabase(newDatabase);
    // Check chunks
    assertEquals("Chunk not found in database loaded.", chunkA1, loadedDatabase.getChunk(chunkA1.getChecksum()));
    assertEquals("Chunk not found in database loaded.", chunkA2, loadedDatabase.getChunk(chunkA2.getChecksum()));
    assertEquals("Chunk not found in database loaded.", chunkA3, loadedDatabase.getChunk(chunkA3.getChecksum()));
    assertEquals("Chunk not found in database loaded.", chunkA4, loadedDatabase.getChunk(chunkA4.getChecksum()));
}
Also used : ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) MemoryDatabase(org.syncany.database.MemoryDatabase) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 5 with MemoryDatabase

use of org.syncany.database.MemoryDatabase in project syncany by syncany.

the class FileSystemActionReconciliatorTest method testFileSystemActionReconDeleteNonExistingFolder.

@Test
public void testFileSystemActionReconDeleteNonExistingFolder() throws Exception {
    // Setup
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    Config testConfigA = clientA.getConfig();
    // - Create first database version
    clientA.createNewFolder("new folder/some subfolder");
    clientA.upWithForceChecksum();
    // Delete this!
    clientA.deleteFile("new folder/some subfolder");
    // - Create new version (delete folder)
    TestSqlDatabase sqlDatabaseA = new TestSqlDatabase(testConfigA);
    PartialFileHistory folderFileHistoryWithLastVersion = sqlDatabaseA.getFileHistoryWithLastVersion("new folder/some subfolder");
    FileVersion deletedFolderVersion = folderFileHistoryWithLastVersion.getLastVersion().clone();
    deletedFolderVersion.setStatus(FileStatus.DELETED);
    deletedFolderVersion.setVersion(deletedFolderVersion.getVersion() + 1);
    PartialFileHistory deletedFolderVersionHistory = new PartialFileHistory(folderFileHistoryWithLastVersion.getFileHistoryId());
    deletedFolderVersionHistory.addFileVersion(deletedFolderVersion);
    DatabaseVersion winnersDatabaseVersion = TestDatabaseUtil.createDatabaseVersion(sqlDatabaseA.getLastDatabaseVersionHeader());
    winnersDatabaseVersion.addFileHistory(deletedFolderVersionHistory);
    // - Create memory database with this version
    MemoryDatabase winnersDatabase = new MemoryDatabase();
    winnersDatabase.addDatabaseVersion(winnersDatabaseVersion);
    // Run! Finally!
    DownOperationResult outDownOperationResult = new DownOperationResult();
    FileSystemActionReconciliator fileSystemActionReconciliator = new FileSystemActionReconciliator(testConfigA, outDownOperationResult.getChangeSet());
    List<FileSystemAction> fileSystemActions = fileSystemActionReconciliator.determineFileSystemActions(winnersDatabase);
    assertNotNull(fileSystemActions);
    assertEquals(0, fileSystemActions.size());
    // Tear down
    clientA.deleteTestData();
}
Also used : Config(org.syncany.config.Config) DownOperationResult(org.syncany.operations.down.DownOperationResult) FileSystemAction(org.syncany.operations.down.actions.FileSystemAction) TestClient(org.syncany.tests.util.TestClient) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) TestSqlDatabase(org.syncany.tests.util.TestSqlDatabase) TransferSettings(org.syncany.plugins.transfer.TransferSettings) PartialFileHistory(org.syncany.database.PartialFileHistory) DatabaseVersion(org.syncany.database.DatabaseVersion) FileSystemActionReconciliator(org.syncany.operations.down.FileSystemActionReconciliator) Test(org.junit.Test)

Aggregations

MemoryDatabase (org.syncany.database.MemoryDatabase)26 DatabaseVersion (org.syncany.database.DatabaseVersion)20 Test (org.junit.Test)16 File (java.io.File)9 FileVersion (org.syncany.database.FileVersion)8 PartialFileHistory (org.syncany.database.PartialFileHistory)8 ChunkEntry (org.syncany.database.ChunkEntry)7 ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)7 MultiChunkEntry (org.syncany.database.MultiChunkEntry)7 ArrayList (java.util.ArrayList)6 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)6 DatabaseXmlSerializer (org.syncany.database.dao.DatabaseXmlSerializer)5 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)5 IOException (java.io.IOException)3 FileChecksum (org.syncany.database.FileContent.FileChecksum)3 VectorClock (org.syncany.database.VectorClock)3 MultichunkRemoteFile (org.syncany.plugins.transfer.files.MultichunkRemoteFile)3 TransactionRemoteFile (org.syncany.plugins.transfer.files.TransactionRemoteFile)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2