public bool CheckFileExists(string path) { if (File.Exists(path)) { return(true); } try { _chunkDbService.GetManagedFile(path); } catch (ChunkDbException ex) { return(false); } return(true); }
public ChunkMapDto BuildChunkMapDto() { ManagedFile file = _chunkDbService.GetManagedFile(DataFilePath); int totalChunkNumber = (int)(file.Size / DataChunk.ChunkSize); logger.DebugFormat("Total number of chunk for this file: {0}.", totalChunkNumber); AccessProfile[] chunkStatsArr = GetChunksOrderedByEarliestRead(); logger.DebugFormat("Brought in totally {0} chunks from profile.", chunkStatsArr.Length); // The hashes have two parts: in the profile and out of the profile. int[] fileIndices = new int[totalChunkNumber]; byte[] hashes = new byte[totalChunkNumber * DataChunk.HashSize]; HashSet <int> indicesSet = new HashSet <int>(); // Number of chunks read directly from file instead of pulled from DB. int numDirectRead = 0; int fileIndicesCur = 0; for (; fileIndicesCur < chunkStatsArr.Length; fileIndicesCur++) { var chunk = chunkStatsArr[fileIndicesCur]; fileIndices[fileIndicesCur] = chunk.ChunkIndex; indicesSet.Add(chunk.ChunkIndex); bool readFile; byte[] hash = _fileHelper.GetHashFromChunkDbOrFile(DataFilePath, chunk.ChunkIndex, out readFile); if (readFile) { numDirectRead++; } Buffer.BlockCopy(hash, 0, hashes, fileIndicesCur * DataChunk.HashSize, hash.Length); } int inProfileChunkNum = fileIndicesCur; logger.DebugFormat("Finished adding {0} in the profile chunks.", inProfileChunkNum); // Begin handling out of the profile chunks. for (int i = 0; i < totalChunkNumber; i++) { if (!indicesSet.Contains(i)) { // This chunk is not in the profile fileIndices[fileIndicesCur] = i; bool readFile; byte[] hash = _fileHelper.GetHashFromChunkDbOrFile(DataFilePath, i, out readFile); if (readFile) { numDirectRead++; } Buffer.BlockCopy(hash, 0, hashes, fileIndicesCur * DataChunk.HashSize, hash.Length); fileIndicesCur++; } } logger.DebugFormat("Finished adding {0} out of profile chunks.", fileIndicesCur - inProfileChunkNum); logger.DebugFormat( "There are {0} chunks in total read directly from file.", numDirectRead); return(new ChunkMapDto { Hashes = hashes, FileIndices = fileIndices }); }