public static void RestoreIndexFile(string fileStorageName, AddFileBehaviour addFileBehaviour, ExposeProgressDelegate exposeProgressDelegate)
        {
            //
            // the following method also checks whether the index file exists or not,
            // if exists, it throws an exception
            //
            CreateNewFileStorage_IndexFile(FilenameFactory.GetFileStorageIndexFilename(fileStorageName));

            var fileStorageHandler = new FileStorageHandler(fileStorageName, VersionBehaviour.ValidateVersion);
            fileStorageHandler.RestoreIndexFile(addFileBehaviour, StreamStateBehaviour.OpenNewStreamForReadingAndWriting, StreamStateBehaviour.OpenNewStreamForReading, exposeProgressDelegate);
        }
 public void ExportToOtherFileStorage(Guid dataIdentifier, FileStorageHandler targetFileStorageHandler, AddFileBehaviour addFileBehaviour, StreamStateBehaviour sourceIndexStreamStateBehaviour, StreamStateBehaviour sourceDataStreamStateBehaviour, StreamStateBehaviour targetIndexStreamStateBehaviour, StreamStateBehaviour targetDataStreamStateBehaviour)
 {
     if (targetFileStorageHandler.FileStorageName.Equals(FileStorageName))
     {
         throw new NotSupportedException(string.Format("Cannot export file from a filestorage to another filestorage with the same name ({0})", FileStorageName));
     }
     byte[] fileData = GetFileByteData(dataIdentifier, sourceIndexStreamStateBehaviour, sourceDataStreamStateBehaviour);
     MetaDataContainer metaDataContainer = GetMetaDataContainer(dataIdentifier, sourceIndexStreamStateBehaviour, sourceDataStreamStateBehaviour);
     targetFileStorageHandler.StoreBytes(dataIdentifier, fileData, metaDataContainer.CustomMetaData, addFileBehaviour, targetIndexStreamStateBehaviour, targetDataStreamStateBehaviour);
 }
        public static void Replicate(string sourcefileStorageName, string targetfileStorageName, ReplicateBehaviour replicateBehaviour, AddFileBehaviour addFileBehaviour, FileStorageHandler.ExposeProgressDelegate exposeProgressDelegatePhase1, FileStorageHandler.ExposeProgressDelegate exposeProgressDelegatePhase2)
        {
            FileStorageHandler sourceFileStorageHandler = FileStorageHandler.Open(sourcefileStorageName);

            switch (replicateBehaviour)
            {
                case ReplicateBehaviour.AddToExistingStorage:
                    //
                    // nothing to check here
                    //

                    break;
                case ReplicateBehaviour.ReplicateToNewStorage:

                    //
                    // ensure target does not yet exist
                    //
                    Create(targetfileStorageName, CreateFileStorageBehaviour.ThrowExceptionWhenExists);

                    break;
                default:
                    throw new NotSupportedException(string.Format("Unsupported replicate behaviour {0}", replicateBehaviour));
            }

            FileStorageHandler targetFileStorageHandler = FileStorageHandler.Open(targetfileStorageName);

            //
            // open source streams
            //
            using (sourceFileStorageHandler.IndexStream = FileStreamFactory.CreateFileStream(sourceFileStorageHandler.IndexFilename, StreamStateBehaviour.OpenNewStreamForReading))
            {
                using (sourceFileStorageHandler.DataStream = FileStreamFactory.CreateFileStream(sourceFileStorageHandler.DataFilename, StreamStateBehaviour.OpenNewStreamForReading))
                {
                    //
                    // open target streams
                    //
                    using (targetFileStorageHandler.IndexStream = FileStreamFactory.CreateFileStream(targetFileStorageHandler.IndexFilename, StreamStateBehaviour.OpenNewStreamForReadingAndWriting))
                    {
                        using (targetFileStorageHandler.DataStream = FileStreamFactory.CreateFileStream(targetFileStorageHandler.DataFilename, StreamStateBehaviour.OpenNewStreamForReadingAndWriting))
                        {
                            List<Guid> allIdentifiers = sourceFileStorageHandler.GetAllDataIdentifiersBasedUponFileStorageIndexFile(StreamStateBehaviour.UseExistingStream, exposeProgressDelegatePhase1);

                            //
                            // start replicate process
                            //
                            foreach (Guid dataIdentifier in allIdentifiers)
                            {
                                sourceFileStorageHandler.ExportToOtherFileStorage(dataIdentifier, targetFileStorageHandler, addFileBehaviour, StreamStateBehaviour.UseExistingStream, StreamStateBehaviour.UseExistingStream, StreamStateBehaviour.UseExistingStream, StreamStateBehaviour.UseExistingStream);
                                if (exposeProgressDelegatePhase2 != null)
                                {
                                    exposeProgressDelegatePhase2.Invoke(dataIdentifier);
                                }
                            }
                            if (exposeProgressDelegatePhase2 != null)
                            {
                                exposeProgressDelegatePhase2.Invoke(true);
                            }
                        }
                    }
                }
            }
        }
 public static void RestoreIndexFile(string fileStorageName, AddFileBehaviour addFileBehaviour, FileStorageHandler.ExposeProgressDelegate exposeProgressDelegate)
 {
     FileStorageHandler.RestoreIndexFile(fileStorageName, addFileBehaviour, exposeProgressDelegate);
 }
 public static List<Guid> GetAllDataIdentifiersBasedUponFileStorageIndexFile(string fileStorageName, FileStorageHandler.ExposeProgressDelegate notificationDelegate)
 {
     FileStorageHandler fileStorageHandler = FileStorageHandler.Open(fileStorageName);
     return fileStorageHandler.GetAllDataIdentifiersBasedUponFileStorageIndexFile(StreamStateBehaviour.OpenNewStreamForReading, notificationDelegate);
 }
        /// <summary>
        ///     Makes a clone of a source filestorage, and re-allocates the data identifiers in the destination filestorage (the first
        ///     data identifier will get 0001, the next one 0002, etc. Next to outputting a new target filestorage with new indexes,
        ///     the method also produces a sql file that should be used to update your DAL logic, since the references need to be
        ///     updated too ofcourse.
        /// </summary>
        public static void Upgrade(string sourceFileStorageName, string destinationFileStorageName, FileStorageHandler.ExposeProgressDelegate exposeProgressDelegateA, FileStorageHandler.ExposeProgressDelegate exposeProgressDelegateB)
        {
            Create(destinationFileStorageName, CreateFileStorageBehaviour.ThrowExceptionWhenExists);

            FileStorageHandler sourceFileStorageHandler = FileStorageHandler.Open(sourceFileStorageName, VersionBehaviour.BypassVersionCheck);
            FileStorageHandler destinationFileStorageHandler = FileStorageHandler.Open(destinationFileStorageName);

            List<Guid> dataIdentifiers = sourceFileStorageHandler.GetAllDataIdentifiersBasedUponFileStorageIndexFile(StreamStateBehaviour.OpenNewStreamForReading, exposeProgressDelegateA);

            for (int currentDataIdentifierIndex = 0; currentDataIdentifierIndex < dataIdentifiers.Count; currentDataIdentifierIndex++)
            {
                Guid currentSourceDataIdentifier = dataIdentifiers[currentDataIdentifierIndex];
                byte[] theBytes = sourceFileStorageHandler.GetFileByteData(currentSourceDataIdentifier, StreamStateBehaviour.OpenNewStreamForReading, StreamStateBehaviour.OpenNewStreamForReading);

                ICustomMetaData customMetaData = sourceFileStorageHandler.SupportsFeature(FileStorageFeatureEnum.StoreMetaData)
                                                     ? sourceFileStorageHandler.GetMetaDataContainer(currentSourceDataIdentifier, StreamStateBehaviour.OpenNewStreamForReading, StreamStateBehaviour.OpenNewStreamForReading).CustomMetaData
                                                     : new EmptyCustomMetaData();

                Guid currentDestinationDataIdentifier = currentSourceDataIdentifier;

                //
                // store item in destination
                //
                destinationFileStorageHandler.StoreBytes(currentDestinationDataIdentifier, theBytes, customMetaData, AddFileBehaviour.ThrowExceptionWhenAlreadyExists, StreamStateBehaviour.OpenNewStreamForReadingAndWriting, StreamStateBehaviour.OpenNewStreamForReadingAndWriting);

                if (exposeProgressDelegateB != null)
                {
                    exposeProgressDelegateB.Invoke(currentDestinationDataIdentifier);
                }
            }
        }
        /// <summary>
        ///     Makes a clone of a source filestorage, and re-allocates the data identifiers in the destination filestorage (the first
        ///     data identifier will get 0001, the next one 0002, etc. Next to outputting a new target filestorage with new indexes,
        ///     the method also produces a sql file that should be used to update your DAL logic, since the references need to be
        ///     updated too ofcourse.
        /// </summary>
        public static void DefragDataIdentifiers(string sourceFileStorageName, string destinationFileStorageName, string sqlTable, string sqlColumn, FileStorageHandler.ExposeProgressDelegate exposeProgressDelegateA, FileStorageHandler.ExposeProgressDelegate exposeProgressDelegateB)
        {
            Create(destinationFileStorageName, CreateFileStorageBehaviour.ThrowExceptionWhenExists);

            string sqlOutputFileName = destinationFileStorageName + ".FileStorage.index.fc.sql";
            if (File.Exists(sqlOutputFileName))
            {
                throw new Exception(string.Format("File {0} already exists", sqlOutputFileName));
            }

            FileStorageHandler sourceFileStorageHandler = FileStorageHandler.Open(sourceFileStorageName);
            FileStorageHandler destinationFileStorageHandler = FileStorageHandler.Open(destinationFileStorageName);

            using (StreamWriter sw = File.CreateText(sqlOutputFileName))
            {
                sw.WriteLine("-- SQL Patch script to adjust the dataidentifiers");

                List<Guid> dataIdentifiers = sourceFileStorageHandler.GetAllDataIdentifiersBasedUponFileStorageIndexFile(StreamStateBehaviour.OpenNewStreamForReading, exposeProgressDelegateA);

                for (int currentDataIdentifierIndex = 0; currentDataIdentifierIndex < dataIdentifiers.Count; currentDataIdentifierIndex++)
                {
                    Guid currentSourceDataIdentifier = dataIdentifiers[currentDataIdentifierIndex];
                    byte[] theBytes = sourceFileStorageHandler.GetFileByteData(currentSourceDataIdentifier, StreamStateBehaviour.OpenNewStreamForReading, StreamStateBehaviour.OpenNewStreamForReading);

                    ICustomMetaData customMetaData = sourceFileStorageHandler.SupportsFeature(FileStorageFeatureEnum.StoreMetaData)
                                                         ? sourceFileStorageHandler.GetMetaDataContainer(currentSourceDataIdentifier, StreamStateBehaviour.OpenNewStreamForReading, StreamStateBehaviour.OpenNewStreamForReading).CustomMetaData
                                                         : new EmptyCustomMetaData();

                    //
                    // determine the new identifier
                    //
                    int b1AsInt = (currentDataIdentifierIndex >> 24) & 255;
                    var b1 = (byte) b1AsInt;
                    int b2AsInt = (currentDataIdentifierIndex >> 16) & 255;
                    var b2 = (byte) b2AsInt;
                    int b3AsInt = (currentDataIdentifierIndex >> 8) & 255;
                    var b3 = (byte) b3AsInt;
                    int b4AsInt = (currentDataIdentifierIndex >> 0) & 255;
                    var b4 = (byte) b4AsInt;
                    var currentDestinationDataIdentifier = new Guid(0, 0, 0, 0, 0, 0, 0, b1, b2, b3, b4);

                    string line = String.Format("update {0} set {1}='{2}' where {1}='{3}'", sqlTable, sqlColumn, currentSourceDataIdentifier, currentDestinationDataIdentifier);
                    sw.WriteLine(line);

                    //
                    // store item in destination
                    //
                    destinationFileStorageHandler.StoreBytes(currentDestinationDataIdentifier, theBytes, customMetaData, AddFileBehaviour.ThrowExceptionWhenAlreadyExists, StreamStateBehaviour.OpenNewStreamForReadingAndWriting, StreamStateBehaviour.OpenNewStreamForReadingAndWriting);

                    if (exposeProgressDelegateB != null)
                    {
                        exposeProgressDelegateB.Invoke(currentDestinationDataIdentifier);
                    }
                }

                sw.WriteLine("-- EOF");
            }
        }