Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed May 10, 2015
1 parent 4685ba8 commit a7243d3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
Expand Up @@ -172,7 +172,7 @@ public Map<FileHistoryId, PartialFileHistory> getFileHistories(List<FileHistoryI
* This function returns FileHistories with the last version for which this last version
* matches the given checksum, size and modified date.
*
* An empty Collection is returned if none exist.
* @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")) {
Expand All @@ -186,18 +186,22 @@ public Collection<PartialFileHistory> getFileHistoriesByChecksumSizeAndModifiedD

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;

// Only if the result is indeed the last in it's history, we can use it
// to base other versions off it. So we return it.

if (resultIsLatestVersion && resultIsNotDelete) {
fileHistories.add(fileHistory);
}
}

return fileHistories;
}

Expand All @@ -217,15 +221,22 @@ public Collection<PartialFileHistory> getFileHistoriesByChecksumSizeAndModifiedD
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
// 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 clock = VectorClock.parseVectorClock(resultSet.getString("vectorclock_serialized"));
if (latestVectorClock == null || VectorClock.compare(clock, latestVectorClock) == VectorClock.VectorClockComparison.GREATER) {
latestVectorClock = clock;
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");
}
Expand All @@ -238,9 +249,11 @@ public PartialFileHistory getFileHistoryWithLastVersionByPath(String path) {

// Find for the latest file history the last file version to check if the file has moved
PartialFileHistory fileHistory = getLastVersionByFileHistoryId(latestFileHistoryId);

if (fileHistory.getLastVersion().getVersion() == latestFileVersion) {
return fileHistory;
} else {
}
else {
return null;
}
}
Expand All @@ -262,8 +275,10 @@ private PartialFileHistory getLastVersionByFileHistoryId(String fileHistoryId) {

PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryIdData);
fileHistory.addFileVersion(lastFileVersion);

