Example #1
0
        /// <summary>
        /// Gets the array of HashMap
        /// </summary>
        /// <param name="entryCount">The number of hash entries.</param>
        /// <param name="blockSize">The size of the data blocks that were hashed.</param>
        /// <param name="create">If true create hashmap on error.</param>
        /// <param name="mapRev">The desired map revision.</param>
        /// <returns></returns>
        internal FileStream GetHashMapStream(out int entryCount, out int blockSize, bool create, ulong mapRev)
        {
            if (File.Exists(file))
            {
                FileStream stream = File.OpenRead(file);
                if (HashFileHeader.ReadHeader(new BinaryReader(stream), out blockSize, out entryCount, mapRev))
                {
                    return(stream);
                }
                stream.Close();
            }

            //Since we do not create hash map at server we are commenting this

            /*	if (create)
             *              this.CreateHashMap();
             *      else
             *              Delete();
             */

            entryCount = 0;
            blockSize  = 0;
            return(null);
        }
Example #2
0
        /// <summary>
        /// Create a hash map file using file stream
        /// </summary>
        /// <param name="mapSrcStream">File stream thru which hash map file will be created</param>
        public void CreateHashMapFileWithStream(FileStream mapSrcStream)
        {
            // Makre sure that mapState is set to true for succes anf failure cases

            if (mapSrcStream == null)
            {
                Log.log.Debug("CreateHashMapFileWithStream: The mapsrcstream is null");
                MapState = true;
                return;
            }


            int blockSize = CalculateBlockSize(mapSrcStream.Length);

            try
            {
                string mapFile    = file;
                string tmpMapFile = mapFile + ".tmp";
                // Copy the current file to a tmp name.
                if (File.Exists(mapFile))
                {
                    File.Move(mapFile, tmpMapFile);
                }

                BinaryWriter writer = new BinaryWriter(File.OpenWrite(tmpMapFile));

                // Write the header.
                HashFileHeader.WriteHeader(writer, blockSize, node.LocalIncarnation);
                try
                {
                    mapSrcStream.Position = 0;
                    HashMap.SerializeHashMap(mapSrcStream, writer, blockSize);
                    writer.Close();

                    File.Move(tmpMapFile, mapFile);
                    File.SetCreationTime(mapFile, node.CreationTime);
                    File.SetLastWriteTime(mapFile, node.LastWriteTime);
                }
                catch (Exception ex)
                {
                    Log.log.Debug("CreateHashMapFileWithStream Exception in {0}--{1}", ex.Message, ex.StackTrace);
                    writer.Close();
                    writer = null;
                    File.Delete(mapFile);
                    if (File.Exists(tmpMapFile))
                    {
                        File.Move(tmpMapFile, mapFile);
                    }

                    MapState = true;

                    throw ex;
                }
                finally
                {
                    if (File.Exists(tmpMapFile))
                    {
                        File.Delete(tmpMapFile);
                    }
                }
            }
            finally
            {
                // Close the file.
                mapSrcStream.Close();
            }
            MapState = true;
        }