Search in sources :

Example 21 with MemoryDatabase

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

the class XmlDatabaseDaoTest method testWriteAndReadChunksWithMultiChunks.

@Test
public void testWriteAndReadChunksWithMultiChunks() 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);
    ChunkEntry chunkB1 = new ChunkEntry(new ChunkChecksum(new byte[] { 3, 3, 3, 3, 3, 3, 3, 3, 3 }), 910);
    ChunkEntry chunkB2 = new ChunkEntry(new ChunkChecksum(new byte[] { 4, 4, 4, 4, 4, 4, 4, 4, 4 }), 1112);
    newDatabaseVersion.addChunk(chunkA1);
    newDatabaseVersion.addChunk(chunkA2);
    newDatabaseVersion.addChunk(chunkA3);
    newDatabaseVersion.addChunk(chunkA4);
    newDatabaseVersion.addChunk(chunkB1);
    newDatabaseVersion.addChunk(chunkB2);
    // Distribute chunks to multichunks
    MultiChunkEntry multiChunkA = new MultiChunkEntry(new MultiChunkId(new byte[] { 6, 6, 6, 6, 6, 6, 6, 6, 6 }), 10);
    multiChunkA.addChunk(chunkA1.getChecksum());
    multiChunkA.addChunk(chunkA2.getChecksum());
    multiChunkA.addChunk(chunkA3.getChecksum());
    newDatabaseVersion.addMultiChunk(multiChunkA);
    MultiChunkEntry multiChunkB = new MultiChunkEntry(new MultiChunkId(new byte[] { 7, 7, 7, 7, 7, 7, 7, 7, 7 }), 11);
    multiChunkB.addChunk(chunkA4.getChecksum());
    multiChunkB.addChunk(chunkB1.getChecksum());
    multiChunkB.addChunk(chunkB2.getChecksum());
    newDatabaseVersion.addMultiChunk(multiChunkB);
    // 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()));
    assertEquals("Chunk not found in database loaded.", chunkB1, loadedDatabase.getChunk(chunkB1.getChecksum()));
    assertEquals("Chunk not found in database loaded.", chunkB2, loadedDatabase.getChunk(chunkB2.getChecksum()));
    // Check multichunks
    MultiChunkEntry loadedMultiChunkA = loadedDatabase.getMultiChunk(multiChunkA.getId());
    MultiChunkEntry loadedMultiChunkB = loadedDatabase.getMultiChunk(multiChunkB.getId());
    assertEquals("Multichunk not found in database loaded.", multiChunkA, loadedMultiChunkA);
    assertEquals("Multichunk not found in database loaded.", multiChunkB, loadedMultiChunkB);
    assertArrayEquals("Chunks in multichunk expected to be different.", multiChunkA.getChunks().toArray(), loadedMultiChunkA.getChunks().toArray());
    assertArrayEquals("Chunks in multichunk expected to be different.", multiChunkB.getChunks().toArray(), loadedMultiChunkB.getChunks().toArray());
}
Also used : MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) 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 22 with MemoryDatabase

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

the class MemoryDatabaseCacheTest method testFilenameCacheDeleteAndNewOfSameFileInOneDatabaseVersion.

