use of org.syncany.database.MultiChunkEntry.MultiChunkId in project syncany by syncany.
the class DatabaseVersionDaoTest method testPersistDatabaseVersion.
@Test
public void testPersistDatabaseVersion() throws Exception {
// Setup
Config testConfig = TestConfigUtil.createTestLocalConfig();
Connection databaseConnection = testConfig.createDatabaseConnection();
// Run
TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set1.sql");
ChunkSqlDao chunkDao = new ChunkSqlDao(databaseConnection);
MultiChunkSqlDao multiChunkDao = new MultiChunkSqlDao(databaseConnection);
FileVersionSqlDao fileVersionDao = new FileVersionSqlDao(databaseConnection);
FileHistorySqlDao fileHistoryDao = new FileHistorySqlDao(databaseConnection, fileVersionDao);
FileContentSqlDao fileContentDao = new FileContentSqlDao(databaseConnection);
DatabaseVersionSqlDao databaseVersionDao = new DatabaseVersionSqlDao(databaseConnection, chunkDao, fileContentDao, fileVersionDao, fileHistoryDao, multiChunkDao);
// a. Capture database version header (now)
DatabaseVersionHeader lastDatabaseVersionHeaderBefore = databaseVersionDao.getLastDatabaseVersionHeader();
// b. Add new database header (with one file history)
DatabaseVersion newDatabaseVersion = new DatabaseVersion();
DatabaseVersionHeader newDatabaseVersionHeader = new DatabaseVersionHeader();
newDatabaseVersionHeader.setClient("C");
newDatabaseVersionHeader.setDate(new Date(1489977288000L));
newDatabaseVersionHeader.setVectorClock(TestDatabaseUtil.createVectorClock("A5,C1"));
newDatabaseVersion.setHeader(newDatabaseVersionHeader);
PartialFileHistory newFileHistory = new PartialFileHistory(FileHistoryId.secureRandomFileId());
FileVersion newFileVersion = new FileVersion();
newFileVersion.setVersion(1L);
newFileVersion.setPath("newfile");
newFileVersion.setChecksum(FileChecksum.parseFileChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"));
newFileVersion.setLinkTarget(null);
newFileVersion.setPosixPermissions("rwxrwxrwx");
newFileVersion.setDosAttributes(null);
newFileVersion.setStatus(FileStatus.NEW);
newFileVersion.setLastModified(new Date());
newFileVersion.setUpdated(new Date());
newFileVersion.setSize(1L);
newFileVersion.setType(FileType.FILE);
newFileHistory.addFileVersion(newFileVersion);
newDatabaseVersion.addFileHistory(newFileHistory);
ChunkEntry newChunkEntry = new ChunkEntry(ChunkChecksum.parseChunkChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"), 1);
newDatabaseVersion.addChunk(newChunkEntry);
MultiChunkEntry newMultiChunkEntry = new MultiChunkEntry(MultiChunkId.parseMultiChunkId("1234567890987654321234567876543456555555"), 10);
newMultiChunkEntry.addChunk(newChunkEntry.getChecksum());
newDatabaseVersion.addMultiChunk(newMultiChunkEntry);
FileContent newFileContent = new FileContent();
newFileContent.setChecksum(FileChecksum.parseFileChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"));
newFileContent.setSize(1L);
newFileContent.addChunk(newChunkEntry.getChecksum());
newDatabaseVersion.addFileContent(newFileContent);
// c. Persist database version
databaseVersionDao.writeDatabaseVersion(newDatabaseVersion);
// d. Capture new last database version header
DatabaseVersionHeader lastDatabaseVersionHeaderAfter = databaseVersionDao.getLastDatabaseVersionHeader();
// Test
assertNotNull(lastDatabaseVersionHeaderBefore);
assertEquals("A/(A5)/T=1388935689000", lastDatabaseVersionHeaderBefore.toString());
assertNotNull(lastDatabaseVersionHeaderAfter);
assertEquals("C/(A5,C1)/T=1489977288000", lastDatabaseVersionHeaderAfter.toString());
assertEquals(newDatabaseVersionHeader.getVectorClock(), lastDatabaseVersionHeaderAfter.getVectorClock());
assertEquals(newChunkEntry, chunkDao.getChunk(ChunkChecksum.parseChunkChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f")));
assertEquals(newFileContent, fileContentDao.getFileContent(FileChecksum.parseFileChecksum("aaaaaaaaaaaaaaaaaaaaab2b263ffa4cc48e282f"), true));
Map<MultiChunkId, MultiChunkEntry> multiChunkIds = multiChunkDao.getMultiChunks(newDatabaseVersionHeader.getVectorClock());
assertNotNull(multiChunkIds);
assertEquals(1, multiChunkIds.size());
MultiChunkEntry actualNewMultiChunkEntry = multiChunkIds.get(MultiChunkId.parseMultiChunkId("1234567890987654321234567876543456555555"));
assertNotNull(actualNewMultiChunkEntry);
assertEquals(newMultiChunkEntry.getId(), actualNewMultiChunkEntry.getId());
// Tear down
databaseConnection.close();
TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
use of org.syncany.database.MultiChunkEntry.MultiChunkId in project syncany by syncany.
the class MultiChunkDaoTest method testGetDirtyMultiChunks.
@Test
public void testGetDirtyMultiChunks() throws Exception {
// Setup
Config testConfig = TestConfigUtil.createTestLocalConfig();
Connection databaseConnection = testConfig.createDatabaseConnection();
// Run
TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set1.sql");
MultiChunkSqlDao multiChunkDao = new MultiChunkSqlDao(databaseConnection);
List<MultiChunkId> dirtyMultiChunkIds = multiChunkDao.getDirtyMultiChunkIds();
// Test
assertNotNull(dirtyMultiChunkIds);
assertEquals(1, dirtyMultiChunkIds.size());
assertEquals(MultiChunkId.parseMultiChunkId("1234567890987654321123456789098765433222"), dirtyMultiChunkIds.get(0));
// Tear down
databaseConnection.close();
TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
use of org.syncany.database.MultiChunkEntry.MultiChunkId in project syncany by syncany.
the class MultiChunkDaoTest method testGetMultiChunkIds.
@Test
public void testGetMultiChunkIds() throws Exception {
// Setup
Config testConfig = TestConfigUtil.createTestLocalConfig();
Connection databaseConnection = testConfig.createDatabaseConnection();
// Run
TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set3.sql");
MultiChunkSqlDao multiChunkDao = new MultiChunkSqlDao(databaseConnection);
List<ChunkChecksum> checksums = new ArrayList<ChunkChecksum>();
checksums.add(ChunkChecksum.parseChunkChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0"));
checksums.add(ChunkChecksum.parseChunkChecksum("0fecbac8ac8a5f8b7aa12b2741a4ef5db88c5dea"));
checksums.add(ChunkChecksum.parseChunkChecksum("38a18897e94a901b833e750e8604d9616a02ca84"));
checksums.add(ChunkChecksum.parseChunkChecksum("beefbeefbeefbeefbeefbeefbeefbeefbeefbeef"));
//Ensure that uniqueness is not required
checksums.add(ChunkChecksum.parseChunkChecksum("38a18897e94a901b833e750e8604d9616a02ca84"));
Map<ChunkChecksum, MultiChunkId> multiChunkIds = multiChunkDao.getMultiChunkIdsByChecksums(checksums);
// Test
assertNotNull(multiChunkIds.get(checksums.get(0)));
assertEquals("0d79eed3fd8ac866b5872ea3f3f079c46dd15ac9", multiChunkIds.get(checksums.get(0)).toString());
assertNotNull(multiChunkIds.get(checksums.get(1)));
assertEquals("51aaca5c1280b1cf95cff8a3266a6bb44b482ad4", multiChunkIds.get(checksums.get(1)).toString());
assertNotNull(multiChunkIds.get(checksums.get(2)));
assertEquals("51aaca5c1280b1cf95cff8a3266a6bb44b482ad4", multiChunkIds.get(checksums.get(2)).toString());
assertEquals(multiChunkIds.get(checksums.get(1)), multiChunkIds.get(checksums.get(2)));
assertNull(multiChunkIds.get(checksums.get(3)));
Map<ChunkChecksum, MultiChunkId> multiChunkIdsEmpty = multiChunkDao.getMultiChunkIdsByChecksums(new ArrayList<ChunkChecksum>());
assertTrue(multiChunkIdsEmpty.isEmpty());
// Tear down
databaseConnection.close();
TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
use of org.syncany.database.MultiChunkEntry.MultiChunkId in project syncany by syncany.
the class MultiChunkDaoTest method testGetMultiChunksByDatabaseVersion2.
@Test
public void testGetMultiChunksByDatabaseVersion2() throws Exception {
// Setup
Config testConfig = TestConfigUtil.createTestLocalConfig();
Connection databaseConnection = testConfig.createDatabaseConnection();
// Run
TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set1.sql");
MultiChunkSqlDao multiChunkDao = new MultiChunkSqlDao(databaseConnection);
Map<MultiChunkId, MultiChunkEntry> multiChunksA4 = multiChunkDao.getMultiChunks(TestDatabaseUtil.createVectorClock("A4"));
Map<MultiChunkId, MultiChunkEntry> multiChunksA5 = multiChunkDao.getMultiChunks(TestDatabaseUtil.createVectorClock("A5"));
// Test
// - Database version "A4"
assertNotNull(multiChunksA4);
assertEquals(0, multiChunksA4.size());
// - Database version "A5"
assertNotNull(multiChunksA5);
assertEquals(1, multiChunksA5.size());
MultiChunkEntry multiChunkInA5 = multiChunksA5.get(MultiChunkId.parseMultiChunkId("dddddddddddddddddddddddddddddddddddddddd"));
assertNotNull(multiChunkInA5);
assertEquals("dddddddddddddddddddddddddddddddddddddddd", multiChunkInA5.getId().toString());
assertTrue(CollectionUtil.containsExactly(multiChunkInA5.getChunks(), ChunkChecksum.parseChunkChecksum("ffffffffffffffffffffffffffffffffffffffff")));
// Tear down
databaseConnection.close();
TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
use of org.syncany.database.MultiChunkEntry.MultiChunkId 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());
}
Aggregations