use of org.syncany.database.PartialFileHistory in project syncany by syncany.
the class FileHistorySqlDao method getFileHistoryWithLastVersionByPath.
/**
* This function returns a FileHistory, with as last version a FileVersion with
* the given path.
*
* If the last FileVersion referring to this path is not the last in the
* FileHistory, or no such FileVersion exists, null is returned.
*/
public PartialFileHistory getFileHistoryWithLastVersionByPath(String path) {
try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.findLatestFileVersionsForPath.sql")) {
preparedStatement.setString(1, path);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
// Fetch the latest versions of all files that once existed with the given
// path and find the most recent by comparing vector clocks
String latestFileHistoryId = null;
Long latestFileVersion = null;
VectorClock latestVectorClock = null;
while (resultSet.next()) {
VectorClock resultSetVectorClock = VectorClock.parseVectorClock(resultSet.getString("vectorclock_serialized"));
boolean vectorClockIsGreater = latestVectorClock == null || VectorClock.compare(resultSetVectorClock, latestVectorClock) == VectorClock.VectorClockComparison.GREATER;
if (vectorClockIsGreater) {
latestVectorClock = resultSetVectorClock;
latestFileHistoryId = resultSet.getString("filehistory_id");
latestFileVersion = resultSet.getLong("version");
}
}
// If no active file history exists for this path, return
if (latestFileHistoryId == null) {
return null;
}
// Get the last FileVersion of the FileHistory in the database with the largest vectorclock.
PartialFileHistory fileHistory = getLastVersionByFileHistoryId(latestFileHistoryId);
// history. We need to check this before returning the file.
if (fileHistory.getLastVersion().getVersion() == latestFileVersion) {
return fileHistory;
} else {
// which should be continued.
return null;
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of org.syncany.database.PartialFileHistory in project syncany by syncany.
the class FileHistorySqlDao method getLastVersionByFileHistoryId.
private PartialFileHistory getLastVersionByFileHistoryId(String fileHistoryId) {
try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getLastVersionByFileHistoryId.sql")) {
preparedStatement.setString(1, fileHistoryId);
preparedStatement.setString(2, fileHistoryId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet);
FileHistoryId fileHistoryIdData = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryIdData);
fileHistory.addFileVersion(lastFileVersion);
return fileHistory;
} else {
return null;
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of org.syncany.database.PartialFileHistory in project syncany by syncany.
the class FileHistorySqlDao method getFileHistoriesByChecksumSizeAndModifiedDate.
/**
* This function returns FileHistories with the last version for which this last version
* matches the given checksum, size and modified date.
*
* @return An empty Collection is returned if none exist.
*/
public Collection<PartialFileHistory> getFileHistoriesByChecksumSizeAndModifiedDate(String filecontentChecksum, long size, Date modifiedDate) {
try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getFileHistoriesByChecksumSizeAndModifiedDate.sql")) {
// This first query retrieves the last version for each FileHistory matching the three requested properties.
// However, it does not guarantee that this version is indeed the last version in that particular
// FileHistory, so we need another query to verify that.
preparedStatement.setString(1, filecontentChecksum);
preparedStatement.setLong(2, size);
preparedStatement.setTimestamp(3, new Timestamp(modifiedDate.getTime()));
try (ResultSet resultSet = preparedStatement.executeQuery()) {
Collection<PartialFileHistory> fileHistories = new ArrayList<>();
while (resultSet.next()) {
String fileHistoryId = resultSet.getString("filehistory_id");
PartialFileHistory fileHistory = getLastVersionByFileHistoryId(fileHistoryId);
boolean resultIsLatestVersion = fileHistory.getLastVersion().getVersion() == resultSet.getLong("version");
boolean resultIsNotDelete = fileHistory.getLastVersion().getStatus() != FileVersion.FileStatus.DELETED;
if (resultIsLatestVersion && resultIsNotDelete) {
fileHistories.add(fileHistory);
}
}
return fileHistories;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of org.syncany.database.PartialFileHistory in project syncany by syncany.
the class DatabaseXmlWriter method writeFileHistories.
private void writeFileHistories(IndentXmlStreamWriter xmlOut, Collection<PartialFileHistory> fileHistories) throws XMLStreamException, IOException {
xmlOut.writeStartElement("fileHistories");
for (PartialFileHistory fileHistory : fileHistories) {
xmlOut.writeStartElement("fileHistory");
xmlOut.writeAttribute("id", fileHistory.getFileHistoryId().toString());
xmlOut.writeStartElement("fileVersions");
Collection<FileVersion> fileVersions = fileHistory.getFileVersions().values();
for (FileVersion fileVersion : fileVersions) {
if (fileVersion.getVersion() == null || fileVersion.getType() == null || fileVersion.getPath() == null || fileVersion.getStatus() == null || fileVersion.getSize() == null || fileVersion.getLastModified() == null) {
throw new IOException("Unable to write file version, because one or many mandatory fields are null (version, type, path, name, status, size, last modified): " + fileVersion);
}
if (fileVersion.getType() == FileType.SYMLINK && fileVersion.getLinkTarget() == null) {
throw new IOException("Unable to write file version: All symlinks must have a target.");
}
xmlOut.writeEmptyElement("fileVersion");
xmlOut.writeAttribute("version", fileVersion.getVersion());
xmlOut.writeAttribute("type", fileVersion.getType().toString());
xmlOut.writeAttribute("status", fileVersion.getStatus().toString());
if (containsXmlRestrictedChars(fileVersion.getPath())) {
xmlOut.writeAttribute("pathEncoded", encodeXmlRestrictedChars(fileVersion.getPath()));
} else {
xmlOut.writeAttribute("path", fileVersion.getPath());
}
xmlOut.writeAttribute("size", fileVersion.getSize());
xmlOut.writeAttribute("lastModified", fileVersion.getLastModified().getTime());
if (fileVersion.getLinkTarget() != null) {
xmlOut.writeAttribute("linkTarget", fileVersion.getLinkTarget());
}
if (fileVersion.getUpdated() != null) {
xmlOut.writeAttribute("updated", fileVersion.getUpdated().getTime());
}
if (fileVersion.getChecksum() != null) {
xmlOut.writeAttribute("checksum", fileVersion.getChecksum().toString());
}
if (fileVersion.getDosAttributes() != null) {
xmlOut.writeAttribute("dosattrs", fileVersion.getDosAttributes());
}
if (fileVersion.getPosixPermissions() != null) {
xmlOut.writeAttribute("posixperms", fileVersion.getPosixPermissions());
}
}
// </fileVersions>
xmlOut.writeEndElement();
// </fileHistory>
xmlOut.writeEndElement();
}
// </fileHistories>
xmlOut.writeEndElement();
}
use of org.syncany.database.PartialFileHistory in project syncany by syncany.
the class FileSystemActionReconciliatorTest method testFileSystemActionReconDeleteNonExistingFolder.
@Test
public void testFileSystemActionReconDeleteNonExistingFolder() throws Exception {
// Setup
TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
TestClient clientA = new TestClient("A", testConnection);
Config testConfigA = clientA.getConfig();
// - Create first database version
clientA.createNewFolder("new folder/some subfolder");
clientA.upWithForceChecksum();
// Delete this!
clientA.deleteFile("new folder/some subfolder");
// - Create new version (delete folder)
TestSqlDatabase sqlDatabaseA = new TestSqlDatabase(testConfigA);
PartialFileHistory folderFileHistoryWithLastVersion = sqlDatabaseA.getFileHistoryWithLastVersion("new folder/some subfolder");
FileVersion deletedFolderVersion = folderFileHistoryWithLastVersion.getLastVersion().clone();
deletedFolderVersion.setStatus(FileStatus.DELETED);
deletedFolderVersion.setVersion(deletedFolderVersion.getVersion() + 1);
PartialFileHistory deletedFolderVersionHistory = new PartialFileHistory(folderFileHistoryWithLastVersion.getFileHistoryId());
deletedFolderVersionHistory.addFileVersion(deletedFolderVersion);
DatabaseVersion winnersDatabaseVersion = TestDatabaseUtil.createDatabaseVersion(sqlDatabaseA.getLastDatabaseVersionHeader());
winnersDatabaseVersion.addFileHistory(deletedFolderVersionHistory);
// - Create memory database with this version
MemoryDatabase winnersDatabase = new MemoryDatabase();
winnersDatabase.addDatabaseVersion(winnersDatabaseVersion);
// Run! Finally!
DownOperationResult outDownOperationResult = new DownOperationResult();
FileSystemActionReconciliator fileSystemActionReconciliator = new FileSystemActionReconciliator(testConfigA, outDownOperationResult.getChangeSet());
List<FileSystemAction> fileSystemActions = fileSystemActionReconciliator.determineFileSystemActions(winnersDatabase);
assertNotNull(fileSystemActions);
assertEquals(0, fileSystemActions.size());
// Tear down
clientA.deleteTestData();
}
Aggregations