internal static FolderSyncState GetSyncState(SyncStateStorage syncStateStorage, Folder syncStateParentFolder, ISyncProviderFactory syncProviderFactory, string syncStateName, Func <SyncStateStorage, StoreObject, FolderSyncStateMetadata, SyncStateInfo, ISyncProviderFactory, bool, ISyncLogger, FolderSyncState> creator, ISyncLogger syncLogger = null)
        {
            ArgumentValidator.ThrowIfNullOrEmpty("syncStateName", syncStateName);
            SyncStateInfo syncStateInfo        = new FolderSyncStateInfo(syncStateName);
            StoreObject   syncStateStoreObject = SyncState.GetSyncStateStoreObject(syncStateStorage, syncStateParentFolder, syncStateInfo, syncLogger, new PropertyDefinition[]
            {
                InternalSchema.SyncFolderSourceKey
            });

            if (syncStateStoreObject == null)
            {
                return(null);
            }
            byte[] valueOrDefault = syncStateStoreObject.GetValueOrDefault <byte[]>(InternalSchema.SyncFolderSourceKey);
            if (syncProviderFactory != null)
            {
                try
                {
                    syncProviderFactory.SetCollectionIdFromBytes(valueOrDefault);
                }
                catch (ArgumentException innerException)
                {
                    syncStateStorage.DeleteFolderSyncState(syncStateName);
                    syncStateStorage.DeviceMetadata.TryRemove(syncStateName, null);
                    throw new CorruptSyncStateException(ServerStrings.ExSyncStateCorrupted(syncStateName), innerException);
                }
            }
            FolderSyncStateMetadata folderSyncStateMetadata = FolderSyncState.GetFolderSyncStateMetadata(syncStateStorage, syncStateStoreObject.Session as MailboxSession, syncStateName, syncLogger);

            if (creator == null)
            {
                return(new FolderSyncState(syncStateStorage, syncStateStoreObject, folderSyncStateMetadata, syncStateInfo, syncProviderFactory, false, syncLogger));
            }
            return(creator(syncStateStorage, syncStateStoreObject, folderSyncStateMetadata, syncStateInfo, syncProviderFactory, false, syncLogger));
        }
        private static FolderSyncStateMetadata GetFolderSyncStateMetadata(SyncStateStorage syncStateStorage, MailboxSession mailboxSession, string name, ISyncLogger syncLogger = null)
        {
            if (syncLogger == null)
            {
                syncLogger = TracingLogger.Singleton;
            }
            SyncStateMetadata       syncState = syncStateStorage.DeviceMetadata.GetSyncState(mailboxSession, name, syncLogger);
            FolderSyncStateMetadata folderSyncStateMetadata = syncState as FolderSyncStateMetadata;

            if (folderSyncStateMetadata == null)
            {
                syncLogger.TraceDebug <SmtpAddress, string>(ExTraceGlobals.SyncProcessTracer, 0L, "[FolderSyncState.GetFolderSyncStateMetadata] SyncStateMetadata in place of FolderSyncStateMetadata for Mailbox: {0}, State: {1}.  Trying re-read...", mailboxSession.MailboxOwner.MailboxInfo.PrimarySmtpAddress, name);
                syncStateStorage.DeviceMetadata.TryRemove(name, syncLogger);
                syncState = syncStateStorage.DeviceMetadata.GetSyncState(mailboxSession, name, syncLogger);
                folderSyncStateMetadata = (syncState as FolderSyncStateMetadata);
                if (folderSyncStateMetadata == null)
                {
                    syncStateStorage.DeleteFolderSyncState(name);
                    syncStateStorage.DeviceMetadata.TryRemove(name, null);
                    throw new CorruptSyncStateException(ServerStrings.ExSyncStateCorrupted(name), new InvalidOperationException("SyncStateMetadata in place of FolderSyncStateMetadata"));
                }
                syncLogger.TraceDebug <string>(ExTraceGlobals.SyncProcessTracer, 0L, "[FolderSyncState.GetFolderSyncStateMetadata] Re-read of sync state {0} was successful.", name);
            }
            return(folderSyncStateMetadata);
        }