Search in sources :

Example 11 with DatabaseVersion

use of org.syncany.database.DatabaseVersion 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 12 with DatabaseVersion

use of org.syncany.database.DatabaseVersion 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 13 with DatabaseVersion

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

the class XmlDatabaseDaoTest method testWriteAndReadFileHistoryAndFileVersion.

@Test
public void testWriteAndReadFileHistoryAndFileVersion() throws IOException {
    // Prepare
    MemoryDatabase newDatabase = new MemoryDatabase();
    DatabaseVersion newDatabaseVersion = createDatabaseVersion();
    // Create directories (no content!)
    // File A
    PartialFileHistory fileHistoryA = new PartialFileHistory(FileHistoryId.secureRandomFileId());
    newDatabaseVersion.addFileHistory(fileHistoryA);
    FileVersion versionA1 = new FileVersion();
    versionA1.setVersion(1L);
    versionA1.setType(FileType.FOLDER);
    versionA1.setPath("Pictures/2013/New York Folder");
    versionA1.setStatus(FileStatus.NEW);
    versionA1.setSize(0L);
    versionA1.setLastModified(new Date());
    fileHistoryA.addFileVersion(versionA1);
    FileVersion versionA2 = new FileVersion();
    versionA2.setVersion(2L);
    versionA2.setType(FileType.FOLDER);
    versionA2.setPath("Pictures/2013/New York");
    versionA2.setStatus(FileStatus.RENAMED);
    versionA2.setSize(0L);
    versionA2.setLastModified(new Date());
    fileHistoryA.addFileVersion(versionA2);
    // File B
    PartialFileHistory fileHistoryB = new PartialFileHistory(FileHistoryId.secureRandomFileId());
    newDatabaseVersion.addFileHistory(fileHistoryB);
    FileVersion versionB1 = new FileVersion();
    versionB1.setVersion(1L);
    versionB1.setType(FileType.FOLDER);
    versionB1.setPath("Pictures/2013/Egypt Folder");
    versionB1.setStatus(FileStatus.NEW);
    versionB1.setSize(0L);
    versionB1.setLastModified(new Date());
    fileHistoryB.addFileVersion(versionB1);
    FileVersion versionB2 = new FileVersion();
    versionB2.setVersion(2L);
    versionB2.setType(FileType.FOLDER);
    versionB2.setPath("Pictures/2013/Egypt");
    versionB2.setStatus(FileStatus.RENAMED);
    versionB2.setSize(0L);
    versionB2.setLastModified(new Date());
    fileHistoryB.addFileVersion(versionB2);
    // Add database version
    newDatabase.addDatabaseVersion(newDatabaseVersion);
    // Write database to disk, read it again, and compare them
    MemoryDatabase loadedDatabase = writeReadAndCompareDatabase(newDatabase);
    // File histories
    PartialFileHistory loadedFileHistoryA = loadedDatabase.getFileHistory(fileHistoryA.getFileHistoryId());
    PartialFileHistory loadedFileHistoryB = loadedDatabase.getFileHistory(fileHistoryB.getFileHistoryId());
    assertEquals("File history not found in database loaded.", fileHistoryA, loadedFileHistoryA);
    assertEquals("File history not found in database loaded.", fileHistoryB, loadedFileHistoryB);
    assertArrayEquals("File versions differ in loaded database.", fileHistoryA.getFileVersions().values().toArray(), loadedFileHistoryA.getFileVersions().values().toArray());
    assertArrayEquals("File versions differ in loaded database.", fileHistoryB.getFileVersions().values().toArray(), loadedFileHistoryB.getFileVersions().values().toArray());
}
Also used : FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) PartialFileHistory(org.syncany.database.PartialFileHistory) Date(java.util.Date) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 14 with DatabaseVersion

use of org.syncany.database.DatabaseVersion 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 15 with DatabaseVersion

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

the class MemoryDatabaseCacheTest method testFilenameCache.

@Test
public void testFilenameCache() throws IOException {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add file history & version
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    FileVersion fileVersion1 = TestDatabaseUtil.createFileVersion("file1.jpg");
    FileHistoryId idFile1 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1 = new PartialFileHistory(idFile1);
    fileHistory1.addFileVersion(fileVersion1);
    databaseVersion1.addFileHistory(fileHistory1);
    database.addDatabaseVersion(databaseVersion1);
    assertEquals(fileHistory1, database.getFileHistory("file1.jpg"));
    // Round 2: Add new version
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    FileVersion fileVersion2 = TestDatabaseUtil.createFileVersion("file2.jpg", fileVersion1);
    // same ID
    FileHistoryId idFile2 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory2 = new PartialFileHistory(idFile2);
    fileHistory2.addFileVersion(fileVersion2);
    databaseVersion2.addFileHistory(fileHistory2);
    database.addDatabaseVersion(databaseVersion2);
    assertNotNull(database.getFileHistory("file2.jpg"));
    assertEquals(2, database.getFileHistory("file2.jpg").getFileVersions().size());
    assertNull(database.getFileHistory("file1.jpg"));
    // Round 3: Add deleted version
    DatabaseVersion databaseVersion3 = TestDatabaseUtil.createDatabaseVersion(databaseVersion2);
    FileVersion fileVersion3 = TestDatabaseUtil.createFileVersion("file2.jpg", fileVersion2);
    fileVersion3.setStatus(FileStatus.DELETED);
    // same ID
    FileHistoryId idFile3 = FileHistoryId.parseFileId("1111111111111111");
    // same ID
    PartialFileHistory fileHistory3 = new PartialFileHistory(idFile3);
    fileHistory3.addFileVersion(fileVersion3);
    databaseVersion3.addFileHistory(fileHistory3);
    database.addDatabaseVersion(databaseVersion3);
    assertNull(database.getFileHistory("file2.jpg"));
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) PartialFileHistory(org.syncany.database.PartialFileHistory) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Aggregations

DatabaseVersion (org.syncany.database.DatabaseVersion)43 Test (org.junit.Test)22 MemoryDatabase (org.syncany.database.MemoryDatabase)20 MultiChunkEntry (org.syncany.database.MultiChunkEntry)12 PartialFileHistory (org.syncany.database.PartialFileHistory)12 ChunkEntry (org.syncany.database.ChunkEntry)11 FileVersion (org.syncany.database.FileVersion)10 ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)9 ArrayList (java.util.ArrayList)8 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)8 File (java.io.File)7 Config (org.syncany.config.Config)7 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)6 ChunkSqlDao (org.syncany.database.dao.ChunkSqlDao)6 DatabaseVersionSqlDao (org.syncany.database.dao.DatabaseVersionSqlDao)6 FileContentSqlDao (org.syncany.database.dao.FileContentSqlDao)6 FileHistorySqlDao (org.syncany.database.dao.FileHistorySqlDao)6 FileVersionSqlDao (org.syncany.database.dao.FileVersionSqlDao)6 MultiChunkSqlDao (org.syncany.database.dao.MultiChunkSqlDao)6 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)6