예제 #1
0
        private void LoadPage(int number)
        {
            _memoryFile.Seek(_markerSize + _intConverter.IntSize * 2 + (_boolConverter.BoolSize + _intConverter.IntSize) * PageCapacity * number, SeekOrigin.Begin);

            var map = new bool[PageCapacity];
            var val = new int[PageCapacity];

            map = map.Select(x => _boolConverter.ToBool(_br.ReadBytes(_boolConverter.BoolSize))).ToArray();
            val = val.Select(x => _intConverter.ToInt(_br.ReadBytes(_intConverter.IntSize))).ToArray();

            CurrentPage = new MemoryPage(number, map, val);
        }
예제 #2
0
        public VirtualMemory(string filePath, int pageCapacity, int pageNumber, bool overwrite = true, IIntConverter intConverter = null, IBoolConverter boolConverter = null, IStringConverter stringConverter = null)
        {
            var simpleConverter = new SimpleConverter();

            _intConverter    = intConverter ?? simpleConverter;
            _boolConverter   = boolConverter ?? simpleConverter;
            _stringConverter = stringConverter ?? simpleConverter;

            _markerSize = _stringConverter.ToBytes(_marker).Length;

            var exist = !overwrite && File.Exists(filePath);

            _memoryFile = new FileStream(filePath, overwrite ? FileMode.Create : FileMode.OpenOrCreate, FileAccess.ReadWrite);
            _memoryFile.Seek(0, SeekOrigin.Begin);

            _br = new BinaryReader(_memoryFile);
            _bw = new BinaryWriter(_memoryFile);

            PageCapacity = pageCapacity;
            PageNumber   = pageNumber;

            if (exist)
            {
                var marker = Encoding.Unicode.GetString(_br.ReadBytes(_markerSize));

                var tempCapacity = _intConverter.ToInt(_br.ReadBytes(_intConverter.IntSize));
                var tempNumber   = _intConverter.ToInt(_br.ReadBytes(_intConverter.IntSize));

                if (marker != _marker || tempCapacity != pageCapacity || tempNumber != pageNumber)
                {
                    throw new FileLoadException();
                }

                LoadPage(0);
            }
            else
            {
                CurrentPage = new MemoryPage(0, PageCapacity);

                _bw.Write(_stringConverter.ToBytes(_marker));
                _bw.Write(_intConverter.ToBytes(PageCapacity));
                _bw.Write(_intConverter.ToBytes(PageNumber));

                for (int i = 0; i < PageNumber; i++)
                {
                    WritePage(i);
                }
            }
        }