public void RestoreIndexFile(AddFileBehaviour addFileBehaviour, StreamStateBehaviour indexStreamStateBehaviour, StreamStateBehaviour dataStreamStateBehaviour, ExposeProgressDelegate exposeProgressDelegate) { // // the following method also checks whether the index file exists or not, // if exists, it throws an exception // CreateNewFileStorage_IndexFile(IndexFilename); switch (indexStreamStateBehaviour) { case StreamStateBehaviour.OpenNewStreamForReadingAndWriting: // ok break; default: throw new NotSupportedException(string.Format("Unexpected indexStreamStateBehaviour {0}", indexStreamStateBehaviour)); } switch (dataStreamStateBehaviour) { case StreamStateBehaviour.OpenNewStreamForReading: // ok break; default: throw new NotSupportedException(string.Format("Unexpected dataStreamStateBehaviour {0}", dataStreamStateBehaviour)); } using (IndexStream = FileStreamFactory.CreateFileStream(IndexFilename, StreamStateBehaviour.OpenNewStreamForReadingAndWriting)) { using (DataStream = FileStreamFactory.CreateFileStream(DataFilename, StreamStateBehaviour.OpenNewStreamForReading)) { long dataSize = DataStream.Length; long offset = OffsetFirstDataBlock; while (true) { InterpretedDataFileDataStruct retrieveFileStruct = GetInterpretedDataStructUsingExistingStream(offset); // // the offset in the index file should point to the starting location // of the data struct, this is not the offset where the actual data of // the file is stored, but the start of the struct before the actual data. // Guid dataIdentifier = retrieveFileStruct.DataIdentification; // // if the data file contains duplicate dataIdentifiers, we will override previous ones (which // is acceptable, as new files are appeneded to the data file, so more up to date files are stored // after older ones). // BuildIndex(dataIdentifier, offset, BuildIndexBehaviour.OverrideWhenAlreadyExists, StreamStateBehaviour.UseExistingStream); // // jump to the next data block // offset = retrieveFileStruct.BinaryDataOffset + retrieveFileStruct.BinaryDataSizeInBytes; // // inform the outside world about the progress we've made, // in case the outside world is interested // if (exposeProgressDelegate != null) { exposeProgressDelegate.Invoke(dataIdentifier); } if (offset >= dataSize) { // // reach the end of the file // break; } } } } if (exposeProgressDelegate != null) { exposeProgressDelegate.Invoke(true); } }
private void GetDataIdentifiersRecursiveUsingIndex(IEnumerable<byte> bytesSoFar, int depth, Int64 offset, ICollection<Guid> result, StreamStateBehaviour indexStreamStateBehaviour, ExposeProgressDelegate anotherDataIdentifierFoundDelegate) { AllocationTableStruct indexStructs = GetIndexStruct(offset, indexStreamStateBehaviour); for (int currentByteValue = 0; currentByteValue <= 255; currentByteValue++) { if (indexStructs.Pointers[currentByteValue] == 0) { // not used, recursion stops } else { // // clone bytes so far, extend with current byte // var bytesForGuid = new List<byte>(); bytesForGuid.AddRange(bytesSoFar); bytesForGuid.Add((byte) currentByteValue); if (depth == 15) { var newGuid = new Guid(bytesForGuid.ToArray()); result.Add(newGuid); // // notify outside world we found another hit, if the outside world wants to hear about it // if (anotherDataIdentifierFoundDelegate != null) { anotherDataIdentifierFoundDelegate.Invoke(newGuid); } // // recursion stops; this is the last byte // } else { // // continue recursion // long newOffset = indexStructs.Pointers[currentByteValue]; GetDataIdentifiersRecursiveUsingIndex(bytesForGuid, depth + 1, newOffset, result, indexStreamStateBehaviour, anotherDataIdentifierFoundDelegate); } } } }
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 List<Guid> GetAllDataIdentifiersBasedUponFileStorageIndexFile(StreamStateBehaviour indexStreamStateBehaviour, ExposeProgressDelegate exposeProgressDelegate) { List<Guid> result; switch (indexStreamStateBehaviour) { case StreamStateBehaviour.UseExistingStream: result = new List<Guid>(); long offset = OffsetFirstIndexBlock; var bytes = new List<byte>(); int depth = 0; GetDataIdentifiersRecursiveUsingIndex(bytes, depth, offset, result, indexStreamStateBehaviour, exposeProgressDelegate); break; case StreamStateBehaviour.OpenNewStreamForReading: // // open the index stream for reading and make a recursive call // using (IndexStream = new FileStream(IndexFilename, FileMode.Open, FileAccess.Read)) { result = GetAllDataIdentifiersBasedUponFileStorageIndexFile(StreamStateBehaviour.UseExistingStream, exposeProgressDelegate); } break; default: throw new NotSupportedException(string.Format("indexStreamStateBehaviour {0} is not supported", indexStreamStateBehaviour)); } if (exposeProgressDelegate != null) { exposeProgressDelegate.Invoke(true); } return result; }