Search in sources :

Example 6 with MemoryDatabase

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

the class MemoryDatabaseCacheTest method testRemoveDatabaseVersion.

@Test
public void testRemoveDatabaseVersion() {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add file history & version
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    // - history 1, version 1
    FileVersion fileVersion1 = TestDatabaseUtil.createFileVersion("file.jpg");
    FileHistoryId idFile1 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1 = new PartialFileHistory(idFile1);
    fileHistory1.addFileVersion(fileVersion1);
    databaseVersion1.addFileHistory(fileHistory1);
    database.addDatabaseVersion(databaseVersion1);
    // - history 1, version 2		
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    FileVersion fileVersion2 = TestDatabaseUtil.createFileVersion("file.jpg", fileVersion1);
    FileHistoryId idFile1b = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1b = new PartialFileHistory(idFile1b);
    fileHistory1b.addFileVersion(fileVersion2);
    databaseVersion2.addFileHistory(fileHistory1b);
    database.addDatabaseVersion(databaseVersion2);
    // Tests: Database should have 2 versions of file
    assertEquals(2, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(2, database.getFileHistory(idFile1b).getFileVersions().size());
    // Round 2: Remove second database version
    database.removeDatabaseVersion(databaseVersion2);
    // Tests: Second version removed, 1 version left
    assertEquals(1, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(1, database.getFileHistory(idFile1b).getFileVersions().size());
    assertEquals(fileVersion1, database.getFileHistory(idFile1).getLastVersion());
    // Round 3: Add database version again
    database.addDatabaseVersion(databaseVersion2);
    // Tests: Second version added, 2 versions of file
    assertEquals(2, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(2, database.getFileHistory(idFile1b).getFileVersions().size());
    // Round 4: Remove FIRST database version		
    database.removeDatabaseVersion(databaseVersion1);
    // Tests: First version removed, 1 version left
    assertEquals(1, database.getFileHistory(idFile1).getFileVersions().size());
    assertEquals(1, database.getFileHistory(idFile1b).getFileVersions().size());
    assertEquals(fileVersion2, database.getFileHistory(idFile1).getLastVersion());
    // Round 5: Remove second database version		
    database.removeDatabaseVersion(databaseVersion2);
    // Tests: Second version removed, none left
    assertNull(database.getFileHistory(idFile1));
}
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 7 with MemoryDatabase

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

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

the class UpOperation method writeAndAddDeltaDatabase.

/**
	 * This method takes the metadata that is to be uploaded, loads it into a {@link MemoryDatabase} and serializes
	 * it to a file. If this is not a resumption of a previous transaction, this file is added to the transaction.
	 * Finally, databaseversions that are uploaded are remembered as known, such that they are not downloaded in future Downs.
	 *
	 * @param newDatabaseVersion {@link DatabaseVersion} containing all metadata that would be locally persisted if the transaction succeeds.
	 * @param resuming boolean indicating if the current transaction is in the process of being resumed.
	 */
private void writeAndAddDeltaDatabase(RemoteTransaction remoteTransaction, DatabaseVersion newDatabaseVersion, boolean resuming) throws InterruptedException, StorageException, IOException, SQLException {
    // Clone database version (necessary, because the original must not be touched)
    DatabaseVersion deltaDatabaseVersion = newDatabaseVersion.clone();
    // New delta database
    MemoryDatabase deltaDatabase = new MemoryDatabase();
    deltaDatabase.addDatabaseVersion(deltaDatabaseVersion);
    // Save delta database locally
    long newestLocalDatabaseVersion = getNewestDatabaseFileVersion(config.getMachineName(), localDatabase.getKnownDatabases());
    DatabaseRemoteFile remoteDeltaDatabaseFile = new DatabaseRemoteFile(config.getMachineName(), newestLocalDatabaseVersion + 1);
    File localDeltaDatabaseFile = config.getCache().getDatabaseFile(remoteDeltaDatabaseFile.getName());
    logger.log(Level.INFO, "Saving local delta database, version {0} to file {1} ... ", new Object[] { deltaDatabaseVersion.getHeader(), localDeltaDatabaseFile });
    saveDeltaDatabase(deltaDatabase, localDeltaDatabaseFile);
    if (!resuming) {
        // Upload delta database, if we are not resuming (in which case the db is in the transaction already)
        logger.log(Level.INFO, "- Uploading local delta database file ...");
        addLocalDatabaseToTransaction(remoteTransaction, localDeltaDatabaseFile, remoteDeltaDatabaseFile);
    }
    // Remember uploaded database as known.
    List<DatabaseRemoteFile> newDatabaseRemoteFiles = new ArrayList<DatabaseRemoteFile>();
    newDatabaseRemoteFiles.add(remoteDeltaDatabaseFile);
    localDatabase.writeKnownRemoteDatabases(newDatabaseRemoteFiles);
}
Also used : DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) MemoryDatabase(org.syncany.database.MemoryDatabase) ArrayList(java.util.ArrayList) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) DatabaseVersion(org.syncany.database.DatabaseVersion)

Example 9 with MemoryDatabase

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

the class MemoryDatabaseCacheTest method testGetFileHistory.

@Test
public void testGetFileHistory() 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);
    FileHistoryId idFile1Get = FileHistoryId.parseFileId("1111111111111111");
    assertNotNull(database.getFileHistory(idFile1Get));
    assertEquals(fileHistory1, database.getFileHistory(idFile1Get));
    // 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);
    // same ID
    FileHistoryId idFile11 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory11 = new PartialFileHistory(idFile11);
    fileHistory11.addFileVersion(fileVersion11);
    databaseVersion2.addFileHistory(fileHistory11);
    database.addDatabaseVersion(databaseVersion2);
    // same ID
    FileHistoryId idFile111 = FileHistoryId.parseFileId("1111111111111111");
    assertNotNull(database.getFileHistory(idFile111));
    assertEquals(2, database.getFileHistory(idFile111).getFileVersions().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 10 with MemoryDatabase

use of org.syncany.database.MemoryDatabase 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

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