/// <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); var sourceFileStorageHandler = FileStorageHandler.Open(sourceFileStorageName, VersionBehaviour.BypassVersionCheck); var destinationFileStorageHandler = FileStorageHandler.Open(destinationFileStorageName); var 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; if (sourceFileStorageHandler.SupportsFeature(FileStorageFeatureEnum.StoreMetaData)) { customMetaData = sourceFileStorageHandler.GetMetaDataContainer(currentSourceDataIdentifier, StreamStateBehaviour.OpenNewStreamForReading, StreamStateBehaviour.OpenNewStreamForReading).CustomMetaData; } else { // // // 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)); } var sourceFileStorageHandler = FileStorageHandler.Open(sourceFileStorageName); var destinationFileStorageHandler = FileStorageHandler.Open(destinationFileStorageName); using (StreamWriter sw = File.CreateText(sqlOutputFileName)) { sw.WriteLine("-- SQL Patch script to adjust the dataidentifiers"); var 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; if (sourceFileStorageHandler.SupportsFeature(FileStorageFeatureEnum.StoreMetaData)) { customMetaData = sourceFileStorageHandler.GetMetaDataContainer(currentSourceDataIdentifier, StreamStateBehaviour.OpenNewStreamForReading, StreamStateBehaviour.OpenNewStreamForReading).CustomMetaData; } else { // // // customMetaData = new EmptyCustomMetaData(); } // // determine the new identifier // int b1AsInt = (currentDataIdentifierIndex >> 24) & 255; byte b1 = (byte)b1AsInt; int b2AsInt = (currentDataIdentifierIndex >> 16) & 255; byte b2 = (byte)b2AsInt; int b3AsInt = (currentDataIdentifierIndex >> 8) & 255; byte b3 = (byte)b3AsInt; int b4AsInt = (currentDataIdentifierIndex >> 0) & 255; byte b4 = (byte)b4AsInt; Guid 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"); } }