Search in sources :

Example 21 with ChunkChecksum

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

the class XmlDatabaseDaoTest method testWritePartialDatabaseOneToFive.

@Test
public void testWritePartialDatabaseOneToFive() 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
    File writtenDatabaseFile = new File(tempDir + "/db-" + Math.random() + "-" + Math.abs(new Random().nextInt(Integer.MAX_VALUE)));
    DatabaseXmlSerializer writeDAO = new DatabaseXmlSerializer();
    writeDAO.save(writtenDatabase.getDatabaseVersions(), writtenDatabaseFile);
    // Read again
    MemoryDatabase readDatabase = new MemoryDatabase();
    DatabaseXmlSerializer readDAO = new DatabaseXmlSerializer();
    readDAO.load(readDatabase, writtenDatabaseFile, null, null, DatabaseReadType.FULL);
    for (int i = 0; i < 10; i++) {
        DatabaseVersion writtenDatabaseVersion = writtenDatabaseVersions.get(i);
        DatabaseVersion readDatabaseVersion = readDatabase.getDatabaseVersion(writtenDatabaseVersion.getVectorClock());
        assertNotNull(readDatabaseVersion);
        assertDatabaseVersionEquals(writtenDatabaseVersion, readDatabaseVersion);
    }
    assertEquals(10, readDatabase.getDatabaseVersions().size());
}
Also used : Random(java.util.Random) 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) DatabaseXmlSerializer(org.syncany.database.dao.DatabaseXmlSerializer) File(java.io.File) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 22 with ChunkChecksum

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

the class XmlDatabaseDaoTest method testWriteAndReadChunksWithFileContents.

@Test
public void testWriteAndReadChunksWithFileContents() 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 file contents    	
    FileContent contentA = new FileContent();
    contentA.addChunk(chunkA1.getChecksum());
    contentA.addChunk(chunkA2.getChecksum());
    contentA.addChunk(chunkA3.getChecksum());
    contentA.addChunk(chunkA4.getChecksum());
    contentA.setChecksum(new FileChecksum(new byte[] { 5, 5, 5, 4, 4, 5, 5, 5, 5 }));
    newDatabaseVersion.addFileContent(contentA);
    FileContent contentB = new FileContent();
    contentB.addChunk(chunkB1.getChecksum());
    contentB.addChunk(chunkB2.getChecksum());
    contentB.setChecksum(new FileChecksum(new byte[] { 1, 1, 1, 3, 3, 5, 5, 5, 5 }));
    newDatabaseVersion.addFileContent(contentB);
    // 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 file contents
    FileContent loadedContentA = loadedDatabase.getContent(contentA.getChecksum());
    FileContent loadedContentB = loadedDatabase.getContent(contentB.getChecksum());
    assertEquals("File content not found in database loaded.", contentA, loadedContentA);
    assertEquals("File content not found in database loaded.", contentB, loadedContentB);
    assertArrayEquals("Chunks in file content expected to be different.", contentA.getChunks().toArray(), loadedContentA.getChunks().toArray());
    assertArrayEquals("Chunks in file content expected to be different.", contentB.getChunks().toArray(), loadedContentB.getChunks().toArray());
}
Also used : FileContent(org.syncany.database.FileContent) ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) MemoryDatabase(org.syncany.database.MemoryDatabase) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileChecksum(org.syncany.database.FileContent.FileChecksum) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 23 with ChunkChecksum

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

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

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

the class ChunkDaoTest method testGetChunksForDatabaseVersion.

@Test
public void testGetChunksForDatabaseVersion() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set1.sql");
    ChunkSqlDao chunkDao = new ChunkSqlDao(databaseConnection);
    Map<ChunkChecksum, ChunkEntry> chunksForA1 = chunkDao.getChunks(TestDatabaseUtil.createVectorClock("A1"));
    Map<ChunkChecksum, ChunkEntry> chunksForA2 = chunkDao.getChunks(TestDatabaseUtil.createVectorClock("A2"));
    Map<ChunkChecksum, ChunkEntry> chunksForA4 = chunkDao.getChunks(TestDatabaseUtil.createVectorClock("A4"));
    Map<ChunkChecksum, ChunkEntry> chunksForB1 = chunkDao.getChunks(TestDatabaseUtil.createVectorClock("B1"));
    Map<ChunkChecksum, ChunkEntry> chunksForNonExistent = chunkDao.getChunks(TestDatabaseUtil.createVectorClock("NonExistent1"));
    // Test
    assertNotNull(chunksForA1);
    assertEquals(1, chunksForA1.size());
    assertNotNull(chunksForA1.get(ChunkChecksum.parseChunkChecksum("fe83f217d464f6fdfa5b2b1f87fe3a1a47371196")));
    assertNotNull(chunksForA2);
    assertEquals(1, chunksForA2.size());
    assertNotNull(chunksForA2.get(ChunkChecksum.parseChunkChecksum("bf8b4530d8d246dd74ac53a13471bba17941dff7")));
    assertNotNull(chunksForA4);
    // Especially: does NOT contain fe83f217d464f6fdfa5b2b1f87fe3a1a47371196		
    assertEquals(0, chunksForA4.size());
    assertNotNull(chunksForB1);
    assertEquals(1, chunksForB1.size());
    assertNotNull(chunksForB1.get(ChunkChecksum.parseChunkChecksum("beefbeefbeefbeefbeefbeefbeefbeefbeefbeef")));
    assertNotNull(chunksForNonExistent);
    assertEquals(0, chunksForNonExistent.size());
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : ChunkSqlDao(org.syncany.database.dao.ChunkSqlDao) Config(org.syncany.config.Config) ChunkEntry(org.syncany.database.ChunkEntry) Connection(java.sql.Connection) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) Test(org.junit.Test)

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