// Called by the framework when an item (file) needs to be updated. public override void UpdateItem(object itemData, IEnumerable<SyncId> changeUnitsToUpdate, ItemFieldDictionary keyAndExpectedVersion, RecoverableErrorReportingContext recoverableErrorReportingContext, out ItemFieldDictionary keyAndUpdatedVersion, out bool commitKnowledgeAfterThisItem) { keyAndUpdatedVersion = null; commitKnowledgeAfterThisItem = false; var dataRetriever = (IFileDataRetriever) itemData; IDictionary<uint, ItemField> expectedFields = keyAndExpectedVersion; var expectedLastUpdate = DateTime.FromBinary((long) (ulong) expectedFields[ItemFields.CustomFieldTimestamp].Value); var oldName = (string) expectedFields[ItemFields.CustomFieldName].Value; try { Stream dataStream = null; if (!dataRetriever.FileData.IsDirectory) { dataStream = dataRetriever.FileStream; } var newName = CreateNewName(dataRetriever.FileData); SyncedBlobAttributes attrs; if (IsMoveOrRename(oldName, newName)) { var args = new ApplyingBlobEventArgs(ChangeType.Rename, oldName, newName); var applyingDelegate = ApplyingChange; applyingDelegate(this, args); // Handle moves and renames as a delete and create. DataStore.DeleteFile(oldName, expectedLastUpdate); attrs = DataStore.InsertFile(dataRetriever.FileData, dataRetriever.RelativeDirectoryPath, dataStream); } else { var args = new ApplyingBlobEventArgs(ChangeType.Update, dataRetriever.FileData.Name); var applyingDelegate = ApplyingChange; applyingDelegate(this, args); attrs = DataStore.UpdateFile(oldName, dataRetriever.FileData, dataRetriever.RelativeDirectoryPath, dataStream, expectedLastUpdate); } // Record new data after update. keyAndUpdatedVersion = new ItemFieldDictionary(); keyAndUpdatedVersion.Add(new ItemField(ItemFields.CustomFieldName, typeof (string), attrs._name)); keyAndUpdatedVersion.Add(new ItemField(ItemFields.CustomFieldTimestamp, typeof (ulong), (ulong) attrs._lastModifiedTime.ToBinary())); } catch (ApplicationException e) { recoverableErrorReportingContext.RecordRecoverableErrorForChange(new RecoverableErrorData(e)); } }
// Called by the framework when an item needs to be deleted. public override void DeleteItem(ItemFieldDictionary keyAndExpectedVersion, RecoverableErrorReportingContext recoverableErrorReportingContext, out bool commitKnowledgeAfterThisItem) { commitKnowledgeAfterThisItem = false; IDictionary<uint, ItemField> expectedFields = keyAndExpectedVersion; var name = (string) expectedFields[ItemFields.CustomFieldName].Value; var args = new ApplyingBlobEventArgs(ChangeType.Delete, name); var applyingDelegate = ApplyingChange; applyingDelegate(this, args); var expectedLastUpdate = DateTime.FromBinary((long) (ulong) expectedFields[ItemFields.CustomFieldTimestamp].Value); try { DataStore.DeleteFile(name, expectedLastUpdate); } catch (ApplicationException e) { recoverableErrorReportingContext.RecordRecoverableErrorForChange(new RecoverableErrorData(e)); } }
// Called by the framework when an item (file) needs to be added to the store. public override void InsertItem(object itemData, IEnumerable<SyncId> changeUnitsToCreate, RecoverableErrorReportingContext recoverableErrorReportingContext, out ItemFieldDictionary keyAndUpdatedVersion, out bool commitKnowledgeAfterThisItem) { var dataRetriever = (IFileDataRetriever) itemData; var relativePath = dataRetriever.RelativeDirectoryPath; keyAndUpdatedVersion = null; commitKnowledgeAfterThisItem = false; var args = new ApplyingBlobEventArgs(ChangeType.Create, dataRetriever.FileData.Name); ApplyingChange(this, args); try { Stream dataStream = null; if (!dataRetriever.FileData.IsDirectory) { dataStream = dataRetriever.FileStream; } var attrs = DataStore.InsertFile(dataRetriever.FileData, dataRetriever.RelativeDirectoryPath, dataStream); keyAndUpdatedVersion = new ItemFieldDictionary(); keyAndUpdatedVersion.Add(new ItemField(ItemFields.CustomFieldName, typeof (string), attrs._name)); keyAndUpdatedVersion.Add(new ItemField(ItemFields.CustomFieldTimestamp, typeof (ulong), (ulong) attrs._lastModifiedTime.ToBinary())); } catch (ApplicationException e) { recoverableErrorReportingContext.RecordRecoverableErrorForChange(new RecoverableErrorData(e)); } }
private void ApplyinRemoteChange(object sender, ApplyingBlobEventArgs args) { switch (args.ChangeType) { case ChangeType.Create: Console.WriteLine("Creating Azure file: {0}...", args.CurrentBlobName); break; case ChangeType.Delete: Console.WriteLine("Deleting Azure file: {0}...", args.CurrentBlobName); break; case ChangeType.Rename: Console.WriteLine("Renaming Azure file: {0} to {1}...", args.CurrentBlobName, args.NewBlobName); break; case ChangeType.Update: Console.WriteLine("Updating Azure file: {0}...", args.CurrentBlobName); break; } }