Search in sources :

Example 6 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum 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 7 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum 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 8 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class MemoryDatabaseCacheTest method testMultiChunkCache.

@Test
public void testMultiChunkCache() throws IOException {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add chunk to multichunk
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    MultiChunkEntry multiChunkP1 = new MultiChunkEntry(new MultiChunkId(new byte[] { 8, 8, 8, 8, 8, 8, 8, 8 }), 10);
    ChunkEntry chunkA1 = new ChunkEntry(new ChunkChecksum(new byte[] { 1, 2, 3, 4, 5, 7, 8, 9, 0 }), 12);
    multiChunkP1.addChunk(chunkA1.getChecksum());
    databaseVersion1.addChunk(chunkA1);
    databaseVersion1.addMultiChunk(multiChunkP1);
    database.addDatabaseVersion(databaseVersion1);
    assertEquals(chunkA1, database.getChunk(new ChunkChecksum(new byte[] { 1, 2, 3, 4, 5, 7, 8, 9, 0 })));
    assertEquals(multiChunkP1, database.getMultiChunk(new MultiChunkId(new byte[] { 8, 8, 8, 8, 8, 8, 8, 8 })));
    // Round 2: Add chunk to multichunk
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    MultiChunkEntry multiChunkP2 = new MultiChunkEntry(new MultiChunkId(new byte[] { 7, 7, 7, 7, 7, 7, 7, 7, 7 }), 11);
    MultiChunkEntry multiChunkP3 = new MultiChunkEntry(new MultiChunkId(new byte[] { 5, 5, 5, 5, 5, 5, 5, 5, 5 }), 12);
    ChunkEntry chunkA2 = new ChunkEntry(new ChunkChecksum(new byte[] { 9, 2, 3, 4, 5, 7, 8, 9, 0 }), 912);
    ChunkEntry chunkA3 = new ChunkEntry(new ChunkChecksum(new byte[] { 8, 2, 3, 4, 5, 7, 8, 9, 0 }), 812);
    ChunkEntry chunkA4 = new ChunkEntry(new ChunkChecksum(new byte[] { 7, 2, 3, 4, 5, 7, 8, 9, 0 }), 712);
    multiChunkP2.addChunk(chunkA2.getChecksum());
    multiChunkP2.addChunk(chunkA3.getChecksum());
    multiChunkP3.addChunk(chunkA4.getChecksum());
    databaseVersion2.addChunk(chunkA2);
    databaseVersion2.addChunk(chunkA3);
    databaseVersion2.addChunk(chunkA4);
    databaseVersion2.addMultiChunk(multiChunkP2);
    databaseVersion2.addMultiChunk(multiChunkP3);
    database.addDatabaseVersion(databaseVersion2);
    // fail("xx");
    assertEquals(chunkA1, database.getChunk(new ChunkChecksum(new byte[] { 1, 2, 3, 4, 5, 7, 8, 9, 0 })));
    assertEquals(chunkA2, database.getChunk(new ChunkChecksum(new byte[] { 9, 2, 3, 4, 5, 7, 8, 9, 0 })));
    assertEquals(chunkA3, database.getChunk(new ChunkChecksum(new byte[] { 8, 2, 3, 4, 5, 7, 8, 9, 0 })));
    assertEquals(chunkA4, database.getChunk(new ChunkChecksum(new byte[] { 7, 2, 3, 4, 5, 7, 8, 9, 0 })));
    assertEquals(multiChunkP1, database.getMultiChunk(new MultiChunkId(new byte[] { 8, 8, 8, 8, 8, 8, 8, 8 })));
    assertEquals(multiChunkP2, database.getMultiChunk(new MultiChunkId(new byte[] { 7, 7, 7, 7, 7, 7, 7, 7, 7 })));
    assertEquals(multiChunkP3, database.getMultiChunk(new MultiChunkId(new byte[] { 5, 5, 5, 5, 5, 5, 5, 5, 5 })));
}
Also used : MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) MultiChunkEntry(org.syncany.database.MultiChunkEntry) ChunkEntry(org.syncany.database.ChunkEntry) MemoryDatabase(org.syncany.database.MemoryDatabase) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) MultiChunkEntry(org.syncany.database.MultiChunkEntry) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 9 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class ChunkSqlDao method createChunkEntries.

protected Map<ChunkChecksum, ChunkEntry> createChunkEntries(ResultSet resultSet) throws SQLException {
    Map<ChunkChecksum, ChunkEntry> chunks = new HashMap<ChunkChecksum, ChunkEntry>();
    while (resultSet.next()) {
        ChunkEntry chunkEntry = createChunkEntryFromRow(resultSet);
        chunks.put(chunkEntry.getChecksum(), chunkEntry);
    }
    return chunks;
}
Also used : HashMap(java.util.HashMap) ChunkEntry(org.syncany.database.ChunkEntry) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum)

Example 10 with ChunkChecksum

use of org.syncany.database.ChunkEntry.ChunkChecksum in project syncany by syncany.

the class DatabaseVersionSqlDao method createDatabaseVersionFromRow.

protected DatabaseVersion createDatabaseVersionFromRow(ResultSet resultSet, boolean excludeChunkData, int fileHistoryMaxCount) throws SQLException {
    DatabaseVersionHeader databaseVersionHeader = createDatabaseVersionHeaderFromRow(resultSet);
    DatabaseVersion databaseVersion = new DatabaseVersion();
    databaseVersion.setHeader(databaseVersionHeader);
    // Add chunk/multichunk/filecontent data
    if (!excludeChunkData) {
        Map<ChunkChecksum, ChunkEntry> chunks = chunkDao.getChunks(databaseVersionHeader.getVectorClock());
        Map<MultiChunkId, MultiChunkEntry> multiChunks = multiChunkDao.getMultiChunks(databaseVersionHeader.getVectorClock());
        Map<FileChecksum, FileContent> fileContents = fileContentDao.getFileContents(databaseVersionHeader.getVectorClock());
        for (ChunkEntry chunk : chunks.values()) {
            databaseVersion.addChunk(chunk);
        }
        for (MultiChunkEntry multiChunk : multiChunks.values()) {
            databaseVersion.addMultiChunk(multiChunk);
        }
        for (FileContent fileContent : fileContents.values()) {
            databaseVersion.addFileContent(fileContent);
        }
    }
    // Add file histories
    Map<FileHistoryId, PartialFileHistory> fileHistories = fileHistoryDao.getFileHistoriesWithFileVersions(databaseVersionHeader.getVectorClock(), fileHistoryMaxCount);
    for (PartialFileHistory fileHistory : fileHistories.values()) {
        databaseVersion.addFileHistory(fileHistory);
    }
    return databaseVersion;
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileChecksum(org.syncany.database.FileContent.FileChecksum) PartialFileHistory(org.syncany.database.PartialFileHistory) FileContent(org.syncany.database.FileContent) ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) MultiChunkEntry(org.syncany.database.MultiChunkEntry) DatabaseVersion(org.syncany.database.DatabaseVersion)

Aggregations

ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)26 ChunkEntry (org.syncany.database.ChunkEntry)11 MultiChunkEntry (org.syncany.database.MultiChunkEntry)10 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)10 Test (org.junit.Test)9 DatabaseVersion (org.syncany.database.DatabaseVersion)9 FileContent (org.syncany.database.FileContent)9 File (java.io.File)7 MemoryDatabase (org.syncany.database.MemoryDatabase)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 PreparedStatement (java.sql.PreparedStatement)4 MultiChunk (org.syncany.chunk.MultiChunk)3 FileChecksum (org.syncany.database.FileContent.FileChecksum)3 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2