return fileHistory;
} else {
}
else {
return null;
}
}
Expand Down
58 changes: 33 additions & 25 deletions syncany-lib/src/main/java/org/syncany/operations/up/Indexer.java
Expand Up @@ -78,10 +78,10 @@
*/
public class Indexer {
private static final Logger logger = Logger.getLogger(Indexer.class.getSimpleName());

private static final String DEFAULT_POSIX_PERMISSIONS_FILE = "rw-r--r--";
private static final String DEFAULT_POSIX_PERMISSIONS_FOLDER = "rwxr-xr-x";
private static final String DEFAULT_DOS_ATTRIBUTES = "--a-";
private static final long CONNECTION_REFRESH_TIME = 60_000L;

private Config config;
private Deduper deduper;
Expand Down Expand Up @@ -119,20 +119,20 @@ public Indexer(Config config, Deduper deduper) {
* @param databaseVersionQueue Queue to which created databaseVersions are offered
* @throws IOException If the chunking/deduplication cannot read/process any of the files
*/
public void index(List<File> files, List<File> deletedFiles, Queue<DatabaseVersion> databaseVersionQueue) throws IOException {
if (!files.isEmpty()) {
indexWithNewFiles(files, deletedFiles, databaseVersionQueue);
}
else {
indexWithoutNewFiles(files, deletedFiles, databaseVersionQueue);
}

public void index(List<File> files, List<File> deletedFiles, Queue<DatabaseVersion> databaseVersionQueue)
throws IOException {
boolean firstFile = true;
int fullFileCount = files.size();
localDatabase.finalize();
}

// If there are no files to index, we still need to check for deletions.
if (files.isEmpty()) {
DatabaseVersion newDatabaseVersion = new DatabaseVersion();
// Find and remove deleted files
removeDeletedFiles(newDatabaseVersion, deletedFiles);
logger.log(Level.FINE, "Added database version with only deletions: " + newDatabaseVersion);
databaseVersionQueue.offer(newDatabaseVersion);
}
private void indexWithNewFiles(List<File> files, List<File> deletedFiles, Queue<DatabaseVersion> databaseVersionQueue) throws IOException {
boolean isFirstFile = true;
int filesCount = files.size();

while (!files.isEmpty()) {
DatabaseVersion newDatabaseVersion = new DatabaseVersion();
Expand All @@ -141,11 +141,11 @@ public void index(List<File> files, List<File> deletedFiles, Queue<DatabaseVersi
DeduperListener deduperListener = new IndexerDeduperListener(newDatabaseVersion);

// Signal the start of indexing if we are about to deduplicate the first file
if (firstFile) {
deduperListener.onStart(files.size());
// Add deletions in first databaseversion.
removeDeletedFiles(newDatabaseVersion, deletedFiles);
firstFile = false;
if (isFirstFile) {
deduperListener.onStart(files.size());
removeDeletedFiles(newDatabaseVersion, deletedFiles); // Add deletions in first database version

isFirstFile = false;
}

// Find and index new files
Expand All @@ -154,17 +154,25 @@ public void index(List<File> files, List<File> deletedFiles, Queue<DatabaseVersi
if (!newDatabaseVersion.getFileHistories().isEmpty()) {
logger.log(Level.FINE, "Processed new database version: " + newDatabaseVersion);
databaseVersionQueue.offer(newDatabaseVersion);
eventBus.post(new UpIndexMidSyncExternalEvent(config.getLocalDir().toString(), fullFileCount, fullFileCount - files.size()));

int remainingFilesCount = filesCount - files.size();
eventBus.post(new UpIndexMidSyncExternalEvent(config.getLocalDir().toString(), filesCount, remainingFilesCount));
}
//else { (comment-only else case)
// Just chunks and multichunks, no filehistory. Since this means the file was being
// written/vanished during operations, it makes no sense to upload it. If the user
// wants it indexed, Up can be run again.
//}
}
}

// Close database connection.
localDatabase.finalize();
private void indexWithoutNewFiles(List<File> files, List<File> deletedFiles, Queue<DatabaseVersion> databaseVersionQueue) {
DatabaseVersion newDatabaseVersion = new DatabaseVersion();

removeDeletedFiles(newDatabaseVersion, deletedFiles);
logger.log(Level.FINE, "Added database version with only deletions: " + newDatabaseVersion);

databaseVersionQueue.offer(newDatabaseVersion);
}

private void removeDeletedFiles(DatabaseVersion newDatabaseVersion, List<File> deletedFiles) {
Expand Down Expand Up @@ -216,7 +224,6 @@ private PartialFileHistory createFileHistoryForDeletion(PartialFileHistory fileH
return deletedFileHistory;
}


private PartialFileHistory getFileHistoryByPathFromDatabaseVersion(DatabaseVersion databaseVersion, String path) {
// TODO [medium] Extremely performance intensive, because this is called inside a loop above. Implement better caching for database version!!!

Expand Down Expand Up @@ -332,7 +339,8 @@ private void addFileVersion(FileProperties fileProperties) {

// 2. If file type changed, "close" the old file history by adding a file version that deletes the old file/directory
PartialFileHistory deletedFileHistory = null;
if (lastFileVersion != null && (lastFileVersion.getType() != fileProperties.getType())) {

if (lastFileVersion != null && lastFileVersion.getType() != fileProperties.getType()) {
logger.log(Level.FINER, " * Detected change in file type. Deleting old file and starting a new file history.");

deletedFileHistory = createFileHistoryForDeletion(lastFileHistory, lastFileVersion);
Expand All @@ -353,6 +361,7 @@ private void addFileVersion(FileProperties fileProperties) {
if (newVersionDiffersFromToLastVersion) {
fileHistory.addFileVersion(fileVersion);
newDatabaseVersion.addFileHistory(fileHistory);

if (deletedFileHistory != null) {
newDatabaseVersion.addFileHistory(deletedFileHistory);
}
Expand Down Expand Up @@ -497,8 +506,6 @@ private PartialFileHistory guessLastFileHistoryForFolderOrSymlink(FileProperties
return null;
}
else {
FileVersion lastFileVersion = lastFileHistory.getLastVersion();

logger.log(Level.FINER,
" * Found old file history " + lastFileHistory.getFileHistoryId() + " (by path: " + fileProperties.getRelativePath()
+ "), " + fileProperties.getType() + ", appending new version.");
Expand Down Expand Up @@ -589,6 +596,7 @@ private PartialFileHistory guessLastFileHistoryForFileWithMatchingChecksum(FileP
// The candidate no longer matches, take the current path.
break;
}

if (!filePath.regionMatches(filePath.length() - i, currentPreviousPath, currentPreviousPath.length() - i, i)) {
// The current previous path no longer matches, take the new candidate
lastFileHistory = fileHistoryWithSameChecksum;
Expand Down
Expand Up @@ -249,8 +249,9 @@ private int executeTransactions(BlockingQueue<DatabaseVersion> databaseVersionQu
private int executeTransactions(BlockingQueue<DatabaseVersion> databaseVersionQueue, Iterator<RemoteTransaction> remoteTransactionsToResume,
TransactionRemoteFile transactionRemoteFileToResume)
throws Exception {
int numberOfCompletedTransactions = 0;

boolean resuming = true;

if (remoteTransactionsToResume == null) {
resuming = false;
}
Expand Down Expand Up @@ -329,7 +330,6 @@ private int executeTransactions(BlockingQueue<DatabaseVersion> databaseVersionQu
localDatabase.commit();

committingFailed = false;
numberOfCompletedTransactions++;
}
catch (Exception e) {
detectedFailure = true;
Expand Down
@@ -1,3 +1,13 @@
-- Select file history identifier, file version and corresponding database version for all non-deleted file versions with the given path.
--
-- Strategy:
-- - Select file history identifier and max. file version from `fileversion` (fvmax)
-- - Join rest of the table (fv)
-- - Join database version table (dbv)
-- - Rule out deleted versions (where)
--
-- Please note: It is important that the initial inner select (fvmax) does not rule out deleted versions, as this has caused trouble in the past

SELECT fvmax.version AS version,
fvmax.filehistory_id AS filehistory_id,
dbv.vectorclock_serialized AS vectorclock_serialized
Expand All @@ -15,4 +25,4 @@ SELECT fvmax.version AS version,
AND fvmax.version = fv.version
JOIN databaseversion AS dbv
ON fv.databaseversion_id = dbv.id
WHERE fv.status <> 'DELETED';
WHERE fv.status <> 'DELETED';

0 comments on commit a7243d3

Please sign in to comment.