public override bool UpdateFileMetadata(FileMetaData oldMetadata, FileMetaData newMetadata) { //Sort list of metadata items before search using linq oldMetadata.MetaDataItems.ToList().Sort(new FileMetaDataItemComparer()); newMetadata.MetaDataItems.ToList().Sort(new FileMetaDataItemComparer()); var mdComparer = new FileMetaDataComparer(oldMetadata, newMetadata); var db = new SQLiteAccess(Path.Combine(this.StoragePath, Configuration.DATABASE_NAME), false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, Configuration.DATABASE_NAME))); } var transaction = (SqliteTransaction)con.BeginTransaction(); try { this.Add(mdComparer.RightOnly.ToList(), con); this.Delete(mdComparer.LeftOnly.ToList(), con); this.Update(mdComparer.BothModified.ToList(), con); transaction.Commit(); } catch (Exception) { transaction.Rollback(); return(false); } } return(true); }
private void GeneratePreview(FileMetaData currentMetadata, FileMetaData oldCurrentMetadata) { //By comparing current metadata and the old metadata from previous sync of a sync folder //we could know the dirty items of the folder. var comparerCurrent = new FileMetaDataComparer(currentMetadata, oldCurrentMetadata); var dirtyItemsInCurrent = new List <FileMetaDataItem>(); dirtyItemsInCurrent.AddRange(comparerCurrent.LeftOnly); dirtyItemsInCurrent.AddRange(comparerCurrent.BothModified); //Compare the _actions and dirty items in current folder. Collision is detected when //2 items in 2 collection have same relative path but different hashes. IEnumerable <SyncAction> conflictItems = from action in _actions join dirtyInCurrent in dirtyItemsInCurrent on action.RelativeFilePath equals dirtyInCurrent.RelativePath where !action.FileHash.Equals(dirtyInCurrent.HashCode) select action; foreach (SyncAction action in conflictItems) { action.ConflictResolution = ConflictResolution.DUPLICATE_RENAME; } IEnumerable <SyncAction> itemsToDelete = from action in _actions where action.ChangeType == ChangeType.DELETED select action; IEnumerable <SyncAction> itemsToCopyOver = from action in _actions where action.ChangeType == ChangeType.NEWLY_CREATED && !conflictItems.Contains(action) select action; IEnumerable <SyncAction> itemsToRename = from action in _actions where action.ChangeType == ChangeType.RENAMED && !conflictItems.Contains(action) select action; ConflictItems = conflictItems.ToList(); ItemsToCopyOver = itemsToCopyOver.ToList(); ItemsToDelete = itemsToDelete.ToList(); ItemsToRename = itemsToRename.ToList(); }
/// <summary> /// Generates SyncActions to be executed by other PC by comparing the metadata /// of current SyncSource and the metadata of other PC. /// </summary> private IList <SyncAction> GenerateActions() { //Metadata mdCurrent = UpdateSyncSourceMetadata(); //Metadata mdOther = mdProvider.Load(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); FileMetaData mdCurrent = MetaDataProvider.GenerateFileMetadata( _job.SyncSource.Path, _job.SyncSource.ID, false, false); FileMetaData mdOldCurrent = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_EQUALS); mdProvider.UpdateFileMetadata(mdOldCurrent, mdCurrent); FileMetaData mdOther = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); //generate list of sync actions by comparing 2 metadata var differences = new FileMetaDataComparer(mdCurrent, mdOther); return(actProvider.Generate(mdCurrent.SourceId, differences.LeftOnly, differences.RightOnly, differences.BothModified)); }
private void GeneratePreview(FileMetaData currentMetadata, FileMetaData oldCurrentMetadata) { //By comparing current metadata and the old metadata from previous sync of a sync folder //we could know the dirty items of the folder. var comparerCurrent = new FileMetaDataComparer(currentMetadata, oldCurrentMetadata); var dirtyItemsInCurrent = new List<FileMetaDataItem>(); dirtyItemsInCurrent.AddRange(comparerCurrent.LeftOnly); dirtyItemsInCurrent.AddRange(comparerCurrent.BothModified); //Compare the _actions and dirty items in current folder. Collision is detected when //2 items in 2 collection have same relative path but different hashes. IEnumerable<SyncAction> conflictItems = from action in _actions join dirtyInCurrent in dirtyItemsInCurrent on action.RelativeFilePath equals dirtyInCurrent.RelativePath where !action.FileHash.Equals(dirtyInCurrent.HashCode) select action; foreach (SyncAction action in conflictItems) action.ConflictResolution = ConflictResolution.DUPLICATE_RENAME; IEnumerable<SyncAction> itemsToDelete = from action in _actions where action.ChangeType == ChangeType.DELETED select action; IEnumerable<SyncAction> itemsToCopyOver = from action in _actions where action.ChangeType == ChangeType.NEWLY_CREATED && !conflictItems.Contains(action) select action; IEnumerable<SyncAction> itemsToRename = from action in _actions where action.ChangeType == ChangeType.RENAMED && !conflictItems.Contains(action) select action; ConflictItems = conflictItems.ToList(); ItemsToCopyOver = itemsToCopyOver.ToList(); ItemsToDelete = itemsToDelete.ToList(); ItemsToRename = itemsToRename.ToList(); }
/// <summary> /// Generates SyncActions to be executed by other PC by comparing the metadata /// of current SyncSource and the metadata of other PC. /// </summary> private IList<SyncAction> GenerateActions() { //Metadata mdCurrent = UpdateSyncSourceMetadata(); //Metadata mdOther = mdProvider.Load(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); FileMetaData mdCurrent = MetaDataProvider.GenerateFileMetadata( _job.SyncSource.Path, _job.SyncSource.ID, false, false); FileMetaData mdOldCurrent = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_EQUALS); mdProvider.UpdateFileMetadata(mdOldCurrent, mdCurrent); FileMetaData mdOther = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); //generate list of sync actions by comparing 2 metadata var differences = new FileMetaDataComparer(mdCurrent, mdOther); return actProvider.Generate(mdCurrent.SourceId, differences.LeftOnly, differences.RightOnly, differences.BothModified); }
public override bool UpdateFileMetadata(FileMetaData oldMetadata, FileMetaData newMetadata) { //Sort list of metadata items before search using linq oldMetadata.MetaDataItems.ToList().Sort(new FileMetaDataItemComparer()); newMetadata.MetaDataItems.ToList().Sort(new FileMetaDataItemComparer()); var mdComparer = new FileMetaDataComparer(oldMetadata, newMetadata); var db = new SQLiteAccess(Path.Combine(this.StoragePath, Configuration.DATABASE_NAME),false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, Configuration.DATABASE_NAME))); var transaction = (SqliteTransaction)con.BeginTransaction(); try { this.Add(mdComparer.RightOnly.ToList(), con); this.Delete(mdComparer.LeftOnly.ToList(), con); this.Update(mdComparer.BothModified.ToList(), con); transaction.Commit(); } catch (Exception) { transaction.Rollback(); return false; } } return true; }