@Test
public void testFilenameCacheDeleteAndNewOfSameFileInOneDatabaseVersion() 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);
    // - delete file1.jpg
    FileVersion fileVersion2 = TestDatabaseUtil.createFileVersion("file1.jpg", fileVersion1);
    fileVersion2.setStatus(FileStatus.DELETED);
    // same ID
    FileHistoryId idFile2 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory2 = new PartialFileHistory(idFile2);
    fileHistory2.addFileVersion(fileVersion2);
    databaseVersion2.addFileHistory(fileHistory2);
    // - add file1.jpg (as FOLDER!)
    // new file!
    FileVersion fileVersion3 = TestDatabaseUtil.createFileVersion("file1.jpg");
    fileVersion3.setType(FileType.FOLDER);
    // different ID
    FileHistoryId idFile3 = FileHistoryId.parseFileId("1111111111111112");
    // same ID
    PartialFileHistory fileHistory3 = new PartialFileHistory(idFile3);
    fileHistory3.addFileVersion(fileVersion3);
    databaseVersion2.addFileHistory(fileHistory3);
    // - add database version
    database.addDatabaseVersion(databaseVersion2);
    assertNotNull(database.getFileHistory("file1.jpg"));
    assertEquals(1, database.getFileHistory("file1.jpg").getFileVersions().size());
    assertEquals(fileHistory3, database.getFileHistory("file1.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)

Example 23 with MemoryDatabase

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

the class MemoryDatabaseCacheTest method testContentChecksumCache.

@Test
public void testContentChecksumCache() throws IOException {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add file history & version
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    // - history 1, version 1
    FileVersion fileVersion1 = TestDatabaseUtil.createFileVersion("samechecksum1.jpg");
    fileVersion1.setChecksum(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }));
    FileHistoryId idFile1 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1 = new PartialFileHistory(idFile1);
    fileHistory1.addFileVersion(fileVersion1);
    databaseVersion1.addFileHistory(fileHistory1);
    database.addDatabaseVersion(databaseVersion1);
    assertNotNull(database.getFileHistories(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })));
    assertEquals(1, database.getFileHistories(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })).size());
    // Round 2: Add two other versions with same checksum to new database version
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    // - history 1, version 2
    FileVersion fileVersion11 = TestDatabaseUtil.createFileVersion("samechecksum2-renamed.jpg", fileVersion1);
    // same checksum!
    fileVersion11.setChecksum(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }));
    fileVersion11.setStatus(FileStatus.RENAMED);
    FileHistoryId idFile11 = FileHistoryId.parseFileId("1111111111111111");
    // same ID as above
    PartialFileHistory fileHistory11 = new PartialFileHistory(idFile11);
    fileHistory11.addFileVersion(fileVersion11);
    databaseVersion2.addFileHistory(fileHistory11);
    // - history 2, version 1
    FileVersion fileVersion2 = TestDatabaseUtil.createFileVersion("samechecksum2.jpg");
    // same checksum!
    fileVersion2.setChecksum(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }));
    // different ID
    FileHistoryId idFile2 = FileHistoryId.parseFileId("1111111122222222");
    PartialFileHistory fileHistory2 = new PartialFileHistory(idFile2);
    fileHistory2.addFileVersion(fileVersion2);
    databaseVersion2.addFileHistory(fileHistory2);
    // - history 3, version 1
    FileVersion fileVersion3 = TestDatabaseUtil.createFileVersion("samechecksum3.jpg");
    // same checksum!
    fileVersion3.setChecksum(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }));
    // different ID
    FileHistoryId idFile3 = FileHistoryId.parseFileId("aaaaaaaa22222222");
    // different ID
    PartialFileHistory fileHistory3 = new PartialFileHistory(idFile3);
    fileHistory3.addFileVersion(fileVersion3);
    databaseVersion2.addFileHistory(fileHistory3);
    database.addDatabaseVersion(databaseVersion2);
    assertNotNull(database.getFileHistories(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })));
    assertEquals(3, database.getFileHistories(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })).size());
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) FileChecksum(org.syncany.database.FileContent.FileChecksum) PartialFileHistory(org.syncany.database.PartialFileHistory) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 24 with MemoryDatabase

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

the class MemoryDatabaseCacheTest method testChunkCache.

