Ejemplo n.º 1
0
        /// <summary>
        /// Files and containers initialisation
        /// </summary>
        private void Init(int bFactor, int overflowBFactor, string mainFileDiskLetter, string overflowFileDiskLetter)
        {
            UtilityOperations.GetDiskFreeSpace(mainFileDiskLetter, out var SectorsPerCluster, out var BytesPerSector, out _, out _);
            var ClusterSize = SectorsPerCluster * BytesPerSector;

            _BFactor         = bFactor == -1 ? (ClusterSize - 8) / _emptyClass.GetSize() : bFactor;
            _OverflowBFactor = overflowBFactor == -1 ? (ClusterSize - 8) / _emptyClass.GetSize() : overflowBFactor;


            _blocksInformations = new List <BlockInfo>((int)Math.Pow(2, _hashDepth));

            var block         = new Block <T>(_BFactor, _emptyClass.GetEmptyClass());
            var overflowBlock = new Block <T>(_OverflowBFactor, _emptyClass.GetEmptyClass());

            _fileManager     = new FileManager(_filePath, block.GetSize());
            _overflowManager = new OverflowFileManager <T>("overflow." + _filePath, overflowBlock.GetSize(), _emptyClass, _OverflowBFactor);

            var address = _fileManager.GetFreeAddress();

            _blocksInformations.Add(new BlockInfo {
                Address = address, Depth = 1, Records = 0
            });
            WriteBlock(address, block);

            address = _fileManager.GetFreeAddress();
            _blocksInformations.Add(new BlockInfo {
                Address = address, Depth = 1, Records = 0
            });
            WriteBlock(address, block);
        }
Ejemplo n.º 2
0
        public bool TryLoad()
        {
            try {
                var stream = new FileStream("config." + _filePath, FileMode.Open);
                var buffer = new byte[sizeof(int)];

                // citanie suboru
                stream.Read(buffer);
                _BFactor = BitConverter.ToInt32(buffer);
                var block = new Block <T>(_BFactor, _emptyClass.GetEmptyClass());
                _fileManager = new FileManager(_filePath, block.GetSize(), false);

                stream.Read(buffer);
                _OverflowBFactor = BitConverter.ToInt32(buffer);
                var overflowBlock = new Block <T>(_OverflowBFactor, _emptyClass.GetEmptyClass());
                _overflowManager = new OverflowFileManager <T>("overflow." + _filePath, overflowBlock.GetSize(), _emptyClass, _OverflowBFactor, false);

                stream.Read(buffer);
                _hashDepth          = BitConverter.ToInt32(buffer);
                _blocksInformations = new List <BlockInfo>((int)Math.Pow(2, _hashDepth));

                stream.Read(buffer);
                var blocksInfoCount = BitConverter.ToInt32(buffer);

                if (blocksInfoCount > 0)
                {
                    stream.Read(buffer);
                    var distinctCount = BitConverter.ToInt32(buffer);

                    BlockInfo blockInfo;

                    for (int i = 0; i < distinctCount; i++)
                    {
                        // zistom kolko opakovani
                        stream.Read(buffer);
                        var repetition = BitConverter.ToInt32(buffer);
                        // ziskam blockInfo
                        blockInfo = new BlockInfo();
                        buffer    = new byte[blockInfo.GetSize()];
                        stream.Read(buffer);
                        blockInfo.FromByteArray(buffer);
                        // vlozim info potrebny pocet krat
                        while (repetition-- > 0)
                        {
                            _blocksInformations.Add(blockInfo);
                        }

                        buffer = new byte[sizeof(int)];
                    }
                }

                stream.Read(buffer);
                var fileManagerAddresses = BitConverter.ToInt32(buffer);
                if (fileManagerAddresses > 0)
                {
                    buffer = new byte[sizeof(int) * fileManagerAddresses];
                    _fileManager.FromByteArray(buffer);
                }

                var overflowManagerSize = stream.Length - stream.Position;
                buffer = new byte[overflowManagerSize];
                stream.Read(buffer);
                _overflowManager.FromByteArray(buffer);

                stream.Close();

                return(true);
            } catch (FileNotFoundException ex) {
                Console.WriteLine("File " + ex.FileName + " does not exist.");
                return(false);
            }
        }