Search in sources :

Example 11 with FileContent

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

the class FileContentDaoTest method testGetFileContentsByVectorClock.

@Test
public void testGetFileContentsByVectorClock() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set3.sql");
    FileContentSqlDao fileContentDao = new FileContentSqlDao(databaseConnection);
    Map<FileChecksum, FileContent> fileContentsA1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A1"));
    Map<FileChecksum, FileContent> fileContentsA2 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A2"));
    Map<FileChecksum, FileContent> fileContentsA3 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A3"));
    Map<FileChecksum, FileContent> fileContentsA4 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A4"));
    Map<FileChecksum, FileContent> fileContentsA5 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A5"));
    Map<FileChecksum, FileContent> fileContentsA6 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A6"));
    Map<FileChecksum, FileContent> fileContentsA6B1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A6,B1"));
    Map<FileChecksum, FileContent> fileContentsA7B1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A7,B1"));
    Map<FileChecksum, FileContent> fileContentsInvalid1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("Invalid1"));
    // Test
    assertNotNull(fileContentsA1);
    assertEquals(0, fileContentsA1.size());
    assertNotNull(fileContentsA2);
    assertEquals(0, fileContentsA2.size());
    assertNotNull(fileContentsA3);
    assertEquals(0, fileContentsA3.size());
    assertNotNull(fileContentsA4);
    assertEquals(0, fileContentsA4.size());
    assertNotNull(fileContentsA5);
    assertEquals(1, fileContentsA5.size());
    assertNotNull(fileContentsA5.get(FileChecksum.parseFileChecksum("0fefb345b62b6c0b0e5212158a9aa7c1eeec2ca6")));
    assertEquals(12, fileContentsA5.get(FileChecksum.parseFileChecksum("0fefb345b62b6c0b0e5212158a9aa7c1eeec2ca6")).getSize());
    assertNotNull(fileContentsA6);
    assertEquals(1, fileContentsA6.size());
    assertNotNull(fileContentsA6.get(FileChecksum.parseFileChecksum("24a39e00d6156804e27f7c0987d00903da8e6682")));
    assertEquals(508, fileContentsA6.get(FileChecksum.parseFileChecksum("24a39e00d6156804e27f7c0987d00903da8e6682")).getSize());
    assertNotNull(fileContentsA6B1);
    assertEquals(1, fileContentsA6B1.size());
    assertNotNull(fileContentsA6B1.get(FileChecksum.parseFileChecksum("7666fd3b860c9d7588d9ca1807eebdf8cfaa8be3")));
    assertEquals(2029, fileContentsA6B1.get(FileChecksum.parseFileChecksum("7666fd3b860c9d7588d9ca1807eebdf8cfaa8be3")).getSize());
    assertNotNull(fileContentsA7B1);
    assertEquals(1, fileContentsA7B1.size());
    assertNotNull(fileContentsA7B1.get(FileChecksum.parseFileChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0")));
    assertEquals(512, fileContentsA7B1.get(FileChecksum.parseFileChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0")).getSize());
    assertNotNull(fileContentsInvalid1);
    assertEquals(0, fileContentsInvalid1.size());
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : FileContent(org.syncany.database.FileContent) Config(org.syncany.config.Config) FileContentSqlDao(org.syncany.database.dao.FileContentSqlDao) Connection(java.sql.Connection) FileChecksum(org.syncany.database.FileContent.FileChecksum) Test(org.junit.Test)

Example 12 with FileContent

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

the class FileContentSqlDao method getFileContentWithoutChunkChecksums.

private FileContent getFileContentWithoutChunkChecksums(FileChecksum fileChecksum) {
    try (PreparedStatement preparedStatement = getStatement("filecontent.select.all.getFileContentByChecksumWithoutChunkChecksums.sql")) {
        preparedStatement.setString(1, fileChecksum.toString());
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                FileContent fileContent = new FileContent();
                fileContent.setChecksum(FileChecksum.parseFileChecksum(resultSet.getString("checksum")));
                fileContent.setSize(resultSet.getLong("size"));
                return fileContent;
            }
        }
        return null;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileContent(org.syncany.database.FileContent) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 13 with FileContent

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

the class FileContentSqlDao method getFileContentWithChunkChecksums.

private FileContent getFileContentWithChunkChecksums(FileChecksum fileChecksum) {
    try (PreparedStatement preparedStatement = getStatement("filecontent.select.all.getFileContentByChecksumWithChunkChecksums.sql")) {
        preparedStatement.setString(1, fileChecksum.toString());
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            FileContent fileContent = null;
            while (resultSet.next()) {
                if (fileContent == null) {
                    fileContent = new FileContent();
                    fileContent.setChecksum(FileChecksum.parseFileChecksum(resultSet.getString("checksum")));
                    fileContent.setSize(resultSet.getLong("size"));
                }
                // Add chunk references
                ChunkChecksum chunkChecksum = ChunkChecksum.parseChunkChecksum(resultSet.getString("chunk_checksum"));
                fileContent.addChunk(chunkChecksum);
            }
            return fileContent;
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileContent(org.syncany.database.FileContent) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum)

Example 14 with FileContent

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

the class ApplyChangesOperation method determineMultiChunksToDownload.

/**
 * Finds the multichunks that need to be downloaded for the given file version -- using the local
 * database and given winners database. Returns a set of multichunk identifiers.
 */
private Collection<MultiChunkId> determineMultiChunksToDownload(FileVersion fileVersion, MemoryDatabase winnersDatabase) {
    Set<MultiChunkId> multiChunksToDownload = new HashSet<MultiChunkId>();
    // First: Check if we know this file locally!
    List<MultiChunkId> multiChunkIds = localDatabase.getMultiChunkIds(fileVersion.getChecksum());
    if (multiChunkIds.size() > 0) {
        multiChunksToDownload.addAll(multiChunkIds);
    } else {
        // Second: We don't know it locally; must be from the winners database
        FileContent winningFileContent = winnersDatabase.getContent(fileVersion.getChecksum());
        boolean winningFileHasContent = winningFileContent != null;
        if (winningFileHasContent) {
            // File can be empty!
            List<ChunkChecksum> fileChunks = winningFileContent.getChunks();
            // TODO [medium] Instead of just looking for multichunks to download here, we should look for chunks in local files as well
            // and return the chunk positions in the local files ChunkPosition (chunk123 at file12, offset 200, size 250)
            Map<ChunkChecksum, MultiChunkId> checksumsWithMultiChunkIds = localDatabase.getMultiChunkIdsByChecksums(fileChunks);
            for (ChunkChecksum chunkChecksum : fileChunks) {
                MultiChunkId multiChunkIdForChunk = checksumsWithMultiChunkIds.get(chunkChecksum);
                if (multiChunkIdForChunk == null) {
                    multiChunkIdForChunk = winnersDatabase.getMultiChunkIdForChunk(chunkChecksum);
                    if (multiChunkIdForChunk == null) {
                        throw new RuntimeException("Cannot find multichunk for chunk " + chunkChecksum);
                    }
                }
                if (!multiChunksToDownload.contains(multiChunkIdForChunk)) {
                    logger.log(Level.INFO, "  + Adding multichunk " + multiChunkIdForChunk + " to download list ...");
                    multiChunksToDownload.add(multiChunkIdForChunk);
                }
            }
        }
    }
    return multiChunksToDownload;
}
Also used : FileContent(org.syncany.database.FileContent) MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) HashSet(java.util.HashSet)

Example 15 with FileContent

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

the class UpOperation method addDirtyData.

/**
 * This methods iterates over all {@link DatabaseVersion}s that are dirty. Dirty means that they are not in the winning
 * branch. All data which is contained in these dirty DatabaseVersions is added to the newDatabaseVersion, so that it
 * is included in the new Up. Note that only metadata is reuploaded, the actual multichunks are still in the repository.
 *
 * @param newDatabaseVersion {@link DatabaseVersion} to which dirty data should be added.
 */
private void addDirtyData(DatabaseVersion newDatabaseVersion) {
    Iterator<DatabaseVersion> dirtyDatabaseVersions = localDatabase.getDirtyDatabaseVersions();
    if (!dirtyDatabaseVersions.hasNext()) {
        logger.log(Level.INFO, "No DIRTY data found in database (no dirty databases); Nothing to do here.");
    } else {
        logger.log(Level.INFO, "Adding DIRTY data to new database version: ");
        while (dirtyDatabaseVersions.hasNext()) {
            DatabaseVersion dirtyDatabaseVersion = dirtyDatabaseVersions.next();
            logger.log(Level.INFO, "- Adding chunks/multichunks/filecontents from database version " + dirtyDatabaseVersion.getHeader());
            for (ChunkEntry chunkEntry : dirtyDatabaseVersion.getChunks()) {
                newDatabaseVersion.addChunk(chunkEntry);
            }
            for (MultiChunkEntry multiChunkEntry : dirtyDatabaseVersion.getMultiChunks()) {
                newDatabaseVersion.addMultiChunk(multiChunkEntry);
            }
            for (FileContent fileContent : dirtyDatabaseVersion.getFileContents()) {
                newDatabaseVersion.addFileContent(fileContent);
            }
        }
    }
}
Also used : FileContent(org.syncany.database.FileContent) ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) DatabaseVersion(org.syncany.database.DatabaseVersion)

Aggregations

FileContent (org.syncany.database.FileContent)15 ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)9 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)6 ChunkEntry (org.syncany.database.ChunkEntry)5 DatabaseVersion (org.syncany.database.DatabaseVersion)5 MultiChunkEntry (org.syncany.database.MultiChunkEntry)5 Test (org.junit.Test)4 FileChecksum (org.syncany.database.FileContent.FileChecksum)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 Config (org.syncany.config.Config)3 FileVersion (org.syncany.database.FileVersion)3 PartialFileHistory (org.syncany.database.PartialFileHistory)3 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)3 FileContentSqlDao (org.syncany.database.dao.FileContentSqlDao)3 File (java.io.File)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 DatabaseVersionHeader (org.syncany.database.DatabaseVersionHeader)2