use of org.syncany.database.FileVersion in project syncany by syncany.
the class FileSystemActionComparatorTest method createDeleteFileSystemAction.
private DeleteFileSystemAction createDeleteFileSystemAction(String path, FileType type) throws Exception {
FileVersion firstFileVersion = createFileVersion(path, type);
FileVersion secondFileVersion = createFileVersion(path, type, firstFileVersion);
return new DeleteFileSystemAction(createDummyConfig(), firstFileVersion, secondFileVersion, null);
}
use of org.syncany.database.FileVersion 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"));
}
use of org.syncany.database.FileVersion 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));
}
use of org.syncany.database.FileVersion 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());
}
use of org.syncany.database.FileVersion in project syncany by syncany.
the class DatabaseXmlParseHandler method startElement.
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
elementPath += "/" + qName;
if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion")) {
databaseVersion = new DatabaseVersion();
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/time")) {
Date timeValue = new Date(Long.parseLong(attributes.getValue("value")));
databaseVersion.setTimestamp(timeValue);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/client")) {
String clientName = attributes.getValue("name");
databaseVersion.setClient(clientName);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/vectorClock")) {
vectorClock = new VectorClock();
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/vectorClock/client")) {
String clientName = attributes.getValue("name");
Long clientValue = Long.parseLong(attributes.getValue("value"));
vectorClock.setClock(clientName, clientValue);
} else if (readType == DatabaseReadType.FULL) {
if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/chunks/chunk")) {
String chunkChecksumStr = attributes.getValue("checksum");
ChunkChecksum chunkChecksum = ChunkChecksum.parseChunkChecksum(chunkChecksumStr);
int chunkSize = Integer.parseInt(attributes.getValue("size"));
ChunkEntry chunkEntry = new ChunkEntry(chunkChecksum, chunkSize);
databaseVersion.addChunk(chunkEntry);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileContents/fileContent")) {
String checksumStr = attributes.getValue("checksum");
long size = Long.parseLong(attributes.getValue("size"));
fileContent = new FileContent();
fileContent.setChecksum(FileChecksum.parseFileChecksum(checksumStr));
fileContent.setSize(size);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileContents/fileContent/chunkRefs/chunkRef")) {
String chunkChecksumStr = attributes.getValue("ref");
fileContent.addChunk(ChunkChecksum.parseChunkChecksum(chunkChecksumStr));
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/multiChunks/multiChunk")) {
String multChunkIdStr = attributes.getValue("id");
MultiChunkId multiChunkId = MultiChunkId.parseMultiChunkId(multChunkIdStr);
long size = Long.parseLong(attributes.getValue("size"));
if (multiChunkId == null) {
throw new SAXException("Cannot read ID from multichunk " + multChunkIdStr);
}
multiChunk = new MultiChunkEntry(multiChunkId, size);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/multiChunks/multiChunk/chunkRefs/chunkRef")) {
String chunkChecksumStr = attributes.getValue("ref");
multiChunk.addChunk(ChunkChecksum.parseChunkChecksum(chunkChecksumStr));
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileHistories/fileHistory")) {
String fileHistoryIdStr = attributes.getValue("id");
FileHistoryId fileId = FileHistoryId.parseFileId(fileHistoryIdStr);
fileHistory = new PartialFileHistory(fileId);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileHistories/fileHistory/fileVersions/fileVersion")) {
String fileVersionStr = attributes.getValue("version");
String path = attributes.getValue("path");
String pathEncoded = attributes.getValue("pathEncoded");
String sizeStr = attributes.getValue("size");
String typeStr = attributes.getValue("type");
String statusStr = attributes.getValue("status");
String lastModifiedStr = attributes.getValue("lastModified");
String updatedStr = attributes.getValue("updated");
String checksumStr = attributes.getValue("checksum");
String linkTarget = attributes.getValue("linkTarget");
String dosAttributes = attributes.getValue("dosattrs");
String posixPermissions = attributes.getValue("posixperms");
if (fileVersionStr == null || (path == null && pathEncoded == null) || typeStr == null || statusStr == null || sizeStr == null || lastModifiedStr == null) {
throw new SAXException("FileVersion: Attributes missing: version, path/pathEncoded, type, status, size and last modified are mandatory");
}
// Filter it if it was purged somewhere in the future, see #58
Long fileVersionNum = Long.parseLong(fileVersionStr);
// Go add it!
FileVersion fileVersion = new FileVersion();
fileVersion.setVersion(fileVersionNum);
if (path != null) {
fileVersion.setPath(path);
} else {
try {
fileVersion.setPath(new String(Base64.decodeBase64(pathEncoded), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Invalid Base64 encoding for filename: " + pathEncoded);
}
}
fileVersion.setType(FileType.valueOf(typeStr));
fileVersion.setStatus(FileStatus.valueOf(statusStr));
fileVersion.setSize(Long.parseLong(sizeStr));
fileVersion.setLastModified(new Date(Long.parseLong(lastModifiedStr)));
if (updatedStr != null) {
fileVersion.setUpdated(new Date(Long.parseLong(updatedStr)));
}
if (checksumStr != null) {
fileVersion.setChecksum(FileChecksum.parseFileChecksum(checksumStr));
}
if (linkTarget != null) {
fileVersion.setLinkTarget(linkTarget);
}
if (dosAttributes != null) {
fileVersion.setDosAttributes(dosAttributes);
}
if (posixPermissions != null) {
fileVersion.setPosixPermissions(posixPermissions);
}
fileHistory.addFileVersion(fileVersion);
}
}
}
Aggregations