use of org.syncany.database.FileContent.FileChecksum 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());
}
use of org.syncany.database.FileContent.FileChecksum 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());
}
use of org.syncany.database.FileContent.FileChecksum in project syncany by syncany.
the class TestDatabaseUtil method createFileVersion.
public static FileVersion createFileVersion(String path) {
FileVersion fileVersion = new FileVersion();
fileVersion.setChecksum(new FileChecksum(TestFileUtil.createRandomArray(20)));
fileVersion.setLastModified(new Date());
fileVersion.setPath(path);
fileVersion.setStatus(FileStatus.NEW);
fileVersion.setType(FileType.FILE);
fileVersion.setUpdated(new Date());
fileVersion.setVersion(1L);
return fileVersion;
}
Aggregations