@Test
public void testChunkCache() throws IOException {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add chunk to new database version, then add database version
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    ChunkEntry chunkA1 = new ChunkEntry(new ChunkChecksum(new byte[] { 1, 2, 3, 4, 5, 7, 8, 9, 0 }), 12);
    databaseVersion1.addChunk(chunkA1);
    database.addDatabaseVersion(databaseVersion1);
    assertEquals(chunkA1, database.getChunk(new ChunkChecksum(new byte[] { 1, 2, 3, 4, 5, 7, 8, 9, 0 })));
    // Round 2: Add chunk to new database version, then add database version
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    ChunkEntry chunkA2 = new ChunkEntry(new ChunkChecksum(new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 }), 112);
    databaseVersion2.addChunk(chunkA2);
    database.addDatabaseVersion(databaseVersion2);
    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, 8, 7, 6, 5, 4, 3, 2, 1 })));
    // Round 3: Add chunk to new database version, then add database version
    DatabaseVersion databaseVersion3 = TestDatabaseUtil.createDatabaseVersion(databaseVersion2);
    ChunkEntry chunkA3 = new ChunkEntry(new ChunkChecksum(new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }), 192);
    databaseVersion3.addChunk(chunkA3);
    database.addDatabaseVersion(databaseVersion3);
    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, 8, 7, 6, 5, 4, 3, 2, 1 })));
    assertEquals(chunkA3, database.getChunk(new ChunkChecksum(new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 })));
}
Also used : MultiChunkEntry(org.syncany.database.MultiChunkEntry) ChunkEntry(org.syncany.database.ChunkEntry) MemoryDatabase(org.syncany.database.MemoryDatabase) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 25 with MemoryDatabase

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

the class UpOperationTest method testUploadLocalDatabase.

@Test
public void testUploadLocalDatabase() throws Exception {
    int fileSize = 1230 * 1024;
    int fileAmount = 3;
    List<File> originalFiles = TestFileUtil.createRandomFilesInDirectory(testConfig.getLocalDir(), fileSize, fileAmount);
    // Run!
    AbstractTransferOperation op = new UpOperation(testConfig);
    op.execute();
    // Get databases (for comparison)
    LocalTransferSettings localConnection = (LocalTransferSettings) testConfig.getConnection();
    File localDatabaseDir = testConfig.getDatabaseDir();
    File remoteDatabaseFile = new File(localConnection.getPath() + "/databases/database-" + testConfig.getMachineName() + "-0000000001");
    assertNotNull(localDatabaseDir.listFiles());
    assertTrue(localDatabaseDir.listFiles().length > 0);
    assertTrue(remoteDatabaseFile.exists());
    // - Memory database
    DatabaseXmlSerializer dDAO = new DatabaseXmlSerializer(testConfig.getTransformer());
    MemoryDatabase remoteDatabase = new MemoryDatabase();
    dDAO.load(remoteDatabase, remoteDatabaseFile, null, null, DatabaseReadType.FULL);
    DatabaseVersion remoteDatabaseVersion = remoteDatabase.getLastDatabaseVersion();
    // - Sql Database
    SqlDatabase localDatabase = new SqlDatabase(testConfig);
    Map<FileHistoryId, PartialFileHistory> localFileHistories = localDatabase.getFileHistoriesWithFileVersions();
    // Compare!
    assertEquals(localDatabase.getLastDatabaseVersionHeader(), remoteDatabaseVersion.getHeader());
    assertEquals(localFileHistories.size(), fileAmount);
    assertEquals(localDatabase.getFileHistoriesWithFileVersions().size(), remoteDatabaseVersion.getFileHistories().size());
    Collection<PartialFileHistory> remoteFileHistories = remoteDatabaseVersion.getFileHistories();
    List<FileVersion> remoteFileVersions = new ArrayList<FileVersion>();
    List<FileVersion> localFileVersions = new ArrayList<FileVersion>();
    for (PartialFileHistory partialFileHistory : remoteFileHistories) {
        remoteFileVersions.add(partialFileHistory.getLastVersion());
        assertNotNull(localFileHistories.get(partialFileHistory.getFileHistoryId()));
    }
    for (PartialFileHistory partialFileHistory : localFileHistories.values()) {
        localFileVersions.add(partialFileHistory.getLastVersion());
    }
    assertTrue(CollectionUtil.containsExactly(localFileVersions, remoteFileVersions));
    compareFileVersionsAgainstOriginalFiles(originalFiles, localFileVersions);
    compareFileVersionsAgainstOriginalFiles(originalFiles, remoteFileVersions);
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) ArrayList(java.util.ArrayList) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) PartialFileHistory(org.syncany.database.PartialFileHistory) AbstractTransferOperation(org.syncany.operations.AbstractTransferOperation) UpOperation(org.syncany.operations.up.UpOperation) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) SqlDatabase(org.syncany.database.SqlDatabase) File(java.io.File) DatabaseVersion(org.syncany.database.DatabaseVersion) 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