public IndexFile(string filename, byte maxKeySize) { _T = RDBDataType <T> .ByteHandler(); if (typeof(T) == typeof(string)) { _externalStrings = true; _maxKeySize = 4;// blocknum:int } else { _maxKeySize = maxKeySize; } _PageNodeCount = Global.PageItemCount; // pageNodeCount; _rowSize = (_maxKeySize + 1 + 4 + 4); //行的大小 string path = Path.GetDirectoryName(filename); Directory.CreateDirectory(path); if (File.Exists(filename)) { // if file exists open and read header _file = File.Open(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); this.ReadFileHeader(); if (_externalStrings == false)// if the file says different { _rowSize = (_maxKeySize + 1 + 4 + 4); } // compute last page number from file length _PageLength = (_BlockHeader.Length + _rowSize * (_PageNodeCount)); _LastPageNumber = (int)((_file.Length - _FileHeader.Length) / _PageLength); } else { // else create new file _file = File.Open(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); _PageLength = (_BlockHeader.Length + _rowSize * (_PageNodeCount)); this.CreateFileHeader(0); _LastPageNumber = (int)((_file.Length - _FileHeader.Length) / _PageLength); } if (_externalStrings) { _strings = new KeyStoreHF(path, Path.GetFileNameWithoutExtension(filename) + ".strings"); } if (_LastPageNumber == 0) { _LastPageNumber = 1; } // bitmap duplicates if (_allowDups) { _bitmap = new BitmapIndex(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename)); } }
public MGIndex(string path, string filename, byte keysize, bool allowdups) { _AllowDuplicates = allowdups; _index = new IndexFile <T>(Path.Combine(path, filename), keysize); // load page list _index.GetPageList(_pageListDiskPages, _pageList, out _LastIndexedRecordNumber); if (_pageList.Count == 0) { Page <T> page = new Page <T>(); page.FirstKey = (T)RDBDataType <T> .GetEmpty(); page.DiskPageNumber = _index.GetNewPageNumber(); page.isDirty = true; _pageList.Add(page.FirstKey, new PageInfo(page.DiskPageNumber, 0, 0)); _cache.Add(page.DiskPageNumber, page); } }
/// <summary> /// 初始化 /// </summary> /// <param name="filename"></param> /// <param name="maxkeysize"></param> /// <param name="AllowDuplicateKeys"></param> private void Initialize(string filename, byte maxkeysize, bool AllowDuplicateKeys) { _MaxKeySize = RDBDataType <T> .GetByteSize(maxkeysize); _T = RDBDataType <T> .ByteHandler(); _Path = Path.GetDirectoryName(filename); Directory.CreateDirectory(_Path); _FileName = Path.GetFileNameWithoutExtension(filename); string db = Path.Combine(_Path, _FileName + _datExtension); _index = new MGIndex <T>(_Path, _FileName + _idxExtension, _MaxKeySize, AllowDuplicateKeys); if (Global.SaveAsBinaryJSON) { _archive = new StorageFile <T>(db, SF_FORMAT.BSON, false); } else { _archive = new StorageFile <T>(db, SF_FORMAT.JSON, false); } _deleted = new BoolIndex(_Path, _FileName); log.Debug("Current Count = " + RecordCount().ToString("#,0")); CheckIndexState(); //保存服务 log.Debug("Starting save timer"); _savetimer = new System.Timers.Timer(); _savetimer.Elapsed += new System.Timers.ElapsedEventHandler(_savetimer_Elapsed); _savetimer.Interval = Global.SaveIndexToDiskTimerSeconds * 1000; _savetimer.AutoReset = true; _savetimer.Start(); }
/// <summary> /// 初始化 /// </summary> /// <param name="filename"></param> /// <param name="StorageOnlyMode"></param> private void Initialize(string filename, bool StorageOnlyMode) { _T = RDBDataType <T> .ByteHandler(); _filename = filename; // search for mgdat00000 extensions -> split files load if (File.Exists(filename + _splitfileExtension)) { LoadSplitFiles(filename); } if (File.Exists(filename) == false) { _datawrite = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite); } else { _datawrite = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); } _dataread = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (_datawrite.Length == 0) { // new file //写入头信息 _datawrite.Write(_fileheader, 0, _fileheader.Length); _datawrite.Flush(); _lastWriteOffset = _fileheader.Length; } else { long i = _datawrite.Seek(0L, SeekOrigin.End); if (_files.Count == 0) { _lastWriteOffset = i; } else { _lastWriteOffset += i; // add to the splits } } if (StorageOnlyMode == false) { //加载记录指针 // load rec pointers _recfilename = filename.Substring(0, filename.LastIndexOf('.')) + _recExtension; if (File.Exists(_recfilename) == false) { _recfilewrite = new FileStream(_recfilename, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite); } else { _recfilewrite = new FileStream(_recfilename, FileMode.Open, FileAccess.Write, FileShare.ReadWrite); } _recfileread = new FileStream(_recfilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); _lastRecordNum = (int)(_recfilewrite.Length / 8); _recfilewrite.Seek(0L, SeekOrigin.End); } }